-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathinit.go
More file actions
114 lines (94 loc) · 2.26 KB
/
init.go
File metadata and controls
114 lines (94 loc) · 2.26 KB
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
/*
* Xenon
*
* Copyright 2018 The Xenon Authors.
* Code is licensed under the GPLv3.
*
*/
package cmd
import (
"fmt"
"strings"
"xbase/common"
"github.com/spf13/cobra"
)
var (
addrStr string
vipStr string
replUserStr string
replPwdStr string
portInt int
)
func NewInitCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "init the xenon config file",
Long: `init the xenon config file.
affects:
1.set endpoint
2.set vip command
3.set repl user and pwd
`,
Run: initCommandFn,
}
cmd.Flags().StringVar(&addrStr, "address", "", "--address=<ip>")
cmd.Flags().IntVar(&portInt, "port", 0, "--port=<port>")
cmd.Flags().StringVar(&vipStr, "vip", "", "--vip=<vip>")
cmd.Flags().StringVar(&replUserStr, "repluser", "", "--repluser=<repluser>")
cmd.Flags().StringVar(&replPwdStr, "replpwd", "", "--replpwd=<replpwd>")
return cmd
}
// initCommandFn
func initCommandFn(cmd *cobra.Command, args []string) {
if !checkInitFlags() {
cmd.Usage()
return
}
eth, err := getEth(addrStr)
ErrorOK(err)
conf, err := GetConfig()
ErrorOK(err)
// server
conf.Server.Endpoint = fmt.Sprintf("%v:%v", addrStr, portInt)
// replication
conf.Replication.User = replUserStr
conf.Replication.Passwd = replPwdStr
// server
conf.Raft.LeaderStartCommand = fmt.Sprintf("ip a d %s/32 dev %s", vipStr, eth)
conf.Raft.LeaderStopCommand = fmt.Sprintf("ip a a %s/32 dev %s", vipStr, eth)
// backup
conf.Backup.SSHHost = addrStr
err = SaveConfig(conf)
ErrorOK(err)
}
func checkInitFlags() bool {
if addrStr == "" ||
portInt == 0 ||
vipStr == "" ||
replUserStr == "" ||
replPwdStr == "" {
log.Error("cmd.init.flags.address[%v].port[%v].vip[%v]",
addrStr, portInt, vipStr)
return false
}
return true
}
// get the eth via ip address
func getEth(ip string) (string, error) {
bash := "bash"
args := []string{
"-c",
fmt.Sprintf("ifconfig | grep -B 1 'inet addr:%s' | grep HWaddr | awk '{print $1}'", ip),
}
result, err := common.RunCommand(bash, args...)
if err != nil {
msg := fmt.Sprintf("cmd.init.getEth[%v].error[%v]", ip, err)
log.Error(msg)
return "", fmt.Errorf(msg)
}
ret := strings.TrimSpace(result)
if ret == "" {
return "", fmt.Errorf("getEth[%v].can.not.found.eth", ip)
}
return ret, nil
}