Compare commits

...
Author SHA1 Message Date
ElevenNotes d978cd42c8 0.1.6 2023-12-19 13:56:06 +01:00
ElevenNotes af8343f89a 0.1.6 simplify webhook 2023-12-19 13:55:52 +01:00
ElevenNotes f60f0ad124 0.1.5, remove container restart on kill 2023-12-19 11:44:50 +01:00
ElevenNotes e7202481b3 0.1.4 2023-12-05 08:58:34 +01:00
4 changed files with 43 additions and 47 deletions
+1 -2
View File
@@ -1,2 +1 @@
maintain/
/build
maintain/
+1 -1
View File
@@ -1,5 +1,5 @@
# Alpine :: Traefik Labels
![size](https://img.shields.io/docker/image-size/11notes/traefik-labels/0.1.3?color=0eb305) ![version](https://img.shields.io/docker/v/11notes/traefik-labels?color=eb7a09) ![pulls](https://img.shields.io/docker/pulls/11notes/traefik-labels?color=2b75d6) ![activity](https://img.shields.io/github/commit-activity/m/11notes/docker-traefik-labels?color=c91cb8) ![commit-last](https://img.shields.io/github/last-commit/11notes/docker-traefik-labels?color=c91cb8)
![size](https://img.shields.io/docker/image-size/11notes/traefik-labels/0.1.6?color=0eb305) ![version](https://img.shields.io/docker/v/11notes/traefik-labels?color=eb7a09) ![pulls](https://img.shields.io/docker/pulls/11notes/traefik-labels?color=2b75d6) ![activity](https://img.shields.io/github/commit-activity/m/11notes/docker-traefik-labels?color=c91cb8) ![commit-last](https://img.shields.io/github/last-commit/11notes/docker-traefik-labels?color=c91cb8)
Run Traefik Labels based on Alpine Linux. Small, lightweight, secure and fast 🏔️
+41 -43
View File
@@ -6,15 +6,21 @@ class Labels{
#docker;
#redis;
#poll = false;
#webhook = {
headers:{'Content-Type':'application/json'}
};
constructor(){
this.#docker = new Docker({socketPath:'/run/docker.sock'});
if('' !== process.env.LABELS_WEBHOOK_AUTH_BASIC){
this.#webhook.headers['Authorization'] = 'Basic ' + Buffer.from(process.env.LABELS_WEBHOOK_AUTH_BASIC).toString('base64')
}
}
#log(message, error){
#log(message, type){
console.log(JSON.stringify({
time:new Date().toISOString(),
type:(error === ERROR) ? 'ERROR' : 'INFO',
type:(type === ERROR) ? 'ERROR' : 'INFO',
message:message,
}));
}
@@ -69,16 +75,16 @@ class Labels{
if(!this.#poll){
try{
this.#poll = true;
this.#log(`poll start (interval:${process.env.LABELS_INTERVAL})}`);
this.#docker.listContainers((error, containers) => {
containers.forEach(async(container) => {
await this.dockerInspect(container.Id);
});
if(!error){
containers.forEach(async(container) => {
await this.dockerInspect(container.Id, 'poll');
});
}
});
}catch(e){
this.#log(e, ERROR);
}finally{
this.#log('poll end');
this.#poll = false;
}
}
@@ -88,47 +94,39 @@ class Labels{
return(new Promise((resolve, reject) => {
const container = this.#docker.getContainer(id);
container.inspect(async(error, data) => {
if(!error){
let update = false;
const webHook = {event:status, labels:{}};
if(/start|restart|poll/i.test(status)){
update = true;
}
let update = false;
const webHook = {event:status, labels:{}};
const headers = {'Content-Type':'application/json'};
if('' !== process.env.LABELS_WEBHOOK_AUTH_BASIC){
headers['Authorization'] = 'Basic ' + Buffer.from(process.env.LABELS_WEBHOOK_AUTH_BASIC).toString('base64')
}
if(error){
reject(error);
}
this.#log(`inspect container ${data.Name.replace(/^\//i, '')}${(
(null === status) ? '' : ` event[${status}]`
)}`);
if(/start|restart/i.test(status)){
update = true;
}
for(const label in data?.Config?.Labels){
if(/traefik\//i.test(label)){
webHook.labels[label] = data?.Config?.Labels[label];
if(update){
await this.#redis.set(label, data?.Config?.Labels[label], {EX:parseInt(process.env.LABELS_INTERVAL) + parseInt(process.env.LABELS_TIMEOUT)});
}else{
await this.#redis.del(label);
this.#log(`inspect container {${(data?.Name || data?.id).replace(/^\//i, '')}}${(
(null === status) ? '' : ` event[${status}]`
)}`);
for(const label in data?.Config?.Labels){
if(/traefik\//i.test(label)){
webHook.labels[label] = data?.Config?.Labels[label];
if(update){
this.#log(`${label} = ${data?.Config?.Labels[label]}`);
await this.#redis.set(label, data?.Config?.Labels[label], {EX:parseInt(process.env.LABELS_INTERVAL) + parseInt(process.env.LABELS_TIMEOUT)});
}else{
await this.#redis.del(label);
}
}
}
}
if('' !== process.env.LABELS_WEBHOOK){
try{
const webHookCall = await fetch(process.env.LABELS_WEBHOOK, {method:(
(update) ? 'PUT' : 'DELETE'
), body:JSON.stringify(webHook), headers:headers});
}catch(e){
this.#log(e, ERROR);
if('' !== process.env.LABELS_WEBHOOK){
try{
await fetch(process.env.LABELS_WEBHOOK, {method:(
(update) ? 'PUT' : 'DELETE'
), body:JSON.stringify(webHook), headers:this.#webhook.headers, signal:AbortSignal.timeout(5000)});
}catch(e){
this.#log(e, ERROR);
}
}
resolve(true);
}
resolve(true);
});
}));
}
-1
View File
@@ -12,7 +12,6 @@ const child = fork(`${__dirname}/app.js`, [], {
});
child.on('error', (error) =>{
console.error(error);
process.exit(1);
});
child.on('close', (code) =>{
console.warn(`child process closed with exit code ${code}`);