/
undertaker.go
350 lines (284 loc) · 8.42 KB
/
undertaker.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package main // import "github.com/daolabs/undertaker"
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"github.com/fsouza/go-dockerclient"
"log"
"os"
"regexp"
"strings"
// "time"
)
// MultiStringOption is a commandline string option which can be specified
// multiple times and will be parsed by flag.Var() / flag.Parse() into an
// array.
type MultiStringOption []string
// implementation of flag.Value interface (part 1)
func (i *MultiStringOption) String() string {
return fmt.Sprintf("%s", *i);
}
// implementation of flag.Value interface (part 2)
func (i *MultiStringOption) Set(value string) error {
*i = append(*i, value)
return nil
}
type RegexpHolder struct {
re *regexp.Regexp
RegExp string
}
type UndertakerData struct {
ContainerExc *[]RegexpHolder
ContainerInc *[]RegexpHolder
ImageExc *[]RegexpHolder
ImageInc *[]RegexpHolder
client *docker.Client
ContainersAll []docker.APIContainers
ContainersExited []docker.APIContainers;
InuseImageIds []string
}
func NewUndertakerData() *UndertakerData {
return &UndertakerData{
new([]RegexpHolder),
new([]RegexpHolder),
new([]RegexpHolder),
new([]RegexpHolder),
nil,
make([]docker.APIContainers,0),
make([]docker.APIContainers,0),
make([]string,0)};
}
func (u UndertakerData) String() string {
b, err := json.MarshalIndent(u,""," ")
if err != nil {
return fmt.Sprintln(err)
}
return fmt.Sprintln(string(b))
}
func generateRegexp(input *MultiStringOption) (*[]RegexpHolder) {
var resptr = new([]RegexpHolder);
for _,text := range *input {
if pattern, err := regexp.Compile(text); err != nil {
log.Fatal("invalid pattern: [", text, " ] - ", err)
} else {
*resptr = append(*resptr, RegexpHolder{pattern,text})
}
}
return resptr;
}
func containsString(slice []string, val string) bool {
for _, a := range slice {
if a == val {
return true
}
}
return false
}
// loads exclude file removing comments and empty lines
func loadFile(filename string) (*MultiStringOption, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var res MultiStringOption;
re_comment_prefix := regexp.MustCompile("^#.*")
re_comment_postfix := regexp.MustCompile("#.*$")
scanner := bufio.NewScanner(file)
text := ""
for scanner.Scan() {
// remove leading and trailing spaces
text = strings.TrimSpace(scanner.Text())
// remove lines starting with comment char (lines starting with #)
if text = re_comment_prefix.ReplaceAllString(text, ""); len(text) == 0 {
continue
}
// remove postfix comments (comments at the end of a line)
if text = re_comment_postfix.ReplaceAllString(text, ""); len(text) == 0 {
continue
}
// trim spaces again
if text = strings.TrimSpace(text); len(text) == 0 {
continue
}
res.Set(text)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return &res, nil
}
func filterContainers(client *docker.Client, containers []docker.APIContainers, patterns []*regexp.Regexp,inuse_ids *[]string) []docker.APIContainers {
var res []docker.APIContainers
var match_found bool
for _, container := range containers {
match_found = false
// loop through pattens and check for match on ID or Names[]
for _, pattern := range patterns {
if len(pattern.FindString(container.ID)) > 0 {
match_found = true
break
}
fmt.Println("names=",container.Names);
for _, name := range container.Names {
// NOTE: the name starts with a slash (don't match against it)!
if len(pattern.FindString(name[1:])) > 0 {
match_found = true
break
}
}
if match_found {
break
}
}
if !match_found {
res = append(res, container)
} else {
tmp, _ := client.InspectContainer(container.ID)
*inuse_ids = append(*inuse_ids,tmp.Image);
//fmt.Printf("image excluded: %s (%s)\n",tmp.Image, container.Image);
}
}
return res
}
func filterImages(images []docker.APIImages, patterns []*regexp.Regexp, inuseids []string) []docker.APIImages {
var res []docker.APIImages
var match_found bool
//fmt.Println("filterImages: size=",len(images))
for _, image := range images {
match_found = false
// check if this image is in use
for _,inuse := range inuseids {
if image.ID == inuse {
//fmt.Println("filterImages: image id ",image.ID," matches ",inuse);
match_found = true;
break;
}
}
if match_found {
continue;
}
// loop over patterns and check ID and RepoTags[] for match
for _, pattern := range patterns {
if len(pattern.FindString(image.ID)) > 0 {
//fmt.Println("filterImages: pattern match against ",image.ID);
match_found = true
break
}
for _, name := range image.RepoTags {
//fmt.Println("filterImages: pattern match against ",name);
if len(pattern.FindString(name)) > 0 {
match_found = true
break
}
}
if match_found {
break
}
}
if !match_found {
res = append(res, image)
}
}
return res
}
func processCommandLine() (*UndertakerData,error) {
var container_excludes_file string
var image_excludes_file string
var conserving_deadline_secs int64
var c_excludes MultiStringOption
var i_excludes MultiStringOption
var c_includes MultiStringOption
var i_includes MultiStringOption
flag.StringVar(&container_excludes_file, "filecexc", "", "read container excludes from file")
flag.StringVar(&image_excludes_file, "fileiexc", "", "read image excludes from file")
flag.Int64Var(&conserving_deadline_secs, "wait", int64(3600), "conserving deadline in seconds")
flag.Var(&c_excludes,"cexc","a single container exclude");
flag.Var(&i_excludes,"iexc","a single image exclude");
flag.Var(&c_includes,"cinc","a single container include");
flag.Var(&i_includes,"iinc","a single image incclude");
flag.Parse()
var resptr = NewUndertakerData();
if len(container_excludes_file) > 0 {
ptr,err := loadFile(container_excludes_file);
if err != nil {
return nil,err
}
resptr.ContainerExc = generateRegexp(ptr);
}
if len(image_excludes_file) > 0 {
ptr,err := loadFile(image_excludes_file);
if err != nil {
return nil,err
}
resptr.ImageExc = generateRegexp(ptr);
}
if len(c_excludes) > 0 {
*resptr.ContainerExc = append(*resptr.ContainerExc,*generateRegexp(&c_excludes)...)
}
if len(i_excludes) > 0 {
*resptr.ImageExc = append(*resptr.ImageExc,*generateRegexp(&i_excludes)...)
}
if len(c_includes) > 0 {
*resptr.ContainerInc = append(*resptr.ContainerInc,*generateRegexp(&c_includes)...)
}
if len(i_includes) > 0 {
*resptr.ImageInc = append(*resptr.ImageInc,*generateRegexp(&i_includes)...)
}
return resptr,nil
}
func main() {
var dataPtr *UndertakerData
var err error
dataPtr,err = processCommandLine();
if err != nil {
log.Fatal(err);
}
endpoint := "unix:///var/run/docker.sock"
dataPtr.client, err = docker.NewClient(endpoint)
if err != nil {
log.Fatal(err)
}
dataPtr.ContainersAll, err = dataPtr.client.ListContainers(docker.ListContainersOptions{All: true})
if err != nil {
log.Fatal(err);
}
for _, cont := range dataPtr.ContainersAll {
//fmt.Printf("%v %v\n",cont.Names,cont.Status);
if strings.Index(cont.Status,"Exited") == 0 {
dataPtr.ContainersExited = append(dataPtr.ContainersExited,cont);
} else {
// we need to inspect the container for the real ID (field
// APIContainers.Image may hold either a name or an ID)
tmp, err := dataPtr.client.InspectContainer(cont.ID)
if err != nil {
log.Fatal(err)
}
if !containsString(dataPtr.InuseImageIds,tmp.Image) {
dataPtr.InuseImageIds = append(dataPtr.InuseImageIds,tmp.Image);
}
}
}
fmt.Println(dataPtr);
/*
containers_exited = filterContainers(client,containers_exited, container_excludes,&inuse_ids)
var containers_to_kill []*docker.Container
for _, cont := range containers_exited {
container, _ := client.InspectContainer(cont.ID)
if (time.Now().Unix() - container.State.FinishedAt.Unix()) < conserving_deadline_secs {
inuse_ids = append(inuse_ids,container.Config.Image);
} else {
containers_to_kill = append(containers_to_kill,container);
}
}
images_all, _ := client.ListImages(docker.ListImagesOptions{All: false})
images_to_kill := filterImages(images_all,image_excludes,inuse_ids);
for i, cont := range containers_to_kill {
fmt.Printf("rm container [%2d]: %64s %v\n",i, cont.ID, cont.Name);
}
for i, img := range images_to_kill {
fmt.Printf("rm image [%2d]: %64s %v\n",i, img.ID, img.RepoTags);
}
*/
}