-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
178 lines (134 loc) · 3.11 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
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/Sirupsen/logrus"
"github.com/urfave/cli"
)
var log = logrus.New()
var client http.Client
func main() {
log.Out = os.Stdout
app := cli.NewApp()
app.Name = "drcleaner"
app.Usage = "Docker Registry Cleaner"
app.Version = "0.0.1"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "url, U",
Value: "http://localhost:5000",
},
cli.StringSliceFlag{
Name: "tags, T",
},
}
app.Action = action
app.Run(os.Args)
}
type tagsModel struct {
Name string `json:"name"`
Tags []string `json:"tags"`
}
func action(c *cli.Context) error {
if c.NArg() == 0 {
err := errors.New("please input image name")
log.Error(err.Error())
return err
}
log.Info("docker registry cleaner start!!")
i := c.Args().First()
u := c.String("U")
ts := c.StringSlice("T")
if 0 == len(ts) {
log.Infof("start get all tags from %s", i)
tags, err := getAllTag(u, i)
if err != nil {
log.Error(err.Error())
return err
}
ts = tags
log.WithField("tags", tags).Infof("finish get all tags from %s", i)
}
for _, t := range ts {
log.Infof("start get digest from %s:%s", i, t)
d, err := getDigest(u, i, t)
if err != nil {
log.Error(err.Error())
return err
}
log.WithField("digest", d).Infof("finish get digest from %s:%s", i, t)
log.WithField("digest", d).Infof("start delete tag from %s:%s", i, t)
if err := deleteTag(u, i, d); err != nil {
log.Error(err.Error())
return err
}
log.Infof("finish delete tag from %s:%s", i, t)
}
log.Info("docker registry cleaner finished!!")
return nil
}
func getAllTag(u, i string) ([]string, error) {
url := fmt.Sprintf("%s/v2/%s/tags/list", u, i)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode < 200 || 300 < res.StatusCode {
return nil, errors.New(res.Status)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var v map[string]interface{}
if err := json.Unmarshal(body, &v); err != nil {
return nil, err
}
tags, ok := v["tags"].([]string)
if !ok {
return nil, fmt.Errorf("%s is have not tag", i)
}
return tags, nil
}
func getDigest(u, i, t string) (string, error) {
url := fmt.Sprintf("%s/v2/%s/manifests/%s", u, i, t)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Add("Accept", "application/vnd.docker.distribution.manifest.v2+json")
res, err := client.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode < 200 || 300 < res.StatusCode {
return "", errors.New(res.Status)
}
return res.Header.Get("Docker-Content-Digest"), nil
}
func deleteTag(u, i, d string) error {
url := fmt.Sprintf("%s/v2/%s/manifests/%s", u, i, d)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode < 200 || 300 < res.StatusCode {
return errors.New(res.Status)
}
return nil
}