-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
208 lines (191 loc) · 4.82 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
package dockertest
import (
"context"
"fmt"
"io"
"path/filepath"
"strings"
"github.com/docker/docker/api/types"
dc "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/pkg/errors"
)
type Container struct {
cli *client.Client
Name string
ImageCfg *ImageCfg
Image string
PortBindings nat.PortMap
Mounts []mount.Mount
Env []string
Cmd []string
Healthcheck *dc.HealthConfig
Hooks []*Hooks
// 是否是新启动的镜像
Fresh bool
}
func NewContainer(cli *client.Client, name string, imageCfg *ImageCfg, dir string) (ct *Container, err error) {
port := nat.PortMap{}
if len(imageCfg.Ports) > 0 {
for _, p := range imageCfg.Ports {
arr := strings.Split(p, ":")
if len(arr) != 2 {
err = errors.New(fmt.Sprintf("wrong port: %v", p))
return
}
port[nat.Port(arr[1]+"/tcp")] = []nat.PortBinding{{
HostIP: "127.0.0.1",
HostPort: arr[0] + "/tcp",
}}
}
}
var ms []mount.Mount
mounts := imageCfg.Volumes
for _, m := range mounts {
as := strings.Split(m, ":")
if len(as) != 2 {
err = errors.New(fmt.Sprintf("wrong volumn: %v", m))
return
}
path := filepath.Join(dir, as[0])
ms = append(ms, mount.Mount{
Type: mount.TypeBind,
Source: path,
Target: as[1],
})
}
var healthy *dc.HealthConfig
if imageCfg.HealthCheck != nil {
healthy = &dc.HealthConfig{
Test: imageCfg.HealthCheck.Test,
Interval: imageCfg.HealthCheck.Interval,
Timeout: imageCfg.HealthCheck.Timeout,
Retries: imageCfg.HealthCheck.Retries,
}
}
ct = &Container{
cli: cli,
ImageCfg: imageCfg,
Image: imageCfg.Image,
PortBindings: port,
Mounts: ms,
Env: imageCfg.Environment,
Cmd: imageCfg.Command,
Healthcheck: healthy,
Name: name,
Hooks: imageCfg.Hooks,
}
return
}
func (ct *Container) shortRef() (t string) {
ref := ct.Image
if strings.Contains(ref, ":") {
return ref
}
return ref + ":latest"
}
func (ct *Container) longRef() (t string) {
ref := ct.Image
if strings.Contains(ref, "/") {
return ref
}
return "docker.io/library/" + ref
}
func (ct *Container) Pull(c context.Context) (io.ReadCloser, error) {
return ct.cli.ImagePull(c, ct.longRef(), types.ImagePullOptions{})
}
func (ct *Container) PullIfNotExist(c context.Context) (io.ReadCloser, error) {
list, err := ct.cli.ImageList(c, types.ImageListOptions{})
if err != nil {
return nil, err
}
for _, l := range list {
for _, t := range l.RepoTags {
if t == ct.shortRef() {
return nil, nil
}
}
}
return ct.Pull(c)
}
func (ct *Container) Create(c context.Context) (dc.ContainerCreateCreatedBody, error) {
host := &dc.HostConfig{
Binds: nil,
LogConfig: dc.LogConfig{},
NetworkMode: "",
PortBindings: ct.PortBindings,
Mounts: ct.Mounts,
}
return ct.cli.ContainerCreate(c, &dc.Config{
Image: ct.longRef(),
Env: ct.Env,
Cmd: ct.Cmd,
Healthcheck: ct.Healthcheck,
}, host, nil, ct.Name)
}
func (ct *Container) Start(c context.Context) (err error) {
return ct.cli.ContainerStart(c, ct.Name, types.ContainerStartOptions{})
}
func (ct *Container) Logs(c context.Context, follow bool) (io.ReadCloser, error) {
return ct.cli.ContainerLogs(c, ct.Name, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: follow,
Tail: "",
})
}
func (ct *Container) Kill(c context.Context) error {
return ct.cli.ContainerKill(c, ct.Name, "KILL")
}
func (ct *Container) Remove(c context.Context) error {
return ct.cli.ContainerRemove(c, ct.Name, types.ContainerRemoveOptions{
RemoveVolumes: true,
RemoveLinks: false,
Force: true,
})
}
func (ct *Container) Healthy(c context.Context) (ok bool, err error) {
cj, err := ct.cli.ContainerInspect(c, ct.Name)
if err != nil {
return
}
health := cj.ContainerJSONBase.State.Health
if health == nil {
return true, nil
}
return health.Status == types.Healthy, nil
}
func (ct *Container) Running(c context.Context) (ok bool, err error) {
var status types.ContainerJSON
if status, err = ct.cli.ContainerInspect(c, ct.Name); err != nil {
if client.IsErrNotFound(err) {
err = nil
}
return
}
return status.State.Running, nil
}
func (ct *Container) Exist(c context.Context) (ok bool, err error) {
if _, err = ct.cli.ContainerInspect(c, ct.Name); err != nil {
if client.IsErrNotFound(err) {
return false, nil
}
return
}
return true, nil
}
func (ct *Container) Exec(c context.Context, cmd []string) (err error) {
res, err := ct.cli.ContainerExecCreate(c, ct.Name, types.ExecConfig{
Cmd: cmd,
})
if err != nil {
return
}
err = ct.cli.ContainerExecStart(c, res.ID, types.ExecStartCheck{
Detach: false,
Tty: false,
})
return
}