forked from nanobox-io/golang-docker-client
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
119 lines (102 loc) · 2.63 KB
/
image.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
package docker
import (
"errors"
"io"
"io/ioutil"
"strings"
dockType "github.com/docker/engine-api/types"
"golang.org/x/net/context"
)
type Image struct {
ID string `json:"id"`
Slug string `json:"slug"`
RepoTags []string `json:"repo_tags"`
Size int64 `json:"size"`
VirtualSize int64 `json:"virtual_size"`
Status string `json:"status"`
}
// ImageExists
func ImageExists(name string) bool {
images, err := ImageList()
if err != nil {
return false
}
for _, image := range images {
for _, tag := range image.RepoTags {
if tag == name+":latest" || tag == name {
return true
}
}
}
return false
}
// pull any new image
func ImagePull(image string, output io.Writer) (Image, error) {
privilegeFunc := func() (string, error) {
return "", errors.New("no privilege function defined")
}
pullOptions := dockType.ImagePullOptions{
PrivilegeFunc: privilegeFunc,
}
ctx := context.Background()
rc, err := client.ImagePull(ctx, canonImage(image), pullOptions)
if err != nil {
return Image{}, err
}
defer rc.Close()
if output == nil {
output = ioutil.Discard
}
io.Copy(output, rc)
return ImageInspect(image)
}
// list the images i have cached on the server
func ImageList() ([]Image, error) {
imgs := []Image{}
dockImages, err := client.ImageList(context.Background(), dockType.ImageListOptions{})
if err != nil {
return imgs, err
}
for _, dockImage := range dockImages {
img := Image{
ID: dockImage.ID,
RepoTags: dockImage.RepoTags,
Size: dockImage.Size,
VirtualSize: dockImage.VirtualSize,
Status: "complete",
}
if len(img.RepoTags) > 0 {
img.Slug = img.RepoTags[0]
}
imgs = append(imgs, img)
}
return imgs, nil
}
func ImageInspect(imageID string) (Image, error) {
// ignore the raw part of the image inspect
dockInspect, _, err := client.ImageInspectWithRaw(context.Background(), imageID)
img := Image{
ID: dockInspect.ID,
RepoTags: dockInspect.RepoTags,
Size: dockInspect.Size,
VirtualSize: dockInspect.VirtualSize,
Status: "complete",
}
if len(img.RepoTags) > 0 {
img.Slug = img.RepoTags[0]
}
return img, err
}
func ImageRemove(imageID string, force bool) error {
_, err := client.ImageRemove(context.Background(), imageID, dockType.ImageRemoveOptions{Force: force, PruneChildren: true})
return err
}
func canonImage(image string) string {
parts := strings.Split(image, "/")
if len(parts) > 2 {
return image
} else if len(parts) > 1 {
return strings.Join(append([]string{"docker.io"}, parts...), "/")
}
return strings.Join(append([]string{"docker.io", "library"}, parts...), "/")
}