-
Notifications
You must be signed in to change notification settings - Fork 0
/
recreation.go
351 lines (284 loc) · 7.97 KB
/
recreation.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
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"net"
"os"
"strconv"
"time"
docker "github.com/fsouza/go-dockerclient"
"github.com/kelseyhightower/envconfig"
)
var config Config
type Config struct {
Host string `default:"127.0.0.1"`
ZKName string `default:"zookeeper-191"`
ZKPort string `default:""`
Topic string `default:"recreation"`
KafkaName string `default:"kafka-191"`
KafkaPort string `default:""`
NetworkName string `default:"issue-191"`
NetworkID string `default:""`
}
func getConfig(app_name string, s interface{}) error {
err := envconfig.Process(app_name, s)
return err
}
func getPort() int {
l, err := net.Listen("tcp", ":0")
if err != nil {
return 0
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}
func startZookeeperContainer(name string, port string) error {
var err error
var container *docker.Container
var authConfig docker.AuthConfiguration
repo := "solsson/zookeeper-statefulset"
tag := "3.4.9"
endpoint := "unix:///var/run/docker.sock"
client, _ := docker.NewClient(endpoint)
exposedPort := map[docker.Port]struct{}{
"2181/tcp": {},
}
createContConf := docker.Config{
ExposedPorts: exposedPort,
Image: fmt.Sprintf("%s:%s", repo, tag),
}
portBindings := map[docker.Port][]docker.PortBinding{
"2081/tcp": {{HostIP: "", HostPort: port}},
}
networkingConfig := docker.NetworkingConfig{
EndpointsConfig: map[string]*docker.EndpointConfig{
config.NetworkName: &docker.EndpointConfig{
NetworkID: config.NetworkID,
},
},
}
createContHostConfig := docker.HostConfig{
PortBindings: portBindings,
PublishAllPorts: false,
Privileged: false,
}
createContOps := docker.CreateContainerOptions{
Name: name,
Config: &createContConf,
HostConfig: &createContHostConfig,
NetworkingConfig: &networkingConfig,
}
pullImageOps := docker.PullImageOptions{
Repository: repo,
Tag: tag,
}
fmt.Println("Pulling Zookeeper Docker image.")
if err = client.PullImage(pullImageOps, authConfig); err != nil {
return err
}
fmt.Println("Creating Zookeeper container.")
container, err = client.CreateContainer(createContOps)
if err != nil {
return err
}
fmt.Println("Starting Zookeeper container.")
if err = client.StartContainer(container.ID, nil); err != nil {
return err
}
return nil
}
func startKafkaContainer(name string, port string, zookeeper string) error {
var err error
var container *docker.Container
var authConfig docker.AuthConfiguration
repo := "solsson/kafka"
tag := "1.0.0"
endpoint := "unix:///var/run/docker.sock"
client, _ := docker.NewClient(endpoint)
exposedPort := map[docker.Port]struct{}{
"9092/tcp": {},
}
var command bytes.Buffer
command.WriteString("./bin/kafka-server-start.sh")
command.WriteString(" config/server.properties")
command.WriteString(" --override broker.id=0")
command.WriteString(" --override auto.create.topics.enable=true")
command.WriteString(" --override num.partitions=1")
command.WriteString(" --override log.dirs=/opt/kafka/data/logs")
command.WriteString(" --override socket.request.max.bytes=318762506")
command.WriteString(" --override log.roll.ms=30000")
command.WriteString(fmt.Sprintf(" --override listeners=PLAINTEXT://%s:9092", name))
command.WriteString(fmt.Sprintf(" --override advertised.listeners=PLAINTEXT://%s:9092", name))
command.WriteString(fmt.Sprintf(" --override zookeeper.connect=%s", zookeeper))
createContConf := docker.Config{
ExposedPorts: exposedPort,
Image: fmt.Sprintf("%s:%s", repo, tag),
Entrypoint: []string{"sh", "-c", command.String()},
}
portBindings := map[docker.Port][]docker.PortBinding{
"9092/tcp": {{HostIP: "", HostPort: port}},
}
networkingConfig := docker.NetworkingConfig{
EndpointsConfig: map[string]*docker.EndpointConfig{
config.NetworkName: &docker.EndpointConfig{
NetworkID: config.NetworkID,
},
},
}
createContHostConfig := docker.HostConfig{
PortBindings: portBindings,
PublishAllPorts: false,
Privileged: false,
}
createContOps := docker.CreateContainerOptions{
Name: name,
Config: &createContConf,
HostConfig: &createContHostConfig,
NetworkingConfig: &networkingConfig,
}
pullImageOps := docker.PullImageOptions{
Repository: repo,
Tag: tag,
}
fmt.Println("Pulling Kafka Docker image.")
if err = client.PullImage(pullImageOps, authConfig); err != nil {
return err
}
fmt.Println("Creating Kafka container.")
container, err = client.CreateContainer(createContOps)
if err != nil {
return err
}
fmt.Println("Starting Kafka container.")
if err = client.StartContainer(container.ID, nil); err != nil {
return err
}
return nil
}
func destroyDockerContainer(containerID string) error {
endpoint := "unix:///var/run/docker.sock"
client, _ := docker.NewClient(endpoint)
fmt.Printf("Stopping container %s.\n", containerID)
err := client.StopContainer(containerID, 30)
fmt.Printf("Removing container %s.\n", containerID)
err = client.RemoveContainer(docker.RemoveContainerOptions{
ID: containerID,
RemoveVolumes: true,
Force: true,
})
return err
}
func getContainer(name string) (*docker.APIContainers, error) {
endpoint := "unix:///var/run/docker.sock"
client, _ := docker.NewClient(endpoint)
containers, err := client.ListContainers(docker.ListContainersOptions{All: true})
if err != nil {
return nil, err
}
for _, i := range containers {
for _, n := range i.Names {
if n == fmt.Sprintf("/%s", name) {
return &i, nil
}
}
}
return nil, errors.New("no container found")
}
func createNetwork(name string) (*docker.Network, error) {
endpoint := "unix:///var/run/docker.sock"
client, _ := docker.NewClient(endpoint)
fmt.Printf("Creating network %s.\n", name)
return client.CreateNetwork(docker.CreateNetworkOptions{
Name: name,
})
}
func deleteNetwork(name string) error {
endpoint := "unix:///var/run/docker.sock"
client, _ := docker.NewClient(endpoint)
networks, err := client.ListNetworks()
if err != nil {
return err
}
for _, n := range networks {
if n.Name == name {
fmt.Printf("Removing network %s.\n", name)
err = client.RemoveNetwork(n.ID)
break
}
}
return err
}
func setup() error {
var err error
if _, err = createNetwork(config.NetworkName); err != nil {
fmt.Printf("Error creating the network: %v", err)
return err
}
config.ZKPort = strconv.Itoa(getPort())
err = startZookeeperContainer(config.ZKName, config.ZKPort)
if err != nil {
fmt.Printf("Error starting container: %v\n", err)
return err
}
config.KafkaPort = strconv.Itoa(getPort())
err = startKafkaContainer(config.KafkaName, config.KafkaPort, fmt.Sprintf("%s:2181", config.ZKName))
if err != nil {
fmt.Printf("Error starting container: %v\n", err)
return err
}
return nil
}
func tearDown() error {
apiContainer, err := getContainer(config.ZKName)
if err != nil {
return err
}
if err = destroyDockerContainer(apiContainer.ID); err != nil {
fmt.Printf("Unable to stop container: %v\n", err)
return err
}
apiContainer, err = getContainer(config.KafkaName)
if err != nil {
return err
}
if err = destroyDockerContainer(apiContainer.ID); err != nil {
fmt.Printf("Unable to stop container: %v\n", err)
return err
}
if err = deleteNetwork(config.NetworkName); err != nil {
fmt.Printf("Unable to delete network: %v\n", err)
}
return nil
}
func performRecreation() error {
fmt.Print("Press Enter to continue: ")
bufio.NewReader(os.Stdin).ReadBytes('\n')
return nil
}
func main() {
var err error
err = getConfig("confluent-kafka-go-191", &config)
if err != nil {
fmt.Printf("Error getting config values: %v\n", err)
return
}
err = setup()
if err != nil {
tearDown()
fmt.Printf("Error during setup.\n")
return
}
time.Sleep(20 * time.Second)
if err = performRecreation(); err != nil {
fmt.Printf("Error performing recreation: %v\n", err)
tearDown()
return
}
err = tearDown()
if err != nil {
fmt.Printf("Error during cleanup: %v\n", err)
return
}
}