-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.go
130 lines (107 loc) · 3.07 KB
/
boot.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
package main
import (
"io/ioutil"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/deis/deis/mesos/bindata/zookeeper"
"github.com/deis/deis/mesos/pkg/boot/zookeeper"
"github.com/deis/deis/mesos/pkg/confd"
"github.com/deis/deis/mesos/pkg/etcd"
logger "github.com/deis/deis/mesos/pkg/log"
oswrapper "github.com/deis/deis/mesos/pkg/os"
"github.com/deis/deis/version"
)
var (
etcdPath = oswrapper.Getopt("ETCD_PATH", "/zookeeper/nodes")
log = logger.New()
signalChan = make(chan os.Signal, 1)
)
func main() {
host := oswrapper.Getopt("HOST", "127.0.0.1")
etcdPort := oswrapper.Getopt("ETCD_PORT", "4001")
etcdCtlPeers := oswrapper.Getopt("ETCD_PEERS", "127.0.0.1:"+etcdPort)
etcdURL := etcd.GetHTTPEtcdUrls(host+":"+etcdPort, etcdCtlPeers)
etcdClient := etcd.NewClient(etcdURL)
etcd.Mkdir(etcdClient, etcdPath)
log.Infof("boot version [%v]", version.Version)
log.Info("zookeeper: starting...")
zookeeper.CheckZkMappingInFleet(etcdPath, etcdClient, etcdURL)
// we need to write the file /opt/zookeeper-data/data/myid with the id of this node
os.MkdirAll("/opt/zookeeper-data/data", 0640)
zkID := etcd.Get(etcdClient, etcdPath+"/"+host+"/id")
ioutil.WriteFile("/opt/zookeeper-data/data/myid", []byte(zkID), 0640)
zkServer := &zookeeper.ZkServer{
Stdout: os.Stdout,
Stderr: os.Stderr,
}
signal.Notify(signalChan,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGKILL,
syscall.SIGTERM,
syscall.SIGQUIT,
os.Interrupt,
)
// Wait for a signal and exit
exitChan := make(chan int)
go func() {
for {
s := <-signalChan
log.Debugf("Signal received: %v", s)
switch s {
case syscall.SIGTERM:
exitChan <- 0
case syscall.SIGQUIT:
exitChan <- 0
case syscall.SIGKILL:
exitChan <- 1
default:
exitChan <- 1
}
}
}()
// wait for confd to run once and install initial templates
confd.WaitForInitialConf(getConfdNodes(host, etcdCtlPeers, 4001), 10*time.Second)
params := make(map[string]string)
params["HOST"] = host
if log.Level.String() == "debug" {
params["DEBUG"] = "true"
}
err := oswrapper.RunScript("pkg/boot/zookeeper/bash/add-node.bash", params, bindata.Asset)
if err != nil {
log.Printf("command finished with error: %v", err)
}
if err := zkServer.Start(); err != nil {
panic(err)
}
log.Info("zookeeper: running...")
go func() {
log.Debugf("starting pprof http server in port 6060")
http.ListenAndServe("localhost:6060", nil)
}()
code := <-exitChan
log.Debugf("execution terminated with exit code %v", code)
log.Debugf("executing pre shutdown script")
err = oswrapper.RunScript("pkg/boot/zookeeper/bash/remove-node.bash", params, bindata.Asset)
if err != nil {
log.Printf("command finished with error: %v", err)
}
log.Info("stopping zookeeper node")
zkServer.Stop()
}
func getConfdNodes(host, etcdCtlPeers string, port int) []string {
result := []string{host + ":" + strconv.Itoa(port)}
if etcdCtlPeers != "127.0.0.1" {
hosts := strings.Split(etcdCtlPeers, ",")
result = []string{}
for _, _host := range hosts {
result = append(result, _host)
}
}
return result
}