/
cluster_add.go
118 lines (90 loc) · 3.03 KB
/
cluster_add.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
package cli
import (
"errors"
"fmt"
"os"
"os/exec"
nsccmd "github.com/nats-io/nsc/v2/cmd"
"github.com/numtide/nits/internal/cmd"
"github.com/charmbracelet/log"
nexec "github.com/numtide/nits/pkg/exec"
)
type clusterAdd struct {
Name string `arg:"" help:"Name of the account under which Agents will run"`
}
func (c *clusterAdd) Run() (err error) {
if err := Cmd.Log.ConfigureLog(); err != nil {
return err
}
var op nsccmd.OperatorDescriber
if op, err = nexec.DescribeOperator(); err != nil {
return
}
log.Info("adding a new account", "name", c.Name)
nsc := cmd.LogExec(nexec.Nsc("add", "account", "-n", c.Name, "--deny-pubsub", ">"))
if _, err = nsc.Output(); err != nil {
var exit *exec.ExitError
if errors.As(err, &exit) && string(exit.Stderr) == fmt.Sprintf("Error: the account \"%s\" already exists\n", c.Name) {
log.Warn("account already exists")
} else {
nexec.LogError("failed to add account", err)
return
}
}
log.Info("setting account permissions")
// todo set sane default limits
nsc = cmd.LogExec(nexec.Nsc("edit", "account", "-n", c.Name,
"--js-mem-storage", "-1",
"--js-disk-storage", "-1",
"--js-streams", "-1",
"--js-consumer", "-1",
))
if _, err = nsc.Output(); err != nil {
nexec.LogError("failed to set account permissions", err)
return
}
log.Info("creating an admin user", "name", "Admin")
nsc = cmd.LogExec(nexec.Nsc("add", "user", "-a", c.Name, "-n", "Admin", "--allow-pubsub", ">"))
if _, err = nsc.Output(); err != nil {
var exit *exec.ExitError
if errors.As(err, &exit) && string(exit.Stderr) == "Error: the user \"Admin\" already exists\n" {
log.Warn("user already exists")
} else {
nexec.LogError("failed to add admin user", err)
return
}
}
adminContext := fmt.Sprintf("%s-%s-%s", op.Name, c.Name, "Admin")
log.Info("generating an admin context", "name", adminContext)
nsc = cmd.LogExec(nexec.Nsc("generate", "context", "-a", c.Name, "-u", "Admin", "--context", adminContext))
if _, err = nsc.Output(); err != nil {
nexec.LogError("failed to add an admin context", err)
return
}
log.Info("pushing account to server", "name", c.Name)
nsc = cmd.LogExec(nexec.Nsc("push", "-a", c.Name))
if _, err = nsc.Output(); err != nil {
nexec.LogError("failed to push account to server", err)
return
}
var logsConfig, registryConfig *os.File
if logsConfig, err = openResourceLocally(streamConfig, "streams/agent-logs.json"); err != nil {
return err
}
if registryConfig, err = openResourceLocally(streamConfig, "streams/agent-registry.json"); err != nil {
return err
}
log.Info("adding streams")
nats := cmd.LogExec(nexec.Nats("--context", adminContext, "stream", "add", "--config", logsConfig.Name()))
if _, err = nats.Output(); err != nil {
nexec.LogError("failed to add logs stream", err)
return
}
nats = cmd.LogExec(nexec.Nats("--context", adminContext, "stream", "add", "--config", registryConfig.Name()))
if _, err = nats.Output(); err != nil {
nexec.LogError("failed to add logs stream", err)
return
}
log.Info("setup complete")
return nil
}