From d989254b1aa4da28ea2fe64fde41dd69bab8bda7 Mon Sep 17 00:00:00 2001 From: ubergesundheit Date: Mon, 27 Nov 2017 12:03:00 +0100 Subject: [PATCH 1/4] change upper case S in sirupsen to lower case to avoid build error --- actions/update.go | 2 +- container/client.go | 2 +- container/trust.go | 2 +- glide.yaml | 2 +- main.go | 2 +- notifications/email.go | 2 +- notifications/notifier.go | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/actions/update.go b/actions/update.go index 15cd017..1f61af5 100644 --- a/actions/update.go +++ b/actions/update.go @@ -4,7 +4,7 @@ import ( "math/rand" "time" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/v2tec/watchtower/container" ) diff --git a/container/client.go b/container/client.go index afa9a33..340c51b 100644 --- a/container/client.go +++ b/container/client.go @@ -5,7 +5,7 @@ import ( "io/ioutil" "time" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/network" dockerclient "github.com/docker/docker/client" diff --git a/container/trust.go b/container/trust.go index a472649..90ba335 100644 --- a/container/trust.go +++ b/container/trust.go @@ -5,7 +5,7 @@ import ( "os" "strings" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/reference" "github.com/docker/docker/cli/command" diff --git a/glide.yaml b/glide.yaml index 8478cfa..6a96785 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,6 +1,6 @@ package: github.com/v2tec/watchtower import: -- package: github.com/Sirupsen/logrus +- package: github.com/sirupsen/logrus version: ~0.11.x - package: github.com/docker/docker version: ~1.13.x diff --git a/main.go b/main.go index fd74fcc..5c69340 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( "strconv" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/robfig/cron" "github.com/urfave/cli" "github.com/v2tec/watchtower/actions" diff --git a/notifications/email.go b/notifications/email.go index 5380f7f..54a2f9f 100644 --- a/notifications/email.go +++ b/notifications/email.go @@ -9,7 +9,7 @@ import ( "strconv" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/urfave/cli" ) diff --git a/notifications/notifier.go b/notifications/notifier.go index a73a63e..b202c65 100644 --- a/notifications/notifier.go +++ b/notifications/notifier.go @@ -1,7 +1,7 @@ package notifications import ( - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/urfave/cli" ) From fd7f8a40ed480e019eae53f05295fb6abe46d334 Mon Sep 17 00:00:00 2001 From: ubergesundheit Date: Mon, 27 Nov 2017 12:04:08 +0100 Subject: [PATCH 2/4] 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() {} From 4ac08dfb30f66c87dba8579f5d821d401f89eadd Mon Sep 17 00:00:00 2001 From: Fabrizio Steiner Date: Thu, 1 Mar 2018 12:53:48 +0100 Subject: [PATCH 3/4] glide alias Sirupsen to sirupsen --- glide.lock | 17 ++++++++++------- glide.yaml | 3 +++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/glide.lock b/glide.lock index 55cd59e..a4dcf44 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: c0e0c69383b9b665e92c40a5ea69632ea846a645f44654411137c75c5ef5017c -updated: 2017-11-27T11:34:21.283697431+01:00 +hash: 23bcd62f9352c61d3aebc85a59f7ebbfaf80bc0201f45037cdea65820673ddeb +updated: 2018-03-01T13:21:55.8472118+01:00 imports: - name: github.com/Azure/go-ansiterm version: 388960b655244e76e24c75f48631564eaefade62 @@ -91,7 +91,7 @@ imports: - client - credentials - name: github.com/docker/go-connections - version: 4ccf312bf1d35e5dbda654e57a9be4c3f3cd0366 + version: ecb4cb2dd420ada7df7f2593d6c25441f65f69f2 subpackages: - nat - sockets @@ -111,15 +111,16 @@ imports: - name: github.com/inconshreveable/mousetrap version: 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75 - name: github.com/johntdyer/slack-go - version: 88ef3038545fa084d81f36e4a6943d112cdd74f6 + version: 95fac1160b220c5abcf8b0ef88e9c3cb213c09f4 - name: github.com/johntdyer/slackrus version: 3992f319fd0ac349483279ef74742cd787841cba - name: github.com/mattn/go-shellwords version: f4e566c536cf69158e808ec28ef4182a37fdc981 - name: github.com/Microsoft/go-winio - version: 24a3e3d3fc7451805e09d11e11e95d9a0a4f205e + version: fff283ad5116362ca252298cfc9b95828956d85d - name: github.com/opencontainers/runc - version: e8149af2910850602cae998dede29e630d1b8ee1 + version: 9df8b306d01f59d3a8029be411de015b7304dd8f + repo: https://github.com/docker/runc.git subpackages: - libcontainer/configs - libcontainer/devices @@ -139,10 +140,12 @@ imports: version: 9585fd555638e77bba25f25db5c44b41f264aeb7 - name: github.com/Sirupsen/logrus version: ba1b36c82c5e05c4f912a88eab0dcd91a171688f + repo: https://github.com/sirupsen/logrus.git - name: github.com/sirupsen/logrus version: ba1b36c82c5e05c4f912a88eab0dcd91a171688f - name: github.com/spf13/cobra - version: 1be1d2841c773c01bee8289f55f7463b6e2c2539 + version: a3c09249f1a24a9d951f2738fb9b9256b8b42fa5 + repo: https://github.com/dnephin/cobra.git - name: github.com/spf13/pflag version: dabebe21bf790f782ea4c7bbd2efc430de182afd - name: github.com/stretchr/objx diff --git a/glide.yaml b/glide.yaml index 24609b9..20ef77d 100644 --- a/glide.yaml +++ b/glide.yaml @@ -2,6 +2,9 @@ package: github.com/v2tec/watchtower import: - package: github.com/sirupsen/logrus version: ~0.11.x +- package: github.com/Sirupsen/logrus + repo: https://github.com/sirupsen/logrus.git + version: ~0.11.x - package: github.com/docker/docker version: ~1.13.x subpackages: From e1ead2f46f597ceb7e353f621e724ee613de52e5 Mon Sep 17 00:00:00 2001 From: Fabrizio Steiner Date: Fri, 2 Mar 2018 13:08:40 +0100 Subject: [PATCH 4/4] Made the notification level flag global for all notification types. --- README.md | 4 ++++ main.go | 12 ++++++------ notifications/email.go | 13 ++++--------- notifications/notifier.go | 12 ++++++++++-- notifications/slack.go | 9 ++------- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index ba4d0ec..224f158 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,10 @@ The types of notifications to send are passed via the comma-separated option `-- * `email` to send notifications via e-mail * `slack` to send notifications through a Slack webhook +### Settings + +* `--notifications-level` (env. `WATCHTOWER_NOTIFICATIONS_LEVEL`): Controls the log level which is used for the notifications. If omitted, the default log level is `info`. Possible values are: `panic`, `fatal`, `error`, `warn`, `info` or `debug`. + ### Notifications via E-Mail To receive notifications by email, the following command-line options, or their corresponding environment variables, can be set: diff --git a/main.go b/main.go index 38b1269..962ab33 100644 --- a/main.go +++ b/main.go @@ -96,6 +96,12 @@ func main() { Usage: "notification types to send (valid: email, slack)", EnvVar: "WATCHTOWER_NOTIFICATIONS", }, + cli.StringFlag{ + Name: "notifications-level", + Usage: "The log level used for sending notifications. Possible values: \"panic\", \"fatal\", \"error\", \"warn\", \"info\" or \"debug\"", + EnvVar: "WATCHTOWER_NOTIFICATIONS_LEVEL", + Value: "info", + }, cli.StringFlag{ Name: "notification-email-from", Usage: "Address to send notification e-mails from", @@ -147,12 +153,6 @@ func main() { 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/email.go b/notifications/email.go index 54a2f9f..5f84ca5 100644 --- a/notifications/email.go +++ b/notifications/email.go @@ -28,9 +28,10 @@ type emailTypeNotifier struct { Port int tlsSkipVerify bool entries []*log.Entry + logLevels []log.Level } -func newEmailNotifier(c *cli.Context) typeNotifier { +func newEmailNotifier(c *cli.Context, acceptedLogLevels []log.Level) typeNotifier { n := &emailTypeNotifier{ From: c.GlobalString("notification-email-from"), To: c.GlobalString("notification-email-to"), @@ -39,6 +40,7 @@ func newEmailNotifier(c *cli.Context) typeNotifier { Password: c.GlobalString("notification-email-server-password"), Port: c.GlobalInt("notification-email-server-port"), tlsSkipVerify: c.GlobalBool("notification-email-server-tls-skip-verify"), + logLevels: acceptedLogLevels, } log.AddHook(n) @@ -112,14 +114,7 @@ func (e *emailTypeNotifier) SendNotification() { } func (e *emailTypeNotifier) Levels() []log.Level { - // TODO: Make this configurable. - return []log.Level{ - log.PanicLevel, - log.FatalLevel, - log.ErrorLevel, - log.WarnLevel, - log.InfoLevel, - } + return e.logLevels } func (e *emailTypeNotifier) Fire(entry *log.Entry) error { diff --git a/notifications/notifier.go b/notifications/notifier.go index ab635bb..274811b 100644 --- a/notifications/notifier.go +++ b/notifications/notifier.go @@ -1,6 +1,7 @@ package notifications import ( + "github.com/johntdyer/slackrus" log "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -19,15 +20,22 @@ type Notifier struct { func NewNotifier(c *cli.Context) *Notifier { n := &Notifier{} + logLevel, err := log.ParseLevel(c.GlobalString("notifications-level")) + if err != nil { + log.Fatalf("Notifications invalid log level: %s", err.Error()) + } + + acceptedLogLevels := slackrus.LevelThreshold(logLevel) + // Parse types and create notifiers. types := c.GlobalStringSlice("notifications") for _, t := range types { var tn typeNotifier switch t { case emailType: - tn = newEmailNotifier(c) + tn = newEmailNotifier(c, acceptedLogLevels) case slackType: - tn = newSlackNotifier(c) + tn = newSlackNotifier(c, acceptedLogLevels) default: log.Fatalf("Unknown notification type %q", t) } diff --git a/notifications/slack.go b/notifications/slack.go index 3b17354..dd44f29 100644 --- a/notifications/slack.go +++ b/notifications/slack.go @@ -14,17 +14,12 @@ 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()) - } - +func newSlackNotifier(c *cli.Context, acceptedLogLevels []log.Level) typeNotifier { n := &slackTypeNotifier{ SlackrusHook: slackrus.SlackrusHook{ HookURL: c.GlobalString("notification-slack-hook-url"), Username: c.GlobalString("notification-slack-identifier"), - AcceptedLevels: slackrus.LevelThreshold(logLevel), + AcceptedLevels: acceptedLogLevels, }, }