1
0
mirror of https://github.com/containrrr/watchtower.git synced 2025-01-05 14:50:44 +02:00
watchtower/pkg/notifications/notifier.go

75 lines
1.9 KiB
Go
Raw Normal View History

package notifications
import (
ty "github.com/containrrr/watchtower/pkg/types"
"github.com/johntdyer/slackrus"
log "github.com/sirupsen/logrus"
2019-06-22 22:04:36 +02:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
2017-10-30 08:45:01 +02:00
// Notifier can send log output as notification to admins, with optional batching.
type Notifier struct {
types []ty.Notifier
}
2017-10-30 08:45:01 +02:00
// NewNotifier creates and returns a new Notifier, using global configuration.
2019-06-22 22:04:36 +02:00
func NewNotifier(c *cobra.Command) *Notifier {
n := &Notifier{}
level := viper.GetString("notifications-level")
2019-06-22 22:04:36 +02:00
logLevel, err := log.ParseLevel(level)
if err != nil {
log.Fatalf("Notifications invalid log level: %s", err.Error())
}
acceptedLogLevels := slackrus.LevelThreshold(logLevel)
// Parse types and create notifiers.
types := viper.GetStringSlice("notifications")
2019-12-27 13:05:56 +02:00
if err != nil {
2020-04-24 13:45:24 +02:00
log.WithField("could not read notifications argument", log.Fields{"Error": err}).Fatal()
2019-12-27 13:05:56 +02:00
}
for _, t := range types {
var tn ty.Notifier
switch t {
case emailType:
tn = newEmailNotifier(c, acceptedLogLevels)
2017-11-27 13:04:08 +02:00
case slackType:
tn = newSlackNotifier(c, acceptedLogLevels)
case msTeamsType:
tn = newMsTeamsNotifier(c, acceptedLogLevels)
case gotifyType:
tn = newGotifyNotifier(c, acceptedLogLevels)
2020-03-23 12:40:55 +02:00
case shoutrrrType:
tn = newShoutrrrNotifier(c, acceptedLogLevels)
default:
log.Fatalf("Unknown notification type %q", t)
}
n.types = append(n.types, tn)
}
return n
}
2017-10-30 08:45:01 +02:00
// StartNotification starts a log batch. Notifications will be accumulated after this point and only sent when SendNotification() is called.
func (n *Notifier) StartNotification() {
for _, t := range n.types {
t.StartNotification()
}
}
2017-10-30 08:45:01 +02:00
// SendNotification sends any notifications accumulated since StartNotification() was called.
func (n *Notifier) SendNotification() {
for _, t := range n.types {
t.SendNotification()
}
}
// Close closes all notifiers.
func (n *Notifier) Close() {
for _, t := range n.types {
t.Close()
}
}