-
Notifications
You must be signed in to change notification settings - Fork 3
/
docker.go
129 lines (109 loc) · 2.83 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
package main
import (
"errors"
"fmt"
"os"
"path"
"time"
"github.com/fsouza/go-dockerclient"
)
type DockerService interface {
AttachToContainer(docker.AttachToContainerOptions) error
CommitContainer(docker.CommitContainerOptions) (*docker.Image, error)
ContainerChanges(string) ([]docker.Change, error)
CreateContainer(docker.CreateContainerOptions) (*docker.Container, error)
RemoveContainer(docker.RemoveContainerOptions) error
StartContainer(string, *docker.HostConfig) error
WaitContainer(string) (int, error)
InspectImage(string) (*docker.Image, error)
}
func NewDockerClient(host string, tlsVerify string, certPath string) (client *docker.Client, err error) {
if host == "" {
return nil, errors.New("DOCKER_HOST must be set")
}
if tlsVerify == "yes" || tlsVerify == "1" {
if certPath == "" {
return client, errors.New("DOCKER_TLS_VERIFY set without DOCKER_CERT_PATH")
}
cert := path.Join(certPath, "cert.pem")
key := path.Join(certPath, "key.pem")
ca := path.Join(certPath, "ca.pem")
client, err = docker.NewTLSClient(host, cert, key, ca)
if err != nil {
return client, err
}
} else {
client, err = docker.NewClient(host)
if err != nil {
return client, err
}
}
return client, nil
}
func Eval(d DockerService, command string, image string) (EvalResult, error) {
res := EvalResult{
Command: command,
Image: image,
Deleted: false,
}
cwd, _ := os.Getwd()
options := docker.CreateContainerOptions{
Config: &docker.Config{
Image: image,
Cmd: []string{"/bin/bash", "-c", command},
},
HostConfig: &docker.HostConfig{
Binds: []string{fmt.Sprintf("%s:/work", cwd)},
},
}
cont, err := d.CreateContainer(options)
if err != nil {
return res, err
}
res.Id = cont.ID
buf := NewBuffer(os.Stdout)
attachOpts := docker.AttachToContainerOptions{
Container: cont.ID,
OutputStream: buf,
ErrorStream: buf,
Logs: true,
Stream: true,
Stdin: false,
Stdout: true,
Stderr: true,
}
go func() {
d.AttachToContainer(attachOpts)
}()
start := time.Now()
if err := d.StartContainer(cont.ID, &docker.HostConfig{}); err != nil {
return res, err
}
res.Code, err = d.WaitContainer(cont.ID)
if err != nil {
return res, err
}
res.Log = buf
res.Duration = time.Since(start)
if res.Code == 0 {
}
res.Changes, err = d.ContainerChanges(cont.ID)
if err != nil {
return res, err
}
return res, nil
}
func CommitContainer(d DockerService, id string) (string, error) {
if image, err := d.CommitContainer(docker.CommitContainerOptions{Container: id}); err != nil {
return "", err
} else {
return image.ID, nil
}
}
func RemoveContainer(d DockerService, id string) error {
return d.RemoveContainer(docker.RemoveContainerOptions{ID: id})
}
func verifyImage(d DockerService, image string) error {
_, err := d.InspectImage(image)
return err
}