-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontainers.go
123 lines (105 loc) · 2.81 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
package main
import (
"fmt"
"io"
"os"
"github.com/docker/docker/api/types"
pepe "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"golang.org/x/net/context"
)
func buildContainer(image string, nodes int) {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.WithVersion("1.35"))
// cli, err := client.NewClientWithOpts(client.FromEnv)
fmt.Print(cli.ClientVersion())
if err != nil {
panic(err)
}
conf := pepe.Config{
Image: image,
Tty: true,
}
bindings := nat.PortMap{}
emptypaths := []string{}
restartpol := pepe.RestartPolicy{
Name: "no",
}
for i := start; i < nodes+start; i++ {
fmt.Printf("Building Node %d...\n", i)
vlan := fmt.Sprintf("vlan%d", i)
hostConf := pepe.HostConfig{
NetworkMode: pepe.NetworkMode(vlan),
PublishAllPorts: false,
PortBindings: bindings,
ReadonlyPaths: emptypaths,
MaskedPaths: emptypaths,
DNS: emptypaths,
DNSOptions: emptypaths,
DNSSearch: emptypaths,
RestartPolicy: restartpol,
}
resp, err := cli.ContainerCreate(ctx, &conf, &hostConf, nil, fmt.Sprintf("node%d", i))
if err != nil {
panic(err)
}
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, pepe.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
panic(err)
}
case <-statusCh:
}
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
if err != nil {
panic(err)
}
io.Copy(os.Stdout, out)
}
}
func startContainer(start, nodes int) {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.WithVersion("1.35"))
// cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
for i := start; i < nodes+start; i++ {
fmt.Printf("Starting Node %d...\n", i)
if err := cli.ContainerStart(ctx, fmt.Sprintf("node%d", i), types.ContainerStartOptions{}); err != nil {
panic(err)
}
}
}
func joinNetwork(start, nodes int) {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.WithVersion("1.35"))
if err != nil {
panic(err)
}
for i := start; i < nodes+start; i++ {
var nodeName = fmt.Sprintf("node%d", i)
var networkName = fmt.Sprintf("vlan%d", i)
epSettings := network.EndpointSettings{}
cli.NetworkConnect(ctx, networkName, nodeName, &epSettings)
}
}
func deleteContainer(start, nodes int) {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.WithVersion("1.35"))
if err != nil {
panic(err)
}
fmt.Printf("Removing Container %d...\n", nodes)
for i := start; i < nodes+start; i++ {
cli.ContainerRemove(ctx, fmt.Sprintf("node%d", i), types.ContainerRemoveOptions{
Force: true,
})
if err != nil {
panic(err)
}
}
}