/
main.go
169 lines (150 loc) · 3.04 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
package main
import (
"github.com/phoenix-io/docker-utils/utils"
"fmt"
"github.com/codegangsta/cli"
"log"
"os"
)
func main() {
app := cli.NewApp()
app.Name = "docker-utils"
app.Usage = "Toolchain for docker"
app.Version = "0.2.0"
app.Commands = []cli.Command{
{
Name: "rmi",
Usage: "deletes the docker images",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "dry",
Usage: "[Optional] dry_run the command",
},
cli.BoolFlag{
Name: "untagged",
Usage: "[Required] deletes untagged images",
},
},
Action: func(c *cli.Context) {
removeImages(c)
},
},
{
Name: "rm",
Usage: "deletes docker containers",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "exited",
Usage: "[Required] deletes exited containers",
},
cli.BoolFlag{
Name: "dry",
Usage: "[Optional] dry_run the command",
},
cli.IntFlag{
Name: "hours",
Value: 24,
Usage: "[Optional] hours before containers exited/stopped",
},
},
Action: func(c *cli.Context) {
removeContainers(c)
},
},
{
Name: "flatten",
Usage: "Compacts the images by flattening",
Flags: []cli.Flag{
cli.StringFlag{
Name: "image",
Usage: "[Required] Image file to flatten",
},
cli.StringFlag{
Name: "name",
Usage: "[Required] New name of Image file",
},
cli.StringFlag{
Name: "tag",
Usage: "[Required] tag for new image",
},
},
Action: func(c *cli.Context) {
flattenImage(c)
},
},
}
app.Run(os.Args)
}
// Intialze the docker client
func getUtilContext() *utils.UtilContext {
ctx, err := utils.InitUtilContext()
if err != nil {
log.Println("Unable to initialze the dockerclient.")
log.Println("Docker daemon should be running")
return nil
}
return ctx
}
// remove untagged images
func removeImages(c *cli.Context) {
dry := c.Bool("dry")
untagged := c.Bool("untagged")
if !untagged {
cli.ShowCommandHelp(c, "rmi")
fmt.Println("EXAMPLE:")
fmt.Println(" command rmi --untagged")
return
}
ctx := getUtilContext()
if ctx == nil {
return
}
ctx = getUtilContext()
if ctx == nil {
return
}
if untagged == true {
ctx.RemoveUntaggedDockerImages(dry)
}
return
}
// delete containers which are not running
func removeContainers(c *cli.Context) {
dry := c.Bool("dry")
exited := c.Bool("exited")
hours := c.Int("hours")
if !exited {
cli.ShowCommandHelp(c, "rm")
fmt.Println("EXAMPLE:")
fmt.Println(" command rm --exited")
return
}
if hours <= 0 {
hours = 24
}
ctx := getUtilContext()
if ctx == nil {
return
}
if exited == true {
ctx.DeleteExitedContainers(dry, hours)
}
return
}
func flattenImage(c *cli.Context) {
image := c.String("image")
name := c.String("name")
tag := c.String("tag")
if image == "" && name == "" && tag == "" {
cli.ShowCommandHelp(c, "flatten")
fmt.Println("EXAMPLE:")
fmt.Println(" command flatten --image debian --name debian-new --tag compact")
return
}
ctx := getUtilContext()
if ctx == nil {
return
}
ctx.FlattenImage(image, name, tag)
return
}