Merge pull request #113 from ubergesundheit/slack-notifications-slackrus

Slack notifications with slackrus
pull/172/head
stffabi 6 years ago committed by GitHub
commit 05513ddc1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -189,6 +189,13 @@ 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
### 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:
@ -215,3 +222,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
```

@ -4,7 +4,7 @@ import (
"math/rand"
"time"
log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/v2tec/watchtower/container"
)

@ -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"

@ -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"

21
glide.lock generated

@ -1,5 +1,5 @@
hash: 9ddd729b207d71ce16ae9a0282ac87a046b9161ac4f15b108e86a03367172516
updated: 2017-01-24T20:45:43.1914053+01:00
hash: 23bcd62f9352c61d3aebc85a59f7ebbfaf80bc0201f45037cdea65820673ddeb
updated: 2018-03-01T13:21:55.8472118+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
@ -91,7 +91,7 @@ imports:
- client
- credentials
- name: github.com/docker/go-connections
version: 4ccf312bf1d35e5dbda654e57a9be4c3f3cd0366
version: ecb4cb2dd420ada7df7f2593d6c25441f65f69f2
subpackages:
- nat
- sockets
@ -110,12 +110,16 @@ imports:
version: 0eeaf8392f5b04950925b8a69fe70f110fa7cbfc
- name: github.com/inconshreveable/mousetrap
version: 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75
- name: github.com/johntdyer/slack-go
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: 2f7393a47307a16f8cee44a37b262e8b81021e3e
version: 9df8b306d01f59d3a8029be411de015b7304dd8f
repo: https://github.com/docker/runc.git
subpackages:
- libcontainer/configs
@ -135,7 +139,10 @@ imports:
- name: github.com/robfig/cron
version: 9585fd555638e77bba25f25db5c44b41f264aeb7
- name: github.com/Sirupsen/logrus
version: d26492970760ca5d33129d2d799e34be5c4782eb
version: ba1b36c82c5e05c4f912a88eab0dcd91a171688f
repo: https://github.com/sirupsen/logrus.git
- name: github.com/sirupsen/logrus
version: ba1b36c82c5e05c4f912a88eab0dcd91a171688f
- name: github.com/spf13/cobra
version: a3c09249f1a24a9d951f2738fb9b9256b8b42fa5
repo: https://github.com/dnephin/cobra.git

@ -1,6 +1,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
@ -26,3 +29,5 @@ import:
- context
- package: github.com/robfig/cron
version: 9585fd555638e77bba25f25db5c44b41f264aeb7
- package: github.com/johntdyer/slackrus
version: 3992f31

@ -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,9 +93,15 @@ 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{
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",
@ -136,6 +142,17 @@ 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",
},
}
if err := app.Run(os.Args); err != nil {

@ -9,7 +9,7 @@ import (
"strconv"
log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
@ -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 {

@ -1,7 +1,8 @@
package notifications
import (
log "github.com/Sirupsen/logrus"
"github.com/johntdyer/slackrus"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
@ -19,13 +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, acceptedLogLevels)
default:
log.Fatalf("Unknown notification type %q", t)
}

@ -0,0 +1,33 @@
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, acceptedLogLevels []log.Level) typeNotifier {
n := &slackTypeNotifier{
SlackrusHook: slackrus.SlackrusHook{
HookURL: c.GlobalString("notification-slack-hook-url"),
Username: c.GlobalString("notification-slack-identifier"),
AcceptedLevels: acceptedLogLevels,
},
}
log.AddHook(n)
return n
}
func (s *slackTypeNotifier) StartNotification() {}
func (s *slackTypeNotifier) SendNotification() {}
Loading…
Cancel
Save