-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker.go
201 lines (187 loc) · 5.61 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"path"
"strconv"
"golang.org/x/net/websocket"
)
type DockerClient struct {
hostAddr string
httpClient *http.Client
pathPrefix string
wsPrefix string
logger *log.Logger
}
func NewDockerClient(hostAddr string) *DockerClient {
dockerClient := &DockerClient{
hostAddr: hostAddr,
}
// Determine if this is a valid TCP or unix socket address
_, err := net.ResolveTCPAddr("tcp", hostAddr)
if err == nil {
// http client transport uses TCP by default
dockerClient.httpClient = &http.Client{}
dockerClient.pathPrefix = "http://" + hostAddr
dockerClient.wsPrefix = "ws://" + hostAddr
} else if path.IsAbs(hostAddr) {
// custom transport for unix socket
dockerClient.httpClient = &http.Client{
Transport: NewUnixTransport(hostAddr),
}
dockerClient.pathPrefix = "unix://"
} else {
log.Fatal("Unsupported transport method: must be unix socket or tcp")
}
return dockerClient
}
func (d *DockerClient) makeRequest(method, url string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
return d.httpClient.Do(req)
}
// Get list of images
func (d *DockerClient) GetImages() ([]DockerImage, error) {
resp, err := d.makeRequest("GET", fmt.Sprintf("%s/images/json", d.pathPrefix), nil)
if err != nil {
return nil, err
} else if resp.StatusCode != 200 {
return nil, fmt.Errorf("GetImages: error status code %s", resp.StatusCode)
}
defer resp.Body.Close()
var images []DockerImage
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &images)
return images, err
}
// Get list of containers
func (d *DockerClient) GetContainers(all bool, filters map[string][]string) ([]DockerContainer, error) {
resp, err := d.makeRequest("GET", fmt.Sprintf("%s/containers/json?all=%s", d.pathPrefix, strconv.FormatBool(all)), nil)
defer resp.Body.Close()
if err != nil {
return nil, err
} else if resp.StatusCode != 200 {
return nil, fmt.Errorf("GetContainers: error status code %d", resp.StatusCode)
}
var containers []DockerContainer
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &containers)
return containers, err
}
// Get Docker info
func (d *DockerClient) GetInfo() (*DockerInfo, error) {
resp, err := d.makeRequest("GET", fmt.Sprintf("%s/info", d.pathPrefix), nil)
defer resp.Body.Close()
if err != nil {
return nil, err
} else if resp.StatusCode != 200 {
return nil, fmt.Errorf("GetInfo: error status code %d", resp.StatusCode)
}
info := DockerInfo{}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &info)
return &info, err
}
// Create a container
func (d *DockerClient) CreateContainer(name string, param CreateContainerParam) (*DockerContainer, error) {
b, err := json.Marshal(param)
if err != nil {
return nil, err
}
resp, err := d.makeRequest("POST", fmt.Sprintf("%s/containers/create?name=%s", d.pathPrefix, name), bytes.NewReader(b))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
} else if resp.StatusCode != 201 {
return nil, fmt.Errorf("CreateContainer: error status code %d", resp.StatusCode)
}
container := DockerContainer{}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &container)
return &container, err
}
// Start a container
func (d *DockerClient) StartContainer(idOrName string) error {
resp, err := d.makeRequest("POST", fmt.Sprintf("%s/containers/%s/start", d.pathPrefix, idOrName), nil)
if err != nil {
return err
} else if resp.StatusCode != 204 {
return fmt.Errorf("StartContainier: error status code %d", resp.StatusCode)
}
return nil
}
// Stop a container after t seconds
func (d *DockerClient) StopContainer(idOrName string, t int) error {
resp, err := d.makeRequest("POST", fmt.Sprintf("%s/containers/%s/stop?t=%d", d.pathPrefix, idOrName, t), nil)
if err != nil {
return err
} else if resp.StatusCode != 204 {
return fmt.Errorf("StopContainier: error status code %d", resp.StatusCode)
}
return nil
}
// Remove a container
func (d *DockerClient) RemoveContainer(idOrName string, volumes, force bool) error {
resp, err := d.makeRequest("DELETE", fmt.Sprintf("%s/containers/%s?v=%v&force=%v", d.pathPrefix, idOrName, volumes, force), nil)
if err != nil {
return err
} else if resp.StatusCode != 204 {
return fmt.Errorf("RemoveContainier: error status code %d", resp.StatusCode)
}
return nil
}
// Attach to a container via websocket
func (d *DockerClient) AttachContainer(idOrName string, logs, stream, stdin, stdout, stderr bool) (chan string, error) {
uri := fmt.Sprintf("%s/containers/%s/attach/ws?logs=%v&stream=%v&stdin=%v&stdout=%v&stderr=%v", d.wsPrefix, idOrName, logs, stream, stdin, stdout, stderr)
ws, err := websocket.Dial(uri, "", "http://127.0.0.1/")
outbound := make(chan string)
if err != nil {
return nil, err
}
go func() {
var msg = make([]byte, 4096)
for {
n, err := ws.Read(msg)
if err != nil {
close(outbound)
return
}
outbound <- string(msg[:n])
}
}()
return outbound, nil
}
// Wait for a container
func (d *DockerClient) WaitContainer(idOrName string) error {
resp, err := d.makeRequest("POST", fmt.Sprintf("%s/containers/%s/wait", d.pathPrefix, idOrName), nil)
if err != nil {
return err
} else if resp.StatusCode != 200 {
return fmt.Errorf("WaitContainier: error status code %d", resp.StatusCode)
}
return nil
}