This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
orchestrator.go
76 lines (66 loc) · 2.39 KB
/
orchestrator.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
package nsq
import (
"fmt"
"log"
"os/exec"
)
const (
nsqlookupd = "nsqio/nsqlookupd"
nsqlookupdPort1 = "4160"
nsqlookupdPort2 = "4161"
nsqlookupdCmd = "docker run -d -p %s:%s -p %s:%s %s"
nsqd = "nsqio/nsqd"
internalPort = "4150"
nsqdPort = "4151"
nsqdCmd = `docker run -d -p %s:%s -p %s:%s %s \
--broadcast-address=%s \
--lookupd-tcp-address=%s:%s`
)
// Broker is an implementation of the broker interface which handles
// orchestrating NSQ.
type Broker struct {
nsqlookupdContainerID string
nsqdContainerID string
}
// Start will start the message broker and prepare it for testing.
func (n *Broker) Start(host, port string) (interface{}, error) {
if port == nsqlookupdPort1 || port == nsqlookupdPort2 || port == nsqdPort {
return nil, fmt.Errorf("Port %s is reserved", port)
}
cmd := fmt.Sprintf(nsqlookupdCmd, nsqlookupdPort1, nsqlookupdPort1, nsqlookupdPort2,
nsqlookupdPort2, nsqlookupd)
nsqlookupdContainerID, err := exec.Command("/bin/sh", "-c", cmd).Output()
if err != nil {
log.Printf("Failed to start container %s: %s", nsqlookupd, err.Error())
return "", err
}
log.Printf("Started container %s: %s", nsqlookupd, nsqlookupdContainerID)
cmd = fmt.Sprintf(nsqdCmd, port, internalPort, nsqdPort, nsqdPort, nsqd, host,
host, nsqlookupdPort1)
nsqdContainerID, err := exec.Command("/bin/sh", "-c", cmd).Output()
if err != nil {
log.Printf("Failed to start container %s: %s", nsqd, err.Error())
return "", err
}
log.Printf("Started container %s: %s", nsqd, nsqdContainerID)
n.nsqlookupdContainerID = string(nsqlookupdContainerID)
n.nsqdContainerID = string(nsqdContainerID)
return string(nsqdContainerID), nil
}
// Stop will stop the message broker.
func (n *Broker) Stop() (interface{}, error) {
_, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("docker kill %s", n.nsqlookupdContainerID)).Output()
if err != nil {
log.Printf("Failed to stop container %s: %s", nsqlookupd, err.Error())
} else {
log.Printf("Stopped container %s: %s", nsqlookupd, n.nsqlookupdContainerID)
}
nsqdContainerID, e := exec.Command("/bin/sh", "-c", fmt.Sprintf("docker kill %s", n.nsqdContainerID)).Output()
if e != nil {
log.Printf("Failed to stop container %s: %s", nsqd, err.Error())
err = e
} else {
log.Printf("Stopped container %s: %s", nsqd, n.nsqdContainerID)
}
return string(nsqdContainerID), err
}