-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
319 lines (272 loc) · 7.97 KB
/
run.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright (C) 2018. See AUTHORS.
package rothko
import (
"context"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"plugin"
"syscall"
"time"
"github.com/urfave/cli"
"github.com/vivint/rothko/api"
"github.com/vivint/rothko/config"
"github.com/vivint/rothko/data"
"github.com/vivint/rothko/dump"
"github.com/vivint/rothko/external"
"github.com/vivint/rothko/internal/junk"
"github.com/vivint/rothko/internal/tgzfs"
"github.com/vivint/rothko/internal/tmplfs"
"github.com/vivint/rothko/registry"
"github.com/vivint/rothko/ui"
"github.com/zeebo/errs"
)
var runCommand = cli.Command{
Name: "run",
Usage: "run rothko with some configuration",
ArgsUsage: t(`
<path to rothko config>
To generate a rothko config, see the init command.
`),
Description: t(`
The run command starts up the rothko system
`),
Action: func(c *cli.Context) error {
if err := checkArgs(c, 1); err != nil {
return err
}
data, err := ioutil.ReadFile(c.Args().Get(0))
if err != nil {
return errs.Wrap(err)
}
conf, err := config.Load(data)
if err != nil {
return err
}
started, err := run(context.Background(), conf)
if started {
return err
}
fmt.Printf("Invalid Configuration: %v\n", err)
return handled.Wrap(err)
},
}
// run creates and starts all of the services defined by the config. It exits
// when the context is canceled, or when an appropriate signal is sent to the
// binary. The started return value is true if the services were created and
// started before returning.
func run(ctx context.Context, conf *config.Config) (started bool, err error) {
// load the plugins
for _, path := range conf.Main.Plugins {
external.Infow("loading plugin",
"plugin", path,
)
_, err := plugin.Open(path)
if err != nil {
return false, errs.Wrap(err)
}
}
// create a launcher to keep track of all the tasks
var launcher junk.Launcher
// create the database
external.Infow("creating database",
"kind", conf.Database.Kind,
"config", conf.Database.Config,
)
db, err := registry.NewDatabase(ctx,
conf.Database.Kind, conf.Database.Config)
if err != nil {
return false, errs.Wrap(err)
}
// create the distribution params from the registry
external.Infow("creating distribution",
"kind", conf.Dist.Kind,
"config", conf.Dist.Config,
)
params, err := registry.NewDistribution(ctx,
conf.Dist.Kind, conf.Dist.Config)
if err != nil {
return false, errs.Wrap(err)
}
// create the writer
w := data.NewWriter(params)
// create the dumper
dumper := dump.New(dump.Options{
DB: db,
Period: conf.Main.Duration,
})
// create the api server
external.Infow("creating api",
"config", conf.API.Redact(),
)
var static http.Handler = tarballWarning{}
if ui.Tarball != nil {
fs, err := tgzfs.New(ui.Tarball)
if err != nil {
return false, errs.Wrap(err)
}
static = tmplfs.New(fs)
}
srv := &http.Server{
Addr: conf.API.Address,
Handler: api.New(db, static, api.Options{
Origin: conf.API.Origin,
Username: conf.API.Security.Username,
Password: conf.API.Security.Password,
}),
}
// create and queue the listeners
for _, entity := range conf.Listeners {
entity := entity
external.Infow("creating listener",
"kind", entity.Kind,
"config", entity.Config,
)
listener, err := registry.NewListener(ctx, entity.Kind, entity.Config)
if err != nil {
return false, errs.Wrap(err)
}
launcher.Queue(func(ctx context.Context) error {
external.Infow("starting listener",
"kind", entity.Kind,
"config", entity.Config,
)
return listener.Run(ctx, w)
})
}
// queue the worker that periodically dumps in to the database
launcher.Queue(func(ctx context.Context) error {
external.Infow("starting dumper")
return dumper.Run(ctx, w)
})
// queue the api server
launcher.Queue(func(ctx context.Context) error {
external.Infow("starting api",
"config", conf.API.Redact(),
)
return runServer(ctx, srv, conf.API.TLS.Cert, conf.API.TLS.Key)
})
// because we don't want to rerun the database on sigint, we launch all of
// the services under one launcher with the sigint_ctx, and launch the
// database under another launcher with a parent of the sigint_ctx.
//
// in the case the sigint_ctx launcher returns no error, we run a dump with
// a timeout of 60 seconds, before returning and causing all of the other
// tasks in the launcher to be canceled.
var parent junk.Launcher
// queue all the other services
parent.Queue(func(ctx context.Context) error {
sigint_ctx, cancel := junk.WithSignal(ctx, syscall.SIGINT)
defer cancel()
if err := launcher.Run(sigint_ctx); err != nil {
return err
}
// run with the parent of the sigint_ctx so that we get canceled if
// the database Run exits.
ctx, cancel = context.WithTimeout(ctx, 60*time.Second)
defer cancel()
external.Infow("performing last dump")
dumper.Dump(ctx, w)
return nil
})
// queue the database
parent.Queue(func(ctx context.Context) error {
external.Infow("starting database",
"kind", conf.Database.Kind,
"config", conf.Database.Config,
)
return db.Run(ctx)
})
// run our stuff
return true, errs.Wrap(parent.Run(ctx))
}
// runServer runs srv and shuts it down when the context is canceled.
func runServer(ctx context.Context, srv *http.Server, cert, key string) (
err error) {
// I must be going goofy or something, but I see absolutely no way to
// safely use srv.ListenAndServe() with srv.Shutdown(ctx), because you
// can't be sure that the ListenAndServe call has started enough for the
// Shutdown to have an effect. So, let's make a listener ourselves so that
// we can at least close that on a context cancel, THEN call Shutdown. The
// bad news about that is, ListenAndServe wraps the listener it makes in
// a TCP keep alive wrapper, whereas Serve doesn't! That means we get to
// implement it ourselves. The net/http library is a tire fire.
lis, err := net.Listen("tcp", srv.Addr)
if err != nil {
return errs.Wrap(err)
}
defer lis.Close()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// monitor when we're done and try to shut down the server.
go func(ctx context.Context) {
<-ctx.Done()
// give the shutdown one minute to clean up
ctx, cancel := context.WithTimeout(
context.Background(), 60*time.Second)
defer cancel()
srv.Shutdown(ctx)
// Just in case we raced and called shutdown before we called Serve.
// We do this second because we don't want to error in the case that
// the context is canceled, but if we lose the race, that's ok to
// error i guess.
lis.Close()
}(ctx)
ke_lis := keepAliveWrapper{lis.(*net.TCPListener)}
if cert != "" || key != "" {
err = srv.ServeTLS(ke_lis, cert, key)
} else {
err = srv.Serve(ke_lis)
}
if err == http.ErrServerClosed {
err = nil
}
return errs.Wrap(err)
}
// keepAliveWrapper sets tcp keep alive options on incomming connections.
type keepAliveWrapper struct {
*net.TCPListener
}
// Listen returns a connection with tcp keep alive options set.
func (k keepAliveWrapper) Accept() (net.Conn, error) {
conn, err := k.TCPListener.AcceptTCP()
if err != nil {
return nil, err
}
conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(3 * time.Minute)
return conn, nil
}
// tarballWarning is an http.Handler that is served for the static site if a
// ui tarball has not been generated.
type tarballWarning struct{}
// ServeHTTP implements the http.Handler interface, responding with a warning
// if index.html is requested.
func (tarballWarning) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/index.html", "index.html", "/", "":
default:
http.NotFound(w, req)
return
}
io.WriteString(w, `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Rothko</title>
</head>
<body>
<h1>Rothko</h1>
<p>You have not generated the ui in this build of Rothko.</p>
<p>If you would like to have a nice ui, run
<span style="font-family: monospace">roth generate</span> after sourcing
<span style="font-family: monospace">.setup</span> in your shell, and
rebuild the binary.
</p>
</body>
</html>
`)
}