fix
sunty
2020-10-23 e189b1942c8130d473a1a4128c3d9ef5edfaa564
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package esutil
 
import (
    "bytes"
    "encoding/json"
    "errors"
    "fmt"
    "github.com/spf13/viper"
    "io/ioutil"
    "net/http"
    "os/exec"
    "strings"
    "time"
)
 
//初始化配置文件
func InitYml(configPath string) (bool, error) {
    v := viper.New()
    v.SetConfigType("yml")
    v.SetConfigName("elasticsearch")
    v.AddConfigPath(configPath)
    err := v.ReadInConfig()
    if err != nil {
        return false, err
    }
    if err := v.WriteConfig(); err != nil {
        return false, err
    }
    return true, nil
}
 
//初始化索引
func InitIndex(indexPath string) (bool) {
    resultMsg := CMDSC("sh " + indexPath + "/indexInit.sh")
    if resultMsg == "运行失败" {
        return false
    }
    return true
}
 
//启动服务
func StartServer(binPath string, ip string, port string) bool {
    resultMsg := CMDSC("sh " + binPath + "/es_start.sh")
    if resultMsg == "运行失败" {
        return false
    }
    return true
}
 
//关闭服务
func StopServer(binPath string, ip string, port string) bool {
    b := VerifyShortNodeServer(ip, port)
    if b == true {
        resultMsg := CMDSC("sh " + binPath + "/es_stop.sh")
        if resultMsg == "运行失败" {
            return true
        }
        time.Sleep(time.Second * 3)
    }
    bs := VerifyShortNodeServer(ip, port)
    return bs
}
 
//更换节点角色
func UpdateNodeRole(configPath string, role string) (bool, error) {
    v := viper.New()
    v.SetConfigType("yml")
    v.SetConfigName("elasticsearch")
    v.AddConfigPath(configPath)
    err := v.ReadInConfig()
    if err != nil {
        return false, err
    }
    if role == "master" {
        v.Set("node.master", true)
    } else if role == "slave" {
        v.Set("node.master", false)
    }
    if err := v.WriteConfig(); err != nil {
        return false, err
    }
    return true, nil
}
 
//验证该节点是否被使用过
func VerifyCreated(configPath string) (bool, error) {
    v := viper.New()
    v.SetConfigType("yml")
    v.SetConfigName("elasticsearch")
    v.AddConfigPath(configPath)
    err := v.ReadInConfig()
    if err != nil {
        return true, err
    }
    hosts := v.GetStringSlice("discovery.zen.ping.unicast.hosts")
    if len(hosts) > 1 {
        return true, errors.New("该节点已经被使用")
    }
    return false, nil
}
 
//验证节点服务是否正常启动
func VerifyNodeServer(ip string, port string, waitTime int) bool {
    b := false
    url := "http://" + ip + ":" + port
    for i := 1; i < 3; i++ {
        _, err := HttpRC("GET", url, nil)
        if err != nil {
            b = false
            if i < 3 {
                time.Sleep(time.Second * time.Duration(waitTime))
                continue
            }
        } else {
            b = true
            break
        }
    }
    return b
}
 
//验证节点服务是否正常启动(短验证)
func VerifyShortNodeServer(ip string, port string) bool {
    url := "http://" + ip + ":" + port
    _, err := HttpRC("GET", url, nil)
    if err != nil {
        return false
    }
    return true
}
 
//验证节点角色
func VerifyNodeRole(configPath string, ) (string, error) {
    role := "slave"
    v := viper.New()
    v.SetConfigType("yml")
    v.SetConfigName("elasticsearch")
    v.AddConfigPath(configPath)
    err := v.ReadInConfig()
    if err != nil {
        return role, err
    }
    flag := v.GetBool("node.master")
    if flag == true {
        role = "master"
    }
    return role, nil
}
 
//查询组播列表
func GetDiscoveryZenPingUnicastHosts(configPath string) ([]string, error) {
    v := viper.New()
    v.SetConfigType("yml")
    v.SetConfigName("elasticsearch")
    v.AddConfigPath(configPath)
    err := v.ReadInConfig()
    if err != nil {
        return nil, err
    }
    hosts := v.GetStringSlice("discovery.zen.ping.unicast.hosts")
    return hosts, nil
}
 
