-
Notifications
You must be signed in to change notification settings - Fork 4
/
docker.go
176 lines (156 loc) · 5.04 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
package executor
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
// buildImage executes the 'docker build' for a image, considering the
// parameters defined for it and returns a CmdResult and an error, if any.
func buildImage(id, baseDir, stageDir string, env map[string]string) (CmdResult, error) {
dir := filepath.Join(baseDir, stageDir)
var b strings.Builder
for k, v := range env {
fmt.Fprintf(&b, "--build-arg %s=%s ", k, fmt.Sprintf(`"%s"`, v))
}
envStr := b.String()
cmdStr := fmt.Sprintf("docker build %s-t %s .", envStr, id)
// sh -c is a workaround that allow us to have double quotes around environment variable values.
// Those are needed when the environment variables have whitespaces, for instance a NAME, like in
// TREPB.
cmd := exec.Command("bash", "-c", cmdStr)
cmd.Dir = dir
var outb, errb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = &errb
log.Printf("$ %s", cmdStr)
err := cmd.Run()
switch err.(type) {
case *exec.Error:
cmdResultError := CmdResult{
ExitStatus: statusCode(err),
Cmd: cmdStr,
}
return cmdResultError, fmt.Errorf("command was not executed correctly: %s", err)
}
cmdResult := CmdResult{
Stdout: outb.String(),
Stderr: errb.String(),
Cmd: cmdStr,
CmdDir: dir,
ExitStatus: statusCode(err),
Env: os.Environ(),
}
return cmdResult, err
}
// runImage executes the 'docker run' for a image, considering the
// parameters defined for it and returns a CmdResult and an error, if any.
// It uses the stdout from the previous stage as the stdin for this new command.
// Associates a volume to the running docker image if volumeName and volumeDir are not empty strings.
func runImage(id, baseDir, stageDir, volumeName, volumeDir, stdin string, env map[string]string) (CmdResult, error) {
dir := filepath.Join(baseDir, stageDir)
var builder strings.Builder
for key, value := range env {
fmt.Fprintf(&builder, "--env %s=%s ", key, fmt.Sprintf(`"%s"`, value))
}
envStr := strings.TrimRight(builder.String(), " ")
volumeStr := ""
if volumeName != "" && volumeDir != "" {
volumeStr = fmt.Sprintf("-v %s:%s", volumeName, volumeDir)
}
cmdStr := fmt.Sprintf("docker run -i %s --rm %s %s", volumeStr, envStr, id)
// sh -c is a workaround that allow us to have double quotes around environment variable values.
// Those are needed when the environment variables have whitespaces, for instance a NAME, like in
// TREPB.
cmd := exec.Command("bash", "-c", cmdStr)
cmd.Dir = dir
cmd.Stdin = strings.NewReader(stdin)
var outb, errb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = &errb
log.Printf("$ %s", cmdStr)
err := cmd.Run()
switch err.(type) {
case *exec.Error:
cmdResultError := CmdResult{
ExitStatus: statusCode(err),
Cmd: cmdStr,
}
return cmdResultError, fmt.Errorf("command was not executed correctly: %s", err)
}
cmdResult := CmdResult{
Stdin: stdin,
Stdout: outb.String(),
Stderr: errb.String(),
Cmd: cmdStr,
CmdDir: cmd.Dir,
ExitStatus: statusCode(err),
Env: os.Environ(),
}
return cmdResult, err
}
func pullImage(id, baseDir, stageDir string) (CmdResult, error) {
dir := filepath.Join(baseDir, stageDir)
cmdStr := fmt.Sprintf("docker pull %s", id)
// sh -c is a workaround that allow us to have double quotes around environment variable values.
// Those are needed when the environment variables have whitespaces, for instance a NAME, like in
// TREPB.
cmd := exec.Command("bash", "-c", cmdStr)
cmd.Dir = dir
var outb, errb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = &errb
log.Printf("$ %s", cmdStr)
err := cmd.Run()
switch err.(type) {
case *exec.Error:
cmdResultError := CmdResult{
ExitStatus: statusCode(err),
Cmd: cmdStr,
}
return cmdResultError, fmt.Errorf("command was not executed correctly: %s", err)
}
cmdResult := CmdResult{
Stdout: outb.String(),
Stderr: errb.String(),
Cmd: cmdStr,
CmdDir: dir,
ExitStatus: statusCode(err),
Env: os.Environ(),
}
return cmdResult, err
}
func createVolume(dir, name string) error {
baseDir, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting working directory:%v", err)
}
cmdList := strings.Split(fmt.Sprintf("docker volume create --driver local --opt type=none --opt device=%s --opt o=bind --name=%s", dir, name), " ")
cmd := exec.Command(cmdList[0], cmdList[1:]...)
cmd.Dir = baseDir
var outb, errb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = &errb
log.Printf("$ %s", strings.Join(cmdList, " "))
switch cmd.Run().(type) {
case *exec.Error:
r := CmdResult{
ExitStatus: statusCode(err),
Cmd: strings.Join(cmdList, " "),
}
return fmt.Errorf("command was not executed correctly: %+v", r)
}
return nil
}
func removeVolume(volume string) error {
cmdList := strings.Split(fmt.Sprintf("docker volume rm -f %s", volume), " ")
cmd := exec.Command(cmdList[0], cmdList[1:]...)
log.Printf("$ %s", strings.Join(cmdList, " "))
if err := cmd.Run(); err != nil {
return fmt.Errorf("error removing existing volume %s: %q", volume, err)
}
return nil
}