Skip to content

Commit

Permalink
Merge pull request #7 from lorello/master
Browse files Browse the repository at this point in the history
Added the possibility to include hostname in messages posted to Slack
  • Loading branch information
int128 authored Apr 19, 2018
2 parents c174e34 + 84fdc23 commit 4652448
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ curl -O https://raw.githubusercontent.com/int128/slack-docker/master/docker-comp
docker-compose up -d
```

### Include docker hostname in the messages

If you run standalone docker hosts you'll probably find usefull include the `HOSTNAME` in the
messages posted to slack. Run the container as follows:

```sh
# Docker
docker run --net=host --env include_hostname=1 -d -e webhook=URL -v /var/run/docker.sock:/var/run/docker.sock int128/slack-docker
```

or set the variable explicitly

```sh
# Docker
docker run --env HOSTNAME=${HOSTNAME} --env include_hostname=1 -d -e webhook=URL -v /var/run/docker.sock:/var/run/docker.sock int128/slack-docker
```


### Filter events by image name

By default all events are sent to Slack, but events can be filtered by the environment variable `image_regexp` as follows:
Expand Down
7 changes: 5 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const Docker = require('dockerode');
const Slack = require('./slack');
const JSONStream = require('JSONStream');
const templates = require('./templates');

const imageRegExp = new RegExp(process.env.image_regexp);
const docker = new Docker();
const slack = new Slack({
Expand All @@ -13,13 +12,17 @@ const slack = new Slack({

async function sendEvent(event) {
console.info(event);
hostname_string = '';
if (process.env.include_hostname) {
hostname_string = '@ ' + process.env.HOSTNAME
}
if (imageRegExp.test(event.from)) {
const template = templates[`${event.Type}_${event.Action}`];
if (template) {
const attachment = template(event);
if (attachment) {
await slack.send({
username: `docker ${event.Type} ${event.Actor.Attributes.name}`,
username: `docker ${event.Type} ${event.Actor.Attributes.name} ${hostname_string}`,
attachments: [attachment],
});
}
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ daemon:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- webhook=https://hooks.slack.com/services/...
#- include_hostname=1

0 comments on commit 4652448

Please sign in to comment.