-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (146 loc) · 3.95 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
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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"sync"
"syscall"
"time"
"code.crute.us/mcrute/golib/secrets"
"code.crute.us/mcrute/simplevisor/supervise"
"code.crute.us/mcrute/simplevisor/supervise/jobs"
"code.crute.us/mcrute/simplevisor/supervise/logging"
"golang.org/x/sys/unix"
)
const (
modeParent = "parent"
modeChild = "child"
)
func readConfig(path string) (*supervise.AppConfig, error) {
cf, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("readConfig: unable to load config")
}
cfg := &supervise.AppConfig{}
if err := json.Unmarshal(cf, &cfg); err != nil {
return nil, fmt.Errorf("readConfig: unable to parse config: %s", err)
}
return cfg, nil
}
func parentMain(cfgLoc string, disableVault bool) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigs := supervise.SetupSignals()
wg := &sync.WaitGroup{}
logger := &logging.InternalLogger{
Logs: make(chan *logging.LogRecord, 100),
Pool: logging.NewBufferPool(),
Cancel: cancel,
WaitGroup: wg,
}
go logging.StdoutWriter(ctx, wg, os.Stdout, logger)
cfg, err := readConfig(cfgLoc)
if err != nil {
logger.Fatalf("parentMain: error loading config: %s", err)
}
var vc secrets.ClientManager
if !disableVault {
vc, err = secrets.NewVaultClient(&secrets.VaultClientConfig{})
if err != nil {
logger.Fatalf("parentMain: unable to setup vault: %s", err)
}
} else {
vc, _ = secrets.NewNoopClient()
}
if err := vc.Authenticate(ctx); err != nil {
logger.Fatalf("parentMain: unable to auth vault: %s", err)
}
// TODO: Support VAULT_TOKEN
env, err := supervise.PrepareEnvironment(ctx, cfg.Environment, vc, "")
if err != nil {
logger.Fatalf("parentMain: unable to prepare environment: %s", err)
}
if err := unix.Prctl(unix.PR_SET_CHILD_SUBREAPER, uintptr(1), 0, 0, 0); err != nil {
logger.Fatalf("parentMain: unable to become subreaper: %s", err)
}
go jobs.SecretsLogger(ctx, wg, vc, logger)
go vc.Run(ctx, wg)
runner := &supervise.CommandRunner{
Logger: logger,
BaseContext: ctx,
WaitGroup: wg,
Environment: env,
}
if cfg.Jobs.Init != nil {
for _, js := range cfg.Jobs.Init {
logger.Logf("parentMain: attempting to start job %s", js.Name)
hnd, err := runner.Run(js)
if err != nil {
logger.Fatalf("parentMain: error starting init job %s: %s", js.Name, err)
}
if err := hnd.Wait(); err != nil {
logger.Fatalf("parentMain: error running init job %s: %s", js.Name, err)
}
if exit := hnd.ExitCode(); exit != 0 {
logger.Fatalf("parentMain: error init job %s exited non-zero: %d", js.Name, exit)
}
hnd.Cleanup()
}
}
// TODO: Restart if crashed
handles := []*supervise.CommandHandle{}
for _, js := range cfg.Jobs.Main {
hnd, err := runner.Run(js)
if err != nil {
logger.Fatalf("parentMain: error starting main job %s: %s", js.Name, err)
}
handles = append(handles, hnd)
}
// TODO: Clean this up
// Reap children and propogate signals until the end
for {
exits, err := supervise.ReapChildren()
if err != nil {
logger.Logf("Error reaping children: %s", err)
} else {
for _, e := range exits {
logger.Logf("Reaped child %d with exit %d", e.Pid, e.Status)
}
}
select {
case s := <-sigs:
switch s {
case syscall.SIGTERM, syscall.SIGINT:
for _, h := range handles {
h.Terminate()
}
cancel()
wg.Wait()
supervise.ReapChildren()
return
}
for _, h := range handles {
h.Signal(s)
}
case <-ctx.Done():
break
case <-time.After(time.Second):
}
}
}
func main() {
mode := flag.String("mode", modeParent, "mode in which to run simplevisor, internal use only")
config := flag.String("config", "simplevisor.json", "config file location")
noVault := flag.Bool("no-vault", false, "disable Vault integrate entirely")
flag.Parse()
switch *mode {
case modeParent:
parentMain(*config, *noVault)
case modeChild:
supervise.ChildMain()
default:
panic("TODO: Add error, invalid mode")
}
}