-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
118 lines (99 loc) · 2.74 KB
/
main.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
package main
import "github.com/samuel/go-zookeeper/zk"
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"strings"
"time"
)
//Configuration is exported
//zookeeper cluster parameters.
type Configuration struct {
Zookeeper struct {
Hosts string `json:"hosts"`
Root string `json:"root"`
} `json:"zookeeper"`
ServerConfig struct {
WebsiteHost string `json:"websitehost"`
CenterHost string `json:"centerhost"`
StorageDriver map[string]interface{} `json:"storagedriver"`
} `json:"serverconfig"`
}
var (
zkFlags = int32(0)
zkACL = zk.WorldACL(zk.PermAll)
zkConnTimeout = time.Second * 15
)
func initServerConfigData(conf *Configuration) (string, []byte, error) {
hosts := strings.Split(conf.Zookeeper.Hosts, ",")
conn, event, err := zk.Connect(hosts, zkConnTimeout)
if err != nil {
return "", nil, err
}
defer conn.Close()
<-event
if ret, _, _ := conn.Exists(conf.Zookeeper.Root); !ret {
if _, err := conn.Create(conf.Zookeeper.Root, []byte{}, zkFlags, zkACL); err != nil {
return "", nil, fmt.Errorf("zookeeper root: %s failure", conf.Zookeeper.Root)
}
}
serverConfigPath := conf.Zookeeper.Root + "/ServerConfig"
ret, _, err := conn.Exists(serverConfigPath)
if err != nil {
return "", nil, err
}
buf := bytes.NewBuffer([]byte{})
if err = json.NewEncoder(buf).Encode(conf.ServerConfig); err != nil {
return "", nil, err
}
data := buf.Bytes()
if !ret {
if _, err := conn.Create(serverConfigPath, data, zkFlags, zkACL); err != nil {
return "", nil, err
}
} else {
if _, err := conn.Set(serverConfigPath, data, -1); err != nil {
return "", nil, err
}
}
return serverConfigPath, data, nil
}
func readConfiguration(configFile string) (*Configuration, error) {
data, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, err
}
conf := &Configuration{}
err = json.NewDecoder(bytes.NewBuffer(data)).Decode(conf)
if err != nil {
return nil, err
}
return conf, nil
}
func main() {
var configFile string
flag.StringVar(&configFile, "f", "./ServerConfig.json", "server config file path.")
flag.Parse()
conf, err := readConfiguration(configFile)
if err != nil {
fmt.Printf("server config file invalid, %s", err)
return
}
if ret := strings.HasPrefix(conf.Zookeeper.Root, "/"); !ret {
conf.Zookeeper.Root = "/" + conf.Zookeeper.Root
}
if ret := strings.HasSuffix(conf.Zookeeper.Root, "/"); ret {
conf.Zookeeper.Root = strings.TrimSuffix(conf.Zookeeper.Root, "/")
}
path, data, err := initServerConfigData(conf)
if err != nil {
fmt.Printf("init server config failure, %s", err)
return
}
fmt.Printf("zookeeper path: %s\n", path)
fmt.Printf("serverconfig: %s\n", string(data))
fmt.Printf("initconfig to zookeeper successed!\n")
}