forked from containers/virtcontainers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.go
405 lines (322 loc) · 8.97 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package virtcontainers
import (
"fmt"
"os"
"path/filepath"
"syscall"
"github.com/01org/ciao/ssntp/uuid"
"github.com/golang/glog"
)
// Process gathers data related to a container process.
type Process struct {
Token string
Pid int
}
// ContainerStatus describes a container status.
type ContainerStatus struct {
ID string
State State
PID int
RootFs string
}
// ContainerConfig describes one container runtime configuration.
type ContainerConfig struct {
ID string
// RootFs is the container workload image on the host.
RootFs string
// Interactive specifies if the container runs in the foreground.
Interactive bool
// Console is a console path provided by the caller.
Console string
// Cmd specifies the command to run on a container
Cmd Cmd
}
// valid checks that the container configuration is valid.
func (containerConfig *ContainerConfig) valid() bool {
if containerConfig.ID == "" {
containerConfig.ID = uuid.Generate().String()
}
return true
}
// Container is composed of a set of containers and a runtime environment.
// A Container can be created, deleted, started, stopped, listed, entered, paused and restored.
type Container struct {
id string
podID string
rootFs string
config *ContainerConfig
pod *Pod
runPath string
configPath string
containerPath string
state State
process Process
}
// ID returns the container identifier string.
func (c *Container) ID() string {
return c.id
}
// Process returns the container process.
func (c *Container) Process() Process {
return c.process
}
// GetToken returns the token related to this container's process.
func (c *Container) GetToken() string {
return c.process.Token
}
// GetPid returns the pid related to this container's process.
func (c *Container) GetPid() int {
return c.process.Pid
}
// SetPid sets and stores the given pid as the pid of container's process.
func (c *Container) SetPid(pid int) error {
c.process.Pid = pid
return c.storeProcess()
}
func (c *Container) storeProcess() error {
return c.pod.storage.storeContainerProcess(c.podID, c.id, c.process)
}
func (c *Container) fetchProcess() (Process, error) {
return c.pod.storage.fetchContainerProcess(c.podID, c.id)
}
// fetchContainer fetches a container config from a pod ID and returns a Container.
func fetchContainer(pod *Pod, containerID string) (*Container, error) {
fs := filesystem{}
config, err := fs.fetchContainerConfig(pod.id, containerID)
if err != nil {
return nil, err
}
glog.Infof("Info structure:\n%+v\n", config)
return createContainer(pod, config)
}
// storeContainer stores a container config.
func (c *Container) storeContainer() error {
fs := filesystem{}
err := fs.storeContainerResource(c.pod.id, c.id, configFileType, *(c.config))
if err != nil {
return err
}
return nil
}
func (c *Container) setContainerState(state stateString) error {
c.state = State{
State: state,
}
err := c.pod.storage.storeContainerResource(c.podID, c.id, stateFileType, c.state)
if err != nil {
return err
}
return nil
}
func (c *Container) createContainersDirs() error {
err := os.MkdirAll(c.runPath, dirMode)
if err != nil {
return err
}
err = os.MkdirAll(c.configPath, dirMode)
if err != nil {
c.pod.storage.deleteContainerResources(c.podID, c.id, nil)
return err
}
return nil
}
func createContainers(pod *Pod, contConfigs []ContainerConfig) ([]*Container, error) {
var containers []*Container
for _, contConfig := range contConfigs {
if contConfig.valid() == false {
return containers, fmt.Errorf("Invalid container configuration")
}
c := &Container{
id: contConfig.ID,
podID: pod.id,
rootFs: contConfig.RootFs,
config: &contConfig,
pod: pod,
runPath: filepath.Join(runStoragePath, pod.id, contConfig.ID),
configPath: filepath.Join(configStoragePath, pod.id, contConfig.ID),
containerPath: filepath.Join(pod.id, contConfig.ID),
state: State{},
process: Process{},
}
state, err := c.pod.storage.fetchContainerState(c.podID, c.id)
if err == nil {
c.state.State = state.State
}
process, err := c.pod.storage.fetchContainerProcess(c.podID, c.id)
if err == nil {
c.process = process
}
containers = append(containers, c)
}
return containers, nil
}
func createContainer(pod *Pod, contConfig ContainerConfig) (*Container, error) {
if contConfig.valid() == false {
return nil, fmt.Errorf("Invalid container configuration")
}
c := &Container{
id: contConfig.ID,
podID: pod.id,
rootFs: contConfig.RootFs,
config: &contConfig,
pod: pod,
runPath: filepath.Join(runStoragePath, pod.id, contConfig.ID),
configPath: filepath.Join(configStoragePath, pod.id, contConfig.ID),
containerPath: filepath.Join(pod.id, contConfig.ID),
state: State{},
process: Process{},
}
err := c.createContainersDirs()
if err != nil {
return nil, err
}
process, err := c.fetchProcess()
if err == nil {
c.process = process
}
state, err := c.pod.storage.fetchContainerState(c.podID, c.id)
if err == nil && state.State != "" {
c.state.State = state.State
return c, nil
}
// If we reached that point, this means that no state file has been
// found and that we are in the first creation of this container.
// We don't want the following code to be executed outside of this
// specific case.
pod.containers = append(pod.containers, c)
if err := c.pod.agent.createContainer(pod, c); err != nil {
return nil, err
}
if err := c.pod.setContainerState(c.id, StateReady); err != nil {
return nil, err
}
return c, nil
}
func (c *Container) delete() error {
state, err := c.pod.storage.fetchContainerState(c.podID, c.id)
if err != nil {
return err
}
if state.State != StateReady && state.State != StateStopped {
return fmt.Errorf("Container not ready or stopped, impossible to delete")
}
err = c.pod.storage.deleteContainerResources(c.podID, c.id, nil)
if err != nil {
return err
}
return nil
}
// fetchState retrieves the container state.
//
// cmd specifies the operation (or verb) that the retieval is destined
// for and is only used to make the returned error as descriptive as
// possible.
func (c *Container) fetchState(cmd string) (State, error) {
state, err := c.pod.storage.fetchPodState(c.pod.id)
if err != nil {
return State{}, err
}
if state.State != StateRunning {
return State{}, fmt.Errorf("Pod not running, impossible to %s the container", cmd)
}
state, err = c.pod.storage.fetchContainerState(c.podID, c.id)
if err != nil {
return State{}, err
}
return state, nil
}
func (c *Container) start() error {
state, err := c.fetchState("start")
if err != nil {
return err
}
if state.State != StateReady && state.State != StateStopped {
return fmt.Errorf("Container not ready or stopped, impossible to start")
}
err = state.validTransition(StateReady, StateRunning)
if err != nil {
err = state.validTransition(StateStopped, StateRunning)
if err != nil {
return err
}
}
err = c.pod.agent.startContainer(*(c.pod), *c)
if err != nil {
c.stop()
return err
}
err = c.setContainerState(StateRunning)
if err != nil {
return err
}
return nil
}
func (c *Container) stop() error {
state, err := c.fetchState("stop")
if err != nil {
return err
}
if state.State != StateRunning {
return fmt.Errorf("Container not running, impossible to stop")
}
err = state.validTransition(StateRunning, StateStopped)
if err != nil {
return err
}
err = c.pod.agent.killContainer(*(c.pod), *c, syscall.SIGTERM)
if err != nil {
return err
}
err = c.pod.agent.stopContainer(*(c.pod), *c)
if err != nil {
return err
}
err = c.setContainerState(StateStopped)
if err != nil {
return err
}
return nil
}
func (c *Container) enter(cmd Cmd) (*Process, error) {
state, err := c.fetchState("enter")
if err != nil {
return nil, err
}
if state.State != StateRunning {
return nil, fmt.Errorf("Container not running, impossible to enter")
}
process, err := c.pod.agent.exec(c.pod, *c, cmd)
if err != nil {
return nil, err
}
return process, nil
}
func (c *Container) kill(signal syscall.Signal) error {
state, err := c.fetchState("signal")
if err != nil {
return err
}
if state.State != StateRunning {
return fmt.Errorf("Container not running, impossible to signal the container")
}
err = c.pod.agent.killContainer(*(c.pod), *c, signal)
if err != nil {
return err
}
return nil
}