This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
/
container.go
133 lines (107 loc) · 2.84 KB
/
container.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
package container
import (
"context"
"os"
dockerCont "github.com/docker/docker/api/types/container"
dockerMnt "github.com/docker/docker/api/types/mount"
docker "github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
"github.com/mee6aas/zeep/api"
)
const (
dockerEngineAPIVersion = api.DockerAPIVersion
)
var (
engineClient *docker.Client
agentNet string
agentHost string
agentPort string
agentDlgt string
)
// Container describes container.
type Container struct {
id string // ID of container
ip string // IP of container
}
func (c Container) Unpause() {
engineClient.ContainerUnpause(context.Background(), c.id)
}
func (c Container) Pause() {
engineClient.ContainerPause(context.Background(), c.id)
}
// ID returns the ID of this container.
func (c Container) ID() string { return c.id }
// IP returns the IP address of this container.
func (c Container) IP() string { return c.ip }
// Config holds the configuration for the container.
type Config struct {
Image string // Image to create container
Storage string // Path of the directory to be mounted on container
}
// NewContainer creates a new container based on the given configuration
// and returns its descriptor.
func NewContainer(ctx context.Context, conf Config) (c Container, e error) {
cConf := &dockerCont.Config{
Image: conf.Image,
Env: []string{
api.AgentHostEnvKey + "=" + agentHost,
api.AgentPortEnvKey + "=" + agentPort,
},
Entrypoint: []string{"/bin/sh", "-c"},
Cmd: []string{
"[ -f " + api.RuntimeSetup + " ] && " + api.RuntimeSetup + "; " + api.RuntimeSpawn,
},
}
mnts := []dockerMnt.Mount{
dockerMnt.Mount{
Type: dockerMnt.TypeBind,
Source: conf.Storage,
Target: api.ActivityStorage,
BindOptions: &dockerMnt.BindOptions{
Propagation: dockerMnt.PropagationShared,
},
},
}
if agentDlgt != "" {
cConf.Cmd = []string{
"[ -f " + api.RuntimeSetup + " ] && " + api.RuntimeSetup + "; " + api.AgentDelegate,
}
if log.GetLevel() == log.DebugLevel {
cConf.Cmd[0] = cConf.Cmd[0] + " --debug"
}
mnts = append(mnts, dockerMnt.Mount{
Type: dockerMnt.TypeBind,
Source: agentDlgt,
Target: api.AgentDelegate,
})
}
res, e := engineClient.ContainerCreate(ctx, cConf, &dockerCont.HostConfig{
NetworkMode: dockerCont.NetworkMode(agentNet),
Mounts: mnts,
}, nil, "")
if e != nil {
return c, e
}
c.id = res.ID
return
}
func init() {
const (
apiVersion = dockerEngineAPIVersion
)
var (
err error
client *docker.Client
)
if client, err = docker.NewClientWithOpts(docker.WithVersion(apiVersion)); err != nil {
panic(err)
}
engineClient = client
agentNet = os.Getenv(api.AgentNetworkEnvKey)
agentHost = os.Getenv(api.AgentHostEnvKey)
agentPort = os.Getenv(api.AgentPortEnvKey)
agentDlgt = os.Getenv(api.AgentDelegateHostPathEnvKey)
if agentNet == "" {
agentNet = "bridge"
}
}