-
Notifications
You must be signed in to change notification settings - Fork 26
/
run.go
80 lines (63 loc) · 1.85 KB
/
run.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
package cmd
import (
"fmt"
"os"
"github.com/aserto-dev/topaz/pkg/cli/cc"
"github.com/aserto-dev/topaz/pkg/cli/dockerx"
"github.com/fatih/color"
)
type RunCmd 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 *RunCmd) 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...)
path, err := dockerx.DefaultRoots()
if err != nil {
return err
}
return dockerx.DockerWith(cmd.env(path), args...)
}
func (cmd *RunCmd) dockerArgs() []string {
args := append([]string{}, dockerCmd...)
args = append(args, "-ti")
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...)
for _, env := range cmd.Env {
args = append(args, "--env", env)
}
if cmd.Hostname != "" {
args = append(args, hostname...)
}
return append(args, containerName...)
}
func (cmd *RunCmd) env(path string) map[string]string {
return map[string]string{
"TOPAZ_CERTS_DIR": path,
"TOPAZ_CFG_DIR": path,
"TOPAZ_EDS_DIR": path,
"CONTAINER_NAME": cmd.ContainerName,
"CONTAINER_VERSION": cmd.ContainerVersion,
"CONTAINER_HOSTNAME": cmd.Hostname,
}
}