-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathcmd_start.go
373 lines (311 loc) · 11.6 KB
/
cmd_start.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package main
import (
"context"
"fmt"
"os"
"os/signal"
"sort"
"strings"
"sync"
"syscall"
"time"
"github.com/ovh/cds/engine/api"
"github.com/ovh/cds/engine/cdn"
"github.com/ovh/cds/engine/elasticsearch"
"github.com/ovh/cds/engine/hatchery/kubernetes"
"github.com/ovh/cds/engine/hatchery/local"
"github.com/ovh/cds/engine/hatchery/openstack"
"github.com/ovh/cds/engine/hatchery/swarm"
"github.com/ovh/cds/engine/hatchery/vsphere"
"github.com/ovh/cds/engine/hooks"
"github.com/ovh/cds/engine/migrateservice"
"github.com/ovh/cds/engine/repositories"
"github.com/ovh/cds/engine/service"
"github.com/ovh/cds/engine/ui"
"github.com/ovh/cds/engine/vcs"
"github.com/ovh/cds/sdk"
cdslog "github.com/ovh/cds/sdk/log"
"github.com/ovh/cds/sdk/telemetry"
"github.com/rockbears/log"
"github.com/spf13/cobra"
)
func init() {
startCmd.Flags().StringVar(&flagStartConfigFile, "config", "", "config file")
startCmd.Flags().StringVar(&flagStartRemoteConfig, "remote-config", "", "(optional) consul configuration store")
startCmd.Flags().StringVar(&flagStartRemoteConfigKey, "remote-config-key", "cds/config.api.toml", "(optional) consul configuration store key")
startCmd.Flags().StringVar(&flagStartVaultAddr, "vault-addr", "", "(optional) Vault address to fetch secrets from vault (example: https://vault.mydomain.net:8200)")
startCmd.Flags().StringVar(&flagStartVaultToken, "vault-token", "", "(optional) Vault token to fetch secrets from vault")
}
var (
flagStartConfigFile string
flagStartRemoteConfig string
flagStartRemoteConfigKey string
flagStartVaultAddr string
flagStartVaultToken string
)
type serviceConf struct {
arg string
service service.Service
cfg interface{}
}
var startCmd = &cobra.Command{
Use: "start",
Short: "Start CDS",
Long: `
Start CDS Engine Services
#### API
This is the core component of CDS.
#### Hatcheries
They are the components responsible for spawning workers. Supported integrations/orchestrators are:
* Local machine
* Openstack
* Docker Swarm
* Openstack
* Vsphere
#### Hooks
This component operates CDS workflow hooks
#### Repositories
This component operates CDS workflow repositories
#### VCS
This component operates CDS VCS connectivity
#### CDN
This component operates CDS CDN to handle storage
Start all of this with a single command:
$ engine start [api] [cdn] [hatchery:local] [hatchery:openstack] [hatchery:swarm] [hatchery:vsphere] [elasticsearch] [hooks] [vcs] [repositories] [migrate] [ui]
All the services are using the same configuration file format.
You have to specify where the toml configuration is. It can be a local file, provided by consul or vault.
You can also use or override toml file with environment variable.
See $ engine config command for more details.
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
args = strings.Split(os.Getenv("CDS_SERVICE"), " ")
}
if len(args) == 0 {
cmd.Help() // nolint
return
}
// Initialize config
conf := configImport(args, flagStartConfigFile, flagStartRemoteConfig, flagStartRemoteConfigKey, flagStartVaultAddr, flagStartVaultToken, false)
ctx, cancel := context.WithCancel(context.Background())
// initialize context
defer cancel()
var (
serviceConfs []serviceConf
names []string
types []string
)
for _, a := range args {
fmt.Printf("Starting service %s\n", a)
switch a {
case sdk.TypeAPI:
if conf.API == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: api.New(), cfg: *conf.API})
names = append(names, conf.API.Name)
types = append(types, sdk.TypeAPI)
case sdk.TypeUI:
if conf.UI == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: ui.New(), cfg: *conf.UI})
names = append(names, conf.UI.Name)
types = append(types, sdk.TypeUI)
case "migrate":
if conf.DatabaseMigrate == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: migrateservice.New(), cfg: *conf.DatabaseMigrate})
names = append(names, conf.DatabaseMigrate.Name)
types = append(types, sdk.TypeDBMigrate)
case sdk.TypeHatchery + ":local":
if conf.Hatchery.Local == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: local.New(), cfg: *conf.Hatchery.Local})
names = append(names, conf.Hatchery.Local.Name)
types = append(types, sdk.TypeHatchery)
case sdk.TypeHatchery + ":kubernetes":
if conf.Hatchery.Kubernetes == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: kubernetes.New(), cfg: *conf.Hatchery.Kubernetes})
names = append(names, conf.Hatchery.Kubernetes.Name)
types = append(types, sdk.TypeHatchery)
case sdk.TypeHatchery + ":openstack":
if conf.Hatchery.Openstack == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: openstack.New(), cfg: *conf.Hatchery.Openstack})
names = append(names, conf.Hatchery.Openstack.Name)
types = append(types, sdk.TypeAPI)
case sdk.TypeHatchery + ":swarm":
if conf.Hatchery.Swarm == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: swarm.New(), cfg: *conf.Hatchery.Swarm})
names = append(names, conf.Hatchery.Swarm.Name)
types = append(types, sdk.TypeHatchery)
case sdk.TypeHatchery + ":vsphere":
if conf.Hatchery.VSphere == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: vsphere.New(), cfg: *conf.Hatchery.VSphere})
names = append(names, conf.Hatchery.VSphere.Name)
types = append(types, sdk.TypeHatchery)
case sdk.TypeHooks:
if conf.Hooks == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: hooks.New(), cfg: *conf.Hooks})
names = append(names, conf.Hooks.Name)
types = append(types, sdk.TypeHooks)
case sdk.TypeCDN:
if conf.CDN == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: cdn.New(), cfg: *conf.CDN})
names = append(names, conf.CDN.Name)
types = append(types, sdk.TypeCDN)
case sdk.TypeVCS:
if conf.VCS == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: vcs.New(), cfg: *conf.VCS})
names = append(names, conf.VCS.Name)
types = append(types, sdk.TypeVCS)
case sdk.TypeRepositories:
if conf.Repositories == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: repositories.New(), cfg: *conf.Repositories})
names = append(names, conf.Repositories.Name)
types = append(types, sdk.TypeRepositories)
case sdk.TypeElasticsearch:
if conf.ElasticSearch == nil {
sdk.Exit("Unable to start: missing service %s configuration", a)
}
serviceConfs = append(serviceConfs, serviceConf{arg: a, service: elasticsearch.New(), cfg: *conf.ElasticSearch})
names = append(names, conf.ElasticSearch.Name)
types = append(types, sdk.TypeElasticsearch)
default:
fmt.Printf("Error: service '%s' unknown\n", a)
os.Exit(1)
}
}
// gracefully shutdown all
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func(ctx context.Context) {
<-c
unregisterServices(ctx, serviceConfs)
signal.Stop(c)
cancel()
}(ctx)
//Initialize logs
logConf := cdslog.Conf{
Level: conf.Log.Level,
Format: conf.Log.Format,
TextFields: conf.Log.TextFields,
SkipTextFields: conf.Log.SkipTextFields,
GraylogProtocol: conf.Log.Graylog.Protocol,
GraylogHost: conf.Log.Graylog.Host,
GraylogPort: fmt.Sprintf("%d", conf.Log.Graylog.Port),
GraylogExtraKey: conf.Log.Graylog.ExtraKey,
GraylogExtraValue: conf.Log.Graylog.ExtraValue,
GraylogFieldCDSVersion: sdk.VERSION,
GraylogFieldCDSOS: sdk.GOOS,
GraylogFieldCDSArch: sdk.GOARCH,
GraylogFieldCDSServiceName: strings.Join(names, "_"),
GraylogFieldCDSServiceType: strings.Join(types, "_"),
}
cdslog.Initialize(ctx, &logConf)
// Sort the slice of services we have to start to be sure to start the API au first
sort.Slice(serviceConfs, func(i, j int) bool {
return serviceConfs[i].arg < serviceConfs[j].arg
})
var wg sync.WaitGroup
//Configure the services
for i := range serviceConfs {
s := serviceConfs[i]
if err := s.service.ApplyConfiguration(s.cfg); err != nil {
sdk.Exit("Unable to init service %s: %v", s.arg, err)
}
serviceCtx := context.WithValue(ctx, cdslog.Service, s.service.Name())
log.Info(ctx, "%s> %s configuration applied", s.arg, s.service.Name())
if srv, ok := s.service.(service.BeforeStart); ok {
if err := srv.BeforeStart(serviceCtx); err != nil {
sdk.Exit("Unable to start service %s: %v", s.arg, err)
}
}
var err error
serviceCtx, err = telemetry.Init(serviceCtx, conf.Telemetry, s.service)
if err != nil {
sdk.Exit("Unable to start tracing exporter: %v", err)
}
wg.Add(1)
go func(srv serviceConf) {
if err := start(serviceCtx, srv.service, srv.arg, srv.cfg); err != nil {
log.Error(ctx, "%s> service has been stopped: %+v", srv.arg, err)
}
wg.Done()
}(s)
// Stupid trick: when API is starting wait a bit before start the other
if s.arg == "API" || s.arg == "api" {
time.Sleep(2 * time.Second)
}
}
// Wait for all services to stop
wg.Wait()
},
}
func unregisterServices(ctx context.Context, serviceConfs []serviceConf) {
// unregister all services
for i := range serviceConfs {
s := serviceConfs[i]
fmt.Printf("%s> Unregister\n", s.service.Name())
if err := s.service.Unregister(ctx); err != nil {
log.Error(ctx, "%s> Unable to unregister: %v", s.service.Name(), err)
}
}
if ctx.Err() != nil {
fmt.Printf("Exiting: %+v\n", ctx.Err())
}
}
func start(ctx context.Context, s service.Service, serviceName string, cfg interface{}) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
srvConfig, err := s.Init(cfg)
if err != nil {
return err
}
// Signin and register to CDS api
if err := s.Signin(ctx, srvConfig, cfg); err != nil {
return sdk.WrapError(err, "unable to signin: %s", serviceName)
}
log.Info(ctx, "%s> Service signed in", serviceName)
if err := s.Start(ctx); err != nil {
return sdk.WrapError(err, "unable to start service: %s", serviceName)
}
go func() {
if err := s.Serve(ctx); err != nil {
log.Error(ctx, "%s> Error serve: %+v", serviceName, err)
cancel()
}
}()
// finally start the heartbeat goroutine
go func() {
if err := s.Heartbeat(ctx, s.Status); err != nil {
log.Error(ctx, "%s> Error heartbeat: %+v", serviceName, err)
cancel()
}
}()
<-ctx.Done()
if ctx.Err() != nil {
log.Error(ctx, "%s> Service exiting with err: %+v", serviceName, ctx.Err())
} else {
log.Info(ctx, "%s> Service exiting", serviceName)
}
return ctx.Err()
}