-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
313 lines (268 loc) · 8.1 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
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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
"github.com/jessevdk/go-flags"
"golang.org/x/sync/errgroup"
"github.com/netsys-lab/scion-orchestrator/conf"
"github.com/netsys-lab/scion-orchestrator/environment"
"github.com/netsys-lab/scion-orchestrator/pkg/apiv1"
"github.com/netsys-lab/scion-orchestrator/pkg/bootstrap"
"github.com/netsys-lab/scion-orchestrator/pkg/certutils"
"github.com/netsys-lab/scion-orchestrator/pkg/fileops"
"github.com/netsys-lab/scion-orchestrator/pkg/metrics"
"github.com/netsys-lab/scion-orchestrator/pkg/scionca"
)
var opts struct {
// Example of verbosity with level
// Verbose []bool `short:"v" long:"verbose" description:"Verbose output"`
// Example of optional value
Config string `short:"c" long:"config" description:"Path to configuration file"`
// Example of optional value
InstallDir string `short:"d" long:"dir" description:"Directory to install scion-orchestrator as service" `
// Example of optional value
// Install bool `short:"i" long:"install" description:"Install scion-orchestrator as a system service" `
}
var scionConfig *conf.SCIONConfig
var install bool
var run bool
var shutdown bool
var restart bool
func main() {
args, err := flags.Parse(&opts)
if err != nil {
log.Println(err)
os.Exit(1)
}
log.Println("[Main] Starting scion-orchestrator")
log.Println("[Main] Running on ", runtime.GOOS)
log.Println("[Main] Args: ", args)
configPath := filepath.Join("config", "scion-orchestrator.toml")
if opts.Config != "" {
configPath = opts.Config
}
config, err := conf.LoadConfig(configPath)
log.Println("[Main] scion-orchestrator config loaded successfully")
if fileops.FileOrFolderExists("config") {
// log.Println("[Main] Config folder exists")
scionConfig, err = conf.LoadSCIONConfig("")
if err != nil {
log.Println("[Main] Error loading scion config: ", err)
log.Fatal(err)
}
// log.Println("[Main] Config loaded")
log.Println(scionConfig.Log())
} else {
// Get base path
scionConfigDir := filepath.Dir(opts.Config)
scionConfig, err = conf.LoadSCIONConfig(scionConfigDir)
if err != nil {
log.Println("[Main] Error loading scion config: ", err)
log.Fatal(err)
}
// log.Println("[Main] Config loaded")
log.Println(scionConfig.Log())
}
env := environment.HostEnv
install = len(args) > 0 && args[0] == "install"
run = len(args) > 0 && args[0] == "run"
shutdown = len(args) > 0 && args[0] == "shutdown"
restart = len(args) > 0 && args[0] == "restart"
cancelChan := make(chan os.Signal, 1)
// catch SIGETRM or SIGINTERRUPT
signal.Notify(cancelChan, syscall.SIGTERM, syscall.SIGINT)
if opts.Config != "" { // Run as a service
log.Println("[Main] Running as service")
if config.Mode == "endhost" {
log.Println("[Main] Running bootstrapper to fetch configuration...")
err = bootstrap.BootstrapFromAddress(env, config)
if err != nil {
log.Println("[Main] Failed to bootstrap host: ", err)
log.Fatal(err)
}
}
err := environment.LoadServices(env, scionConfig, config)
if err != nil {
log.Fatal(err)
}
go func() {
err = runBackgroundServices(env, config)
if err != nil {
log.Fatal(err)
}
}()
go func() {
err = runService(env, config)
if err != nil {
log.Fatal(err)
}
}()
log.Println("[Main] scion-orchestrator Service started, waiting for termination signal")
sig := <-cancelChan
log.Printf("[Signal] Caught signal %v", sig)
// TODO: Add proper service initialization here
// time.Sleep(10 * time.Minute)
} else if run {
log.Println("[Main] Running in standalone mode")
go func() {
err = runBackgroundServices(env, config)
if err != nil {
log.Fatal(err)
}
}()
go func() {
sig := <-cancelChan
log.Printf("[Signal] Caught signal %v", sig)
environment.KillAllChilds()
log.Fatal("[Main] Shutting down...")
}()
err = runStandalone(env, config)
} else if install {
log.Println("[Main] Installing as service")
err = runInstall(env, scionConfig, config)
} else if shutdown {
// log.Println("[Main] Shutting down all service")
err = runShutdown(env, scionConfig, *config)
if err != nil {
log.Fatal(err)
}
log.Println("[Main] All services stopped. AS Status is now:")
jsonStatus, _ := metrics.ASStatus.Json()
fmt.Printf("%s", string(jsonStatus))
} else if restart {
// log.Println("[Main] Shutting down all service")
err = runRestart(env, scionConfig, *config)
if err != nil {
log.Fatal(err)
}
log.Println("[Main] All services restarted. AS Status is now:")
jsonStatus, _ := metrics.ASStatus.Json()
fmt.Printf("%s", string(jsonStatus))
}
if err != nil {
log.Fatal(err)
}
}
func runShutdown(env *environment.HostEnvironment, config *conf.SCIONConfig, asConfig conf.Config) error {
log.Println("[Main] Shutting down SCION AS")
err := environment.LoadServices(env, config, &asConfig)
if err != nil {
return err
}
log.Println("[Main] Stopping all services")
err = environment.StopAllServices()
if err != nil {
return err
}
return nil
}
func runRestart(env *environment.HostEnvironment, config *conf.SCIONConfig, asConfig conf.Config) error {
log.Println("[Main] Restarting SCION AS")
err := environment.LoadServices(env, config, &asConfig)
if err != nil {
return err
}
log.Println("[Main] Stopping all services")
err = environment.StopAllServices()
if err != nil {
return err
}
time.Sleep(10 * time.Second)
log.Println("[Main] Starting all services")
err = environment.StartAllServices()
if err != nil {
return err
}
return nil
}
func runBackgroundServices(env *environment.HostEnvironment, config *conf.Config) error {
log.Println("[Main] Running background services")
var eg errgroup.Group
if config.Mode == "endhost" {
eg.Go(func() error {
return metrics.RunStatusHTTPServer(config.Metrics.Server)
})
// Standalone does its own health check
if !run {
eg.Go(func() error {
healtchCheck := environment.NewServiceHealthCheck()
healtchCheck.Run()
return nil
})
}
} else {
if !config.ServiceConfig.DisableBootstrapServer {
eg.Go(func() error {
return bootstrap.RunBootstrapService(env.ConfigPath, config.Bootstrap.Server, config)
})
}
eg.Go(func() error {
return metrics.RunStatusHTTPServer(config.Metrics.Server)
})
log.Println("[Main] Running background services for CA")
// TODO: Check which services do really need a control plane cert/key
// TODO: Need to fail here??
if len(scionConfig.ControlServices) > 0 || len(scionConfig.BorderRouters) > 0 {
err := certutils.EnsureASPrivateKeyExists(env.ConfigPath, config.IsdAs)
if err != nil {
log.Println("[Main] Error ensuring AS private key exists: ", err)
}
}
if len(config.Api.Users) == 0 {
log.Println("[Main] Warning: No users defined for API, endpoints can not be accessed")
} else {
log.Println("[Main] Starting API server...")
eg.Go(func() error {
return apiv1.RunApiServer(env, config)
})
}
// log.Println(config.Ca.Clients)
if config.Ca.Clients != nil && len(config.Ca.Clients) > 0 {
eg.Go(func() error {
// TODO: Only run if core AS
parts := strings.Split(config.IsdAs, "-")
ca := scionca.NewSCIONCertificateAuthority(env.ConfigPath, parts[0], config.Ca.CertValidityHours)
err := ca.LoadCA()
if err != nil {
return err
}
apiServer := scionca.NewCaApiServer(env.ConfigPath, &config.Ca, ca)
err = apiServer.LoadClientsAndSecrets()
if err != nil {
return err
}
return apiServer.Run()
})
}
// Standalone does its own health check
if !run {
eg.Go(func() error {
healtchCheck := environment.NewServiceHealthCheck()
healtchCheck.Run()
return nil
})
}
if !config.ServiceConfig.DisableCertRenewal {
eg.Go(func() error {
renewer := scionca.NewCertificateRenewer(env.ConfigPath, config.IsdAs, 6)
log.Println("[Main] Starting certificate renewal with 30s delay...")
time.Sleep(30 * time.Second)
renewer.Run()
return nil
})
}
}
return eg.Wait()
}
func runService(env *environment.HostEnvironment, config *conf.Config) error {
//log.Println("[Main] Running service")
//time.Sleep(30 * time.Second)
return nil
}