You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
watchtower/pkg/notifications/shoutrrr.go

129 lines
3.1 KiB
Go

4 years ago
package notifications
import (
"bytes"
4 years ago
"fmt"
"text/template"
4 years ago
"github.com/containrrr/shoutrrr"
4 years ago
"github.com/containrrr/shoutrrr/pkg/router"
4 years ago
t "github.com/containrrr/watchtower/pkg/types"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const (
shoutrrrDefaultTemplate = "{{range .}}{{.Message}}{{println}}{{end}}"
shoutrrrType = "shoutrrr"
4 years ago
)
// Implements Notifier, logrus.Hook
type shoutrrrTypeNotifier struct {
Urls []string
4 years ago
Router *router.ServiceRouter
4 years ago
entries []*log.Entry
logLevels []log.Level
template *template.Template
4 years ago
}
func newShoutrrrNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifier {
flags := c.PersistentFlags()
4 years ago
urls, _ := flags.GetStringArray("notification-url")
4 years ago
r, _ := shoutrrr.CreateSender(urls...)
4 years ago
n := &shoutrrrTypeNotifier{
Urls: urls,
4 years ago
Router: r,
4 years ago
logLevels: acceptedLogLevels,
template: getShoutrrrTemplate(c),
4 years ago
}
log.AddHook(n)
return n
}
func (e *shoutrrrTypeNotifier) buildMessage(entries []*log.Entry) string {
var body bytes.Buffer
e.template.Execute(&body, entries)
4 years ago
return body.String()
4 years ago
}
func (e *shoutrrrTypeNotifier) sendEntries(entries []*log.Entry) {
// Do the sending in a separate goroutine so we don't block the main process.
msg := e.buildMessage(entries)
go func() {
4 years ago
errs := e.Router.Send(msg, nil)
for i, err := range errs {
4 years ago
if err != nil {
// Use fmt so it doesn't trigger another notification.
fmt.Println("Failed to send notification via shoutrrr (url="+e.Urls[i]+"): ", err)
4 years ago
}
}
}()
}
func (e *shoutrrrTypeNotifier) StartNotification() {
if e.entries == nil {
e.entries = make([]*log.Entry, 0, 10)
}
}
func (e *shoutrrrTypeNotifier) SendNotification() {
if e.entries == nil || len(e.entries) <= 0 {
return
}
e.sendEntries(e.entries)
e.entries = nil
}
func (e *shoutrrrTypeNotifier) Levels() []log.Level {
return e.logLevels
}
func (e *shoutrrrTypeNotifier) Fire(entry *log.Entry) error {
if e.entries != nil {
e.entries = append(e.entries, entry)
} else {
// Log output generated outside a cycle is sent immediately.
e.sendEntries([]*log.Entry{entry})
}
return nil
}
func getShoutrrrTemplate(c *cobra.Command) *template.Template {
var tpl *template.Template
flags := c.PersistentFlags()
tplString, err := flags.GetString("notification-template")
// If we succeed in getting a non-empty template configuration
// try to parse the template string.
if tplString != "" && err == nil {
tpl, err = template.New("").Parse(tplString)
}
// In case of errors (either from parsing the template string
// or from getting the template configuration) log an error
// message about this and the fact that we'll use the default
// template instead.
if err != nil {
log.Errorf("Could not use configured notification template: %s. Using default template", err)
}
// If we had an error (either from parsing the template string
// or from getting the template configuration) or we a
// template wasn't configured (the empty template string)
// fallback to using the default template.
if err != nil || tplString == "" {
tpl = template.Must(template.New("").Parse(shoutrrrDefaultTemplate))
}
return tpl
}