liuxiaolong
2019-10-30 c38c4bda17e3774ec0fd58f90ba5e59e1f0639a2
controllers/es.go
@@ -21,6 +21,7 @@
type EsController struct{}
// @Security ApiKeyAuth
// @Summary 比对数据查询
// @Description  比对数据查询
// @Accept  json
@@ -195,7 +196,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 +210,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
}