forked from longhorn/upgrade-responder
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
183 lines (163 loc) · 4.9 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/longhorn/upgrade-responder/upgraderesponder"
)
var VERSION = "v0.0.0-dev"
const (
FlagUpgradeResponseConfiguration = "upgrade-response-config"
EnvUpgradeResponseConfiguration = "UPGRADE_RESPONSE_CONFIG"
FlagApplicationName = "application-name"
EnvApplicationName = "APPLICATION_NAME"
FlagInfluxDBURL = "influxdb-url"
EnvInfluxDBURL = "INFLUXDB_URL"
FlagInfluxDBUser = "influxdb-user"
EnvInfluxDBUser = "INFLUXDB_USER"
FlagInfluxDBPass = "influxdb-pass"
EnvInfluxDBPass = "INFLUXDB_PASS"
FlagQueryPeriod = "query-period"
EnvQueryPeriod = "QUERY_PERIOD"
FlagGeoDB = "geodb"
EnvGeoDB = "GEODB"
FlagPort = "port"
EnvPort = "PORT"
)
func main() {
app := cli.NewApp()
app.Name = "upgrade-responder"
app.Version = VERSION
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, d",
Usage: "enable debug logging level",
EnvVar: "DEBUG",
},
}
app.Before = func(c *cli.Context) error {
if c.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
}
app.Commands = []cli.Command{
UpgradeResponderCmd(),
}
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
func UpgradeResponderCmd() cli.Command {
return cli.Command{
Name: "start",
Flags: []cli.Flag{
cli.StringFlag{
Name: FlagUpgradeResponseConfiguration,
EnvVar: EnvUpgradeResponseConfiguration,
Usage: "Specify the response configuration file for upgrade query",
},
cli.StringFlag{
Name: FlagApplicationName,
EnvVar: EnvApplicationName,
Usage: "Specify the name of the application that is using this upgrade checker. This will be used to create a database name <application-name>_upgrade_responder in the InfluxDB to store all data for this upgrade checker",
},
cli.StringFlag{
Name: FlagInfluxDBURL,
EnvVar: EnvInfluxDBURL,
Usage: "Specify the URL of InfluxDB",
},
cli.StringFlag{
Name: FlagInfluxDBUser,
EnvVar: EnvInfluxDBUser,
Usage: "Specify the InfluxDB user name",
},
cli.StringFlag{
Name: FlagInfluxDBPass,
EnvVar: EnvInfluxDBPass,
Usage: "Specify the InfluxDB password",
},
cli.StringFlag{
Name: FlagQueryPeriod,
EnvVar: EnvQueryPeriod,
Value: "1h",
Usage: "Specify the period for how often each instance of the application makes the request. Cannot change after set for the first time. This value should be the same as time in GROUP BY clause in Grafana",
},
cli.StringFlag{
Name: FlagGeoDB,
EnvVar: EnvGeoDB,
Usage: "Specify the path of to GeoDB file",
},
cli.IntFlag{
Name: FlagPort,
EnvVar: EnvPort,
Value: 8314,
Usage: "Specify the port number",
},
},
Action: func(c *cli.Context) error {
return startUpgradeResponder(c)
},
}
}
func startUpgradeResponder(c *cli.Context) error {
if err := validateCommandLineArguments(c); err != nil {
return err
}
cfg := c.String(FlagUpgradeResponseConfiguration)
influxURL := c.String(FlagInfluxDBURL)
influxUser := c.String(FlagInfluxDBUser)
influxPass := c.String(FlagInfluxDBPass)
queryPeriod := c.String(FlagQueryPeriod)
applicationName := c.String(FlagApplicationName)
geodb := c.String(FlagGeoDB)
port := c.Int(FlagPort)
done := make(chan struct{})
server, err := upgraderesponder.NewServer(done, applicationName, cfg, influxURL, influxUser, influxPass, queryPeriod, geodb)
if err != nil {
return err
}
router := http.Handler(upgraderesponder.NewRouter(server))
listeningAddress := fmt.Sprintf("0.0.0.0:%v", port)
go func() {
logrus.Infof("Server is listening at %v", listeningAddress)
// always returns error. ErrServerClosed on graceful close
if err := http.ListenAndServe(listeningAddress, router); err != http.ErrServerClosed {
logrus.Fatalf("%v", err)
}
<-done
}()
RegisterShutdownChannel(done)
<-done
return nil
}
func RegisterShutdownChannel(done chan struct{}) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
logrus.Infof("Receive %v to exit", sig)
close(done)
}()
}
func validateCommandLineArguments(c *cli.Context) error {
cfg := c.String(FlagUpgradeResponseConfiguration)
if cfg == "" {
return fmt.Errorf("no upgrade response configuration file specified")
}
applicationName := c.String(FlagApplicationName)
if applicationName == "" {
return fmt.Errorf("no application name specified")
}
queryPeriod := c.String(FlagQueryPeriod)
if _, err := time.ParseDuration(queryPeriod); err != nil {
return errors.Wrap(err, "fail to parse --query-period")
}
return nil
}