2017-10-29 23:30:44 -07:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
2022-01-05 09:31:01 +01:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2019-07-21 19:58:19 +02:00
|
|
|
ty "github.com/containrrr/watchtower/pkg/types"
|
2018-03-02 13:08:40 +01:00
|
|
|
"github.com/johntdyer/slackrus"
|
2017-11-27 12:03:00 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-06-22 22:04:36 +02:00
|
|
|
"github.com/spf13/cobra"
|
2017-10-29 23:30:44 -07:00
|
|
|
)
|
|
|
|
|
2017-10-29 23:45:01 -07:00
|
|
|
// NewNotifier creates and returns a new Notifier, using global configuration.
|
2021-06-27 09:05:01 +02:00
|
|
|
func NewNotifier(c *cobra.Command) ty.Notifier {
|
2020-12-21 23:08:23 +01:00
|
|
|
f := c.PersistentFlags()
|
|
|
|
|
|
|
|
level, _ := f.GetString("notifications-level")
|
2019-06-22 22:04:36 +02:00
|
|
|
logLevel, err := log.ParseLevel(level)
|
2018-03-02 13:08:40 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Notifications invalid log level: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptedLogLevels := slackrus.LevelThreshold(logLevel)
|
2021-01-12 20:19:14 +01:00
|
|
|
// 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)
|
|
|
|
}
|
2018-03-02 13:08:40 +01:00
|
|
|
|
2021-06-27 09:05:01 +02:00
|
|
|
reportTemplate, _ := f.GetBool("notification-report")
|
|
|
|
tplString, _ := f.GetString("notification-template")
|
|
|
|
urls, _ := f.GetStringArray("notification-url")
|
2021-01-06 20:06:56 +01:00
|
|
|
|
2022-01-05 12:08:47 +01:00
|
|
|
hostname := GetHostname(c)
|
|
|
|
urls, delay := AppendLegacyUrls(urls, c, GetTitle(hostname))
|
2021-01-06 20:06:56 +01:00
|
|
|
|
2022-01-05 12:08:47 +01:00
|
|
|
return newShoutrrrNotifier(tplString, acceptedLogLevels, !reportTemplate, hostname, delay, urls...)
|
2021-01-06 20:06:56 +01:00
|
|
|
}
|
|
|
|
|
2021-06-27 09:05:01 +02:00
|
|
|
// AppendLegacyUrls creates shoutrrr equivalent URLs from legacy notification flags
|
2022-01-05 12:08:47 +01:00
|
|
|
func AppendLegacyUrls(urls []string, cmd *cobra.Command, title string) ([]string, time.Duration) {
|
2021-03-28 21:04:11 +02:00
|
|
|
|
2021-06-27 09:05:01 +02:00
|
|
|
// Parse types and create notifiers.
|
|
|
|
types, err := cmd.Flags().GetStringSlice("notifications")
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatal("could not read notifications argument")
|
2021-04-18 18:11:46 +02:00
|
|
|
}
|
|
|
|
|
2022-03-09 11:03:06 +01:00
|
|
|
legacyDelay := time.Duration(0)
|
2022-01-05 09:31:01 +01:00
|
|
|
|
2017-10-29 23:30:44 -07:00
|
|
|
for _, t := range types {
|
2021-01-06 20:06:56 +01:00
|
|
|
|
2021-03-13 08:58:11 +01:00
|
|
|
var legacyNotifier ty.ConvertibleNotifier
|
|
|
|
var err error
|
2021-01-06 20:06:56 +01:00
|
|
|
|
2017-10-29 23:30:44 -07:00
|
|
|
switch t {
|
|
|
|
case emailType:
|
2021-01-06 20:06:56 +01:00
|
|
|
legacyNotifier = newEmailNotifier(cmd, []log.Level{})
|
2017-11-27 12:04:08 +01:00
|
|
|
case slackType:
|
2021-01-06 20:06:56 +01:00
|
|
|
legacyNotifier = newSlackNotifier(cmd, []log.Level{})
|
2018-03-13 08:39:54 +02:00
|
|
|
case msTeamsType:
|
2021-06-27 09:05:01 +02:00
|
|
|
legacyNotifier = newMsTeamsNotifier(cmd, []log.Level{})
|
2019-07-22 21:17:54 +02:00
|
|
|
case gotifyType:
|
2021-01-06 20:06:56 +01:00
|
|
|
legacyNotifier = newGotifyNotifier(cmd, []log.Level{})
|
2021-06-27 09:05:01 +02:00
|
|
|
case shoutrrrType:
|
|
|
|
continue
|
2017-10-29 23:30:44 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-05 12:08:47 +01:00
|
|
|
shoutrrrURL, err := legacyNotifier.GetURL(cmd, title)
|
2021-03-13 08:58:11 +01:00
|
|
|
if err != nil {
|
2021-09-19 18:05:10 +02:00
|
|
|
log.Fatal("failed to create notification config: ", err)
|
2017-10-29 23:30:44 -07:00
|
|
|
}
|
2021-06-27 09:05:01 +02:00
|
|
|
urls = append(urls, shoutrrrURL)
|
2021-01-06 20:06:56 +01:00
|
|
|
|
2022-01-05 09:31:01 +01:00
|
|
|
if delayNotifier, ok := legacyNotifier.(ty.DelayNotifier); ok {
|
2022-03-09 11:03:06 +01:00
|
|
|
legacyDelay = delayNotifier.GetDelay()
|
2022-01-05 09:31:01 +01:00
|
|
|
}
|
|
|
|
|
2021-04-18 18:35:15 +02:00
|
|
|
log.WithField("URL", shoutrrrURL).Trace("created Shoutrrr URL from legacy notifier")
|
2020-08-08 22:55:51 +02:00
|
|
|
}
|
2022-03-09 11:03:06 +01:00
|
|
|
|
|
|
|
delay := GetDelay(cmd, legacyDelay)
|
2022-01-05 09:31:01 +01:00
|
|
|
return urls, delay
|
2020-08-08 22:55:51 +02:00
|
|
|
}
|
2021-03-13 08:58:11 +01:00
|
|
|
|
2022-03-09 11:03:06 +01:00
|
|
|
// GetDelay returns the legacy delay if defined, otherwise the delay as set by args is returned
|
|
|
|
func GetDelay(c *cobra.Command, legacyDelay time.Duration) time.Duration {
|
|
|
|
if legacyDelay > 0 {
|
|
|
|
return legacyDelay
|
|
|
|
}
|
|
|
|
|
|
|
|
delay, _ := c.PersistentFlags().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
|
2022-01-05 12:08:47 +01:00
|
|
|
func GetTitle(hostname string) string {
|
|
|
|
title := "Watchtower updates"
|
|
|
|
if hostname != "" {
|
|
|
|
title += " on " + hostname
|
|
|
|
}
|
|
|
|
return title
|
|
|
|
}
|
2021-03-13 08:58:11 +01:00
|
|
|
|
2022-01-05 12:08:47 +01:00
|
|
|
// GetHostname returns the hostname as set by args or resolved from OS
|
|
|
|
func GetHostname(c *cobra.Command) string {
|
2021-06-24 08:29:20 +10:00
|
|
|
|
2022-01-05 12:08:47 +01:00
|
|
|
f := c.PersistentFlags()
|
2021-06-24 08:29:20 +10:00
|
|
|
hostname, _ := f.GetString("notifications-hostname")
|
|
|
|
|
|
|
|
if hostname != "" {
|
2022-01-05 12:08:47 +01:00
|
|
|
return hostname
|
2021-06-24 08:29:20 +10:00
|
|
|
} else if hostname, err := os.Hostname(); err == nil {
|
2022-01-05 12:08:47 +01:00
|
|
|
return hostname
|
2021-03-13 08:58:11 +01:00
|
|
|
}
|
|
|
|
|
2022-01-05 12:08:47 +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
|