-
Notifications
You must be signed in to change notification settings - Fork 24
/
startrun.go
170 lines (142 loc) · 4.8 KB
/
startrun.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
package cmd
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/aserto-dev/topaz/pkg/cc/config"
"github.com/aserto-dev/topaz/pkg/cli/cc"
"github.com/aserto-dev/topaz/pkg/cli/dockerx"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/samber/lo"
)
type StartRunCmd struct {
ContainerRegistry string `optional:"" default:"${container_registry}" env:"CONTAINER_REGISTRY" help:"container registry (host[:port]/repo)"`
ContainerImage string `optional:"" default:"${container_image}" env:"CONTAINER_IMAGE" help:"container image name"`
ContainerTag string `optional:"" default:"${container_tag}" env:"CONTAINER_TAG" help:"container tag"`
ContainerPlatform string `optional:"" default:"${container_platform}" env:"CONTAINER_PLATFORM" help:"container platform"`
ContainerName string `optional:"" default:"${container_name}" env:"CONTAINER_NAME" help:"container name"`
ContainerHostname string `optional:"" name:"hostname" default:"" env:"CONTAINER_HOSTNAME" help:"hostname for docker to set"`
Env []string `optional:"" short:"e" help:"additional environment variable names to be passed to container"`
ContainerVersion string `optional:"" hidden:"" default:"" env:"CONTAINER_VERSION"`
}
type runMode int
const (
modeDaemon runMode = iota
modeInteractive
)
func (cmd *StartRunCmd) run(c *cc.CommonCtx, mode runMode) error {
if c.CheckRunStatus(cmd.ContainerName, cc.StatusRunning) {
return ErrIsRunning
}
if _, err := os.Stat(path.Join(cc.GetTopazCfgDir(), "config.yaml")); errors.Is(err, os.ErrNotExist) {
return errors.Errorf("%s does not exist, please run 'topaz configure'", path.Join(cc.GetTopazCfgDir(), "config.yaml"))
}
cfg, err := config.LoadConfiguration(filepath.Join(cc.GetTopazCfgDir(), "config.yaml"))
if err != nil {
return err
}
generator := config.NewGenerator("config.yaml")
if _, err := generator.CreateCertsDir(); err != nil {
return err
}
if _, err := generator.CreateDataDir(); err != nil {
return err
}
ports, err := getPorts(cfg)
if err != nil {
return err
}
volumes, err := getVolumes(cfg)
if err != nil {
return err
}
color.Green(">>> starting topaz...")
dc, err := dockerx.New()
if err != nil {
return err
}
opts := []dockerx.RunOption{
dockerx.WithContainerImage(cc.Container(
cmd.ContainerRegistry, // registry
cmd.ContainerImage, // image
cmd.ContainerTag, // tag
)),
dockerx.WithContainerPlatform("linux", strings.TrimPrefix(cmd.ContainerPlatform, "linux/")),
dockerx.WithContainerName(cmd.ContainerName),
dockerx.WithContainerHostname(cmd.ContainerHostname),
dockerx.WithWorkingDir("/app"),
dockerx.WithEntrypoint([]string{"./topazd"}),
dockerx.WithCmd([]string{"run", "--config-file", "/config/config.yaml"}),
dockerx.WithAutoRemove(),
dockerx.WithEnvs(getEnvFromVolumes(volumes)),
dockerx.WithEnvs(cmd.Env),
dockerx.WithPorts(ports),
dockerx.WithVolumes(volumes),
dockerx.WithOutput(c.UI.Output()),
dockerx.WithError(c.UI.Err()),
}
if mode == modeInteractive {
if err := dc.Run(opts...); err != nil {
return err
}
}
if mode == modeDaemon {
if err := dc.Start(opts...); err != nil {
return err
}
}
fmt.Fprintf(c.UI.Output(), "\n")
return nil
}
func getPorts(cfg *config.Loader) ([]string, error) {
portArray, err := cfg.GetPorts()
if err != nil {
return nil, err
}
// ensure unique assignment for each port
portMap := lo.Associate(portArray, func(port string) (string, string) {
return port, fmt.Sprintf("%s:%s/tcp", port, port)
})
var ports []string
for _, v := range portMap {
ports = append(ports, v)
}
return ports, nil
}
func getVolumes(cfg *config.Loader) ([]string, error) {
paths, err := cfg.GetPaths()
if err != nil {
return nil, err
}
volumeMap := lo.Associate(paths, func(path string) (string, string) {
dir := filepath.Dir(path)
return dir, fmt.Sprintf("%s:%s", dir, fmt.Sprintf("/%s", filepath.Base(dir)))
})
volumes := []string{
fmt.Sprintf("%s:/config:ro", cc.GetTopazCfgDir()), // manually attach the configuration folder
fmt.Sprintf("%s:/root/.policy:ro", dockerx.PolicyRoot()), // manually attache policy store
}
for _, v := range volumeMap {
volumes = append(volumes, v)
}
return volumes, nil
}
func getEnvFromVolumes(volumes []string) []string {
envs := []string{}
for i := range volumes {
destination := strings.Split(volumes[i], ":")
mountedPath := fmt.Sprintf("/%s", filepath.Base(destination[1])) // last value from split.
switch {
case strings.Contains(volumes[i], "certs"):
envs = append(envs, fmt.Sprintf("TOPAZ_CERTS_DIR=%s", mountedPath))
case strings.Contains(volumes[i], "db"):
envs = append(envs, fmt.Sprintf("TOPAZ_DB_DIR=%s", mountedPath))
case strings.Contains(volumes[i], "cfg"):
envs = append(envs, fmt.Sprintf("TOPAZ_CFG_DIR=%s", mountedPath))
}
}
return envs
}