From b8e48712c2a521225d7f2ef7a982f766a6005615 Mon Sep 17 00:00:00 2001
From: sunty <1172534965@qq.com>
Date: 星期四, 07 十一月 2019 19:21:04 +0800
Subject: [PATCH] add updateEsHosts

---
 controllers/es.go |   99 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 88 insertions(+), 11 deletions(-)

diff --git a/controllers/es.go b/controllers/es.go
index 46cacf6..bdc4d3a 100644
--- a/controllers/es.go
+++ b/controllers/es.go
@@ -26,6 +26,7 @@
 type EsClusterInfo struct {
 	Ip string `json:"ip"`
 }
+
 // @Security ApiKeyAuth
 // @Summary 姣斿鏁版嵁鏌ヨ
 // @Description  姣斿鏁版嵁鏌ヨ
@@ -43,28 +44,28 @@
 		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎")
 		return
 	}
-	if searchBody.CompareNum !="" {
+	if searchBody.CompareNum != "" {
 		//浜屾鎼滅储锛屼笉闇�瑕佸啀姣斿浜�
 		co := service.GetCompResultByNum(searchBody.CompareNum)
 		if co != nil {
 			//浜屾鎼滅储鍜屾帓搴�
-			twiceM := GetCompareDataTwice(co,searchBody)
-			util.ResponseFormat(c,code.Success,twiceM)
+			twiceM := GetCompareDataTwice(co, searchBody)
+			util.ResponseFormat(c, code.Success, twiceM)
 			return
 		} else {
-			m :=make(map[string]interface{},0)
+			m := make(map[string]interface{}, 0)
 			m["compareNum"] = searchBody.CompareNum
 			m["total"] = 0
 			m["totalList"] = []CompareResult{}
-			util.ResponseFormat(c,code.CompareResultGone,m)
+			util.ResponseFormat(c, code.CompareResultGone, m)
 			return
 		}
 	}
-	m :=make(map[string]interface{},0)
+	m := make(map[string]interface{}, 0)
 	m["compareNum"] = searchBody.CompareNum
 	m["total"] = 0
 	m["totalList"] = []CompareResult{}
-	util.ResponseFormat(c,code.CompareResultGone,m)
+	util.ResponseFormat(c, code.CompareResultGone, m)
 }
 
 func searchEsData(searchBody models.EsSearch) map[string]interface{} {
@@ -114,7 +115,7 @@
 	//浣跨敤es搴曞眰鏈哄埗澶勭悊鍒嗛〉
 	//璇锋眰澶�
 	localConf, err2 := cache.GetServerInfo()
-	if err2 !=nil || localConf.AlarmIp == "" || localConf.ServerId == "" {
+	if err2 != nil || localConf.AlarmIp == "" || localConf.ServerId == "" {
 		logger.Debug("localConfig is wrong!!!")
 		return nil
 	}
@@ -214,12 +215,12 @@
 		nodeInfo := make(map[string]interface{})
 		context := strings.Fields(infos[i])
 		nodeIp := context[0]
-		Type := context[8]
+		Type := context[7]
 		var nodeType string
-		if Type == "*" {
+		if Type == "mdi" {
 			nodeType = "涓昏妭鐐�"
 		}
-		if Type == "-" {
+		if Type == "di" {
 			nodeType = "浠庤妭鐐�"
 		}
 		nodeInfo["ip"] = nodeIp
@@ -382,6 +383,82 @@
 	return msg, err
 }
 
+//淇敼elasticsearch.yml鏄犲皠鏂囦欢
+// @Security ApiKeyAuth
+// @Summary 淇敼es闆嗙兢Ip
+// @Description  淇敼es闆嗙兢Ip
+// @Accept  json
+// @Produce json
+// @Tags es
+// @Param obj body controllers.Hosts true "淇敼es闆嗙兢Ip鍙傛暟"
+// @Success 200 {string} json "{"code":200, msg:"", success:true}"
+// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
+// @Router /data/api-v/es/updateEsHosts [POST]
+func (em *EsManagementController) UpdateEsHosts(c *gin.Context) {
+	var hosts Hosts
+	c.BindJSON(&hosts)
+	nodeInfos, err := getEsClusterInfors(hosts.OldIp)
+	var nodeType string
+	if err != nil {
+		logger.Fatal(err)
+	}
+	newMasterHosts := make([]string, 0)
+	allHosts := make([]string, 0)
+	for _, val := range nodeInfos {
+		ip := val["ip"].(string)
+		tmpType := val["nodeType"].(string)
+		if ip == hosts.OldIp {
+			nodeType = tmpType
+		}
+		if tmpType == "master" {
+			newMasterHosts = append(newMasterHosts, ip)
+		}
+		if ip != hosts.OldIp {
+			allHosts = append(allHosts, ip)
+		}
+	}
+
+	if nodeType == "master" {
+		str1 := "sed -ie 's/network.host: " + hosts.OldIp + "/network.host: " + hosts.NewIp + "/g' /opt/elasticsearch/config/elasticsearch.yml"
+		runScript(str1)
+		for i, host := range newMasterHosts {
+			if host == hosts.OldIp {
+				newMasterHosts[i] = hosts.NewIp
+			}
+		}
+		for i, val := range newMasterHosts {
+			val = "\\\"" + val + ":9300\\\""
+			newMasterHosts[i] = val
+		}
+		newUnicastHost := strings.Replace(strings.Trim(fmt.Sprint(newMasterHosts), ""), " ", ",", -1)
+
+		tmpStr := "cat /opt/elasticsearch/config/elasticsearch.yml | grep discovery.zen.ping.unicast.hosts:"
+		rs := runScript(tmpStr)
+		ts := strings.Split(rs, "\n")[0]
+		ots := strings.Split(ts, " ")[1]
+		outs := strings.Replace(ots, "\"", "\\\"", -1)
+		oldUnicastHost := "\\" + strings.Replace(outs, "]", "\\]", -1)
+		str2 := "sed -ie 's/discovery.zen.ping.unicast.hosts: " + oldUnicastHost + "/discovery.zen.ping.unicast.hosts: " + newUnicastHost + "/g' /opt/elasticsearch/config/elasticsearch.yml"
+		runScript(str2)
+		for _, host := range allHosts {
+			str3 := "sshpass -p \"123\" ssh basic@" + host + " \"cd /opt/elasticsearch/config  ; " + str2 + " ; cat elasticsearch.yml\""
+			runScript(str3)
+		}
+	} else {
+		str1 := "sed -ie 's/network.host: " + hosts.OldIp + "/network.host: " + hosts.NewIp + "/g' /opt/elasticsearch/config/elasticsearch.yml"
+		runScript(str1)
+	}
+}
+
+type Hosts struct {
+	NewIp string `json:newIp`
+	OldIp string `json:oldIp`
+}
+
+func forwardCommand() {
+
+}
+
 //func AddEsCluster(hosts []string) (string){
 //	msg := "鍔犲叆澶辫触"
 //	for i,val := range hosts{

--
Gitblit v1.8.0