-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontainer.go
140 lines (126 loc) · 2.63 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
package docker
import (
"context"
"time"
"github.com/docker/docker/api/types"
)
type Container struct {
ID string
isStarted bool
client *Client
options ContainerOptions
}
func NewContainer(client *Client, paramOpts ...ContainerOption) (*Container, error) {
options := NewContainerOptions(client, paramOpts...)
if !client.HasImage(options.containerConfig.Image) {
err := client.PullImage(options.containerConfig.Image)
if err != nil {
return nil, err
}
}
c, err := client.ContainerCreate(
options.context,
options.containerConfig,
options.hostConfig,
options.networkConfig,
options.name,
)
if err != nil {
return nil, err
}
container := &Container{
ID: c.ID,
client: client,
options: *options,
}
go func() {
ctx := container.options.context
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded {
container.Stop()
}
}()
return container, nil
}
func (c *Container) Start() error {
client := c.client
err := client.ContainerStart(
c.options.context,
c.ID,
types.ContainerStartOptions{},
)
if err != nil {
return err
}
c.isStarted = true
return nil
}
func (c *Container) Info() (types.ContainerJSON, error) {
client := c.client
info, err := client.ContainerInspect(
c.options.context,
c.ID,
)
if err != nil {
return types.ContainerJSON{}, err
}
return info, nil
}
func (c *Container) Close() error {
return c.Stop()
}
func (c *Container) Stop() error {
defer func() {
c.options.cancelFunc()
c.isStarted = false
}()
if c.options.visibleGPUs != nil && GPUDeviceUsageState != nil {
for key, val := range c.options.visibleGPUs {
GPUDeviceUsageState.Add(key, val)
}
}
if c.isStarted {
// if err := c.stop(); err != nil {
// log.WithError(err).Errorf("failed to stop container %v", c.ID)
// }
// if err := c.kill(); err != nil {
// log.WithError(err).Errorf("failed to stop container %v", c.ID)
// }
c.stop()
c.kill()
}
return c.remove()
}
func (c *Container) stop() error {
containerStopTimeout := 30 * time.Second
err := c.client.ContainerStop(context.TODO(), c.ID, &containerStopTimeout)
return err
}
func (c *Container) killWithSignal(sig string) error {
client := c.client
err := client.ContainerKill(
c.options.context,
c.ID,
sig,
)
return err
}
func (c *Container) kill() error {
return c.killWithSignal("SIGKILL")
}
func (c *Container) remove() error {
client := c.client
err := client.ContainerRemove(
c.options.context,
c.ID,
types.ContainerRemoveOptions{
RemoveVolumes: true,
// RemoveLinks: true,
Force: true,
},
)
return err
}
func (c *Container) Options() ContainerOptions {
return c.options
}