From a564ed60bc1a6824bcfa504432e2b6bfb0992d4a Mon Sep 17 00:00:00 2001
From: sunty <1172534965@qq.com>
Date: 星期三, 30 十月 2019 15:53:46 +0800
Subject: [PATCH] add es cluster manger
---
extend/esutil/EsClient.go | 15 +++++++
controllers/es.go | 57 ++++++++++++++++++++++++++++
2 files changed, 71 insertions(+), 1 deletions(-)
diff --git a/controllers/es.go b/controllers/es.go
index 93d686f..d67a80b 100644
--- a/controllers/es.go
+++ b/controllers/es.go
@@ -195,7 +195,7 @@
nodeInfo["ip"] = nodeIp
nodeInfo["nodeType"] = nodeType
url := "http://"+nodeIp+":9200"
- buf := esutil.httpGet(url)
+ buf := esutil.HttpGet(url)
var info interface{}
json.Unmarshal(buf,&info)
tmpInfo := info.(map[string]interface{})
@@ -209,3 +209,58 @@
return nodeInfos,nil
}
+
+func AddEsCluster(hosts []string) (string){
+ msg := "鍔犲叆澶辫触"
+ for i,val := range hosts{
+ val =val+":9300"
+ hosts[i] = val
+ }
+ verificationHosts := "[\""+strings.Replace(strings.Trim(fmt.Sprint(hosts), "[]"), " ", "\",\"", -1)+"\"]"
+ for i,val := range hosts{
+ val ="\\\""+val+"\\\""
+ hosts[i] = val
+ }
+ oldUnicastHost := "\\[\\\"0.0.0.0:9300\\\"\\]"
+ newUnicastHost := strings.Replace(strings.Trim(fmt.Sprint(hosts), ""), " ", ",", -1)
+ str := "sed -ie 's/discovery.zen.ping.unicast.hosts: "+oldUnicastHost+"/discovery.zen.ping.unicast.hosts: "+newUnicastHost+"/g' /opt/elasticsearch/config/elasticsearch.yml"
+ fmt.Println(str)
+ cmd := exec.Command("sh","-c",str)
+ var out bytes.Buffer
+ cmd.Stdout = &out
+ err := cmd.Run()
+ if err != nil {
+
+ }
+ res := getUnicastHosts()
+ fmt.Println("res: ",res)
+ res1 := "discovery.zen.ping.unicast.hosts: "+verificationHosts+""
+ fmt.Println("res1: ",res1)
+ if res == res1{
+ msg = "鍔犲叆鎴愬姛"
+ }
+ str2 := "echo \"node.master: true\" >> /opt/elasticsearch/config/elasticsearch.yml"
+ cmd2 := exec.Command("sh","-c",str2)
+ var out2 bytes.Buffer
+ cmd2.Stdout = &out2
+ err2 := cmd2.Run()
+ if err2 != nil {
+ msg = "鍔犲叆澶辫触"
+ }
+ return msg
+
+}
+
+func getUnicastHosts() (string){
+ str := "cat /opt/elasticsearch/config/elasticsearch.yml | grep discovery.zen.ping.unicast.hosts:"
+ cmd := exec.Command("sh","-c",str)
+ var out bytes.Buffer
+ cmd.Stdout = &out
+ err := cmd.Run()
+ if err != nil {
+
+ }
+ infos := strings.Split(string(out.String()),"\n")[0]
+ return infos
+
+}
diff --git a/extend/esutil/EsClient.go b/extend/esutil/EsClient.go
index bd8a28f..0fb6e16 100644
--- a/extend/esutil/EsClient.go
+++ b/extend/esutil/EsClient.go
@@ -303,3 +303,18 @@
}
return sources, nil
}
+
+
+func HttpGet(str string) ([]byte){
+ resp, err := http.Get(str)
+ if err != nil {
+ // handle error
+ }
+
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ // handle error
+ }
+ return body
+}
\ No newline at end of file
--
Gitblit v1.8.0