-
Notifications
You must be signed in to change notification settings - Fork 22
/
docker.go
221 lines (196 loc) · 5.59 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
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
package checks
import (
"os"
"regexp"
"strings"
"github.com/CiscoCloud/distributive/chkutil"
"github.com/CiscoCloud/distributive/dockerstatus"
"github.com/CiscoCloud/distributive/errutil"
"github.com/CiscoCloud/distributive/tabular"
log "github.com/Sirupsen/logrus"
"github.com/fsouza/go-dockerclient"
)
func init() {
chkutil.Register("DockerImage", func() chkutil.Check {
return &DockerImage{}
})
chkutil.Register("DockerImageRegexp", func() chkutil.Check {
return &DockerRunningRegexp{}
})
chkutil.Register("DockerRunning", func() chkutil.Check {
return &DockerRunning{}
})
chkutil.Register("DockerRunningRegexp", func() chkutil.Check {
return &DockerRunningRegexp{}
})
}
/*
#### DockerImage
Description: Is this Docker image present?
Parameters:
- Name (string): Name of the image
Example parameters:
- "user/image", "ubuntu"
*/
type DockerImage struct{ name string }
func (chk DockerImage) New(params []string) (chkutil.Check, error) {
if len(params) != 1 {
return chk, errutil.ParameterLengthError{1, params}
}
chk.name = params[0]
return chk, nil
}
func (chk DockerImage) Status() (int, string, error) {
images, err := dockerstatus.DockerImageRepositories()
if err != nil {
return 1, "", err
}
if tabular.StrIn(chk.name, images) {
return errutil.Success()
}
return errutil.GenericError("Docker image was not found", chk.name, images)
}
/*
#### DockerImageRegexp
Description: Works like DockerImage, but matches via a regexp, rather than a
string.
*/
type DockerImageRegexp struct{ re *regexp.Regexp }
func (chk DockerImageRegexp) ID() string { return "DockerImageRegexp" }
func (chk DockerImageRegexp) New(params []string) (chkutil.Check, error) {
if len(params) != 1 {
return chk, errutil.ParameterLengthError{1, params}
}
re, err := regexp.Compile(params[0])
if err != nil {
return chk, errutil.ParameterTypeError{params[0], "regexp"}
}
chk.re = re
return chk, nil
}
func (chk DockerImageRegexp) Status() (int, string, error) {
images, err := dockerstatus.DockerImageRepositories()
if err != nil {
return 1, "", err
}
if tabular.ReIn(chk.re, images) {
return errutil.Success()
}
msg := "Docker image was not found."
return errutil.GenericError(msg, chk.re.String(), images)
}
/*
#### DockerRunning
Description: Is this Docker container running?
Parameters:
- Name (string): Name of the container
Example parameters:
- "user/container", "user/container:latest"
*/
type DockerRunning struct{ name string }
func (chk DockerRunning) ID() string { return "DockerRunning" }
func (chk DockerRunning) New(params []string) (chkutil.Check, error) {
if len(params) != 1 {
return chk, errutil.ParameterLengthError{1, params}
}
chk.name = params[0]
return chk, nil
}
func (chk DockerRunning) Status() (int, string, error) {
running, err := dockerstatus.RunningContainers()
if err != nil {
return 1, "", err
}
if tabular.StrContainedIn(chk.name, running) {
return errutil.Success()
}
msg := "Docker container not runnning"
return errutil.GenericError(msg, chk.name, running)
}
// getRunningContainersAPI is like getRunningContainers, but uses an external
// library in order to access the Docker API
func getRunningContainersAPI(endpoint string) (containers []string) {
client, err := docker.NewClient(endpoint)
if err != nil {
log.WithFields(log.Fields{
"endpoint": endpoint,
"error": err.Error(),
}).Fatal("Couldn't create Docker API client")
}
ctrs, err := client.ListContainers(docker.ListContainersOptions{All: false})
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Fatal("Couldn't list Docker containers")
}
for _, ctr := range ctrs {
if strings.Contains(ctr.Status, "Up") {
containers = append(containers, ctr.Image)
}
}
return containers
}
/*
#### DockerRunningAPI
Description: Works like DockerRunning, but fetches information from the Docker
API endpoint instead.
Parameters:
- Path (filepath): Path to Docker socket
- Name (string): Name of the container
Example parameters:
- "/var/run/docker.sock", "/path/to/docker.sock"
- "user/container", "user/container:latest"
*/
type DockerRunningAPI struct{ path, name string }
func (chk DockerRunningAPI) New(params []string) (chkutil.Check, error) {
if len(params) != 2 {
return chk, errutil.ParameterLengthError{2, params}
}
path := params[0]
if _, err := os.Stat(path); err != nil {
return chk, errutil.ParameterTypeError{path, "filepath"}
}
chk.path = path
chk.name = params[1]
return chk, nil
}
func (chk DockerRunningAPI) Status() (int, string, error) {
running := getRunningContainersAPI(chk.path)
if tabular.StrContainedIn(chk.name, running) {
return errutil.Success()
}
msg := "Docker container not runnning"
return errutil.GenericError(msg, chk.name, running)
}
/*
#### DockerRunningRegexp
Description: Works like DockerRunning, but matches with a regexp instead of a
string.
Parameters:
- Regexp (regexp): Regexp to match names with
Example parameters:
- "user/.+", "user/[cC](o){2,3}[nta]tai\w{2}r"
*/
type DockerRunningRegexp struct{ re *regexp.Regexp }
func (chk DockerRunningRegexp) New(params []string) (chkutil.Check, error) {
if len(params) != 1 {
return chk, errutil.ParameterLengthError{1, params}
}
re, err := regexp.Compile(params[0])
if err != nil {
return chk, errutil.ParameterTypeError{params[0], "regexp"}
}
chk.re = re
return chk, nil
}
func (chk DockerRunningRegexp) Status() (int, string, error) {
running, err := dockerstatus.RunningContainers()
if err != nil {
return 1, "", err
}
if tabular.ReIn(chk.re, running) {
return errutil.Success()
}
msg := "Docker container not runnning"
return errutil.GenericError(msg, chk.re.String(), running)
}