forked from cockroachdb/cockroach
/
docker.go
269 lines (244 loc) · 7.05 KB
/
docker.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
// Copyright 2015 The Cockroach Authors.
//
// 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.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
package cluster
import (
"encoding/binary"
"encoding/json"
"fmt"
"io"
"math"
"net"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/container"
"github.com/docker/go-connections/nat"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/util/log"
)
// Retrieve the IP address of docker itself.
func dockerIP() net.IP {
if host := os.Getenv("DOCKER_HOST"); host != "" {
u, err := url.Parse(host)
if err != nil {
panic(err)
}
h, _, err := net.SplitHostPort(u.Host)
if err != nil {
panic(err)
}
return net.ParseIP(h)
}
if runtime.GOOS == "linux" {
return net.IPv4zero
}
panic("unable to determine docker ip address")
}
// Container provides the programmatic interface for a single docker
// container.
type Container struct {
id string
name string
cluster *LocalCluster
}
// Name returns the container's name.
func (c Container) Name() string {
return c.name
}
func pullImage(l *LocalCluster, options types.ImagePullOptions) error {
log.Infof("ImagePull %s:%s starting", options.ImageID, options.Tag)
defer log.Infof("ImagePull %s:%s complete", options.ImageID, options.Tag)
rc, err := l.client.ImagePull(context.Background(), options, nil)
if err != nil {
return err
}
defer rc.Close()
dec := json.NewDecoder(rc)
for {
var message jsonmessage.JSONMessage
if err := dec.Decode(&message); err != nil {
if err == io.EOF {
return nil
}
return err
}
if log.V(2) {
log.Infof("ImagePull response: %s", message)
}
}
}
// createContainer creates a new container using the specified options. Per the
// docker API, the created container is not running and must be started
// explicitly.
func createContainer(l *LocalCluster, containerConfig container.Config, hostConfig container.HostConfig, containerName string) (*Container, error) {
resp, err := l.client.ContainerCreate(&containerConfig, &hostConfig, nil, containerName)
if err != nil {
return nil, err
}
return &Container{
id: resp.ID,
name: containerName,
cluster: l,
}, nil
}
func maybePanic(err error) {
if err != nil {
panic(err)
}
}
// Remove removes the container from docker. It is an error to remove a running
// container.
func (c *Container) Remove() error {
err := c.cluster.client.ContainerRemove(types.ContainerRemoveOptions{
ContainerID: c.id,
RemoveVolumes: true,
})
if os.Getenv("CIRCLECI") == "true" {
// HACK: Removal of docker containers on circleci reports the error:
// "Driver btrfs failed to remove root filesystem". So if we're running on
// circleci, just ignore the error, the containers are still removed.
return nil
}
return err
}
// Kill stops a running container, without removing it.
func (c *Container) Kill() error {
// Paused containers cannot be killed. Attempt to unpause it first
// (which might fail) before killing.
_ = c.Unpause()
if err := c.cluster.client.ContainerKill(c.id, "9"); err != nil && !strings.Contains(err.Error(), "is not running") {
return err
}
c.cluster.expectEvent(c, eventDie)
return nil
}
// Start starts a non-running container.
//
// TODO(pmattis): Generalize the setting of parameters here.
func (c *Container) Start() error {
return c.cluster.client.ContainerStart(c.id)
}
// Pause pauses a running container.
func (c *Container) Pause() error {
return c.cluster.client.ContainerPause(c.id)
}
// Unpause resumes a paused container.
func (c *Container) Unpause() error {
return c.cluster.client.ContainerUnpause(c.id)
}
// Restart restarts a running container.
// Container will be killed after 'timeout' seconds if it fails to stop.
func (c *Container) Restart(timeoutSeconds int) error {
var exp []string
if ci, err := c.Inspect(); err != nil {
return err
} else if ci.State.Running {
exp = append(exp, eventDie)
}
if err := c.cluster.client.ContainerRestart(c.id, timeoutSeconds); err != nil {
return err
}
c.cluster.expectEvent(c, append(exp, eventRestart)...)
return nil
}
// Stop a running container.
func (c *Container) Stop(timeoutSeconds int) error {
if err := c.cluster.client.ContainerStop(c.id, timeoutSeconds); err != nil {
return err
}
c.cluster.expectEvent(c, eventDie)
return nil
}
// Wait waits for a running container to exit.
func (c *Container) Wait() error {
exitCode, err := c.cluster.client.ContainerWait(context.Background(), c.id)
if err != nil {
return err
}
if exitCode != 0 {
if err := c.Logs(os.Stderr); err != nil {
log.Warning(err)
}
return fmt.Errorf("non-zero exit code: %d", exitCode)
}
return nil
}
// Logs outputs the containers logs to the given io.Writer.
func (c *Container) Logs(w io.Writer) error {
rc, err := c.cluster.client.ContainerLogs(context.Background(), types.ContainerLogsOptions{
ContainerID: c.id,
ShowStdout: true,
ShowStderr: true,
})
if err != nil {
return err
}
defer rc.Close()
// The docker log output is not quite plaintext: each line has a
// prefix consisting of one byte file descriptor (stdout vs stderr),
// three bytes padding, four byte length. We could use this to
// disentangle stdout and stderr if we wanted to output them into
// separate streams, but we don't really care.
for {
var header uint64
if err := binary.Read(rc, binary.BigEndian, &header); err == io.EOF {
break
} else if err != nil {
return err
}
size := header & math.MaxUint32
if _, err := io.CopyN(w, rc, int64(size)); err != nil {
return err
}
}
return nil
}
// Inspect retrieves detailed info about a container.
func (c *Container) Inspect() (types.ContainerJSON, error) {
return c.cluster.client.ContainerInspect(c.id)
}
// Addr returns the address to connect to the specified port.
func (c *Container) Addr(name nat.Port) *net.TCPAddr {
containerInfo, err := c.Inspect()
if err != nil {
return nil
}
if name == "" {
name = cockroachTCP
}
bindings, ok := containerInfo.NetworkSettings.Ports[name]
if !ok || len(bindings) == 0 {
return nil
}
port, _ := strconv.Atoi(bindings[0].HostPort)
return &net.TCPAddr{
IP: dockerIP(),
Port: port,
}
}
// PGAddr returns the address to connect to the Postgres port.
func (c *Container) PGAddr() *net.TCPAddr {
return c.Addr(pgTCP)
}
// GetJSON retrieves the URL specified by https://Addr(<port>)<path>
// and unmarshals the result as JSON.
func (c *Container) GetJSON(port nat.Port, path string, v interface{}) error {
return getJSON(true /* tls */, c.Addr(port).String(), path, v)
}