123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package global
- import (
- "fmt"
- "os"
- "time"
- "xg_auto_deploy/internal/config"
- "xg_auto_deploy/internal/logger"
- "xg_auto_deploy/internal/models"
- "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"])
- var err error
- if ClusterConfigMap["local_file"] != "" {
- ClusterInfo, err = config.GetClusterConfig(ClusterConfigMap["local_file"])
- if err != nil {
- Logs.Fatalf("config.GetClusterConfig 失败")
- }
- }
- 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
- }
- }
- 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 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)
- }
- }
|