This repository has been archived by the owner on Nov 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdgc.go
223 lines (199 loc) · 5.68 KB
/
dgc.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
package main
import (
"bufio"
"fmt"
"github.com/urfave/cli"
dockerTypes "github.com/docker/docker/api/types"
dockerClient "github.com/docker/docker/client"
"log"
"os"
"sync"
"time"
"context"
)
func readExcludes(fileName string) []string {
var excludeNames []string
file, err := os.Open(fileName)
if err != nil {
log.Fatal("Error opening input file:", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
excludeNames = append(excludeNames, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal("Error reading exclude file:", scanner.Err())
}
return excludeNames
}
func collectAPIImages(images []dockerTypes.ImageSummary, client *dockerClient.Client, ctx *cli.Context, excludes []string) {
var imageSync sync.WaitGroup
grace := ctx.Duration("grace")
quiet := ctx.Bool("quiet")
options := dockerTypes.ImageRemoveOptions{
Force: ctx.Bool("force"),
PruneChildren: ctx.Bool("no-prune"),
}
for _, image := range images {
imageSync.Add(1)
go func(image dockerTypes.ImageSummary) {
defer imageSync.Done()
// Check if the image id or tag is on excludes list
for _, excludeName := range excludes {
if image.ID == excludeName {
return
}
for _, tag := range image.RepoTags {
if tag == excludeName {
return
}
}
}
// End if the image is still in the grace period
log.Printf("Inspecting image: %s\n", image.ID)
now := time.Now()
if now.Sub(time.Unix(image.Created, 0)) < grace {
return
}
// Delete image
log.Printf("Deleting image: %s\n", image.ID)
if _, err := client.ImageRemove(context.Background(), image.ID, options); err == nil {
log.Printf("Deleted image: %s\n", image.ID)
if !quiet {
fmt.Printf("Deleted image: %s\n", image.ID)
}
} else {
log.Printf("Error. Failed to delete image: %s\n", image.ID)
return
}
}(image)
}
imageSync.Wait()
}
func collectAPIContainers(containers []dockerTypes.Container, client *dockerClient.Client, ctx *cli.Context, excludes []string) {
var containerSync sync.WaitGroup
grace := ctx.Duration("grace")
quiet := ctx.Bool("quiet")
for _, container := range containers {
containerSync.Add(1)
go func(container dockerTypes.Container) {
defer containerSync.Done()
// Check if the container id or tag is on excludes list
for _, excludeName := range excludes {
if container.ID == excludeName {
return
}
if container.Image == excludeName {
return
}
for _, name := range container.Names {
if name == excludeName {
return
}
}
}
// End if the container is still in the grace period
now := time.Now()
if now.Sub(time.Unix(container.Created, 0)) < grace {
return
}
// Delete container
options := dockerTypes.ContainerRemoveOptions{
RemoveVolumes: ctx.Bool("remove-volumes"),
Force: ctx.Bool("force"),
}
log.Printf("Deleting container: %s\n", container.ID)
if err := client.ContainerRemove(context.Background(), container.ID, options); err == nil {
log.Printf("Deleted container: %s\n", container.ID)
if !quiet {
fmt.Printf("Deleted container: %s\n", container.ID)
}
} else {
log.Printf("Error. Failed to delete container: %s\n", container.ID)
return
}
}(container)
}
containerSync.Wait()
}
func runDgc(ctx *cli.Context) {
var dgcSync sync.WaitGroup
var excludes []string
// TODO: change this to use socket
client, err := dockerClient.NewEnvClient()
if err != nil {
log.Fatalf("Error. Failed to create a docker client to: %s", ctx.String("socket"))
}
log.Println("Getting a List of images...")
images, err := client.ImageList(context.Background(), dockerTypes.ImageListOptions{All: true})
if err != nil {
log.Fatal("Error. Failed to retrieve images from the docker host.")
}
log.Println("Getting a list of containers...")
containers, err := client.ContainerList(context.Background(), dockerTypes.ContainerListOptions{All: true})
if err != nil {
log.Fatal("Error. Failed to retrieve containers from the docker host.")
}
if ctx.String("exclude") != "" {
excludes = readExcludes(ctx.String("exclude"))
}
dgcSync.Add(2)
log.Println("Performing garbage collection...")
go func() {
defer dgcSync.Done()
collectAPIContainers(containers, client, ctx, excludes)
}()
go func() {
defer dgcSync.Done()
collectAPIImages(images, client, ctx, excludes)
}()
dgcSync.Wait()
log.Println("Finished garbage collection!")
}
func main() {
dgc := cli.NewApp()
dgc.EnableBashCompletion = true
dgc.Name = "dgc"
dgc.Usage = "A minimal docker garbage collector"
dgc.Version = "0.1.0"
dgc.Author = "David J Felix <[email protected]>"
dgc.Action = runDgc
dgc.Flags = []cli.Flag{
cli.DurationFlag{
Name: "grace, g",
Value: time.Duration(3600) * time.Second,
Usage: "the grace period for a container. Accepted compostable time units: [h, m, s, ms, ns us]",
EnvVar: "GRACE_PERIOD_SECONDS,GRACE_PERIOD",
},
cli.StringFlag{
Name: "socket, s",
Value: "unix:///var/run/docker.sock",
Usage: "the docker remote socket",
EnvVar: "DOCKER_SOCKET",
},
cli.StringFlag{
Name: "exclude, e",
Value: "",
Usage: "the list of containers to exclude from garbage collection, as a file or directory",
EnvVar: "EXCLUDE_FROM_GC",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "don't print name of garbage-collected containers",
},
cli.BoolTFlag{
Name: "remove-volumes, r",
Usage: "remove volumes with the container",
},
cli.BoolFlag{
Name: "force, f",
Usage: "force images and containers to stop and be collected",
},
cli.BoolFlag{
Name: "no-prune, n",
Usage: "don't prune parent images to a GC'd image",
},
}
dgc.Run(os.Args)
}