-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocker.go
122 lines (106 loc) · 2.74 KB
/
docker.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
package main
import (
"archive/tar"
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
)
type DockerImageInfo struct {
Id string
}
type DockerClient struct {
httpClient http.Client
}
func NewDockerClient() (*DockerClient, error) {
client := new(DockerClient)
client.httpClient = http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", "/var/run/docker.sock")
},
},
}
// TODO: try to do version negotiation
// - check /_ping for the API-Version header?
// - check /version for supported range (can you call this without a version?)
// - if it's not v1.44, throw a big warning but let the user keep going
return client, nil
}
func (client *DockerClient) ImageList(ctx context.Context, reference string) ([]DockerImageInfo, error) {
req, err := http.NewRequestWithContext(
ctx,
"GET",
fmt.Sprintf("http://unix/v1.44/images/json?filters={\"reference\":[\"%s\"]}", reference),
nil)
if err != nil {
return nil, err
}
resp, err := client.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
content, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var images []DockerImageInfo
err = json.Unmarshal(content, &images)
if err != nil {
err = fmt.Errorf("%w while parsing: %v", err, string(content))
}
return images, err
}
func (client *DockerClient) ImagePull(ctx context.Context, reference string, statusHandler func(statusMessage string)) error {
req, err := http.NewRequestWithContext(
ctx,
"POST",
fmt.Sprintf("http://unix/v1.44/images/create?fromImage=%s", reference),
nil)
if err != nil {
return err
}
resp, err := client.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == 404 {
return nil
}
if resp.StatusCode != 200 {
content, _ := io.ReadAll(resp.Body)
return errors.New(fmt.Sprint(resp.Status, ": ", string(content)))
}
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
if statusHandler != nil {
statusHandler(scanner.Text())
}
}
return scanner.Err()
}
func (client *DockerClient) ImageExport(ctx context.Context, reference string, tarballHandler func(*tar.Reader) error) error {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://unix/v1.44/images/%s/get", reference), nil)
if err != nil {
return err
}
resp, err := client.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
content, _ := io.ReadAll(resp.Body)
return errors.New(fmt.Sprint(resp.Status, ": ", string(content)))
}
return tarballHandler(tar.NewReader(resp.Body))
}
func (client *DockerClient) Close() {
client.httpClient.CloseIdleConnections()
}