forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_client.go
193 lines (177 loc) · 4.44 KB
/
docker_client.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
package container
import (
"bytes"
"fmt"
"github.com/fsouza/go-dockerclient"
)
const (
defaultShell = "sh"
)
type dockerClient struct {
// confusing
client *docker.Client
}
func newDockerClient(client *docker.Client) *dockerClient {
return &dockerClient{client}
}
func (c *dockerClient) Build(imageName string, contextDir string, options BuildOptions) error {
return c.client.BuildImage(
docker.BuildImageOptions{
Name: imageName,
Dockerfile: options.Dockerfile,
SuppressOutput: true,
OutputStream: options.OutputStream,
ContextDir: contextDir,
},
)
}
func (c *dockerClient) Pull(imageName string, options PullOptions) error {
repository, tag := docker.ParseRepositoryTag(imageName)
if tag == "" {
tag = "latest"
}
if options.NoPullIfLocal {
images, err := c.client.ListImages(
docker.ListImagesOptions{
All: true,
Digests: false,
},
)
if err != nil {
return err
}
repositoryTag := fmt.Sprintf("%s:%s", repository, tag)
for _, image := range images {
for _, foundRepositoryTag := range image.RepoTags {
if repositoryTag == foundRepositoryTag {
return nil
}
}
}
}
return c.client.PullImage(
docker.PullImageOptions{
Repository: repository,
Tag: tag,
OutputStream: options.OutputStream,
},
docker.AuthConfiguration{},
)
}
func (c *dockerClient) Create(imageName string, options CreateOptions) (_ string, retErr error) {
createContainerOptions, err := getDockerCreateContainerOptions(imageName, options)
if err != nil {
return "", err
}
var containerID string
defer func() {
if retErr != nil {
_ = c.Remove(containerID, RemoveOptions{})
}
}()
container, err := c.client.CreateContainer(createContainerOptions)
if err != nil {
return "", err
}
containerID = container.ID
return containerID, nil
}
func (c *dockerClient) Start(containerID string, options StartOptions) error {
container, err := c.client.InspectContainer(containerID)
if err != nil {
return err
}
if err := c.client.StartContainer(container.ID, container.HostConfig); err != nil {
return err
}
if options.Commands != nil && len(options.Commands) > 0 {
buffer := bytes.NewBuffer(nil)
for _, command := range options.Commands {
if _, err := buffer.WriteString(command + "\n"); err != nil {
return err
}
}
if err := c.client.AttachToContainer(
docker.AttachToContainerOptions{
Container: container.ID,
InputStream: buffer,
Stdin: true,
Stream: true,
},
); err != nil {
return err
}
}
return nil
}
func (c *dockerClient) Logs(containerID string, options LogsOptions) error {
return c.client.Logs(
docker.LogsOptions{
Container: containerID,
OutputStream: options.Stdout,
ErrorStream: options.Stderr,
Stdout: options.Stdout != nil,
Stderr: options.Stderr != nil,
},
)
}
func (c *dockerClient) Wait(containerID string, options WaitOptions) error {
exitCode, err := c.client.WaitContainer(containerID)
if err != nil {
return err
}
if exitCode != 0 {
return fmt.Errorf("container %s had exit code %d", containerID, exitCode)
}
return nil
}
func (c *dockerClient) Kill(containerID string, options KillOptions) error {
return c.client.KillContainer(
docker.KillContainerOptions{
ID: containerID,
},
)
}
func (c *dockerClient) Remove(containerID string, options RemoveOptions) error {
return c.client.RemoveContainer(
docker.RemoveContainerOptions{
ID: containerID,
Force: true,
},
)
}
func getDockerCreateContainerOptions(imageName string, options CreateOptions) (docker.CreateContainerOptions, error) {
config, err := getDockerConfig(imageName, options)
if err != nil {
return docker.CreateContainerOptions{}, err
}
hostConfig, err := getDockerHostConfig(options)
if err != nil {
return docker.CreateContainerOptions{}, err
}
return docker.CreateContainerOptions{
Config: config,
HostConfig: hostConfig,
}, nil
}
func getDockerConfig(imageName string, options CreateOptions) (*docker.Config, error) {
config := &docker.Config{
Image: imageName,
}
if options.HasCommand {
config.AttachStdin = true
config.OpenStdin = true
config.StdinOnce = true
if options.Shell != "" {
config.Entrypoint = []string{options.Shell}
} else {
config.Entrypoint = []string{defaultShell}
}
}
return config, nil
}
func getDockerHostConfig(options CreateOptions) (*docker.HostConfig, error) {
return &docker.HostConfig{
Binds: options.Binds,
}, nil
}