From fd7f8a40ed480e019eae53f05295fb6abe46d334 Mon Sep 17 00:00:00 2001 From: ubergesundheit Date: Mon, 27 Nov 2017 12:04:08 +0100 Subject: [PATCH] add slackrus slack notifications --- README.md | 22 ++++++++++++++++++++++ glide.lock | 20 ++++++++++++-------- glide.yaml | 2 ++ main.go | 21 +++++++++++++++++++-- notifications/notifier.go | 2 ++ notifications/slack.go | 38 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 notifications/slack.go diff --git a/README.md b/README.md index 8b4a3f3..ba4d0ec 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,9 @@ Watchtower can send notifications when containers are updated. Notifications are The types of notifications to send are passed via the comma-separated option `--notifications` (or corresponding environment variable `WATCHTOWER_NOTIFICATIONS`), which has the following valid values: * `email` to send notifications via e-mail +* `slack` to send notifications through a Slack webhook + +### Notifications via E-Mail To receive notifications by email, the following command-line options, or their corresponding environment variables, can be set: @@ -215,3 +218,22 @@ docker run -d \ v2tec/watchtower ``` +### Notifications through Slack webhook + +To receive notifications in Slack, add `slack` to the `--notifications` option or the `WATCHTOWER_NOTIFICATIONS` environment variable. + +Additionally, you should set the Slack webhook url using the `--notification-slack-hook-url` option or the `WATCHTOWER_NOTIFICATION_SLACK_HOOK_URL` environment variable. + +By default, watchtower will send messages under the name `watchtower`, you can customize this string through the `--notification-slack-identifier` option or the `WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER` environment variable. + +Example: + +```bash +docker run -d \ + --name watchtower \ + -v /var/run/docker.sock:/var/run/docker.sock \ + -e WATCHTOWER_NOTIFICATIONS=slack \ + -e WATCHTOWER_NOTIFICATION_SLACK_HOOK_URL="https://hooks.slack.com/services/xxx/yyyyyyyyyyyyyyy" \ + -e WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER=watchtower-server-1 \ + v2tec/watchtower +``` diff --git a/glide.lock b/glide.lock index 7ae50d9..55cd59e 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 9ddd729b207d71ce16ae9a0282ac87a046b9161ac4f15b108e86a03367172516 -updated: 2017-01-24T20:45:43.1914053+01:00 +hash: c0e0c69383b9b665e92c40a5ea69632ea846a645f44654411137c75c5ef5017c +updated: 2017-11-27T11:34:21.283697431+01:00 imports: - name: github.com/Azure/go-ansiterm version: 388960b655244e76e24c75f48631564eaefade62 @@ -25,7 +25,7 @@ imports: - registry/storage/cache/memory - uuid - name: github.com/docker/docker - version: 49bf474f9ed7ce7143a59d1964ff7b7fd9b52178 + version: 092cba3727bb9b4a2f0e922cd6c0f93ea270e363 subpackages: - api - api/server/httputils @@ -110,13 +110,16 @@ imports: version: 0eeaf8392f5b04950925b8a69fe70f110fa7cbfc - name: github.com/inconshreveable/mousetrap version: 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75 +- name: github.com/johntdyer/slack-go + version: 88ef3038545fa084d81f36e4a6943d112cdd74f6 +- name: github.com/johntdyer/slackrus + version: 3992f319fd0ac349483279ef74742cd787841cba - name: github.com/mattn/go-shellwords version: f4e566c536cf69158e808ec28ef4182a37fdc981 - name: github.com/Microsoft/go-winio version: 24a3e3d3fc7451805e09d11e11e95d9a0a4f205e - name: github.com/opencontainers/runc - version: 2f7393a47307a16f8cee44a37b262e8b81021e3e - repo: https://github.com/docker/runc.git + version: e8149af2910850602cae998dede29e630d1b8ee1 subpackages: - libcontainer/configs - libcontainer/devices @@ -135,10 +138,11 @@ imports: - name: github.com/robfig/cron version: 9585fd555638e77bba25f25db5c44b41f264aeb7 - name: github.com/Sirupsen/logrus - version: d26492970760ca5d33129d2d799e34be5c4782eb + version: ba1b36c82c5e05c4f912a88eab0dcd91a171688f +- name: github.com/sirupsen/logrus + version: ba1b36c82c5e05c4f912a88eab0dcd91a171688f - name: github.com/spf13/cobra - version: a3c09249f1a24a9d951f2738fb9b9256b8b42fa5 - repo: https://github.com/dnephin/cobra.git + version: 1be1d2841c773c01bee8289f55f7463b6e2c2539 - name: github.com/spf13/pflag version: dabebe21bf790f782ea4c7bbd2efc430de182afd - name: github.com/stretchr/objx diff --git a/glide.yaml b/glide.yaml index 6a96785..24609b9 100644 --- a/glide.yaml +++ b/glide.yaml @@ -26,3 +26,5 @@ import: - context - package: github.com/robfig/cron version: 9585fd555638e77bba25f25db5c44b41f264aeb7 +- package: github.com/johntdyer/slackrus + version: 3992f31 diff --git a/main.go b/main.go index 5c69340..38b1269 100644 --- a/main.go +++ b/main.go @@ -8,8 +8,8 @@ import ( "strconv" - log "github.com/sirupsen/logrus" "github.com/robfig/cron" + log "github.com/sirupsen/logrus" "github.com/urfave/cli" "github.com/v2tec/watchtower/actions" "github.com/v2tec/watchtower/container" @@ -93,7 +93,7 @@ func main() { cli.StringSliceFlag{ Name: "notifications", Value: &cli.StringSlice{}, - Usage: "notification types to send (valid: email)", + Usage: "notification types to send (valid: email, slack)", EnvVar: "WATCHTOWER_NOTIFICATIONS", }, cli.StringFlag{ @@ -136,6 +136,23 @@ func main() { Usage: "SMTP server password for sending notifications", EnvVar: "WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD", }, + cli.StringFlag{ + Name: "notification-slack-hook-url", + Usage: "The Slack Hook URL to send notifications to", + EnvVar: "WATCHTOWER_NOTIFICATION_SLACK_HOOK_URL", + }, + cli.StringFlag{ + Name: "notification-slack-identifier", + Usage: "A string which will be used to identify the messages coming from this watchtower instance. Default if omitted is \"watchtower\"", + EnvVar: "WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER", + Value: "watchtower", + }, + cli.StringFlag{ + Name: "notification-slack-level", + Usage: "The log level used for sending notifications through Slack. Default if omitted is \"info\". Possible values: \"panic\",\"fatal\",\"error\",\"warn\",\"info\" or \"debug\"", + EnvVar: "WATCHTOWER_NOTIFICATION_SLACK_LEVEL", + Value: "info", + }, } if err := app.Run(os.Args); err != nil { diff --git a/notifications/notifier.go b/notifications/notifier.go index b202c65..ab635bb 100644 --- a/notifications/notifier.go +++ b/notifications/notifier.go @@ -26,6 +26,8 @@ func NewNotifier(c *cli.Context) *Notifier { switch t { case emailType: tn = newEmailNotifier(c) + case slackType: + tn = newSlackNotifier(c) default: log.Fatalf("Unknown notification type %q", t) } diff --git a/notifications/slack.go b/notifications/slack.go new file mode 100644 index 0000000..3b17354 --- /dev/null +++ b/notifications/slack.go @@ -0,0 +1,38 @@ +package notifications + +import ( + "github.com/johntdyer/slackrus" + log "github.com/sirupsen/logrus" + "github.com/urfave/cli" +) + +const ( + slackType = "slack" +) + +type slackTypeNotifier struct { + slackrus.SlackrusHook +} + +func newSlackNotifier(c *cli.Context) typeNotifier { + logLevel, err := log.ParseLevel(c.GlobalString("notification-slack-level")) + if err != nil { + log.Fatalf("Slack notifications: %s", err.Error()) + } + + n := &slackTypeNotifier{ + SlackrusHook: slackrus.SlackrusHook{ + HookURL: c.GlobalString("notification-slack-hook-url"), + Username: c.GlobalString("notification-slack-identifier"), + AcceptedLevels: slackrus.LevelThreshold(logLevel), + }, + } + + log.AddHook(n) + + return n +} + +func (s *slackTypeNotifier) StartNotification() {} + +func (s *slackTypeNotifier) SendNotification() {}