-
Notifications
You must be signed in to change notification settings - Fork 3
/
command_run.go
96 lines (74 loc) · 1.89 KB
/
command_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
// Copyright (c) R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package machineroom
import (
"context"
"fmt"
"sync"
"github.com/choria-io/fisk"
"github.com/choria-io/go-choria/build"
"github.com/choria-io/go-choria/choria"
"github.com/choria-io/go-choria/config"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
)
func (c *cliInstance) runCommand(pc *fisk.ParseContext) error {
_, _, err := c.CommonConfigure()
if err != nil {
return err
}
var cfg *config.Config
if choria.FileExist(c.cfgFile) {
cfg, err = config.NewConfig(c.cfgFile)
if err != nil {
return err
}
c.isLeader = cfg.Option(configKeyRole, "follower") == "leader"
}
c.log = c.log.WithFields(logrus.Fields{"leader": c.isLeader})
c.log.Warnf("Starting %s version %s with config file %s", c.opts.Name, c.opts.Version, c.cfgFile)
err = c.createServerNKey()
if err != nil {
c.log.Errorf("Could not create nkey: %v", err)
}
// makes sure we have some facts to send during provisioning
err = c.factsCommand(pc)
if err != nil {
c.log.Errorf("Could not write initial facts: %v", err)
}
wg := sync.WaitGroup{}
var inproc nats.InProcessConnProvider
if c.isLeader {
b, err := newBroker(c.opts, c.cfgFile, &build.Info{}, c.log)
if err != nil {
return err
}
err = b.Start(c.ctx, &wg)
if err != nil {
return err
}
inproc = b.InProcessConnProvider()
err = b.StartReplication(c.ctx, &wg)
if err != nil {
return err
}
}
err = c.startServer(c.ctx, &wg, inproc)
if err != nil {
return fmt.Errorf("machine room server failed: %v", err)
}
wg.Wait()
return nil
}
func (c *cliInstance) startServer(ctx context.Context, wg *sync.WaitGroup, inproc nats.InProcessConnProvider) error {
srv, err := newServer(c.opts, c.cfgFile, inproc, c.log)
if err != nil {
return err
}
err = srv.Start(ctx, wg)
if err != nil {
return err
}
return nil
}