-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhlm.go
107 lines (91 loc) · 2.57 KB
/
dhlm.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
package main
import (
"dhlm/dockerhub"
"fmt"
"github.com/urfave/cli"
"os"
"time"
)
func main() {
app := cli.NewApp()
app.Usage = "DockerHub lifecycle manager"
app.UsageText = "dhlm [global options] [organization name] [repository name]"
app.HideVersion = true
var dhUsername string
var dhPassword string
var dhOrg string
var dhRepo string
var days int
var dryRun bool
var pageSize int
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "username",
Destination: &dhUsername,
},
cli.StringFlag{
Name: "password",
Destination: &dhPassword,
},
cli.IntFlag{
Name: "days",
Destination: &days,
Value: 30,
},
cli.IntFlag{
Name: "pageSize",
Destination: &pageSize,
Value: 10,
},
cli.BoolFlag{
Name: "dry-run",
Destination: &dryRun,
},
}
app.Action = func(c *cli.Context) error {
dhOrg = c.Args().Get(0)
dhRepo = c.Args().Get(1)
if len(dhOrg) == 0 || len(dhRepo) == 0 {
cli.ShowAppHelp(c)
return nil
}
dh := dockerhub.NewClient(dockerhub.Auth{
Username: dhUsername,
Password: dhPassword,
})
timeBefore := time.Now().Add(-time.Hour * 24 * time.Duration(days))
pageNumber := 1
for tagsList := dh.GetImages(dhOrg, dhRepo, pageNumber, timeBefore, pageSize); len(tagsList.Next) > 0; pageNumber++ {
fmt.Println("Checking page:", pageNumber)
var digests []string
var ignoreList []*dockerhub.IgnoreWarnings
for _, tag := range tagsList.Results {
if tag.LastPulled.Unix() < timeBefore.Unix() {
fmt.Println("Removing " + dhOrg + "/" + dhRepo + ":" + tag.Digest + " | " + tag.LastPulled.Format(time.RFC3339) + " | " + tag.LastPushed.Format(time.RFC3339))
digests = append(digests, tag.Digest)
var ignTags []string
for _, t := range tag.Tags {
if t.IsCurrent == true {
ignTags = append(ignTags, t.Tag)
}
}
ignoreList = append(ignoreList, &dockerhub.IgnoreWarnings{
Repository: dhRepo,
Digest: tag.Digest,
Warning: "current_tag",
Tags: ignTags,
})
}
}
deletedImages := dh.DeleteImages(dhOrg, dhRepo, digests, timeBefore, dryRun, ignoreList)
fmt.Printf("Summary of deleted images ➡ manifest_deletes: %d, manifest_errors: %d, tag_deletes: %d, tag_errors: %d \n",
deletedImages.Metrics.ManifestDeletes, deletedImages.Metrics.ManifestErrors, deletedImages.Metrics.TagDeletes, deletedImages.Metrics.TagDeletes)
tagsList = dh.GetImages(dhOrg, dhRepo, pageNumber, timeBefore, pageSize)
}
return nil
}
err := app.Run(os.Args)
if err != nil {
panic(err)
}
}