/
docker.go
155 lines (134 loc) · 3.47 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
package main
import (
"bytes"
"fmt"
log "github.com/Sirupsen/logrus"
dockerclient "github.com/fsouza/go-dockerclient"
)
func newDocker(opt *containerOption, logger *log.Entry) *docker {
return &docker{opt: opt, client: client(), logger: logger}
}
type docker struct {
opt *containerOption
client *dockerclient.Client
logger *log.Entry
}
type containerOption struct {
image string
env []string
cmd []string
volumes []string
workingDir string
}
func (c containerOption) toLog() map[string]interface{} {
return map[string]interface{}{
"image": c.image,
"env": c.env,
"cmd": c.cmd,
"volumes": c.volumes,
"workingDir": c.workingDir,
}
}
func (d docker) run() (int, error) {
d.logger.Info("creating container...")
con, err := d.createContainer()
if err != nil {
d.logger.Warnf("creating container error: %v", err)
return 0, err
}
log := d.logger.WithField("container", con.ID)
log.Info("starting container...")
err = d.startContainer(con.ID)
if err != nil {
log.Warnf("starting container error: %v", err)
return 0, err
}
log.Info("waiting container...")
status, err := d.waitContainer(con.ID)
if err != nil {
log.Warnf("waiting container error: %v", err)
return 0, err
}
err = d.loggingContainerLog(con.ID)
if err != nil {
log.Warnf("logging container error: %v", err)
return 0, err
}
log.Info("removing container...")
err = d.removeContainer(con.ID)
if err != nil {
log.Warnf("removing container error: %v", err)
return 0, err
}
return status, nil
}
func (d docker) createContainer() (*dockerclient.Container, error) {
volumes := map[string]struct{}{}
for _, v := range d.opt.volumes {
volumes[v] = struct{}{}
}
return d.client.CreateContainer(dockerclient.CreateContainerOptions{
Config: &dockerclient.Config{
Image: d.opt.image,
Env: d.opt.env,
Cmd: d.opt.cmd,
Volumes: volumes,
WorkingDir: d.opt.workingDir,
},
HostConfig: &dockerclient.HostConfig{
Binds: d.opt.volumes,
},
})
}
func (d docker) startContainer(id string) error {
return d.client.StartContainer(id, nil)
}
func (d docker) waitContainer(id string) (int, error) {
return d.client.WaitContainer(id)
}
func (d docker) removeContainer(id string) error {
return d.client.RemoveContainer(
dockerclient.RemoveContainerOptions{ID: id},
)
}
func (d docker) inspectContainer(id string) (*dockerclient.Container, error) {
return d.client.InspectContainer(id)
}
func (d docker) loggingContainerLog(id string) error {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
err := d.client.Logs(dockerclient.LogsOptions{
Container: id,
OutputStream: stdout,
ErrorStream: stderr,
Stdout: true,
Stderr: true,
Follow: false,
})
if err != nil {
return err
}
d.logger.WithFields(log.Fields{
"container": id,
"command": "/containers/(id)/logs",
"args": []string{"GET", fmt.Sprintf("/containers/%s/logs", id)},
"stdout": stdout.String(),
"stderr": stderr.String(),
}).Info("logging container...")
return nil
}
func client() *dockerclient.Client {
if dockerCertPath != "" {
return tlsClient()
} else {
client, _ := dockerclient.NewClient(dockerHost)
return client
}
}
func tlsClient() *dockerclient.Client {
ca := fmt.Sprintf("%s/ca.pem", dockerCertPath)
cert := fmt.Sprintf("%s/cert.pem", dockerCertPath)
key := fmt.Sprintf("%s/key.pem", dockerCertPath)
client, _ := dockerclient.NewTLSClient(dockerHost, cert, key, ca)
return client
}