From b5e236e28dee3e9481a48c615be22d70df44a42c Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期二, 19 十一月 2019 15:25:49 +0800
Subject: [PATCH] add log

---
 controllers/es.go |  148 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 134 insertions(+), 14 deletions(-)

diff --git a/controllers/es.go b/controllers/es.go
index 412d64f..8a1fa1d 100644
--- a/controllers/es.go
+++ b/controllers/es.go
@@ -27,6 +27,7 @@
 	Ip string `json:"ip"`
 }
 
+// @Security ApiKeyAuth
 // @Summary 姣斿鏁版嵁鏌ヨ
 // @Description  姣斿鏁版嵁鏌ヨ
 // @Accept  json
@@ -163,7 +164,16 @@
 	return dataSource
 }
 
-//鏌ヨES闆嗙兢淇℃伅-鍏ュ彛
+// @Security ApiKeyAuth
+// @Summary 鏌ヨES闆嗙兢淇℃伅-鍏ュ彛
+// @Description  鏌ヨES闆嗙兢淇℃伅-鍏ュ彛
+// @Accept  json
+// @Produce json
+// @Tags es
+// @Param obj body controllers.EsClusterInfo true "鏌ヨ闆嗙兢鍙傛暟"
+// @Success 200 {string} json "{"code":200, msg:"", success:true}"
+// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
+// @Router /data/api-v/es/getEsClusterInfo [POST]
 func (em *EsManagementController) GetEsClusterInfo(c *gin.Context) {
 	var body EsClusterInfo
 	c.BindJSON(&body)
@@ -185,7 +195,7 @@
 		util.ResponseFormat(c, code.QueryClusterInfoErr, err)
 		return
 	}
-	util.ResponseFormat(c, code.QueryClusterInfoErr, nodeInfos)
+	util.ResponseFormat(c, code.Success, nodeInfos)
 }
 
 //鏌ヨES闆嗙兢淇℃伅-涓氬姟閫昏緫
@@ -205,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
@@ -230,7 +240,15 @@
 	return nodeInfos, err
 }
 
-//鍒涘缓鑺傜偣
+// @Security ApiKeyAuth
+// @Summary 鍒涘缓鑺傜偣
+// @Description  鍒涘缓鑺傜偣
+// @Accept  json
+// @Produce json
+// @Tags es
+// @Success 200 {string} json "{"code":200, msg:"", success:true}"
+// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
+// @Router /data/api-v/es/createNode [POST]
 func (em *EsManagementController) CreateNode(c *gin.Context) {
 	msg := "鍒涘缓鑺傜偣澶辫触锛岃鑱旂郴绠$悊鍛�"
 	str := "sh /opt/script/create_first_node.sh"
@@ -241,10 +259,8 @@
 	if err != nil {
 
 	}
-	infos := strings.Split(string(out.String()), "\n")
-	len := len(infos)
-	res := infos[len-1]
-	if res == "鏈嶅姟鍚姩鎴愬姛" {
+	infos := strings.Split(string(out.String()), "\n")[0]
+	if infos == "鏈嶅姟鍚姩鎴愬姛" {
 		msg = "鍒涘缓鑺傜偣鎴愬姛"
 		util.ResponseFormat(c, code.Success, msg)
 		return
@@ -252,7 +268,16 @@
 	util.ResponseFormat(c, code.CreateFirstNodeErr, msg)
 }
 
-//鍔犲叆闆嗙兢
+// @Security ApiKeyAuth
+// @Summary 鍔犲叆闆嗙兢
+// @Description  鍔犲叆闆嗙兢
+// @Accept  json
+// @Produce json
+// @Tags es
+// @Param obj body controllers.AddCluster true "鍔犲叆闆嗙兢鍙傛暟"
+// @Success 200 {string} json "{"code":200, msg:"", success:true}"
+// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
+// @Router /data/api-v/es/addCluster [POST]
 func (em *EsManagementController) AddCluster(c *gin.Context) {
 	var ac AddCluster
 	err := c.BindJSON(&ac)
@@ -260,7 +285,7 @@
 		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎")
 		return
 	}
-	str := "sh /opt/script/create_node.sh " + ac.Option + ""
+	str := "sh /opt/script/add_cluster.sh " + ac.Option + ""
 	if ac.Option == "1" {
 		info, err := updateUnicastHosts(ac.Ip)
 		if err != nil || info == false {
@@ -301,8 +326,8 @@
 	cmd := exec.Command("sh", "-c", str)
 	var out bytes.Buffer
 	cmd.Stdout = &out
-	err1 := cmd.Run()
-	if err1 != nil {
+	err := cmd.Run()
+	if err != nil {
 		return "杩愯澶辫触"
 	}
 	return out.String()
@@ -358,6 +383,101 @@
 	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) {
+	flag := "淇敼鎴愬姛"
+	var hosts Hosts
+	c.BindJSON(&hosts)
+	nodeInfos, err := getEsClusterInfors(hosts.OldIp)
+	var nodeType string
+	if err != nil {
+		logger.Fatal(err)
+		util.ResponseFormat(c, code.QueryClusterInfoErr, 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 == "涓昏妭鐐�" {
+			newMasterHosts = append(newMasterHosts, ip)
+		}
+		if ip != hosts.OldIp {
+			allHosts = append(allHosts, ip)
+		}
+	}
+
+	if nodeType == "涓昏妭鐐�" {
+		str1 := "sed -ie 's/network.host: " + hosts.OldIp + "/network.host: " + hosts.NewIp + "/g' /opt/elasticsearch/config/elasticsearch.yml"
+		msg1 := runScript(str1)
+		if msg1 == "杩愯澶辫触" {
+			flag = "淇敼閰嶇疆鏂囦欢澶辫触"
+		}
+		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"
+		msg2 := runScript(str2)
+		if msg2 == "杩愯澶辫触" {
+			flag = "淇敼閰嶇疆鏂囦欢澶辫触"
+		}
+		for _, host := range allHosts {
+			str3 := "sshpass -p \"123\" ssh basic@" + host + " \"cd /opt/elasticsearch/config  ; " + str2 + " ; cat elasticsearch.yml\""
+			msg := runScript(str3)
+			if msg == "杩愯澶辫触" {
+				flag = "淇敼閰嶇疆鏂囦欢澶辫触"
+			}
+		}
+	} else {
+		str1 := "sed -ie 's/network.host: " + hosts.OldIp + "/network.host: " + hosts.NewIp + "/g' /opt/elasticsearch/config/elasticsearch.yml"
+		msg1 := runScript(str1)
+		if msg1 == "杩愯澶辫触" {
+			flag = "淇敼閰嶇疆鏂囦欢澶辫触"
+		}
+	}
+	if flag == "淇敼閰嶇疆鏂囦欢澶辫触" {
+		util.ResponseFormat(c, code.UpdateFail, flag)
+	}
+	util.ResponseFormat(c, code.Success, flag)
+
+}
+
+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