-
Notifications
You must be signed in to change notification settings - Fork 2
/
container.go
195 lines (166 loc) · 5.24 KB
/
container.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
/*
docker-systemd-shim - Shim to allow easy container control via systemd
Copyright (C) 2018 - 2019, Marc Hoersken <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
)
func createClient() *client.Client {
cli, err := client.NewEnvClient()
if err != nil {
log.Panicln(logError, err)
}
return cli
}
func checkContainer(ctx context.Context, cli *client.Client, response types.ContainerJSON,
usePID bool, useCGroup bool) bool {
log.Println(logNotice, "Checking container ...")
if !usePID {
log.Println(logInfo, "Skipped check for PID existence.")
return true
}
log.Println(logNotice, "Checking for PID existence ...")
if !checkProcess(response.State.Pid) {
log.Println(logNotice, "Failed to check for PID existence.")
return false
}
if !useCGroup {
log.Println(logNotice, "Successfully checked for PID existence, but skipped CGroup.")
return true
}
log.Println(logNotice, "Checking for PID existence in CGroup ...")
cgroup := fmt.Sprintf(dockerCGroupFormat, response.ID)
if !checkCGroup(response.State.Pid, cgroup) {
log.Println(logNotice, "Failed to check for PID existence in CGroup.")
return false
}
log.Println(logNotice, "Successfully checked for PID existence in CGroup.")
return true
}
func startContainer(ctx context.Context, cli *client.Client, containerName string) bool {
startOptions := types.ContainerStartOptions{
CheckpointID: "",
CheckpointDir: "",
}
log.Println(logNotice, "Starting container ...")
err := cli.ContainerStart(ctx, containerName, startOptions)
if err != nil {
log.Println(logError, err)
return false
}
log.Println(logNotice, "Successfully started container.")
return true
}
func runContainer(ctx context.Context, cli *client.Client, containerName string,
startTries int, checkTries int, usePID bool, useCGroup bool, notifySD bool) (string, int) {
var containerID = ""
var containerPID = 0
started:
for {
log.Println(logNotice, "Inspecting container ...")
response, err := cli.ContainerInspect(ctx, containerName)
if err != nil {
log.Panicln(logError, err)
} else {
log.Println(logInfo, "Container ID:", response.ID)
log.Println(logInfo, "Container Name:", response.Name)
log.Println(logInfo, "Container Status:", response.State.Status)
log.Println(logInfo, "Container PID:", response.State.Pid)
}
if notifySD {
var containerStatus = response.State.Status
if response.State.Health != nil {
containerStatus += " [" + response.State.Health.Status + "]"
}
log.Println(logNotice, "Reporting status to systemd ...")
res, err := notifyStatus(containerStatus)
if err != nil {
log.Println(logError, err)
} else if res {
log.Println(logNotice, "Reported status to systemd:", containerStatus)
} else {
log.Println(logError, "Reporting status to systemd is not supported.")
}
}
if response.State.Status == "running" {
log.Println(logNotice, "Container is running.")
checkTries = checkTries - 1
if checkContainer(ctx, cli, response, usePID, useCGroup) {
containerID = response.ID
containerPID = response.State.Pid
break started
} else if checkTries == 0 {
break started
} else {
continue started
}
} else if startTries == 0 {
log.Println(logError, "Could not start container!")
break started
} else {
startTries = startTries - 1
startContainer(ctx, cli, response.ID)
}
}
return containerID, containerPID
}
func watchContainer(ctx context.Context, cli *client.Client, containerName string) <-chan bool {
stopped := make(chan bool)
go func() {
waits, errs := cli.ContainerWait(ctx, containerName, container.WaitConditionNotRunning)
waited:
for {
select {
case <-ctx.Done():
// returning not to leak the goroutine
break waited
case err := <-errs:
if err != nil {
log.Println(logError, err)
stopped <- false
}
break waited
case w := <-waits:
if w.Error != nil {
log.Println(logError, w.Error)
stopped <- false
} else {
log.Println(logInfo, "Container exit code:", w.StatusCode)
log.Println(logNotice, "Container has stopped.")
stopped <- true
}
break waited
}
}
close(stopped)
}()
return stopped
}
func stopContainer(ctx context.Context, cli *client.Client, containerName string,
stopTimeout *time.Duration) bool {
log.Println(logNotice, "Stopping container ...")
err := cli.ContainerStop(ctx, containerName, stopTimeout)
if err != nil {
log.Println(logError, err)
return false
}
log.Println(logNotice, "Successfully stopped container.")
return true
}