global.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package global
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "xg_auto_deploy/internal/config"
  9. "xg_auto_deploy/internal/logger"
  10. "xg_auto_deploy/internal/models"
  11. "xg_auto_deploy/internal/utils"
  12. "golang.org/x/crypto/ssh"
  13. )
  14. var ServerNodeConfigs = make(map[string]models.ServerNodeConfig)
  15. var XginiConfigMap map[string]string
  16. var ClusterConfigMap map[string]string
  17. var ClusterInfo = &models.ClusterConfig{}
  18. //var Logs *logrus.Logger
  19. var Logs = logger.Init("./logs/xg_autodeploy.log", "info")
  20. func InitNodeSetting(path string) {
  21. initFiles()
  22. chenckDefaultConfig()
  23. XginiConfigMap = make(map[string]string)
  24. ClusterConfigMap = make(map[string]string)
  25. //Logs = logger.Init("./logs/xg_autodeploy.log", "info")
  26. configTemp := config.InitNodeSetting(path)
  27. //获取配置文件里的[xugu]组内容
  28. XginiConfigMap = configTemp.GetXuguini()
  29. ClusterConfigMap = configTemp.GetCluster()
  30. //ClusterInfo = config.ReadXgClusterConfig(ClusterConfigMap["local_file"])
  31. for key, value := range configTemp.GetNodeConfig() {
  32. //fmt.Println(key, value)
  33. sysInfo := &models.SysInfo{
  34. OsStackSize: "未检测",
  35. OsOpenFiles: "未检测",
  36. CoreWmemDefault: "未检测",
  37. CoreRmemDefault: "未检测",
  38. CoreRmemMax: "未检测",
  39. CoreWmemMax: "未检测",
  40. }
  41. appInfo := &models.AppInfo{
  42. Gcc: "未检测",
  43. Libaio: "未检测",
  44. Snmpd: "未检测",
  45. Ntpd: "未检测",
  46. }
  47. sshInfo := &models.SSHInfo{
  48. SSHClient: &ssh.ClientConfig{
  49. User: value.User,
  50. Auth: []ssh.AuthMethod{
  51. ssh.Password(value.Password),
  52. },
  53. // 设置其他字段
  54. ClientVersion: "",
  55. Timeout: 5 * time.Second, // 设置连接超时时间
  56. HostKeyCallback: ssh.InsecureIgnoreHostKey(), // 不验证远程主机的身份,仅用于示例,请不要在生产环境中使用
  57. },
  58. }
  59. nodeTemp := models.ServerNodeConfig{
  60. NodeId: key,
  61. ServerNodeInfo: value,
  62. SysInfo: sysInfo,
  63. AppInfo: appInfo,
  64. SSHInfo: sshInfo,
  65. }
  66. ServerNodeConfigs[key] = nodeTemp
  67. }
  68. //初始化集群配置
  69. initCluster()
  70. }
  71. func initFiles() {
  72. chenckFiles("./file")
  73. chenckFiles("./logs")
  74. }
  75. func chenckFiles(dir string) {
  76. // 检查目录是否存在
  77. if _, err := os.Stat(dir); os.IsNotExist(err) {
  78. // 目录不存在,创建目录
  79. err := os.MkdirAll(dir, os.ModePerm)
  80. if err != nil {
  81. fmt.Printf("Error creating directory %s: %s", dir, err)
  82. return
  83. }
  84. fmt.Println("Directory created:", dir)
  85. }
  86. }
  87. func initCluster() {
  88. //设置cluster.ini文件配置
  89. if ClusterConfigMap["template"] != "" {
  90. nodeNum := 0
  91. var clustersecondParts []models.ClustersecondPart
  92. var msgPortNum string
  93. for _, node := range ServerNodeConfigs {
  94. nodeNum++
  95. //切分ip端口
  96. nodeIp, _ := utils.SplitColon(node.IpPort)
  97. //识别template端口数量,并添加端口
  98. portsRet := strings.Split(ClusterConfigMap["template"], ",")
  99. for _, v := range portsRet {
  100. nid := ""
  101. NodeId, err := strconv.Atoi(node.NodeId)
  102. if err != nil {
  103. fmt.Println("NodeId转换失败")
  104. os.Exit(1)
  105. }
  106. if NodeId < 10 {
  107. nid = fmt.Sprintf("000%s", node.NodeId)
  108. } else if NodeId < 100 {
  109. nid = fmt.Sprintf("00%s", node.NodeId)
  110. } else if NodeId < 1000 {
  111. nid = fmt.Sprintf("0%s", node.NodeId)
  112. }
  113. // 50000:2
  114. port, num := utils.SplitColon(v)
  115. msgPortNum = num
  116. numI, err := strconv.Atoi(num)
  117. if err != nil {
  118. fmt.Println("端口数量转换失败")
  119. os.Exit(1)
  120. }
  121. portI, err := strconv.Atoi(port)
  122. if err != nil {
  123. fmt.Println("端口转换失败")
  124. os.Exit(1)
  125. }
  126. Ports := ""
  127. for i := 0; i < numI; i++ {
  128. Ports += fmt.Sprintf("%s:%v,", nodeIp, portI)
  129. portI++
  130. }
  131. //去除Ports 最后的逗号
  132. Ports = Ports[:len(Ports)-1]
  133. // 筛选主副节点
  134. Role := ""
  135. gRole := 0
  136. if node.NodeId == "1" || node.NodeId == "2" {
  137. Role = "MSQW"
  138. } else if gRole == 0 {
  139. Role = "SQWG"
  140. } else {
  141. Role = "SQW"
  142. }
  143. cTemp := &models.ClustersecondPart{
  144. NID: nid,
  145. RACK: "0001",
  146. PORTS: Ports,
  147. ROLE: Role,
  148. LPU: "3",
  149. StoreWeight: "3",
  150. STATE: "DETECT",
  151. }
  152. clustersecondParts = append(clustersecondParts, *cTemp)
  153. }
  154. }
  155. ClusterInfo = &models.ClusterConfig{
  156. FirstPart: models.ClusterfirstPart{
  157. MaxNodes: "32",
  158. MasterGrps: "1",
  159. Protocol: "UDP",
  160. MsgPortNum: msgPortNum,
  161. MaxSendWin: "1024",
  162. MsgHaveCRC: "0",
  163. MergeSmallMsg: "1",
  164. MsgSize: "9000",
  165. Timeout: "20000",
  166. RPCWindow: "16",
  167. EJEWindow: "16",
  168. MaxShakeTime: "1200",
  169. MyNID: "",
  170. CheckRack: "0",
  171. },
  172. SecondParts: clustersecondParts,
  173. }
  174. }
  175. var err error
  176. if ClusterConfigMap["local_file"] != "" {
  177. ClusterInfo, err = config.GetClusterConfig(ClusterConfigMap["local_file"])
  178. if err != nil {
  179. Logs.Fatalf("config.GetClusterConfig 失败")
  180. }
  181. }
  182. }
  183. func chenckDefaultConfig() {
  184. // 检查文件是否存在
  185. // 要检查和创建的文件路径
  186. filePath := "./config.toml"
  187. // 文件内容
  188. content := `[node]
  189. local_file = "./file/xugu/XuguDB-12.4_20240416-cluster-linux-aarch64"
  190. target_file = "/DATA2/GT/xugu/start"
  191. xugu_addr = "/DATA2/GT/xugu/start"
  192. [node.1]
  193. ip_port = "IP:PORT"
  194. user_password = "用户名:密码"
  195. [node.2]
  196. ip_port = "IP:PORT"
  197. user_password = "用户名:密码"
  198. [node.3]
  199. ip_port = "IP:PORT"
  200. user_password = "用户名:密码"
  201. #井号为注释
  202. [xugu]
  203. #local_file = "./file/xugu.ini"
  204. data_buff_mem = "1024"
  205. listen_port = "5181"
  206. [cluster]
  207. #local_file = "./file/cluster.ini"
  208. `
  209. // 检查文件是否存在
  210. if _, err := os.Stat(filePath); os.IsNotExist(err) {
  211. // 文件不存在,创建文件并写入内容
  212. err := os.WriteFile(filePath, []byte(content), 0644)
  213. if err != nil {
  214. fmt.Println("默认配置文件创建失败 (Error creating file):", err)
  215. }
  216. fmt.Printf("默认配置文件创建成功 请更改文件 %s 后,重新运行", filePath)
  217. os.Exit(1)
  218. }
  219. }