-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
660 lines (615 loc) · 19 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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"
"os"
"reflect"
"strconv"
"strings"
"sync"
"time"
amzaws "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/acm"
"github.com/aws/aws-sdk-go-v2/service/ec2"
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
"github.com/aws/aws-sdk-go-v2/service/route53"
log "github.com/cantara/bragi/sbragi"
"github.com/cantara/gober/consensus"
"github.com/cantara/gober/discovery/local"
"github.com/cantara/gober/eventmap"
"github.com/cantara/gober/stream"
"github.com/cantara/gober/stream/event"
"github.com/cantara/gober/stream/event/store/inmemory"
"github.com/cantara/gober/stream/event/store/ondisk"
syncExt "github.com/cantara/gober/sync"
"github.com/cantara/gober/webserver"
"github.com/cantara/gober/webserver/health"
"github.com/cantara/gober/websocket"
"github.com/cantara/nerthus2/aws"
"github.com/cantara/nerthus2/cloud/aws/executor/workers"
"github.com/cantara/nerthus2/cloud/aws/security"
"github.com/cantara/nerthus2/cloud/aws/server"
"github.com/cantara/nerthus2/message"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-git/go-git/v5"
gitHttp "github.com/go-git/go-git/v5/plumbing/transport/http"
)
var bootstrap bool
var gitRepo string
var gitToken string
var bootstrapEnv string
func init() {
const ( //TODO: Add bootstrap git as a separate command from bootstrap.
defaultBootstrap = false
bootstrapUsage = "tells nerthus to bootstrap itself into aws"
defaultGitRepo = "github.com/cantara/nerthus2"
gitRepoUsage = "github repository for solution config"
defaultGitToken = ""
gitTokenUsage = "github repository granular access token"
defaultSystemName = "exoreaction_demo"
systemNameUsage = "defines the system that Nerthus should use to provision itself"
)
flag.BoolVar(&bootstrap, "bootstrap", defaultBootstrap, bootstrapUsage)
flag.BoolVar(&bootstrap, "b", defaultBootstrap, bootstrapUsage+" (shorthand)")
flag.StringVar(&gitRepo, "git-repo", defaultGitRepo, gitRepoUsage)
flag.StringVar(&gitRepo, "r", defaultGitRepo, gitRepoUsage+" (shorthand)")
flag.StringVar(&gitToken, "git-token", defaultGitToken, gitTokenUsage)
flag.StringVar(&gitToken, "t", defaultGitToken, gitTokenUsage+" (shorthand)")
flag.StringVar(&bootstrapEnv, "environment", defaultSystemName, systemNameUsage)
flag.StringVar(&bootstrapEnv, "e", defaultSystemName, systemNameUsage+" (shorthand)")
}
type environment struct {
GitToken string `json:"git_token"`
GitRepo string `json:"git_repo"`
EnvName string `json:"name"`
lastExecute *time.Timer
}
func main() {
flag.Parse()
err := os.MkdirAll("systems", 0750)
if err != nil {
log.WithError(err).Fatal("while creating systems dir on boot")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var deploymentStream stream.Stream
if bootstrap {
deploymentStream, err = inmemory.Init("deployments", ctx)
} else {
deploymentStream, err = ondisk.Init("deployments", ctx)
}
if err != nil {
log.WithError(err).Fatal("while initializing fairytale stream")
}
portString := os.Getenv("webserver.port")
port, err := strconv.Atoi(portString)
if err != nil {
log.WithError(err).Fatal("while getting webserver port")
}
p, err := consensus.Init(uint16(port+1), os.Getenv("consensus.token"), local.New())
if err != nil {
log.WithError(err).Fatal("initing consensus")
}
// Load the Shared AWS Configuration (~/.aws/config)
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
log.WithError(err).Fatal("while getting aws config")
}
cfg.RetryMode = amzaws.RetryModeAdaptive
cfg.RetryMaxAttempts = 5
e2, elb, rc, ac := ec2.NewFromConfig(cfg), elbv2.NewFromConfig(cfg), route53.NewFromConfig(cfg), acm.NewFromConfig(cfg)
d, err := workers.New(deploymentStream, p.AddTopic, os.Getenv("deployment.crypto.key"), hostActions, e2, elb, rc, ac, ctx)
if err != nil {
log.WithError(err).Fatal("creating worker")
}
for i := 0; i < 100; i++ {
go d.Work()
}
var envStream stream.Stream
if bootstrap {
envStream, err = inmemory.Init("environments", ctx)
} else {
envStream, err = ondisk.Init("environments", ctx)
}
if err != nil {
log.WithError(err).Fatal("while initializing public key stream")
}
environments, err := eventmap.Init[environment](envStream, "environment", "v0.0.1",
stream.StaticProvider(log.RedactedString(os.Getenv("environments.static_key"))), ctx, func(t event.Type, env *environment) {
if t != event.Created {
return
}
if env.EnvName == "" {
return
}
log.Info("registered env", "name", env.EnvName)
env.lastExecute = time.AfterFunc(time.Minute*15, func() {
log.Info("-------timer executed new func--------")
_, err := GitCloneEnvironment(*env)
if log.WithError(err).Error("while cloning git repo during environment execution", "env", env.EnvName) {
return
}
ExecuteEnv(env.EnvName, d.Provision)
})
})
//TODO: If bootstrap add local users ssh key to map and add key on bootstrap
if bootstrap {
env := environment{
GitToken: gitToken,
GitRepo: gitRepo,
EnvName: bootstrapEnv,
}
err = environments.Set(bootstrapEnv, env)
if err != nil {
log.WithError(err).Fatal("while storing bootstrap env in map", "isBootstrapping", bootstrap, "environments", environments.Len())
}
_, err := GitCloneEnvironment(env)
if err != nil {
log.WithError(err).Fatal("while cloning git repo during bootstrap")
}
ExecuteEnv(bootstrapEnv, d.Provision)
return
}
if environments.Len() == 0 {
err = environments.Set(os.Getenv("boot_env"), environment{
GitToken: os.Getenv("git.token"),
GitRepo: os.Getenv("git.repo"),
EnvName: os.Getenv("boot_env"),
})
if err != nil {
log.WithError(err).Fatal("while storing bootstrap env in map", "isBootstrapping", bootstrap, "environments", environments.Len())
}
}
serv, err := webserver.Init(uint16(port), true)
if err != nil {
log.WithError(err).Fatal("while initializing webserver")
}
configEnv := func(c *gin.Context) {
name := c.Params.ByName("env")
env, err := environments.Get(name)
//if ok := environments.Exists(env); !ok {
if err != nil {
c.AbortWithStatus(404)
return
}
//_, err := GitCloneEnvironment(env, environments)
_, err = GitCloneEnvironment(env)
if err != nil {
log.WithError(err).Fatal("while cloning git repo during environment execution", "env", env)
}
env.lastExecute.Reset(time.Minute * 15)
go ExecuteEnv(env.EnvName, d.Provision)
/*
t := time.NewTicker(time.Second * 30)
for {
select {
case <-c.Request.Context().Done():
return
case <-t.C:
c.SSEvent("ping", nil)
case result, ok := <-resultChan:
if !ok {
return
}
out, _ := jsoniter.ConfigFastest.Marshal(result)
c.SSEvent("result", string(out))
}
}
*/
}
serv.API().POST("/config/:env", configEnv)
serv.API().PUT("/config/:env", configEnv)
/*
serv.API().PUT("/config/:env/:sys", func(c *gin.Context) {
env := c.Params.ByName("env")
if ok := environments.Exists(env); !ok {
c.AbortWithStatus(404)
return
}
sys := c.Params.ByName("sys")
_, err := GitCloneEnvironment(env, environments)
if err != nil {
log.WithError(err).Fatal("while cloning git repo during system execution", "env", env, "system", sys)
}
go ExecuteSys(env, sys, d.ProvisionSystem)
/*
resultChan := make(chan string)
t := time.NewTicker(time.Second * 30)
for {
select {
case <-c.Request.Context().Done():
return
case <-t.C:
c.SSEvent("ping", nil)
case result, ok := <-resultChan:
if !ok {
return
}
out, _ := jsoniter.ConfigFastest.Marshal(result)
c.SSEvent("result", string(out))
}
}
*/
//})
/*
serv.API().PUT("/config/:env/:sys/:cluster", func(c *gin.Context) {
env := c.Params.ByName("env")
if ok := environments.Exists(env); !ok {
log.Warning("put aborted", "env", env, "envs", environments.Keys())
c.AbortWithStatus(404)
return
}
sys := c.Params.ByName("sys")
cluster := c.Params.ByName("cluster")
_, err := GitCloneEnvironment(env, environments)
if err != nil {
log.WithError(err).Fatal("while cloning git repo during service execution", "env", env, "system", sys, "cluster", cluster)
}
go ExecuteCluster(env, sys, cluster, d.ProvisionCluster)
})
serv.API().PUT("/config/:env/:sys/:cluster/:serv", func(c *gin.Context) {
env := c.Params.ByName("env")
if ok := environments.Exists(env); !ok {
log.Warning("put aborted", "env", env, "envs", environments.Keys())
c.AbortWithStatus(404)
return
}
sys := c.Params.ByName("sys")
cluster := c.Params.ByName("cluster")
service := c.Params.ByName("serv")
_, err := GitCloneEnvironment(env, environments)
if err != nil {
log.WithError(err).Fatal("while cloning git repo during service execution", "env", env, "system", sys, "cluster", cluster, "service", service)
}
ExecuteServ(env, sys, cluster, service)
})
*/
keyStream, err := ondisk.Init("pubKeys", ctx)
if err != nil {
log.WithError(err).Fatal("while initializing public key stream")
}
keyMap, err := eventmap.Init[key](keyStream, "pubkey", "v0.0.1",
stream.StaticProvider(log.RedactedString(os.Getenv("pubkey.static_key"))), ctx)
if err != nil {
log.WithError(err).Fatal("while initializing public key event map")
}
{
auth := serv.API().Group("")
accounts := gin.Accounts{}
accounts[os.Getenv("api.username")] = os.Getenv("api.password")
auth.Use(gin.BasicAuth(accounts))
auth.GET("/servers", func(c *gin.Context) {
servers, err := aws.GetServers()
if err != nil {
log.WithError(err).Error("while getting servers from aws")
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
return
}
c.JSON(http.StatusOK, servers)
})
auth.PUT("/ssh/:server", func(c *gin.Context) {
name := c.Params.ByName("server")
servs, err := server.GetServers(name, e2)
if err != nil {
if errors.Is(err, server.ErrServerNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "server not found"})
} else {
log.WithError(err).Error("while getting servers from aws")
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
}
return
}
g, err := security.ById(servs[0].SecutityGroupId, e2)
if err != nil {
log.WithError(err).Error("while getting server security group from aws")
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
}
err = g.OpenSSH("TMP_ALL_USER", c.ClientIP(), e2)
if err != nil {
log.WithError(err).Error("while opening ssh to server")
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
}
c.JSON(http.StatusOK, nil)
})
auth.PUT("/key/:user/:name", func(c *gin.Context) {
var ky key
err := c.MustBindWith(&ky, binding.JSON)
if err != nil {
log.WithError(err).Debug("while binding json body from key put")
return
}
if ky.Name != c.Params.ByName("name") {
c.JSON(http.StatusBadRequest, gin.H{"error": "name does not match name of key"})
return
}
err = keyMap.Set(fmt.Sprintf("%s-%s", c.Params.ByName("user"), ky.Name), ky)
if err != nil {
log.WithError(err).Error("while storing new public key")
c.JSON(http.StatusInternalServerError, gin.H{"error": "error while storing new public key"})
return
}
var authorizedKeys bytes.Buffer
for _, k := range keyMap.GetMap() {
authorizedKeys.WriteString(k.Data)
authorizedKeys.WriteRune('\n')
}
b := authorizedKeys.Bytes()
for _, srv := range hostActions.GetMap() {
srv <- message.Action{
Action: message.AuthorizedKeys,
Data: b,
}
}
})
auth.GET("/env", func(c *gin.Context) {
c.JSON(http.StatusOK, environments.Keys())
})
auth.PUT("/env/:env", func(c *gin.Context) {
var env environment
err := c.MustBindWith(&env, binding.JSON)
if err != nil {
log.WithError(err).Debug("while binding json body from key put")
return
}
if env.EnvName != c.Params.ByName("env") {
c.JSON(http.StatusBadRequest, gin.H{"error": "name does not match name of env", "name": c.Params.ByName("name"), "env": env})
return
}
err = environments.Set(env.EnvName, env)
if err != nil {
log.WithError(err).Error("while storing env in map", "environments", environments.Keys())
c.JSON(http.StatusInternalServerError, gin.H{"error": "while storing env in map"})
return
}
configEnv(c)
})
}
websocket.Serve(serv.API(), "/probe/:host", nil, func(reader <-chan message.Action, writer chan<- websocket.Write[message.Action], p gin.Params, ctx context.Context) {
defer close(writer)
host := p.ByName("host")
log.Info("opening websocket", "host", host)
defer log.Info("closed websocket", "host", host)
var authorizedKeys bytes.Buffer
for _, k := range keyMap.GetMap() {
authorizedKeys.WriteString(k.Data)
authorizedKeys.WriteRune('\n')
}
b := authorizedKeys.Bytes()
errChan := make(chan error, 1)
writer <- websocket.Write[message.Action]{
Data: message.Action{
Action: message.AuthorizedKeys,
Data: b,
},
Err: errChan,
}
err := <-errChan
if err != nil {
log.WithError(err).Error("unable to write action to nerthus probe")
return //TODO continue
}
hostChan, ok := hostActions.Get(host)
if !ok {
hostChan = make(chan message.Action, 10)
hostActions.Set(host, hostChan)
}
Worker:
for {
select {
case <-ctx.Done():
break Worker
case msg, ok := <-reader:
if !ok {
break Worker
}
if msg.Response == nil {
log.Warning("read action response without response", "action", msg)
continue Worker
}
log.Info("response from action", "message", msg.Response.Message, "status", msg.Response.Status)
case a := <-hostChan:
errChan := make(chan error, 1)
action := websocket.Write[message.Action]{
Data: a,
Err: errChan,
}
select {
case <-ctx.Done():
break Worker
case writer <- action:
err := <-errChan
if err != nil {
log.WithError(err).Error("unable to write action to nerthus probe",
"action_type", reflect.TypeOf(action))
continue Worker
}
}
}
}
log.Info("reader closed, ending websocket function")
})
//https://visuale.greps.dev/api/status/prod/Stamp-server/prod-greps-stamp-server?service_tag=Greps&service_type=A2A
visuale := make(map[string]map[string]map[string][]health.Report)
var vLock sync.Mutex
serv.Base().PUT("/api/status/:env/:service/:hostname", func(c *gin.Context) {
env := c.Params.ByName("env")
service := c.Params.ByName("service")
hostname := c.Params.ByName("hostname")
var report health.Report
err := c.MustBindWith(&report, binding.JSON)
if err != nil {
log.WithError(err).Debug("while binding json body from key put")
return
}
vLock.Lock()
defer vLock.Unlock()
if _, ok := visuale[env]; !ok {
visuale[env] = make(map[string]map[string][]health.Report)
}
if _, ok := visuale[env][service]; !ok {
visuale[env][service] = make(map[string][]health.Report)
}
if _, ok := visuale[env][service][hostname]; !ok {
visuale[env][service][hostname] = make([]health.Report, 0)
}
reports := visuale[env][service][hostname]
reports = append(reports, report)
visuale[env][service][hostname] = reports
if strings.ToLower(report.Status) != strings.ToLower(reports[len(reports)-2].Status) {
log.Warning("node changed visuale status", "env", env, "service", service, "hostname", hostname, "status", report.Status)
}
})
go func() {
for t := range time.NewTicker(time.Second).C {
if t.Second()%30 == 0 {
vLock.Lock()
d, err := json.Marshal(visuale)
for env := range visuale {
for service := range visuale[env] {
for hostname, reports := range visuale[env][service] {
visuale[env][service][hostname] = reports[len(reports)-1:]
}
}
}
vLock.Unlock()
if err != nil {
log.WithError(err).Error("while marshalling json")
continue
}
f, err := os.OpenFile("visuale.json", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0640)
if err != nil {
log.WithError(err).Error("while opening visuale.json file")
continue
}
_, err = f.WriteString(string(d))
if err != nil {
log.WithError(err).Error("while writing visuale.json file")
}
f.Close()
}
}
}()
log.Info("starting webserver", "environments", environments.Keys())
go p.Run()
serv.Run()
}
var hostActions = syncExt.NewMap[chan message.Action]()
func GitAuth(gitConf environment) *gitHttp.BasicAuth {
return &gitHttp.BasicAuth{ //This is so stupid, but what GitHub wants
Username: "nerthus",
Password: gitConf.GitToken,
}
}
//var ErrEnvNotFound = errors.New("environment not found")
func GitCloneEnvironment(env environment) (r *git.Repository, err error) {
// Clones the repository into the given dir, just as a normal git clone does
/*
gitConf, err := environments.Get(env)
if err != nil {
//err = ErrEnvNotFound
return
}
*/
r, err = git.PlainClone("systems/"+env.EnvName, false, &git.CloneOptions{
Auth: GitAuth(env),
URL: fmt.Sprintf("https://%s.git", env.GitRepo),
})
if err != nil {
if errors.Is(err, git.ErrRepositoryAlreadyExists) {
r, err = git.PlainOpen("systems/" + env.EnvName)
if err != nil {
return
}
var w *git.Worktree
w, err = r.Worktree()
if err != nil {
return
}
err = w.Pull(&git.PullOptions{Auth: GitAuth(env)})
if errors.Is(err, git.NoErrAlreadyUpToDate) {
err = nil
}
return
}
return
}
return
}
/*
func GitBootstrap(r *git.Repository, env string, gitConf properties.BootstrapVars) {
w, err := r.Worktree()
if err != nil {
log.WithError(err).Fatal("while getting git work tree")
}
err = fs.WalkDir(EFS, "bootstrap", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == "bootstrap" {
return nil
}
filename := strings.TrimPrefix(path, "bootstrap/")
fullFilename := filepath.Join(env, filename)
log.Info("processing file from EFS", "filename", filename)
if d.IsDir() {
err = os.Mkdir(fullFilename, 0750)
if errors.Is(err, os.ErrExist) {
return nil
}
return err
}
data, err := EFS.ReadFile(path)
if err != nil {
log.WithError(err).Fatal("while reading file from EFS")
}
err = os.WriteFile(fullFilename, data, 0640)
if err != nil {
log.WithError(err).Fatal("while writing file from EFS to gitrepo")
}
_, err = w.Add(filename)
if err != nil {
log.WithError(err).Fatal("while adding file to commit")
}
return nil
})
if err != nil {
log.WithError(err).Fatal("while walking bootstrap dir")
}
_, err = w.Commit("committing bootstrap", &git.CommitOptions{
Author: &object.Signature{
Name: "Nerthus",
When: time.Now(),
},
})
if err != nil {
log.WithError(err).Fatal("while committing bootstrap")
}
err = r.Push(&git.PushOptions{
Auth: GitAuth(gitConf),
})
if err != nil {
log.WithError(err).Fatal("while pushing")
}
}
*/
func keys[T any](m map[string]T) (keys []string) {
keys = make([]string, len(m))
i := 0
for k := range m {
keys[i] = k
i++
}
return
}
type key struct {
Name string `json:"name"`
Data string `json:"data"`
}
//var kys = map[string]key{}