/
main.go
133 lines (107 loc) · 3.81 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
package main
import (
"encoding/gob"
"net"
"os"
"time"
"github.com/Sirupsen/logrus"
"github.com/alecthomas/kingpin"
metrics_graphite "github.com/cyberdelia/go-metrics-graphite"
gocache "github.com/patrickmn/go-cache"
"github.com/resourced/resourced-master/application"
"github.com/resourced/resourced-master/models/pg"
)
var (
appConfDirFromEnv = os.Getenv("RESOURCED_MASTER_CONFIG_DIR")
appConfDirFromFlag = kingpin.Flag("conf", "Path to config directory").Short('c').String()
appServerArg = kingpin.Command("server", "Run resourced-master server.").Default()
appMigrateArg = kingpin.Command("migrate", "CLI interface for resourced-master database migration.")
appMigratePG = appMigrateArg.Command("pg", "Run PostgreSQL migrations.")
appMigratePGCmd = appMigratePG.Arg("command", "PostgreSQL migration commands. Valid choices: up or down").Required().String()
appMigrateCassandra = appMigrateArg.Command("cassandra", "Run Cassandra migrations.")
appMigrateCassandraCmd = appMigrateCassandra.Arg("command", "Cassandra migration commands. Valid choices: up or down").Required().String()
)
func init() {
gob.Register(&pg.UserRow{})
gob.Register(&pg.ClusterRow{})
}
func main() {
kingpin.UsageTemplate(kingpin.CompactUsageTemplate).Author("Didip Kerabat")
parsedCLIArgs := kingpin.Parse()
if appConfDirFromEnv == "" && *appConfDirFromFlag == "" {
logrus.Fatal("Path to config directory is required. You must set RESOURCED_MASTER_CONFIG_DIR environment variable or -c flag.")
}
configDir := appConfDirFromEnv
if configDir == "" {
configDir = *appConfDirFromFlag
}
app, err := application.New(configDir)
if err != nil {
logrus.Fatal(err)
}
if app.GeneralConfig.LogLevel == "debug" {
logrus.SetLevel(logrus.DebugLevel)
} else if app.GeneralConfig.LogLevel == "error" {
logrus.SetLevel(logrus.ErrorLevel)
} else if app.GeneralConfig.LogLevel == "fatal" {
logrus.SetLevel(logrus.FatalLevel)
} else if app.GeneralConfig.LogLevel == "panic" {
logrus.SetLevel(logrus.PanicLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
switch parsedCLIArgs {
case "server":
// Create MessageBus
bus, err := app.NewMessageBus(app.GeneralConfig)
if err != nil {
logrus.Fatal(err)
}
app.MessageBus = bus
go app.MessageBus.ManageClients()
go app.MessageBus.OnReceive(app.MessageBusHandlers())
// Broadcast heartbeat
go app.SendHeartbeat()
go func() {
// On boot, assign self to peers map and send a message to RefetchChecksChan
app.Peers.Set(app.FullAddr(), true, gocache.DefaultExpiration)
app.RefetchChecksChan <- true
}()
// Run all checks
app.CheckAndRunTriggers()
// Prune old timeseries data
go app.PruneAll()
// Publish metrics to local agent, which is a graphite endpoint.
go func() {
statsInterval, err := time.ParseDuration(app.GeneralConfig.LocalAgent.ReportMetricsInterval)
if err == nil {
addr, err := net.ResolveTCPAddr("tcp", "localhost:"+app.GeneralConfig.LocalAgent.GraphiteTCPPort)
if err == nil {
go metrics_graphite.Graphite(app.MetricsRegistry, statsInterval, "ResourcedMaster", addr)
}
}
}()
// Create HTTP server
srv, err := app.NewHTTPServer()
if err != nil {
logrus.Fatal(err)
}
if app.GeneralConfig.HTTPS.CertFile != "" && app.GeneralConfig.HTTPS.KeyFile != "" {
logrus.WithFields(logrus.Fields{"Addr": app.GeneralConfig.Addr}).Info("Running HTTPS server")
srv.ListenAndServeTLS(app.GeneralConfig.HTTPS.CertFile, app.GeneralConfig.HTTPS.KeyFile)
} else {
logrus.WithFields(logrus.Fields{"Addr": app.GeneralConfig.Addr}).Info("Running HTTP server")
srv.ListenAndServe()
}
case "migrate pg":
err := app.MigrateAllPG(*appMigratePGCmd)
if err != nil {
logrus.Fatal(err)
}
case "migrate cassandra":
err := app.MigrateAllCassandra(*appMigrateCassandraCmd)
if err != nil {
logrus.Fatal(err)
}
}
}