-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimages.go
153 lines (138 loc) · 4 KB
/
images.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
package adoc
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
// This part contains apis for the images listed in
// https://docs.docker.com/reference/api/docker_remote_api_v1.17/#22-images
type Image struct {
Created int64
Id string
Labels map[string]string
ParentId string
RepoTags []string
Size int64
VirtualSize int64
RepoDigests []string // v1.18
}
type ImageDetail struct {
Architecture string
Author string
Comment string
Container string
ContainerConfig ContainerConfig
Created time.Time
DockerVersion string
Id string
Os string
Parent string
Size int64
VirtualSize int64
//Config ContainerConfig // don't know what this is for
}
const (
ImagePuSecs = 100 * time.Second // 1000M / (10M/s)
)
func (client *DockerClient) ListImages(showAll bool, filters ...string) ([]Image, error) {
v := url.Values{}
v.Set("all", formatBoolToIntString(showAll))
if len(filters) > 0 && filters[0] != "" {
v.Set("filters", filters[0])
}
uri := fmt.Sprintf("images/json?%s", v.Encode())
if data, err := client.sendRequest("GET", uri, nil, nil, nil); err != nil {
return nil, err
} else {
var images []Image
err := json.Unmarshal(data, &images)
return images, err
}
}
func (client *DockerClient) InspectImage(name string) (ImageDetail, error) {
var ret ImageDetail
uri := fmt.Sprintf("images/%s/json", name)
if data, err := client.sendRequest("GET", uri, nil, nil, nil); err != nil {
return ret, err
} else {
err := json.Unmarshal(data, &ret)
return ret, err
}
}
func (client *DockerClient) PullImage(name string, tag string, authConfig ...AuthConfig) error {
v := url.Values{}
v.Set("fromImage", name)
v.Set("tag", tag)
uri := fmt.Sprintf("images/create?%s", v.Encode())
header := make(map[string]string)
if len(authConfig) > 0 {
header["X-Registry-Auth"] = authConfig[0].Encode()
}
// extra time for pull image
rc := &RequestConfig{ExtraTimeout: ImagePuSecs}
err := client.sendRequestCallback("POST", uri, nil, header, func(resp *http.Response) error {
var status map[string]interface{}
var cbErr error
decoder := json.NewDecoder(resp.Body)
for ; cbErr == nil; cbErr = decoder.Decode(&status) {
}
if cbErr != io.EOF {
return cbErr
}
if errMsg, ok := status["error"]; ok {
return fmt.Errorf("Pull image error: %s", errMsg)
}
return nil
}, rc)
return err
}
func (client *DockerClient) RemoveImage(name string, force, noprune bool) error {
v := url.Values{}
v.Set("force", formatBoolToIntString(force))
v.Set("noprune", formatBoolToIntString(noprune))
uri := fmt.Sprintf("images/%s?%s", name, v.Encode())
_, err := client.sendRequest("DELETE", uri, nil, nil, nil)
return err
}
func (client *DockerClient) TagImage(name string, repo string, tag string, force bool) error {
v := url.Values{}
v.Set("force", formatBoolToIntString(force))
v.Set("repo", repo)
v.Set("tag", tag)
uri := fmt.Sprintf("images/%s/tag?%s", name, v.Encode())
_, err := client.sendRequest("POST", uri, nil, nil, nil)
return err
}
func (client *DockerClient) PushImage(name string, repo string, tag string, authConfig ...AuthConfig) error {
v := url.Values{}
v.Set("tag", tag)
uri := fmt.Sprintf("images/%s/%s/push?%s", repo, name, v.Encode())
header := make(map[string]string)
if len(authConfig) > 0 {
header["X-Registry-Auth"] = authConfig[0].Encode()
}
// extra time for push image
rc := &RequestConfig{ExtraTimeout: ImagePuSecs}
err := client.sendRequestCallback("POST", uri, nil, header, func(resp *http.Response) error {
var status map[string]interface{}
var cbErr error
decoder := json.NewDecoder(resp.Body)
for ; cbErr == nil; cbErr = decoder.Decode(&status) {
}
if cbErr != io.EOF {
return cbErr
}
if errMsg, ok := status["error"]; ok {
return fmt.Errorf("Push image error: %s", errMsg)
}
return nil
}, rc)
return err
}
// Missing apis for
// build: Build image from a Dockerfile
// images/(name)/history
// images/search