package global import ( "fmt" "os" "strconv" "strings" "time" "xg_auto_deploy/internal/config" "xg_auto_deploy/internal/logger" "xg_auto_deploy/internal/models" "xg_auto_deploy/internal/utils" "golang.org/x/crypto/ssh" ) var ServerNodeConfigs = make(map[string]models.ServerNodeConfig) var XginiConfigMap map[string]string var ClusterConfigMap map[string]string var ClusterInfo = &models.ClusterConfig{} //var Logs *logrus.Logger var Logs = logger.Init("./logs/xg_autodeploy.log", "info") func InitNodeSetting(path string) { initFiles() chenckDefaultConfig() XginiConfigMap = make(map[string]string) ClusterConfigMap = make(map[string]string) //Logs = logger.Init("./logs/xg_autodeploy.log", "info") configTemp := config.InitNodeSetting(path) //获取配置文件里的[xugu]组内容 XginiConfigMap = configTemp.GetXuguini() ClusterConfigMap = configTemp.GetCluster() //ClusterInfo = config.ReadXgClusterConfig(ClusterConfigMap["local_file"]) for key, value := range configTemp.GetNodeConfig() { //fmt.Println(key, value) sysInfo := &models.SysInfo{ OsStackSize: "未检测", OsOpenFiles: "未检测", CoreWmemDefault: "未检测", CoreRmemDefault: "未检测", CoreRmemMax: "未检测", CoreWmemMax: "未检测", } appInfo := &models.AppInfo{ Gcc: "未检测", Libaio: "未检测", Snmpd: "未检测", Ntpd: "未检测", } sshInfo := &models.SSHInfo{ SSHClient: &ssh.ClientConfig{ User: value.User, Auth: []ssh.AuthMethod{ ssh.Password(value.Password), }, // 设置其他字段 ClientVersion: "", Timeout: 5 * time.Second, // 设置连接超时时间 HostKeyCallback: ssh.InsecureIgnoreHostKey(), // 不验证远程主机的身份,仅用于示例,请不要在生产环境中使用 }, } nodeTemp := models.ServerNodeConfig{ NodeId: key, ServerNodeInfo: value, SysInfo: sysInfo, AppInfo: appInfo, SSHInfo: sshInfo, } ServerNodeConfigs[key] = nodeTemp } //初始化集群配置 initCluster() } func initFiles() { chenckFiles("./file") chenckFiles("./logs") } func chenckFiles(dir string) { // 检查目录是否存在 if _, err := os.Stat(dir); os.IsNotExist(err) { // 目录不存在,创建目录 err := os.MkdirAll(dir, os.ModePerm) if err != nil { fmt.Printf("Error creating directory %s: %s", dir, err) return } fmt.Println("Directory created:", dir) } } func initCluster() { //设置cluster.ini文件配置 if ClusterConfigMap["template"] != "" { nodeNum := 0 var clustersecondParts []models.ClustersecondPart var msgPortNum string for _, node := range ServerNodeConfigs { nodeNum++ //切分ip端口 nodeIp, _ := utils.SplitColon(node.IpPort) //识别template端口数量,并添加端口 portsRet := strings.Split(ClusterConfigMap["template"], ",") for _, v := range portsRet { nid := "" NodeId, err := strconv.Atoi(node.NodeId) if err != nil { fmt.Println("NodeId转换失败") os.Exit(1) } if NodeId < 10 { nid = fmt.Sprintf("000%s", node.NodeId) } else if NodeId < 100 { nid = fmt.Sprintf("00%s", node.NodeId) } else if NodeId < 1000 { nid = fmt.Sprintf("0%s", node.NodeId) } // 50000:2 port, num := utils.SplitColon(v) msgPortNum = num numI, err := strconv.Atoi(num) if err != nil { fmt.Println("端口数量转换失败") os.Exit(1) } portI, err := strconv.Atoi(port) if err != nil { fmt.Println("端口转换失败") os.Exit(1) } Ports := "" for i := 0; i < numI; i++ { Ports += fmt.Sprintf("%s:%v,", nodeIp, portI) portI++ } //去除Ports 最后的逗号 Ports = Ports[:len(Ports)-1] // 筛选主副节点 Role := "" gRole := 0 if node.NodeId == "1" || node.NodeId == "2" { Role = "MSQW" } else if gRole == 0 { Role = "SQWG" } else { Role = "SQW" } cTemp := &models.ClustersecondPart{ NID: nid, RACK: "0001", PORTS: Ports, ROLE: Role, LPU: "3", StoreWeight: "3", STATE: "DETECT", } clustersecondParts = append(clustersecondParts, *cTemp) } } ClusterInfo = &models.ClusterConfig{ FirstPart: models.ClusterfirstPart{ MaxNodes: "32", MasterGrps: "1", Protocol: "UDP", MsgPortNum: msgPortNum, MaxSendWin: "1024", MsgHaveCRC: "0", MergeSmallMsg: "1", MsgSize: "9000", Timeout: "20000", RPCWindow: "16", EJEWindow: "16", MaxShakeTime: "1200", MyNID: "", CheckRack: "0", }, SecondParts: clustersecondParts, } } var err error if ClusterConfigMap["local_file"] != "" { ClusterInfo, err = config.GetClusterConfig(ClusterConfigMap["local_file"]) if err != nil { Logs.Fatalf("config.GetClusterConfig 失败") } } } func chenckDefaultConfig() { // 检查文件是否存在 // 要检查和创建的文件路径 filePath := "./config.toml" // 文件内容 content := `[node] local_file = "./file/xugu/XuguDB-12.4_20240416-cluster-linux-aarch64" target_file = "/DATA2/GT/xugu/start" xugu_addr = "/DATA2/GT/xugu/start" [node.1] ip_port = "IP:PORT" user_password = "用户名:密码" [node.2] ip_port = "IP:PORT" user_password = "用户名:密码" [node.3] ip_port = "IP:PORT" user_password = "用户名:密码" #井号为注释 [xugu] #local_file = "./file/xugu.ini" data_buff_mem = "1024" listen_port = "5181" [cluster] #local_file = "./file/cluster.ini" ` // 检查文件是否存在 if _, err := os.Stat(filePath); os.IsNotExist(err) { // 文件不存在,创建文件并写入内容 err := os.WriteFile(filePath, []byte(content), 0644) if err != nil { fmt.Println("默认配置文件创建失败 (Error creating file):", err) } fmt.Printf("默认配置文件创建成功 请更改文件 %s 后,重新运行", filePath) os.Exit(1) } }