-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (151 loc) · 4.19 KB
/
main.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
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/signal"
"strings"
"syscall"
docker "github.com/fsouza/go-dockerclient"
"github.com/kballard/go-shellquote"
"github.com/spf13/pflag"
"github.com/pkg/errors"
)
var imageName, containerName, containerArgs string
var containerStopTimeout int
var containerBinds []string
func main() {
os.Exit(runOperator())
}
func runOperator() int {
// parse arguments
pflag.StringVar(&imageName, "image", "docker:18.09-dind", "the image to use for DinD")
pflag.StringVar(&containerName, "name", "swarm-dind-operator", "the name to give to the DinD container")
pflag.StringVar(&containerArgs, "args", "", "the arguments to give to the DinD container")
pflag.IntVar(&containerStopTimeout, "stop-timeout", 10, "the timeout to wait when the container is stopped in seconds")
pflag.StringArrayVar(&containerBinds, "binds", []string{}, "the directories to bind in the container")
pflag.Parse()
filteredBinds := make([]string, 0, len(containerBinds))
for _, bind := range containerBinds {
if bind != "" {
filteredBinds = append(filteredBinds, bind)
}
}
args, err := shellquote.Split(containerArgs)
if err != nil {
fmt.Println(errors.Wrap(err, "invalid arguments given"))
return 42
}
data, err := ioutil.ReadFile("/proc/self/cpuset")
if err != nil {
fmt.Println(errors.Wrap(err, "cannot read cgroup file"))
return 42
}
parts := strings.Split(string(data), "/")
selfID := strings.TrimSpace(parts[len(parts)-1])
client, err := docker.NewClientFromEnv()
if err != nil {
fmt.Println(errors.Wrap(err, "cannot create client"))
return 42
}
containers, err := client.ListContainers(docker.ListContainersOptions{
All: true,
Filters: map[string][]string{
"name": []string{containerName},
},
})
if err != nil {
fmt.Println(errors.Wrap(err, "cannot list containers"))
return 42
}
for _, container := range containers {
if err := client.RemoveContainer(docker.RemoveContainerOptions{
Force: true,
ID: container.ID,
}); err != nil {
fmt.Println(errors.Wrap(err, "cannot remove container"))
return 42
}
}
if _, err := client.InspectImage(imageName); err != nil {
if err != docker.ErrNoSuchImage {
fmt.Println(errors.Wrap(err, "cannot inspect image"))
return 42
}
if err := client.PullImage(docker.PullImageOptions{
Repository: imageName,
}, docker.AuthConfiguration{}); err != nil {
fmt.Println(errors.Wrap(err, "cannot pull image"))
return 42
}
}
container, err := client.CreateContainer(docker.CreateContainerOptions{
Name: containerName,
Config: &docker.Config{
Cmd: args,
Image: imageName,
Labels: map[string]string{
"com.sourcelair.swarm-dind-operator": "true",
"com.sourcelair.swarm-dind-operator.name": containerName,
},
StopTimeout: containerStopTimeout,
},
HostConfig: &docker.HostConfig{
Privileged: true,
Binds: filteredBinds,
NetworkMode: fmt.Sprintf("container:%s", selfID),
PidMode: fmt.Sprintf("container:%s", selfID),
},
})
if err != nil {
fmt.Println(errors.Wrap(err, "cannot create container"))
return 42
}
if err := client.StartContainer(container.ID, nil); err != nil {
fmt.Println(errors.Wrap(err, "cannot start container"))
return 42
}
ctx, done := context.WithCancel(context.Background())
defer done()
// forward container logs
go func() {
client.Logs(docker.LogsOptions{
Container: container.ID,
Stdout: true,
Stderr: true,
OutputStream: os.Stdout,
ErrorStream: os.Stderr,
Context: ctx,
Follow: true,
})
}()
defer client.RemoveContainer(docker.RemoveContainerOptions{
Force: true,
ID: container.ID,
})
// wait for container to exit
containerExited := make(chan int, 1)
go func() {
exitCode, _ := client.WaitContainer(container.ID)
containerExited <- exitCode
}()
// catch signals
sigs := make(chan os.Signal, 1)
signal.Notify(sigs)
exitCode := 0
for {
select {
// propagate signals
case sig := <-sigs:
client.KillContainer(docker.KillContainerOptions{
ID: container.ID,
Context: ctx,
Signal: docker.Signal(sig.(syscall.Signal)),
})
// exit with same exit code
case exitCode = <-containerExited:
return exitCode
}
}
}