forked from cloudfoundry-attic/hm9000
/
hm9000.go
194 lines (181 loc) · 6.41 KB
/
hm9000.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
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"fmt"
"github.com/cloudfoundry-incubator/cf-debug-server"
"github.com/cloudfoundry/gosteno"
"github.com/cloudfoundry/hm9000/config"
"github.com/cloudfoundry/hm9000/helpers/logger"
"github.com/cloudfoundry/hm9000/hm"
"github.com/codegangsta/cli"
"os"
"path"
)
func main() {
app := cli.NewApp()
app.Name = "HM9000"
app.Usage = "Start the various HM9000 components"
app.Version = "0.0.9000"
app.Commands = []cli.Command{
{
Name: "fetch_desired",
Description: "Fetches desired state",
Usage: "hm fetch_desired --config=/path/to/config --poll",
Flags: []cli.Flag{
cli.StringFlag{Name: "config", Value: "", Usage: "Path to config file"},
cli.BoolFlag{Name: "poll", Usage: "If true, poll repeatedly with an interval defined in config"},
cli.StringFlag{Name: "debugAddr", Value: "", Usage: "address to serve debug info"},
},
Action: func(c *cli.Context) {
logger, _, conf := loadLoggerAndConfig(c, "fetcher")
hm.FetchDesiredState(logger, conf, c.Bool("poll"))
},
},
{
Name: "listen",
Description: "Listens over the NATS for the actual state",
Usage: "hm listen --config=/path/to/config",
Flags: []cli.Flag{
cli.StringFlag{Name: "config", Value: "", Usage: "Path to config file"},
cli.StringFlag{Name: "debugAddr", Value: "", Usage: "address to serve debug info"},
},
Action: func(c *cli.Context) {
logger, _, conf := loadLoggerAndConfig(c, "listener")
hm.StartListeningForActual(logger, conf)
},
},
{
Name: "analyze",
Description: "Analyze the desired and actual state and enqueue start/stop messages",
Usage: "hm analyze --config=/path/to/config --poll",
Flags: []cli.Flag{
cli.StringFlag{Name: "config", Value: "", Usage: "Path to config file"},
cli.BoolFlag{Name: "poll", Usage: "If true, poll repeatedly with an interval defined in config"},
cli.StringFlag{Name: "debugAddr", Value: "", Usage: "address to serve debug info"},
},
Action: func(c *cli.Context) {
logger, _, conf := loadLoggerAndConfig(c, "analyzer")
hm.Analyze(logger, conf, c.Bool("poll"))
},
},
{
Name: "send",
Description: "Send the enqueued start/stop messages",
Usage: "hm send --config=/path/to/config --poll",
Flags: []cli.Flag{
cli.StringFlag{Name: "config", Value: "", Usage: "Path to config file"},
cli.BoolFlag{Name: "poll", Usage: "If true, poll repeatedly with an interval defined in config"},
cli.StringFlag{Name: "debugAddr", Value: "", Usage: "address to serve debug info"},
},
Action: func(c *cli.Context) {
logger, _, conf := loadLoggerAndConfig(c, "sender")
hm.Send(logger, conf, c.Bool("poll"))
},
},
{
Name: "evacuator",
Description: "Send NATS start message for evacuated apps",
Usage: "hm evacuator --config=/path/to/config",
Flags: []cli.Flag{
cli.StringFlag{Name: "config", Value: "", Usage: "Path to config file"},
cli.StringFlag{Name: "debugAddr", Value: "", Usage: "address to serve debug info"},
},
Action: func(c *cli.Context) {
logger, _, conf := loadLoggerAndConfig(c, "evacuator")
hm.StartEvacuator(logger, conf)
},
},
{
Name: "serve_metrics",
Description: "Listens for Varz calls to serve metrics",
Usage: "hm serve_metrics --config=/path/to/config",
Flags: []cli.Flag{
cli.StringFlag{Name: "config", Value: "", Usage: "Path to config file"},
cli.StringFlag{Name: "debugAddr", Value: "", Usage: "address to serve debug info"},
},
Action: func(c *cli.Context) {
logger, steno, conf := loadLoggerAndConfig(c, "metrics_server")
hm.ServeMetrics(steno, logger, conf)
},
},
{
Name: "serve_api",
Description: "Serve app API over http",
Usage: "hm serve_api --config=/path/to/config",
Flags: []cli.Flag{
cli.StringFlag{Name: "config", Value: "", Usage: "Path to config file"},
cli.StringFlag{Name: "debugAddr", Value: "", Usage: "address to serve debug info"},
},
Action: func(c *cli.Context) {
logger, _, conf := loadLoggerAndConfig(c, "apiserver")
hm.ServeAPI(logger, conf)
},
},
{
Name: "shred",
Description: "Deletes empty directories from the store",
Usage: "hm shred --config=/path/to/config --poll",
Flags: []cli.Flag{
cli.StringFlag{Name: "config", Value: "", Usage: "Path to config file"},
cli.BoolFlag{Name: "poll", Usage: "If true, poll repeatedly with an interval defined in config"},
cli.StringFlag{Name: "debugAddr", Value: "", Usage: "address to serve debug info"},
},
Action: func(c *cli.Context) {
logger, _, conf := loadLoggerAndConfig(c, "shredder")
hm.Shred(logger, conf, c.Bool("poll"))
},
},
{
Name: "dump",
Description: "Dumps contents of the data store",
Usage: "hm dump --config=/path/to/config",
Flags: []cli.Flag{
cli.StringFlag{Name: "config", Value: "", Usage: "Path to config file"},
cli.BoolFlag{Name: "raw", Usage: "If set, dump the unstructured contents of the database"},
cli.StringFlag{Name: "debugAddr", Value: "", Usage: "address to serve debug info"},
},
Action: func(c *cli.Context) {
logger, _, conf := loadLoggerAndConfig(c, "dumper")
hm.Dump(logger, conf, c.Bool("raw"))
},
},
}
app.Run(os.Args)
}
func loadLoggerAndConfig(c *cli.Context, component string) (logger.Logger, *gosteno.Logger, *config.Config) {
configPath := c.String("config")
if configPath == "" {
fmt.Printf("Config path required")
os.Exit(1)
}
conf, err := config.FromFile(configPath)
if err != nil {
fmt.Printf("Failed to load config: %s", err.Error())
os.Exit(1)
}
sinks := make([]gosteno.Sink, 0, 3)
if conf.LogDirectory != "" {
logName := fmt.Sprintf("hm9000_%s.log", component)
logFile := path.Join(conf.LogDirectory, logName)
sinks = append(sinks, gosteno.NewFileSink(logFile))
} else {
sinks = append(sinks, gosteno.NewIOSink(os.Stdout))
}
sinks = append(sinks, gosteno.NewSyslogSink("vcap.hm9000."+component))
stenoConf := &gosteno.Config{
Sinks: sinks,
Level: conf.LogLevel(),
Codec: gosteno.NewJsonCodec(),
}
gosteno.Init(stenoConf)
steno := gosteno.NewLogger("vcap.hm9000." + component)
hmLogger := logger.NewRealLogger(steno)
debugAddr := c.String("debugAddr")
if debugAddr != "" {
_, err = cf_debug_server.Run(debugAddr, nil)
if err != nil {
hmLogger.Error("Failed to start debug server", err)
os.Exit(1)
}
}
return hmLogger, steno, conf
}