-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnotification_config.go
127 lines (111 loc) · 2.55 KB
/
notification_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package patrol
import (
"context"
"fmt"
"net/http"
"net/url"
"os/exec"
"strings"
"time"
"github.com/karimsa/patrol/internal/logger"
)
type webhookNotification struct {
client http.Client
URL *url.URL `yaml:"url"`
Method string
Headers map[string]string
Body string
}
func (wn *webhookNotification) UnmarshalYAML(unmarshal func(interface{}) error) error {
var raw struct {
URL string `yaml:"url"`
Method string
Headers map[string]string
Body string
}
if err := unmarshal(&raw); err != nil {
return err
}
*wn = webhookNotification{
Method: "GET",
Headers: raw.Headers,
Body: raw.Body,
}
if u, err := url.Parse(raw.URL); err != nil {
return fmt.Errorf("Failed to parse url: %s", err)
} else {
wn.URL = u
}
if raw.Method != "" {
wn.Method = strings.ToUpper(raw.Method)
}
if wn.URL.Host == "" {
return fmt.Errorf("Hostname is required for URLs in webhooks")
}
return nil
}
func (wn *webhookNotification) exec() error {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
req, err := http.NewRequestWithContext(ctx, wn.Method, wn.URL.String(), strings.NewReader(wn.Body))
if err != nil {
return err
}
for key, val := range wn.Headers {
req.Header[key] = []string{val}
}
res, err := wn.client.Do(req)
cancel()
if err != nil {
return err
}
if res.StatusCode >= 400 {
return fmt.Errorf("Webhook returned status: %d", res.StatusCode)
}
return nil
}
type commandNotification struct {
command string
}
func (cn *commandNotification) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal(&cn.command); err != nil {
return err
}
return nil
}
func (cn *commandNotification) exec() error {
cmd := exec.Command("/bin/sh", "-c", cn.command)
if err := cmd.Run(); err != nil {
return err
}
if cmd.ProcessState.ExitCode() != 0 {
return fmt.Errorf("command '%s' failed with exit %d", cn.command, cmd.ProcessState.ExitCode())
}
return nil
}
type singleNotificationConfig struct {
Webhook *webhookNotification
Command *commandNotification
}
type specificNotifier interface {
exec() error
}
func (sn *singleNotificationConfig) Run() {
logger := logger.New(logger.LevelInfo, "notifier:")
var notifier specificNotifier
if sn.Webhook != nil {
notifier = sn.Webhook
}
if sn.Command != nil {
notifier = sn.Command
}
if notifier == nil {
logger.Warnf("Could not send notification using empty notifier")
} else {
go func() {
if err := notifier.exec(); err != nil {
logger.Warnf("Failed to send notification: %s", err)
}
}()
}
}