//设置组播列表
func SetDiscoveryZenPingUnicastHosts(configPath string, hosts []string) (bool, error) {
    v := viper.New()
    v.SetConfigType("yml")
    v.SetConfigName("elasticsearch")
    v.AddConfigPath(configPath)
    err := v.ReadInConfig()
    if err != nil {
        return false, err
    }
    v.Set("discovery.zen.ping.unicast.hosts", hosts)
    if err := v.WriteConfig(); err != nil {
        return false, err
    }
    return true, nil
}
 
//更新组播列表
func UpdateDiscoveryZenPingUnicastHosts(configPath string, oldIp string, newIp string) (bool, error) {
    v := viper.New()
    v.SetConfigType("yml")
    v.SetConfigName("elasticsearch")
    v.AddConfigPath(configPath)
    err := v.ReadInConfig()
    if err != nil {
        return false, err
    }
    flag := false
    hosts := v.GetStringSlice("discovery.zen.ping.unicast.hosts")
    for i, pick := range hosts {
        if pick == oldIp {
            hosts[i] = newIp
            flag = true
        }
    }
    if flag == false {
        fmt.Println("要修改的ip不在列表中")
    }
    v.Set("discovery.zen.ping.unicast.hosts", hosts)
    if err := v.WriteConfig(); err != nil {
        return false, err
    }
    return true, nil
}
 
//添加组播成员
func AddDiscoveryZenPingUnicastHosts(configPath string, ip string) (bool, error) {
    v := viper.New()
    v.SetConfigType("yml")
    v.SetConfigName("elasticsearch")
    v.AddConfigPath(configPath)
    errR := v.ReadInConfig()
    if errR != nil {
        return false, errR
    }
    hosts := v.GetStringSlice("discovery.zen.ping.unicast.hosts")
    hosts = append(hosts, ip)
    v.Set("discovery.zen.ping.unicast.hosts", hosts)
    if errW := v.WriteConfig(); errW != nil {
        return false, errW
    }
    return true, nil
}
 
type NodeInfo struct {
    NodeIp     string `json:"nodeIp"`
    NodeRole   string `json:"nodeRole"`
    NodeMaster bool   `json:"nodeMaster"`
    NodeName   string `json:"nodeName"`
}
 
//查询集群信息
func GetClusterInfo(ip string, port string) ([]NodeInfo, error) {
    url := "http://" + ip + ":" + port + "/_cat/nodes?v"
    buf, err := HttpRC("GET", url, nil)
    if err != nil {
        return nil, err
    }
    var inf = []NodeInfo{}
    res := strings.Split(string(buf), "\n")[1:]
    for _, r := range res {
        if r != "" {
            inx := strings.Fields(r)
            ip := inx[0]
            role := "slave"
            if find := strings.Contains(inx[7], "m"); find {
                role = "master"
            }
            master := false
            if find := strings.Contains(inx[8], "*"); find {
                master = true
            }
            name := inx[9]
            inf = append(inf, NodeInfo{
                NodeName:   name,
                NodeRole:   role,
                NodeIp:     ip,
                NodeMaster: master,
            })
        }
    }
    return inf, nil
}
 
//排除即将退出集群的节点
func ExcludeNode(ip string, port string) (bool, error) {
    flag := false
    url := "http://" + ip + ":" + port + "/_cluster/settings"
    body := `{
                "transient":{
                    "cluster.routing.allocation.exclude._ip":"` + ip + `"
                }
            }`
    buf, err := HttpRC("PUT", url, []byte(body))
    if err != nil {
        return false, err
    }
    var inf map[string]interface{}
    json.Unmarshal(buf, &inf)
    if inf["acknowledged"] != nil {
        flag = inf["acknowledged"].(bool)
    }
    return flag, nil
}
 
func CMDSC(scriptStr string) string {
    cmd := exec.Command("sh", "-c", scriptStr)
    var out bytes.Buffer
    cmd.Stdout = &out
    err := cmd.Run()
    if err != nil {
        return "运行失败"
    }
    return out.String()
}
 
func HttpRC(method string, url string, parama []byte) (buf []byte, err error) {
    timeout := time.Duration(10 * time.Second)
    client := http.Client{
        Timeout: timeout,
    }
    request, err := http.NewRequest(method, url, bytes.NewBuffer(parama))
    request.Header.Set("Content-type", "application/json")
    if err != nil {
        fmt.Println("build request fail !")
        return nil, err
    }
 
    resp, err := client.Do(request)
    if err != nil {
        fmt.Println("request error: ", err)
        return nil, err
    }
 
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
        return nil, err
    }
    return body, nil
}