-
Notifications
You must be signed in to change notification settings - Fork 5
/
containers.go
407 lines (324 loc) · 8.92 KB
/
containers.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package test
import (
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"text/template"
"time"
"github.com/aerospike/aerospike-client-go/v7"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)
var ClusterName = "mgmt-lib-test"
var PortStart = 10000
var IP = "127.0.0.1"
var WordDirAbs = "test/work"
var Image = "aerospike/aerospike-server-enterprise:7.0.0.2"
var ContainerPrefix = "aerospike_mgmt_lib_test_" //nolint:gosec // This is not a credential
var User = "admin"
var Password = "admin"
var configTemplate = fmt.Sprintf(`
security {
}
service {
cluster-name %s
# Uncomment if multi-node EE tests are needed
# feature-key-file env-b64:FEATURES
run-as-daemon false
proto-fd-max 1024
transaction-retry-ms 10000
transaction-max-ms 10000
}
logging {
console {
context any info
context security info
}
}
network {
service {
port {{.ServicePort}}
address any
access-address {{.AccessAddress}}
}
heartbeat {
mode mesh
address any
port {{.HeartbeatPort}}
interval 100
timeout 10
connect-timeout-ms 100
{{.PeerConnection}}
}
fabric {
port {{.FabricPort}}
address any
}
info {
port {{.InfoPort}}
address any
}
}
namespace test {
default-ttl 30d # use 0 to never expire/evict.
nsup-period 120
replication-factor 1
storage-engine memory {
data-size 1G
}
}
`, ClusterName)
type AerospikeContainer struct {
ip string
configPath string
portBase int
}
type Containers struct {
namesToContainers map[string]*AerospikeContainer
dockerCLI *client.Client
workDir string
}
var containers = &Containers{make(map[string]*AerospikeContainer), nil, ""}
func Start(size int) error {
cli, _ := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
ctx := context.Background()
containers.dockerCLI = cli
containers.workDir, _ = filepath.Abs(WordDirAbs)
reader, err := cli.ImagePull(ctx, Image, image.PullOptions{})
if err != nil {
log.Printf("Unable to pull aerospike image: %s", err)
return err
}
defer reader.Close()
_, err = io.Copy(os.Stdout, reader)
if err != nil {
log.Printf("Unable to pull aerospike image: %s", err)
return err
}
for i := 0; i < size; i++ {
peerConnection := ""
if i > 0 {
peerConnection = fmt.Sprintf("mesh-seed-address-port %s %d", IP, PortStart+2)
}
name := GetAerospikeContainerName(i)
RmAerospikeContainer(name) //nolint:errcheck // Removing containers just in case they were left over from a previous run
asContainer, err := RunAerospikeContainer(i, name, IP, PortStart+(i*4), peerConnection)
if err != nil {
log.Printf("Unable to start testing containers")
return err
}
containers.namesToContainers[name] = asContainer
}
return nil
}
func Stop() error {
log.Println("Stopping test containers")
for name := range containers.namesToContainers {
err := RmAerospikeContainer(name)
if err != nil {
log.Printf("Unable to remove container %s: %s", name, err)
return err
}
}
abs, _ := filepath.Abs(containers.workDir)
err := os.RemoveAll(abs)
if err != nil {
log.Printf("Unable to remove work directory: %s", err)
return err
}
return nil
}
func GetAerospikeContainerName(index int) string {
return ContainerPrefix + fmt.Sprintf("%d", index)
}
func createConfigFile(portBase int, accessAddress, peerConnection string) (string, error) {
templateInput := struct {
FeaturePath string
AccessAddress string
PeerConnection string
Namespace string
ServicePort int
HeartbeatPort int
FabricPort int
InfoPort int
}{
// Uncomment if multi-node EE tests are needed
// FeaturePath: "/opt/aerospike/features.conf",
AccessAddress: accessAddress,
PeerConnection: peerConnection,
Namespace: "test",
ServicePort: portBase,
HeartbeatPort: portBase + 1,
FabricPort: portBase + 2,
InfoPort: portBase + 3,
}
tmpl, _ := template.New("config").Parse(configTemplate)
err := os.MkdirAll(containers.workDir, 0o755)
if err != nil {
log.Printf("Unable to create work directory: %s", err)
return "", err
}
file, err := os.CreateTemp(containers.workDir, "aerospike_*.conf")
if err != nil {
log.Printf("Unable to create config file: %s", err)
return "", err
}
defer file.Close()
err = tmpl.Execute(file, templateInput)
if err != nil {
log.Printf("Unable to create config file using template: %s", err)
return "", err
}
return file.Name(), nil
}
func waitForASDToStart(name string) error {
startTime := time.Now()
timeout := 10 * time.Second
policy := aerospike.NewClientPolicy()
policy.User = User
policy.Password = Password
for {
asClient, err := aerospike.NewClientWithPolicy(
policy, IP, PortStart)
if err == nil {
if asClient.IsConnected() {
break
}
asClient.Close()
}
if time.Since(startTime) > timeout {
log.Printf("Timed out waiting for asd %s to start %s", name, err)
return err
}
log.Printf("Waiting for asd %s to start %s", name, err)
time.Sleep(1 * time.Second)
}
log.Printf("asd %s started", name)
return nil
}
func RunAerospikeContainer(
index int,
name,
ip string,
portBase int,
peerConnection string) (*AerospikeContainer, error) {
ctx := context.Background()
cli := containers.dockerCLI
log.Printf("Starting container %s", name)
confFile, err := createConfigFile(portBase, ip, peerConnection)
if err != nil {
log.Printf("Unable to create config file for container %s: %s", name, err)
return nil, err
}
containerWorkDir := "/opt/" + containers.workDir
containerConfFile := containerWorkDir + "/" + filepath.Base(confFile)
cmd := []string{
"/usr/bin/asd",
"--foreground",
"--config-file",
containerConfFile,
"--instance",
fmt.Sprintf("%d", index),
}
// Uncomment if multi-node EE tests are needed
// featKey := os.Getenv("FEATKEY")
// if featKey == "" {
// log.Printf("Env var FEATKEY must be set with a base64 encoded feature key file.")
// return "", fmt.Errorf("env var FEATKEY must be set with a base64 encoded feature key file")
// }
ports := make([]string, 4)
for i := 0; i < 4; i++ {
ports[i] = fmt.Sprintf("%d/tcp", portBase+i)
}
config := container.Config{
Image: Image,
Hostname: ip,
Cmd: cmd,
Tty: true,
// Uncomment if multi-node EE tests are needed
// Env: []string{fmt.Sprintf("FEATURES=%s", featKey)},
ExposedPorts: nat.PortSet{
nat.Port(ports[0]): {},
nat.Port(ports[1]): {},
nat.Port(ports[2]): {},
nat.Port(ports[3]): {},
},
}
if err != nil {
log.Printf("Unable to get absolute path for work directory: %s", err)
return nil, err
}
hostConfig := &container.HostConfig{
PortBindings: nat.PortMap{
nat.Port(ports[0]): []nat.PortBinding{{
HostPort: ports[0],
}}, nat.Port(ports[1]): []nat.PortBinding{{
HostPort: ports[1],
}}, nat.Port(ports[2]): []nat.PortBinding{{
HostPort: ports[2],
}}, nat.Port(ports[3]): []nat.PortBinding{{
HostPort: ports[3],
}},
},
Mounts: []mount.Mount{
{Type: mount.TypeBind, Source: containers.workDir, Target: containerWorkDir},
},
}
if _, err = cli.ContainerCreate(ctx, &config, hostConfig, nil, nil, name); err != nil {
log.Printf("Unable to create container %s: %s", name, err)
return nil, err
}
err = cli.ContainerStart(ctx, name, container.StartOptions{})
if err != nil {
log.Printf("Unable to start container %s: %s", name, err)
return nil, err
}
inspect, _ := cli.ContainerInspect(ctx, name)
log.Printf("Started container %s with IP %s", name, inspect.NetworkSettings.IPAddress)
log.Printf("Waiting for asd %s to start", name)
if waitForASDToStart(name) != nil {
return nil, err
}
return &AerospikeContainer{inspect.NetworkSettings.IPAddress, confFile, portBase}, nil
}
func RestartAerospikeContainer(name, confFileContents string) error {
cli := containers.dockerCLI
ctx := context.Background()
ip := containers.namesToContainers[name].ip
log.Printf("Restarting container %s with IP %s", name, ip)
if confFileContents != "" {
confPath := containers.namesToContainers[name].configPath
file, err := os.OpenFile(confPath, os.O_TRUNC|os.O_WRONLY, 0o644)
if err != nil {
log.Printf("Unable to open config file %s: %s", confPath, err)
return err
}
_, err = file.WriteString(confFileContents)
if err != nil {
log.Printf("Unable to write to config file %s: %s", confPath, err)
return err
}
}
err := cli.ContainerRestart(ctx, name, container.StopOptions{})
if err != nil {
log.Printf("Unable to restart container %s: %s", name, err)
return err
}
log.Printf("Restarted container %s with IP %s", name, ip)
log.Printf("Waiting for asd %s to start", name)
if waitForASDToStart(name) != nil {
return err
}
return nil
}
func RmAerospikeContainer(name string) error {
cli := containers.dockerCLI
ctx := context.Background()
return cli.ContainerRemove(ctx, name, container.RemoveOptions{Force: true})
}