global.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package global
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "xg_auto_deploy/internal/config"
  7. "xg_auto_deploy/internal/logger"
  8. "xg_auto_deploy/internal/models"
  9. "golang.org/x/crypto/ssh"
  10. )
  11. var ServerNodeConfigs = make(map[string]models.ServerNodeConfig)
  12. var XginiConfigMap map[string]string
  13. var ClusterConfigMap map[string]string
  14. var ClusterInfo = &models.ClusterConfig{}
  15. //var Logs *logrus.Logger
  16. var Logs = logger.Init("./logs/xg_autodeploy.log", "info")
  17. func InitNodeSetting(path string) {
  18. initFiles()
  19. chenckDefaultConfig()
  20. XginiConfigMap = make(map[string]string)
  21. ClusterConfigMap = make(map[string]string)
  22. //Logs = logger.Init("./logs/xg_autodeploy.log", "info")
  23. configTemp := config.InitNodeSetting(path)
  24. //获取配置文件里的[xugu]组内容
  25. XginiConfigMap = configTemp.GetXuguini()
  26. ClusterConfigMap = configTemp.GetCluster()
  27. //ClusterInfo = config.ReadXgClusterConfig(ClusterConfigMap["local_file"])
  28. var err error
  29. if ClusterConfigMap["local_file"] != "" {
  30. ClusterInfo, err = config.GetClusterConfig(ClusterConfigMap["local_file"])
  31. if err != nil {
  32. Logs.Fatalf("config.GetClusterConfig 失败")
  33. }
  34. }
  35. for key, value := range configTemp.GetNodeConfig() {
  36. //fmt.Println(key, value)
  37. sysInfo := &models.SysInfo{
  38. OsStackSize: "未检测",
  39. OsOpenFiles: "未检测",
  40. CoreWmemDefault: "未检测",
  41. CoreRmemDefault: "未检测",
  42. CoreRmemMax: "未检测",
  43. CoreWmemMax: "未检测",
  44. }
  45. appInfo := &models.AppInfo{
  46. Gcc: "未检测",
  47. Libaio: "未检测",
  48. Snmpd: "未检测",
  49. Ntpd: "未检测",
  50. }
  51. sshInfo := &models.SSHInfo{
  52. SSHClient: &ssh.ClientConfig{
  53. User: value.User,
  54. Auth: []ssh.AuthMethod{
  55. ssh.Password(value.Password),
  56. },
  57. // 设置其他字段
  58. ClientVersion: "",
  59. Timeout: 5 * time.Second, // 设置连接超时时间
  60. HostKeyCallback: ssh.InsecureIgnoreHostKey(), // 不验证远程主机的身份,仅用于示例,请不要在生产环境中使用
  61. },
  62. }
  63. nodeTemp := models.ServerNodeConfig{
  64. NodeId: key,
  65. ServerNodeInfo: value,
  66. SysInfo: sysInfo,
  67. AppInfo: appInfo,
  68. SSHInfo: sshInfo,
  69. }
  70. ServerNodeConfigs[key] = nodeTemp
  71. }
  72. }
  73. func initFiles() {
  74. chenckFiles("./file")
  75. chenckFiles("./logs")
  76. }
  77. func chenckFiles(dir string) {
  78. // 检查目录是否存在
  79. if _, err := os.Stat(dir); os.IsNotExist(err) {
  80. // 目录不存在,创建目录
  81. err := os.MkdirAll(dir, os.ModePerm)
  82. if err != nil {
  83. fmt.Printf("Error creating directory %s: %s", dir, err)
  84. return
  85. }
  86. fmt.Println("Directory created:", dir)
  87. }
  88. }
  89. func chenckDefaultConfig() {
  90. // 检查文件是否存在
  91. // 要检查和创建的文件路径
  92. filePath := "./config.toml"
  93. // 文件内容
  94. content := `[node]
  95. local_file = "./file/xugu/XuguDB-12.4_20240416-cluster-linux-aarch64"
  96. target_file = "/DATA2/GT/xugu/start"
  97. xugu_addr = "/DATA2/GT/xugu/start"
  98. [node.1]
  99. ip_port = "IP:PORT"
  100. user_password = "用户名:密码"
  101. [node.2]
  102. ip_port = "IP:PORT"
  103. user_password = "用户名:密码"
  104. [node.3]
  105. ip_port = "IP:PORT"
  106. user_password = "用户名:密码"
  107. #井号为注释
  108. [xugu]
  109. #local_file = "./file/xugu.ini"
  110. data_buff_mem = "1024"
  111. listen_port = "5181"
  112. [cluster]
  113. #local_file = "./file/cluster.ini"
  114. `
  115. // 检查文件是否存在
  116. if _, err := os.Stat(filePath); os.IsNotExist(err) {
  117. // 文件不存在,创建文件并写入内容
  118. err := os.WriteFile(filePath, []byte(content), 0644)
  119. if err != nil {
  120. fmt.Println("默认配置文件创建失败 (Error creating file):", err)
  121. }
  122. fmt.Printf("默认配置文件创建成功 请更改文件 %s 后,重新运行", filePath)
  123. os.Exit(1)
  124. }
  125. }