sunty
2024-05-31 f6ca7bb43270474fa876ff6ba62c6b2113b045ad
ClusterApi.go
@@ -5,14 +5,16 @@
   "encoding/json"
   "errors"
   "fmt"
   "github.com/spf13/viper"
   "io/ioutil"
   "net/http"
   "os/exec"
   "strings"
   "time"
   "github.com/spf13/viper"
)
//初始化配置文件
func InitYml(configPath string) (bool, error) {
   v := viper.New()
   v.SetConfigType("yml")
@@ -28,19 +30,39 @@
   return true, nil
}
func StartServer(binPath string, ip string) bool {
   resultMsg := CMDSC("sh " + binPath + "/elasticsearch -d")
//初始化索引
func InitIndex(indexPath string) bool {
   resultMsg := CMDSC("sh " + indexPath + "/indexInit.sh")
   if resultMsg == "运行失败" {
      return false
   }
   bool := VerifyNodeServer(ip, "9200")
   return bool
   return true
}
func StopServer(binPath string) {
//启动服务
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")
@@ -58,10 +80,11 @@
   if err := v.WriteConfig(); err != nil {
      return false, err
   }
   return true,nil
   return true, nil
}
func VerifyCreated(configPath string, ip string) (bool, error) {
//更换节点集群名称
func UpdateClusterName(configPath string, name string) (bool, error) {
   v := viper.New()
   v.SetConfigType("yml")
   v.SetConfigName("elasticsearch")
@@ -70,19 +93,32 @@
   if err != nil {
      return false, err
   }
   hosts := v.GetStringSlice("discovery.zen.ping.unicast.hosts")
   for _, pick := range hosts {
      if pick == ip {
         return true, errors.New("该节点已经被执行“创建集群”")
      }
   v.Set("cluster.name", name)
   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 true, errors.New("该节点已经被使用")
   }
   return false, nil
}
func VerifyNodeServer(ip string, port string) bool {
//验证节点服务是否正常启动
func VerifyNodeServer(ip string, port string, waitTime int) bool {
   b := false
   url := "http://" + ip + ":" + port
   for i := 1; i < 3; i++ {
@@ -90,7 +126,7 @@
      if err != nil {
         b = false
         if i < 3 {
            time.Sleep(time.Second * 10)
            time.Sleep(time.Second * time.Duration(waitTime))
            continue
         }
      } else {
@@ -101,7 +137,18 @@
   return b
}
func VerifyNodeRole(configPath string, ) (string, error) {
//验证节点服务是否正常启动(短验证)
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")
@@ -118,6 +165,38 @@
   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")
@@ -145,18 +224,23 @@
   return true, nil
}
func AddDiscoveryZenPingUnicastHosts(configPath string, ip string) []string {
//添加组播成员
func AddDiscoveryZenPingUnicastHosts(configPath string, ip string) (bool, error) {
   v := viper.New()
   v.SetConfigType("yml")
   v.SetConfigName("elasticsearch")
   v.AddConfigPath(configPath)
   err := v.ReadInConfig()
   if err != nil {
   errR := v.ReadInConfig()
   if errR != nil {
      return false, errR
   }
   hosts := v.GetStringSlice("discovery.zen.ping.unicast.hosts")
   hosts = append(hosts, ip)
   return hosts
   v.Set("discovery.zen.ping.unicast.hosts", hosts)
   if errW := v.WriteConfig(); errW != nil {
      return false, errW
   }
   return true, nil
}
type NodeInfo struct {
@@ -166,6 +250,7 @@
   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)
@@ -198,6 +283,7 @@
   return inf, nil
}
//排除即将退出集群的节点
func ExcludeNode(ip string, port string) (bool, error) {
   flag := false
   url := "http://" + ip + ":" + port + "/_cluster/settings"
@@ -236,7 +322,6 @@
   }
   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