-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathservice_docker.go
More file actions
284 lines (255 loc) · 7.33 KB
/
service_docker.go
File metadata and controls
284 lines (255 loc) · 7.33 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package test
import (
"context"
"fmt"
"log"
"net"
"runtime"
"strconv"
"strings"
"sync"
"time"
docker "github.com/fsouza/go-dockerclient"
)
// ServiceDocker defines an interface to create service via docker
type ServiceDocker interface {
// Start creates and starts an instance of supported service by the give type. It
// returns its listening ip:port and the corresponding stop function.
Start(ServiceType, ...ServiceOption) (ipport string, stopFunc func() error, err error)
// StopAll stop all created services
StopAll() error
// Get retruns service, return nil if no service for the given ipport
Get(ipport string) interface{}
}
// NewServiceDocker returns an instance of ServiceDocker
// TODO: with specified docker address
func NewServiceDocker() ServiceDocker {
return &serviceDockerImpl{
services: map[string]Service{},
dockerclient: NewDockerClient(),
}
}
// ContainerOptionFunc is a function that configures a Client.
// It is used in createDockerOptions.
type ContainerOptionFunc func(*docker.CreateContainerOptions) error
type ContainerNode struct {
Command []string
Env []string
Image string
Port []string
}
// serviceDockerImpl implements ServiceDocker
type serviceDockerImpl struct {
// docker client
dockerclient *docker.Client
// service stores created services
services map[string]Service
// mutx to protected services
sync.Mutex
}
// Create returns an instance of supported service by the give type
func (s *serviceDockerImpl) Start(t ServiceType, options ...ServiceOption) (string, func() error, error) {
s.Lock()
defer s.Unlock()
srvFactories.RLock()
fac, ok := srvFactories.facs[t]
srvFactories.RUnlock()
if !ok {
return "", nil, fmt.Errorf("unsupported service type %v", t)
}
// guard with state checker
srv := &stateChkService{
state: stateNew,
Service: fac(),
cl: s.dockerclient,
}
// apply option functions
for _, opt := range options {
if err := opt(srv.Service); err != nil {
return "", nil, fmt.Errorf("failed to apply option %v", opt)
}
}
// start service
ipport, err := srv.Start()
if err != nil {
return "", nil, fmt.Errorf("unable to start service %v, err %v", t, err)
}
// store raw service
s.services[ipport] = srv.Service
return ipport, srv.Stop, nil
}
// StopAll stop all created services
func (s *serviceDockerImpl) StopAll() error {
s.Lock()
defer s.Unlock()
errs := []error{}
for _, ss := range s.services {
errs = append(errs, ss.StopDocker(s.dockerclient))
}
s.services = map[string]Service{}
return CombineError(errs...)
}
// Get return service by givne ip port
func (s *serviceDockerImpl) Get(ipport string) interface{} {
s.Lock()
defer s.Unlock()
return s.services[ipport]
}
// NewDockerClient create docker client via unix socket
func NewDockerClient() (cl *docker.Client) {
endpoint := "unix:///var/run/docker.sock"
var err error
cl, err = docker.NewClient(endpoint)
if err != nil {
log.Fatal(err)
}
return cl
}
// RemoveContainer remove the started container
func RemoveContainer(client *docker.Client, container *docker.Container) error {
return client.RemoveContainer(docker.RemoveContainerOptions{
ID: container.ID,
Force: true,
})
}
// StartContainer starts the required container
func StartContainer(client *docker.Client, options ...ContainerOptionFunc) (c *docker.Container, ipaddr string, err error) {
// TODO: handle context
ctx := context.Background()
opts, err := createDockerOptions(ctx, options...)
if err != nil {
return c, "", err
}
c, err = client.CreateContainer(opts)
if err != nil {
return c, "", err
}
err = client.StartContainerWithContext(c.ID, nil, ctx)
if err != nil {
RemoveContainer(client, c)
return c, "", err
}
// wait for container to wake up
if err := waitStarted(client, c.ID, 5*time.Second); err != nil {
RemoveContainer(client, c)
return c, "", err
}
c, err = client.InspectContainerWithContext(c.ID, ctx)
if err != nil {
RemoveContainer(client, c)
return c, "", err
}
// determine IP address for Component
var ip string
if runtime.GOOS == "darwin" {
ip = "localhost"
} else {
ip = strings.TrimSpace(c.NetworkSettings.IPAddress)
}
port := opts.Context.Value("csigo_test_port")
// wait Component to wake up
ipaddr = fmt.Sprintf("%s:%s", ip, port)
fmt.Printf("%s\n", ipaddr)
if err = waitReachable(ctx, ipaddr, 10*time.Second); err != nil {
RemoveContainer(client, c)
return c, "", err
}
return c, ipaddr, nil
}
//TODO:
//PortBindings: map[docker.Port][]docker.PortBinding{
// "8888/tcp": {{HostIP: "", HostPort: "12345"}},
//},
// SetExposedPorts accepts format as "1234/tcp", "5678/udp"
func SetExposedPorts(ports []string) ContainerOptionFunc {
return func(opts *docker.CreateContainerOptions) error {
for i, exp := range ports {
if i == 0 { // only first port will be checked and exposed
connectPort := docker.Port(exp).Port()
if runtime.GOOS == "darwin" {
connectPort = strconv.Itoa(GetPort())
opts.HostConfig.PortBindings[docker.Port(exp)] = append(opts.HostConfig.PortBindings[docker.Port(exp)], docker.PortBinding{
HostPort: connectPort,
HostIP: "",
})
}
opts.Context = context.WithValue(opts.Context, "csigo_test_port", connectPort)
}
opts.Config.ExposedPorts[docker.Port(exp)] = struct{}{}
}
return nil
}
}
// SetImage set the docker image for the container
func SetImage(image string) ContainerOptionFunc {
return func(opts *docker.CreateContainerOptions) error {
opts.Config.Image = image
return nil
}
}
// SetEnv set the command for the container
func SetEnv(env []string) ContainerOptionFunc {
return func(opts *docker.CreateContainerOptions) error {
opts.Config.Env = env
return nil
}
}
// SetCommand set the command for the container
func SetCommand(cmd []string) ContainerOptionFunc {
return func(opts *docker.CreateContainerOptions) error {
opts.Config.Cmd = cmd
return nil
}
}
func createDockerOptions(ctx context.Context, options ...ContainerOptionFunc) (docker.CreateContainerOptions, error) {
opts := docker.CreateContainerOptions{
Config: &docker.Config{
ExposedPorts: map[docker.Port]struct{}{},
},
HostConfig: &docker.HostConfig{
PortBindings: map[docker.Port][]docker.PortBinding{},
},
NetworkingConfig: &docker.NetworkingConfig{},
Context: ctx,
}
// Run the options on it
for _, option := range options {
if err := option(&opts); err != nil {
return opts, err
}
}
return opts, nil
}
// waitReachable waits for hostport to became reachable for the maxWait time.
func waitReachable(ctx context.Context, hostport string, maxWait time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, maxWait)
defer cancel()
for {
select {
case <-time.After(100 * time.Millisecond):
c, err := net.DialTimeout("tcp", hostport, 3*time.Second)
fmt.Printf("%s %v\n", hostport, err)
if err == nil {
c.Close()
return nil
}
case <-ctx.Done():
return fmt.Errorf("cannot connect %v for %v: %v", hostport, maxWait, ctx.Err())
}
}
}
// waitStarted waits for a container to start for the maxWait time.
func waitStarted(client *docker.Client, id string, maxWait time.Duration) error {
done := time.Now().Add(maxWait)
for time.Now().Before(done) {
c, err := client.InspectContainer(id)
if err != nil {
break
}
if c.State.Running {
return nil
}
time.Sleep(100 * time.Millisecond)
}
return fmt.Errorf("cannot start container %s for %v", id, maxWait)
}