-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 1366 - Slack integrations [ Docker Events ] #1472
Changes from all commits
dd95140
5456f52
bc6a67a
25167fa
bee603f
6a7fffe
ba275eb
8cf88b2
591d4de
505e065
39d5652
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This script listens to docker events and will report only the following events: | ||
# START, UNPAUSE, RESTART, PAUSE, DIE and STOP. | ||
|
||
|
||
#!/bin/bash | ||
|
||
docker events --format '{{json .}}' --filter 'event=start' --filter 'event=unpause' --filter 'event=restart' --filter 'event=pause' --filter 'event=die' --filter 'event=stop'| | ||
while read event; | ||
do | ||
echo ${event} | ||
done |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
const shell = require('shelljs'); | ||
const fetch = require('node-fetch'); | ||
const Events = require('events'); | ||
|
||
const { SLACK_SEND_MESSAGE } = process.env; | ||
|
||
const dockerEvents = new Events.EventEmitter(); | ||
|
||
const server = process.DEPLOY_TYPE === 'production' ? 'PRODUCTION' : 'DEV'; | ||
|
||
const eventsDown = ['pause', 'die', 'stop']; | ||
const eventsBack = ['start', 'unpause', 'restart']; | ||
|
||
const containersUp = new Set(); | ||
const containersDown = new Set(); | ||
|
||
const message = (eventLog) => { | ||
const { | ||
status, | ||
Action, | ||
Actor: { | ||
Attributes: { name }, | ||
}, | ||
} = eventLog; | ||
|
||
const isDown = eventsDown.includes(Action); | ||
|
||
const logo = isDown | ||
? 'https://www.iconsdb.com/icons/preview/red/warning-xxl.png' | ||
: 'https://www.iconsdb.com/icons/preview/green/up-circular-xxl.png'; | ||
|
||
return { | ||
blocks: [ | ||
{ | ||
type: 'divider', | ||
}, | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: `* ${server} DOCKER CONTAINER: ${ | ||
isDown ? 'DOWN' : 'UP' | ||
}*\n\nContainer: ${name}\nAction: ${Action}\nCurrent Status: ${status}\n`, | ||
}, | ||
accessory: { | ||
type: 'image', | ||
image_url: logo, | ||
alt_text: 'alt text for image', | ||
}, | ||
}, | ||
{ | ||
type: 'divider', | ||
}, | ||
], | ||
}; | ||
}; | ||
|
||
const sendMessage = async (containerEvent) => { | ||
await fetch(SLACK_SEND_MESSAGE, { | ||
method: 'post', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(message(containerEvent)), | ||
}); | ||
}; | ||
|
||
const eventHandle = (containerEvent) => { | ||
const { | ||
Action, | ||
Actor: { | ||
Attributes: { name }, | ||
}, | ||
} = containerEvent; | ||
|
||
if ( | ||
(containersDown.has(name) && eventsDown.includes(Action)) || | ||
(containersUp.has(name) && eventsBack.includes(Action)) | ||
) | ||
return; | ||
|
||
if (eventsDown.includes(Action)) { | ||
containersUp.has(name) ? containersUp.delete(name) : null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @humphd |
||
containersDown.add(name); | ||
} else { | ||
containersDown.has(name) ? containersDown.delete(name) : null; | ||
containersUp.add(name); | ||
} | ||
sendMessage(containerEvent); | ||
}; | ||
|
||
const dockerListener = () => { | ||
const event = shell.exec('./docker-listener.sh', { | ||
silent: true, | ||
async: true, | ||
}); | ||
event.stdout.on('data', (data) => { | ||
const containerEvent = JSON.parse(data); | ||
if (containerEvent.Actor.Attributes.name.includes('telescope')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @manekenpix, this line is for filter containers. Because we could have more than one project running Docker in the machine, let's think of how it would work on CDOT machines. |
||
eventHandle(containerEvent); | ||
} | ||
}); | ||
}; | ||
|
||
dockerEvents.on('start', dockerListener); | ||
|
||
const dockerMonitor = () => dockerEvents.emit('start'); | ||
|
||
module.exports = { dockerMonitor }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add an example of what this should look like in addition to the docs.