forked from ethereum/hive
-
Notifications
You must be signed in to change notification settings - Fork 4
/
containers.go
365 lines (347 loc) · 11.9 KB
/
containers.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
// This file contains the utility methods for managing docker containers.
package main
import (
"archive/tar"
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/fsouza/go-dockerclient"
"gopkg.in/inconshreveable/log15.v2"
)
// hiveEnvvarPrefix is the prefix of the environment variables names that should
// be moved from test images to client container to fine tune their setup.
const hiveEnvvarPrefix = "HIVE_"
// hiveLogsFolder is the directory in which to place runtime logs from each of
// the docker containers.
var hiveLogsFolder = filepath.Join("workspace", "logs")
// createShellContainer creates a docker container from the hive shell's image.
func createShellContainer(daemon *docker.Client, image string, overrides []string) (*docker.Container, error) {
// Configure any workspace requirements for the container
pwd, err := os.Getwd()
if err != nil {
return nil, err
}
for _, dir := range []string{"docker", "ethash", "logs"} {
if err := os.MkdirAll(filepath.Join(pwd, "workspace", dir), os.ModePerm); err != nil {
return nil, err
}
}
// Create the list of bind points to make host files available internally
binds := make([]string, 0, len(overrides)+2)
for _, override := range overrides {
file := override
if strings.Contains(override, ":") {
file = override[strings.LastIndex(override, ":")+1:]
}
if path, err := filepath.Abs(file); err == nil {
binds = append(binds, fmt.Sprintf("%s:%s:ro", path, path)) // Mount to the same place, read only
}
}
binds = append(binds, []string{
fmt.Sprintf("%s/workspace/docker:/var/lib/docker", pwd), // Surface any docker-in-docker data caches
fmt.Sprintf("%s/workspace/ethash:/gopath/src/github.com/karalabe/hive/workspace/ethash", pwd), // Surface any generated DAGs from the shell
fmt.Sprintf("%s/workspace/logs:/gopath/src/github.com/karalabe/hive/workspace/logs", pwd), // Surface all the log files from the shell
}...)
// Create and return the actual docker container
return daemon.CreateContainer(docker.CreateContainerOptions{
Config: &docker.Config{
Image: image,
Env: []string{fmt.Sprintf("UID=%d", os.Getuid())}, // Forward the user ID for the workspace permissions
Cmd: os.Args[1:],
},
HostConfig: &docker.HostConfig{
Privileged: true, // Docker in docker requires privileged mode
Binds: binds,
},
})
}
// createEthashContainer creates a docker container to generate ethash DAGs.
func createEthashContainer(daemon *docker.Client, image string) (*docker.Container, error) {
// Configure the workspace for ethash generation
pwd, err := os.Getwd()
if err != nil {
return nil, err
}
ethash := filepath.Join(pwd, "workspace", "ethash")
if err := os.MkdirAll(ethash, os.ModePerm); err != nil {
return nil, err
}
// Create and return the actual docker container
return daemon.CreateContainer(docker.CreateContainerOptions{
Config: &docker.Config{
Image: image,
Env: []string{fmt.Sprintf("UID=%d", os.Getuid())}, // Forward the user ID for the workspace permissions
},
HostConfig: &docker.HostConfig{
Binds: []string{fmt.Sprintf("%s:/root/.ethash", ethash)},
},
})
}
// createClientContainer creates a docker container from a client image and moves
// any hive environment variables and initial chain configuration files from the
// tester image into the new client. Dynamic chain configs may also be pulled from
// a live container.
//
// A batch of environment variables may be specified to override from originating
// from the tester image. This is useful in particular during simulations where
// the tester itself can fine tune parameters for individual nodes.
//
// Also a batch of files may be specified to override either the chain configs or
// the client binaries. This is useful in particular during client development as
// local executables may be injected into a client docker container without them
// needing to be rebuilt inside hive.
func createClientContainer(daemon *docker.Client, client string, tester string, live *docker.Container, overrideFiles []string, overrideEnvs map[string]string) (*docker.Container, error) {
// Configure the client for ethash consumption
pwd, err := os.Getwd()
if err != nil {
return nil, err
}
ethash := filepath.Join(pwd, "workspace", "ethash")
// Gather all the hive environment variables from the tester
ti, err := daemon.InspectImage(tester)
if err != nil {
return nil, err
}
vars := []string{}
for _, envvar := range ti.Config.Env {
if strings.HasPrefix(envvar, hiveEnvvarPrefix) && overrideEnvs[envvar] == "" {
vars = append(vars, envvar)
}
}
// Inject any explicit envvar overrides
for key, val := range overrideEnvs {
if strings.HasPrefix(key, hiveEnvvarPrefix) {
vars = append(vars, key+"="+val)
}
}
// Create the client container with tester envvars injected
c, err := daemon.CreateContainer(docker.CreateContainerOptions{
Config: &docker.Config{
Image: client,
Env: vars,
},
HostConfig: &docker.HostConfig{
Binds: []string{fmt.Sprintf("%s:/root/.ethash", ethash)},
},
})
if err != nil {
return nil, err
}
// Inject all the chain configuration files from the tester (or live container) into the client
t, err := daemon.CreateContainer(docker.CreateContainerOptions{Config: &docker.Config{Image: tester}})
if err != nil {
return nil, err
}
defer daemon.RemoveContainer(docker.RemoveContainerOptions{ID: t.ID, Force: true})
if path := overrideEnvs["HIVE_INIT_GENESIS"]; path != "" {
err = copyBetweenContainers(daemon, c.ID, live.ID, path, "/genesis.json", false)
} else {
err = copyBetweenContainers(daemon, c.ID, t.ID, "", "/genesis.json", false)
}
if err != nil {
return nil, err
}
if path := overrideEnvs["HIVE_INIT_CHAIN"]; path != "" {
err = copyBetweenContainers(daemon, c.ID, live.ID, path, "/chain.rlp", true)
} else {
err = copyBetweenContainers(daemon, c.ID, t.ID, "", "/chain.rlp", true)
}
if err != nil {
return nil, err
}
if path := overrideEnvs["HIVE_INIT_BLOCKS"]; path != "" {
err = copyBetweenContainers(daemon, c.ID, live.ID, path, "/blocks", true)
} else {
err = copyBetweenContainers(daemon, c.ID, t.ID, "", "/blocks", true)
}
if err != nil {
return nil, err
}
if path := overrideEnvs["HIVE_INIT_KEYS"]; path != "" {
err = copyBetweenContainers(daemon, c.ID, live.ID, path, "/keys", true)
} else {
err = copyBetweenContainers(daemon, c.ID, t.ID, "", "/keys", true)
}
if err != nil {
return nil, err
}
// Inject any explicit file overrides into the client container
overrides := make([]string, 0, len(overrideFiles))
for _, override := range overrideFiles {
// Split the override into a pattern/path combo
pattern, file := ".", override
if strings.Contains(override, ":") {
pattern = override[:strings.LastIndex(override, ":")]
file = override[strings.LastIndex(override, ":")+1:]
}
// If the pattern matches the client image, override the file
re, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}
if re.MatchString(client) {
overrides = append(overrides, file)
}
}
if err := uploadToContainer(daemon, c.ID, overrides); err != nil {
daemon.RemoveContainer(docker.RemoveContainerOptions{ID: c.ID, Force: true})
return nil, err
}
return c, nil
}
// uploadToContainer injects a batch of files into the target container.
func uploadToContainer(daemon *docker.Client, id string, files []string) error {
// Short circuit if there are no files to upload
if len(files) == 0 {
return nil
}
// Create a tarball archive with all the data files
tarball := new(bytes.Buffer)
tw := tar.NewWriter(tarball)
for _, path := range files {
// Fetch the next file to inject into the container
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return err
}
info, err := file.Stat()
if err != nil {
return err
}
// Insert the file into the tarball archive
header := &tar.Header{
Name: filepath.Base(file.Name()),
Mode: int64(info.Mode()),
Size: int64(len(data)),
}
if err := tw.WriteHeader(header); err != nil {
return err
}
if _, err := tw.Write(data); err != nil {
return err
}
}
if err := tw.Close(); err != nil {
return err
}
// Upload the tarball into the destination container
return daemon.UploadToContainer(id, docker.UploadToContainerOptions{
InputStream: tarball,
Path: "/",
})
}
// copyBetweenContainers copies a file from one docker container to another one.
func copyBetweenContainers(daemon *docker.Client, dest, src string, path, target string, optional bool) error {
// If no path was specified, use the target as the default
if path == "" {
path = target
}
// Download a tarball of the file from the source container
download, upload := new(bytes.Buffer), new(bytes.Buffer)
if err := daemon.DownloadFromContainer(src, docker.DownloadFromContainerOptions{
Path: path,
OutputStream: download,
}); err != nil {
// Check whether we're missing an optional file only
if err.(*docker.Error).Status == 404 && optional {
return nil
}
return err
}
// Rewrite all the paths in the tarball to the default ones
in, out := tar.NewReader(download), tar.NewWriter(upload)
for {
// Fetch the next file header from the download archive
header, err := in.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
// Rewrite the path and push into the upload archive
header.Name = strings.Replace(header.Name, path[1:], target[1:], -1)
if err := out.WriteHeader(header); err != nil {
return err
}
// Copy the file content over from the download to the upload archive
if _, err := io.Copy(out, in); err != nil {
return err
}
}
// Upload the tarball into the destination container
if err := daemon.UploadToContainer(dest, docker.UploadToContainerOptions{
InputStream: upload,
Path: "/",
}); err != nil {
return err
}
return nil
}
// runContainer attaches to the output streams of an existing container, then
// starts executing the container and returns the CloseWaiter to allow the caller
// to wait for termination.
func runContainer(daemon *docker.Client, id string, logger log15.Logger, logfile string, shell bool) (docker.CloseWaiter, error) {
// If we're the outer shell, log straight to stderr, nothing fancy
stdout := io.Writer(os.Stdout)
stream := io.Writer(os.Stderr)
if !shell {
// For non shell containers, create and open the log file for the output
if err := os.MkdirAll(filepath.Dir(logfile), os.ModePerm); err != nil {
return nil, err
}
log, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_TRUNC, os.ModePerm)
if err != nil {
return nil, err
}
stream = io.Writer(log)
// If console logging was requested, tee the output and tag it with the container id
if *loglevelFlag > 5 {
// Hook into the containers output stream and tee it out
hookedR, hookedW := io.Pipe()
stream = io.MultiWriter(log, hookedW)
// Tag all log messages with the container ID if not the outer shell
copy := func(dst io.Writer, src io.Reader) (int64, error) {
scanner := bufio.NewScanner(src)
for scanner.Scan() {
dst.Write([]byte(fmt.Sprintf("[%s] %s\n", id[:8], scanner.Text())))
}
return 0, nil
}
go copy(os.Stderr, hookedR)
}
// Only the shell gets to keep its standard output
stdout = stream
}
logger.Debug("attaching to container")
waiter, err := daemon.AttachToContainerNonBlocking(docker.AttachToContainerOptions{
Container: id,
OutputStream: stdout,
ErrorStream: stream,
Stream: true,
Stdout: true,
Stderr: true,
})
if err != nil {
logger.Error("failed to attach to container", "error", err)
return nil, err
}
// Start the requested container and wait until it terminates
logger.Debug("starting container")
if err := daemon.StartContainer(id, nil); err != nil {
logger.Error("failed to start container", "error", err)
return nil, err
}
return waiter, nil
}