-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
313 lines (275 loc) · 8.12 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package mdocker
import (
"errors"
"net/http"
"github.com/fsouza/go-dockerclient"
"github.com/mistifyio/mistify-agent/rpc"
)
const (
cStateRunning = "running"
cStatePaused = "paused"
cStateStopped = "stopped"
)
func (md *MDocker) containersFromAPIContainers(acs []docker.APIContainers) ([]*docker.Container, error) {
containers := make([]*docker.Container, 0, len(acs))
for _, ac := range acs {
container, err := md.client.InspectContainer(ac.ID)
if err != nil {
return nil, err
}
containers = append(containers, container)
}
return containers, nil
}
func (md *MDocker) fetchContainerState(containerID string) (string, error) {
container, err := md.client.InspectContainer(containerID)
if err != nil {
return "", err
}
if container.State.Paused {
return cStatePaused, nil
}
if container.State.Running {
return cStateRunning, nil
}
return cStateStopped, nil
}
func assertContainerState(expected, actual string) error {
if actual != expected {
return errors.New("unexpected container state")
}
return nil
}
func requestContainerName(request *rpc.GuestRequest) (string, error) {
if request.Guest == nil || request.Guest.ID == "" {
return "", errors.New("missing guest with id")
}
return request.Guest.ID, nil
}
// ListContainers retrieves a list of Docker containers
func (md *MDocker) ListContainers(h *http.Request, request *rpc.ContainerRequest, response *rpc.ContainerResponse) error {
var opts docker.ListContainersOptions
if err := md.RequestOpts(request, &opts); err != nil {
return err
}
apiContainers, err := md.client.ListContainers(opts)
if err != nil {
return err
}
containers, err := md.containersFromAPIContainers(apiContainers)
if err != nil {
return err
}
response.Containers = containers
return nil
}
// GetContainer retrieves information about a specific Docker container
func (md *MDocker) GetContainer(h *http.Request, request *rpc.ContainerRequest, response *rpc.ContainerResponse) error {
container, err := md.client.InspectContainer(request.ID)
if err != nil {
return err
}
response.Containers = []*docker.Container{
container,
}
return nil
}
// DeleteContainer deletes a Docker container
func (md *MDocker) DeleteContainer(h *http.Request, request *rpc.GuestRequest, response *rpc.GuestResponse) error {
containerName, err := requestContainerName(request)
if err != nil {
return err
}
opts := docker.RemoveContainerOptions{
ID: containerName,
}
if err := md.client.RemoveContainer(opts); err != nil {
return err
}
response.Guest = request.Guest
response.Guest.State = "deleted"
return nil
}
// SaveContainer saves a Docker container
func (md *MDocker) SaveContainer(h *http.Request, request *rpc.ContainerRequest, response *rpc.ImageResponse) error {
var opts docker.CommitContainerOptions
if err := md.RequestOpts(request, &opts); err != nil {
return err
}
if request.ID != "" {
opts.Container = request.ID
}
image, err := md.client.CommitContainer(opts)
if err != nil {
return err
}
response.Images = []*rpc.Image{
{
ID: image.ID,
Type: "container",
Size: uint64(image.Size) / 1024 / 1024,
},
}
return nil
}
// CreateContainer creates a new Docker container
func (md *MDocker) CreateContainer(h *http.Request, request *rpc.GuestRequest, response *rpc.GuestResponse) error {
containerName, err := requestContainerName(request)
if err != nil {
return err
}
guest := request.Guest
if len(guest.Nics) == 0 {
return errors.New("must specify at least one nic")
}
// TODO: Some of these options might be better handled as guest metadata
// instead of hardcoding, such as the openstdin option, port forwarding,
// and zfs devices
opts := docker.CreateContainerOptions{
Name: containerName,
Config: &docker.Config{
Hostname: guest.ID,
Image: guest.Image,
OpenStdin: true,
MacAddress: guest.Nics[0].Mac,
Memory: int64(guest.Memory) * 1024 * 1024, // Convert MB to bytes
},
HostConfig: &docker.HostConfig{
// A network interface will be added separately. The "none" option
// may not be listed in the docker remote api docs, but it works
NetworkMode: "none",
// Expose /dev/zfs inside all containers (okay because they are unprivileged)
Devices: []docker.Device{
{
PathOnHost: "/dev/zfs",
PathInContainer: "/dev/zfs",
CgroupPermissions: "rwm",
},
},
},
}
container, err := md.client.CreateContainer(opts)
if err != nil {
return err
}
state, err := md.fetchContainerState(container.ID)
if err != nil {
return err
}
guest.State = state
response.Guest = guest
return nil
}
// StartContainer starts a Docker container
func (md *MDocker) StartContainer(h *http.Request, request *rpc.GuestRequest, response *rpc.GuestResponse) error {
// Make sure there are no lingering interfaces for the guest from a previous
// run
if err := removeInterfaces(request.Guest); err != nil {
return err
}
containerName, err := requestContainerName(request)
if err != nil {
return err
}
err = md.client.StartContainer(containerName, nil)
if _, ok := err.(*docker.ContainerAlreadyRunning); err != nil && !ok {
return err
}
state, err := md.fetchContainerState(containerName)
if err != nil {
return err
}
if err := assertContainerState(cStateRunning, state); err != nil {
return err
}
response.Guest = request.Guest
response.Guest.State = state
if err := addInterfaces(request.Guest); err != nil {
return err
}
return nil
}
// StopContainer stops a Docker container or kills it after a timeout
func (md *MDocker) StopContainer(h *http.Request, request *rpc.GuestRequest, response *rpc.GuestResponse) error {
containerName, err := requestContainerName(request)
if err != nil {
return err
}
err = md.client.StopContainer(containerName, 10)
if _, ok := err.(*docker.ContainerNotRunning); err != nil && !ok {
return err
}
state, err := md.fetchContainerState(containerName)
if err != nil {
return err
}
if err := assertContainerState(cStateStopped, state); err != nil {
return err
}
response.Guest = request.Guest
response.Guest.State = state
// The virtual interfaces are destroyed when the container stops, but are
// still being tracked in OVS. Clean things up.
if err := removeInterfaces(request.Guest); err != nil {
return err
}
return nil
}
// RestartContainer restarts a Docker container
func (md *MDocker) RestartContainer(h *http.Request, request *rpc.GuestRequest, response *rpc.GuestResponse) error {
// Since action needs to be taken relating to network interfaces when the guest
// is stopped and again when the guest is started, the individual methods are
// used here. The `docker restart` command is an alias for a stop then start
// anyway.
if err := md.StopContainer(h, request, response); err != nil {
return err
}
if err := md.StartContainer(h, request, response); err != nil {
return err
}
return nil
}
// RebootContainer restarts a Docker container
func (md *MDocker) RebootContainer(h *http.Request, request *rpc.GuestRequest, response *rpc.GuestResponse) error {
return md.RestartContainer(h, request, response)
}
// PauseContainer pauses a Docker container
func (md *MDocker) PauseContainer(h *http.Request, request *rpc.GuestRequest, response *rpc.GuestResponse) error {
containerName, err := requestContainerName(request)
if err != nil {
return err
}
if err := md.client.PauseContainer(containerName); err != nil {
return err
}
state, err := md.fetchContainerState(containerName)
if err != nil {
return err
}
if err := assertContainerState(cStatePaused, state); err != nil {
return err
}
response.Guest = request.Guest
response.Guest.State = state
return nil
}
// UnpauseContainer restarts a Docker container
func (md *MDocker) UnpauseContainer(h *http.Request, request *rpc.GuestRequest, response *rpc.GuestResponse) error {
containerName, err := requestContainerName(request)
if err != nil {
return err
}
if err := md.client.UnpauseContainer(containerName); err != nil {
return err
}
state, err := md.fetchContainerState(containerName)
if err != nil {
return err
}
if err := assertContainerState(cStateRunning, state); err != nil {
return err
}
response.Guest = request.Guest
response.Guest.State = state
return nil
}