-
Notifications
You must be signed in to change notification settings - Fork 7
/
docker_option.go
88 lines (75 loc) · 2.41 KB
/
docker_option.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
package domain
import (
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/flowci/flow-agent-x/util"
)
type (
DockerOption struct {
Image string `json:"image"`
Auth string `json:"auth"`
Name string `json:"name"`
Entrypoint []string `json:"entrypoint"` // host:container
Command []string `json:"command"`
Ports []string `json:"ports"`
Network string `json:"network"`
Environment Variables `json:"environment"`
User string `json:"user"`
IsRuntime bool `json:"isRuntime"`
IsStopContainer bool `json:"isStopContainer"`
IsDeleteContainer bool `json:"isDeleteContainer"`
ContainerID string // try to resume if container id is existed
AuthContent *SimpleAuthPair // the real auth secret from 'auth' name
}
)
func (d *DockerOption) HasAuth() bool {
return d.Auth != ""
}
func (d *DockerOption) ToRuntimeConfig(vars Variables, workingDir string, binds []string) *DockerConfig {
return d.toConfig(vars, workingDir, binds, true)
}
func (d *DockerOption) ToConfig() *DockerConfig {
return d.toConfig(nil, "", nil, false)
}
func (d *DockerOption) SetDefaultNetwork(network string) {
if d.Network == "bridge" || d.Network == "" {
d.Network = network
}
}
func (d *DockerOption) toConfig(vars Variables, workingDir string, binds []string, enableInput bool) (config *DockerConfig) {
portSet, portMap, err := nat.ParsePortSpecs(d.Ports)
util.PanicIfErr(err)
vars = ConnectVars(vars, d.Environment)
config = &DockerConfig{
Name: d.Name,
Config: &container.Config{
Image: util.ParseStringWithSource(d.Image, vars),
Env: vars.ToStringArray(),
Entrypoint: d.Entrypoint,
Cmd: d.Command,
ExposedPorts: portSet,
User: d.User,
Tty: false,
AttachStdin: enableInput,
AttachStderr: enableInput,
AttachStdout: enableInput,
OpenStdin: enableInput,
StdinOnce: enableInput,
},
Host: &container.HostConfig{
NetworkMode: container.NetworkMode(d.Network),
PortBindings: portMap,
},
IsStop: d.IsStopContainer,
IsDelete: d.IsDeleteContainer,
ContainerID: d.ContainerID,
Auth: d.AuthContent,
}
if util.HasString(workingDir) {
config.Config.WorkingDir = workingDir
}
if binds != nil {
config.Host.Binds = binds
}
return
}