-
Notifications
You must be signed in to change notification settings - Fork 26
/
start.go
129 lines (103 loc) · 2.83 KB
/
start.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
package cmd
import (
"fmt"
"os"
"path"
"github.com/aserto-dev/topaz/pkg/cli/cc"
"github.com/aserto-dev/topaz/pkg/cli/dockerx"
"github.com/pkg/errors"
"github.com/fatih/color"
)
type StartCmd struct {
ContainerName string `optional:"" default:"topaz" help:"container name"`
ContainerVersion string `optional:"" default:"latest" help:"container version" `
Hostname string `optional:"" help:"hostname for docker to set"`
Env []string `optional:"" short:"e" help:"additional environment variable names to be passed to container"`
}
func (cmd *StartCmd) Run(c *cc.CommonCtx) error {
if running, err := dockerx.IsRunning(dockerx.Topaz); running || err != nil {
if !running {
return ErrNotRunning
}
if err != nil {
return err
}
}
color.Green(">>> starting topaz...")
args := cmd.dockerArgs()
cmdArgs := []string{
"run",
"--config-file", "/config/config.yaml",
}
args = append(args, cmdArgs...)
rootPath, err := dockerx.DefaultRoots()
if err != nil {
return err
}
if _, err := os.Stat(path.Join(rootPath, "cfg", "config.yaml")); errors.Is(err, os.ErrNotExist) {
return errors.Errorf("%s does not exist, please run 'topaz configure'", path.Join(rootPath, "cfg", "config.yaml"))
}
if _, err := CreateCertsDir(); err != nil {
return err
}
if _, err := CreateDataDir(); err != nil {
return err
}
return dockerx.DockerWith(cmd.env(rootPath), args...)
}
var (
dockerCmd = []string{
"run",
}
dockerArgs = []string{
"--rm",
"--name", dockerx.Topaz,
"--platform=linux/amd64",
"-p", "8282:8282",
"-p", "8383:8383",
"-p", "8484:8484",
"-p", "9292:9292",
"-v", "$TOPAZ_CERTS_DIR/certs:/certs:rw",
"-v", "$TOPAZ_CFG_DIR/cfg:/config:ro",
"-v", "$TOPAZ_EDS_DIR/db:/db:rw",
}
daemonArgs = []string{
"-d",
}
containerName = []string{
"ghcr.io/aserto-dev/$CONTAINER_NAME:$CONTAINER_VERSION",
}
hostname = []string{
"--hostname", "$CONTAINER_HOSTNAME",
}
platform = []string{
"--platform", "linux/amd64",
}
)
func (cmd *StartCmd) dockerArgs() []string {
args := append([]string{}, dockerCmd...)
policyRoot := os.Getenv("POLICY_FILE_STORE_ROOT ")
if policyRoot == "" {
policyRoot = dockerx.DefaultPolicyRoot
}
dockerArgs = append(dockerArgs, "-v", fmt.Sprintf("%s:/root/.policy:ro", policyRoot))
args = append(args, dockerArgs...)
args = append(args, daemonArgs...)
for _, env := range cmd.Env {
args = append(args, "--env", env)
}
if cmd.Hostname != "" {
args = append(args, hostname...)
}
return append(args, containerName...)
}
func (cmd *StartCmd) env(rootPath string) map[string]string {
return map[string]string{
"TOPAZ_CERTS_DIR": rootPath,
"TOPAZ_CFG_DIR": rootPath,
"TOPAZ_EDS_DIR": rootPath,
"CONTAINER_NAME": cmd.ContainerName,
"CONTAINER_VERSION": cmd.ContainerVersion,
"CONTAINER_HOSTNAME": cmd.Hostname,
}
}