Skip to content

Commit 853c4ce

Browse files
committed
add initconfig source.
1 parent b0496bc commit 853c4ce

4 files changed

Lines changed: 147 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212

1313
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
1414
.glide/
15+
16+
cloudtask-initconfig
17+
cloudtask-initconfig.exe

ServerConfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"websitehost": "192.168.2.80:8901",
3+
"centerhost": "192.168.2.80:8985",
4+
"storage": {
5+
"mongo": {
6+
"hosts": "192.168.2.80:27017,192.168.2.81:27017,192.168.2.82:27017",
7+
"database": "cloudtask",
8+
"auth": {
9+
"user": "datastoreAdmin",
10+
"password": "ds4dev"
11+
},
12+
"options": [
13+
"maxPoolSize=20",
14+
"replicaSet=mgoCluster",
15+
"authSource=admin"
16+
]
17+
}
18+
}
19+
}

config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
zookeeper:
2+
hosts: 192.168.2.80:2181,192.168.2.81:2181,192.168.2.82:2181
3+
root: /cloudtask

main.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package main
2+
3+
import "github.com/samuel/go-zookeeper/zk"
4+
import yaml "gopkg.in/yaml.v2"
5+
6+
import (
7+
"flag"
8+
"fmt"
9+
"io/ioutil"
10+
"os"
11+
"strings"
12+
"time"
13+
)
14+
15+
//Configuration is exported
16+
//zookeeper cluster parameters.
17+
type Configuration struct {
18+
Zookeeper struct {
19+
Hosts string `yaml:"hosts" json:"hosts"`
20+
Root string `yaml:"root" json:"root"`
21+
} `yaml:"zookeeper" json:"zookeeper"`
22+
}
23+
24+
var (
25+
zkFlags = int32(0)
26+
zkACL = zk.WorldACL(zk.PermAll)
27+
zkConnTimeout = time.Second * 15
28+
)
29+
30+
func readConfiguration(file string) (*Configuration, error) {
31+
32+
fd, err := os.OpenFile(file, os.O_RDONLY, 0777)
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
defer fd.Close()
38+
buf, err := ioutil.ReadAll(fd)
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
conf := &Configuration{}
44+
if err := yaml.Unmarshal(buf, conf); err != nil {
45+
return nil, err
46+
}
47+
return conf, nil
48+
}
49+
50+
func initServerConfigData(hosts string, root string) (string, []byte, error) {
51+
52+
servers := strings.Split(hosts, ",")
53+
conn, event, err := zk.Connect(servers, zkConnTimeout)
54+
if err != nil {
55+
return "", nil, err
56+
}
57+
58+
<-event
59+
defer conn.Close()
60+
serverConfigPath := root + "/ServerConfig"
61+
ret, _, err := conn.Exists(serverConfigPath)
62+
if err != nil {
63+
return "", nil, err
64+
}
65+
66+
data, err := ioutil.ReadFile("./ServerConfig.json")
67+
if err != nil {
68+
return "", nil, fmt.Errorf("ServerConfig.json read failure, %s", err)
69+
}
70+
71+
if !ret {
72+
if _, err := conn.Create(serverConfigPath, data, zkFlags, zkACL); err != nil {
73+
return "", nil, err
74+
}
75+
} else {
76+
if _, err := conn.Set(serverConfigPath, data, -1); err != nil {
77+
return "", nil, err
78+
}
79+
}
80+
return serverConfigPath, data, nil
81+
}
82+
83+
func main() {
84+
85+
var (
86+
configFile string
87+
zkHosts string
88+
zkRoot string
89+
)
90+
91+
flag.StringVar(&configFile, "f", "./config.yaml", "coudtask initconfig etc.")
92+
flag.StringVar(&zkHosts, "hosts", "127.0.0.1:2181", "zookeeper hosts.")
93+
flag.StringVar(&zkRoot, "root", "/cloudtask", "zookeeper root path.")
94+
flag.Parse()
95+
96+
if configFile != "" {
97+
conf, err := readConfiguration(configFile)
98+
if err != nil {
99+
fmt.Errorf("config file invalid, %s", err)
100+
return
101+
}
102+
zkHosts = conf.Zookeeper.Hosts
103+
zkRoot = conf.Zookeeper.Root
104+
}
105+
106+
if ret := strings.HasPrefix(zkRoot, "/"); !ret {
107+
zkRoot = "/" + zkRoot
108+
}
109+
110+
if ret := strings.HasSuffix(zkRoot, "/"); ret {
111+
zkRoot = strings.TrimSuffix(zkRoot, "/")
112+
}
113+
114+
path, data, err := initServerConfigData(zkHosts, zkRoot)
115+
if err != nil {
116+
fmt.Errorf("init server config failure, %s", err)
117+
return
118+
}
119+
fmt.Printf("zookeeper path: %s\n", path)
120+
fmt.Printf("data: %s\n", string(data))
121+
fmt.Printf("init to zookeeper successed!\n")
122+
}

0 commit comments

Comments
 (0)