watchtower/pkg/notifications/notifier.go

124 lines
3.3 KiB
Go
Raw Normal View History

package notifications
import (
"os"
"time"
ty "github.com/containrrr/watchtower/pkg/types"
"github.com/johntdyer/slackrus"
log "github.com/sirupsen/logrus"
2021-06-27 00:19:52 +02:00
"github.com/spf13/viper"
)
2017-10-29 23:45:01 -07:00
// NewNotifier creates and returns a new Notifier, using global configuration.
2021-06-27 00:19:52 +02:00
func NewNotifier() ty.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)
// slackrus does not allow log level TRACE, even though it's an accepted log level for logrus
if len(acceptedLogLevels) == 0 {
log.Fatalf("Unsupported notification log level provided: %s", level)
}
2021-06-27 00:19:52 +02:00
reportTemplate := viper.GetBool("notification-report")
tplString := viper.GetString("notification-template")
urls := viper.GetStringSlice("notification-url")
2021-06-27 00:19:52 +02:00
hostname := GetHostname()
urls, delay := AppendLegacyUrls(urls, GetTitle(hostname))
return newShoutrrrNotifier(tplString, acceptedLogLevels, !reportTemplate, hostname, delay, urls...)
}
// AppendLegacyUrls creates shoutrrr equivalent URLs from legacy notification flags
2021-06-27 00:19:52 +02:00
func AppendLegacyUrls(urls []string, title string) ([]string, time.Duration) {
// Parse types and create notifiers.
2021-06-27 00:19:52 +02:00
types := viper.GetStringSlice("notifications")
legacyDelay := time.Duration(0)
for _, t := range types {
2021-03-13 08:58:11 +01:00
var legacyNotifier ty.ConvertibleNotifier
var err error
switch t {
case emailType:
2021-06-27 00:19:52 +02:00
legacyNotifier = newEmailNotifier()
2017-11-27 12:04:08 +01:00
case slackType:
2021-06-27 00:19:52 +02:00
legacyNotifier = newSlackNotifier()
case msTeamsType:
2021-06-27 00:19:52 +02:00
legacyNotifier = newMsTeamsNotifier()
case gotifyType:
2021-06-27 00:19:52 +02:00
legacyNotifier = newGotifyNotifier()
case shoutrrrType:
continue
default:
log.Fatalf("Unknown notification type %q", t)
2021-03-13 08:58:11 +01:00
// Not really needed, used for nil checking static analysis
continue
}
2021-06-27 00:19:52 +02:00
shoutrrrURL, err := legacyNotifier.GetURL(title)
2021-03-13 08:58:11 +01:00
if err != nil {
log.Fatal("failed to create notification config: ", err)
}
urls = append(urls, shoutrrrURL)
if delayNotifier, ok := legacyNotifier.(ty.DelayNotifier); ok {
legacyDelay = delayNotifier.GetDelay()
}
log.WithField("URL", shoutrrrURL).Trace("created Shoutrrr URL from legacy notifier")
}
2021-06-27 00:19:52 +02:00
delay := GetDelay(legacyDelay)
return urls, delay
}
2021-03-13 08:58:11 +01:00
// GetDelay returns the legacy delay if defined, otherwise the delay as set by args is returned
2021-06-27 00:19:52 +02:00
func GetDelay(legacyDelay time.Duration) time.Duration {
if legacyDelay > 0 {
return legacyDelay
}
2021-06-27 00:19:52 +02:00
delay := viper.GetInt("notifications-delay")
if delay > 0 {
return time.Duration(delay) * time.Second
}
return time.Duration(0)
}
2021-03-13 08:58:11 +01:00
// GetTitle returns a common notification title with hostname appended
func GetTitle(hostname string) string {
title := "Watchtower updates"
if hostname != "" {
title += " on " + hostname
}
return title
}
2021-03-13 08:58:11 +01:00
// GetHostname returns the hostname as set by args or resolved from OS
2021-06-27 00:19:52 +02:00
func GetHostname() string {
hostname := viper.GetString("notifications-hostname")
if hostname != "" {
return hostname
} else if hostname, err := os.Hostname(); err == nil {
return hostname
2021-03-13 08:58:11 +01:00
}
return ""
2021-03-13 08:58:11 +01:00
}
// ColorHex is the default notification color used for services that support it (formatted as a CSS hex string)
const ColorHex = "#406170"
// ColorInt is the default notification color used for services that support it (as an int value)
const ColorInt = 0x406170