-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
156 lines (127 loc) · 3.76 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/smtp"
"os"
"os/signal"
"strings"
"syscall"
"time"
"google.golang.org/appengine"
"github.com/genuinetools/pkg/cli"
"github.com/genuinetools/upmail/email"
"github.com/genuinetools/upmail/version"
"github.com/sirupsen/logrus"
"github.com/sourcegraph/checkup"
)
var (
configFile string
recipient string
interval time.Duration
ae bool
mailgunAPIKey string
mailgunDomain string
smtpServer string
smtpSender string
smtpUsername string
smtpPassword string
debug bool
)
func main() {
// Create a new cli program.
p := cli.NewProgram()
p.Name = "upmail"
p.Description = "Email notification hook for https://github.com/sourcegraph/checkup"
// Set the GitCommit and Version.
p.GitCommit = version.GITCOMMIT
p.Version = version.VERSION
// Setup the global flags.
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.StringVar(&configFile, "config", "checkup.json", "config file location")
p.FlagSet.StringVar(&recipient, "recipient", "", "recipient for email notifications")
p.FlagSet.DurationVar(&interval, "interval", 10*time.Minute, "check interval (ex. 5ms, 10s, 1m, 3h)")
p.FlagSet.BoolVar(&ae, "appengine", false, "enable the server for running in Google App Engine")
p.FlagSet.StringVar(&mailgunAPIKey, "mailgun", "", "Mailgun API Key to use for sending email (optional)")
p.FlagSet.StringVar(&mailgunDomain, "mailgun-domain", "", "Mailgun Domain to use for sending email (optional)")
p.FlagSet.StringVar(&smtpServer, "server", "", "SMTP server for email notifications")
p.FlagSet.StringVar(&smtpSender, "sender", "", "SMTP default sender email address for email notifications")
p.FlagSet.StringVar(&smtpUsername, "username", "", "SMTP server username")
p.FlagSet.StringVar(&smtpPassword, "password", "", "SMTP server password")
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
// Set the before function.
p.Before = func(ctx context.Context) error {
// Set the log level.
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
if len(configFile) < 1 {
return fmt.Errorf("config file cannot be empty")
}
if len(recipient) < 1 {
return fmt.Errorf("recipient cannot be empty")
}
if len(smtpServer) < 1 && len(mailgunAPIKey) < 1 && len(mailgunDomain) < 1 {
return fmt.Errorf("SMTP server OR Mailgun API Key cannot be empty")
}
return nil
}
// Set the main program action.
p.Action = func(ctx context.Context, args []string) error {
ticker := time.NewTicker(interval)
// On ^C, or SIGTERM handle exit.
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt)
signal.Notify(s, syscall.SIGTERM)
go func() {
for sig := range s {
ticker.Stop()
logrus.Infof("Received %s, exiting.", sig.String())
os.Exit(0)
}
}()
configBytes, err := ioutil.ReadFile(configFile)
if err != nil {
logrus.Fatal(err)
}
var c checkup.Checkup
err = json.Unmarshal(configBytes, &c)
if err != nil {
logrus.Fatal(err)
}
n := email.Notifier{
MailgunAPIKey: mailgunAPIKey,
MailgunDomain: mailgunDomain,
Recipient: recipient,
Server: smtpServer,
Sender: smtpSender,
Auth: smtp.PlainAuth(
"",
smtpUsername,
smtpPassword,
strings.SplitN(smtpServer, ":", 2)[0],
),
}
c.Notifier = n
logrus.Infof("Starting checks that will send emails to: %s", recipient)
if ae {
// setup necessary app engine health checks and listener
go appengine.Main()
}
logrus.Info("Performing initial check")
if err := c.CheckAndStore(); err != nil {
logrus.Fatalf("CheckAndStore failed: %v", err)
}
for range ticker.C {
if err := c.CheckAndStore(); err != nil {
logrus.Warnf("CheckAndStore failed: %v", err)
}
}
return nil
}
// Run our program.
p.Run()
}