/
localdocker.go
165 lines (123 loc) · 3.65 KB
/
localdocker.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
package main
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/hashicorp/go-version"
)
type LocalDocker struct {
cli *client.Client
images []types.ImageSummary
}
// Init will connect to local docker daemon
func (ld *LocalDocker) Init() error {
// try and connect to docker
cli, err := client.NewEnvClient()
if err != nil {
// does not seem to be running
return err
}
ld.cli = cli
return nil
}
// Find will search local docker images for requested image
func (ld *LocalDocker) Find(image string) error {
// get local docker images that match requested name
listFilters := filters.NewArgs()
listFilters.Add("reference", image)
ctx := context.Background()
// images, err := cli.ImageList(ctx, types.ImageListOptions{Filters: listFilters, All: false})
images, err := ld.cli.ImageList(ctx, types.ImageListOptions{Filters: listFilters})
if err != nil {
return err
}
if len(images) == 0 {
msg := fmt.Sprintf("no local images match: %s", image)
return errors.New(msg)
}
ld.images = images
return nil
}
// ListTags will print key info on each tag for current image
func (ld *LocalDocker) ListTags() {
fmt.Println(" Local Images:")
// for each image tag
for _, image := range ld.images {
var tags []string
imageTimestamp := time.Unix(image.Created, 0)
// get name from the tags, which is actually name:tag (ie node:latest)
for _, fullTag := range image.RepoTags {
// note may have several images for a specific project
// python:3-7 & python:3.7-slim
tagParts := strings.SplitN(fullTag, ":", 2)
tag := tagParts[1]
// ignore images that are unlabeled
// if strings.Compare(imageName, "<none>") == 0 {
// continue
// }
tags = append(tags, tag)
}
if len(tags) > 0 {
fmt.Printf(" %-25s %s created: %s\n", tags[0], image.ID[7:19], imageTimestamp.Format("01-02-2006 15:04"))
}
}
// done
}
// GetOldestImage will return timestamp of the oldest image
func (ld *LocalDocker) GetOldestImage() (oldestTimestamp time.Time, err error) {
oldestTimestamp = time.Now()
for _, image := range ld.images {
if time.Unix(image.Created, 0).Before(oldestTimestamp) {
oldestTimestamp = time.Unix(image.Created, 0)
}
}
return oldestTimestamp, nil
}
// GetMinVersion will go through local tags and return oldest version
func (ld *LocalDocker) GetMinVersion() (minVersion *version.Version, err error) {
// note, not all tags have version numbers in them
// we will test each one and only use ones that have version numbers (using semver)
minVersion, _ = version.NewVersion("99")
for _, image := range ld.images {
if len(image.RepoTags) > 0 {
// need to split (remove image name) ie postgres:13.2-alping
tagParts := strings.Split(image.RepoTags[0], ":")
if len(tagParts) == 1 {
// image doesn't have a tag version (normally for old orphaned images)
continue
}
imageTag := tagParts[1]
imageVersion, err := version.NewVersion(imageTag)
if err != nil {
// not semver format, skip
continue
}
if imageVersion.LessThan(minVersion) {
minVersion = imageVersion.Core()
}
}
}
if minVersion.Segments()[0] == 99 {
return minVersion, errors.New("no tags have version number")
} else {
return minVersion, nil
}
}
// GetAllTags returns list of all tags on given image
func (ld *LocalDocker) GetAllTags() []string {
var allTags []string
for _, image := range ld.images {
for _, tag := range image.RepoTags {
tagParts := strings.Split(tag, ":")
if len(tagParts) == 2 {
allTags = append(allTags, tagParts[1])
}
}
}
return allTags
}