| | |
| | | package models |
| | | |
| | | import ( |
| | | "errors" |
| | | "fmt" |
| | | "strconv" |
| | | "time" |
| | |
| | | return result, nil |
| | | } |
| | | |
| | | func (c *Cluster) Create() bool { |
| | | func (c *Cluster) Create() error { |
| | | var localConfig LocalConfig |
| | | e := localConfig.Select() |
| | | if e != nil { |
| | | return false |
| | | err := localConfig.Select() |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | serverId := config.Server.AnalyServerId |
| | | if serverId == "" { |
| | | return false |
| | | return errors.New("serverId 为空") |
| | | } |
| | | |
| | | serverIp, _, e := util.GetLocalIP(config.Server.NetworkAdapter) |
| | | if e != nil { |
| | | return false |
| | | return e |
| | | } |
| | | var err error |
| | | |
| | | tx := db.Begin() |
| | | defer func() { |
| | | if err != nil && tx != nil { |
| | | tx.Rollback() |
| | | } |
| | | }() |
| | | |
| | | sql := "insert into cluster (cluster_id,cluster_name,password,virtual_ip) values ('" + c.ClusterId + "','" + c.ClusterName + "','" + c.Password + "','" + c.VirtualIp + "')" |
| | | if err = tx.Exec(sql).Error; err != nil { |
| | | return false |
| | | return err |
| | | } |
| | | timeUnix := time.Now().Unix() |
| | | fmtTimeStr := time.Unix(timeUnix, 0).Format("2006-01-02 15:04:05") |
| | | //添加本身节点 |
| | | sql = "insert into cluster_node (id,cluster_id,node_name,node_id,node_ip,create_time,isDelete,device_type) values ('" + serverId + "','" + c.ClusterId + "','" + localConfig.ServerName + "','" + serverId + "','" + (serverIp + ":" + strconv.Itoa(syncdb.DefaultBindPort)) + "','" + fmtTimeStr + "',0,'" + config.Server.DeviceType + "')" |
| | | sql = "insert into cluster_node (id,cluster_id,node_name,node_id,node_ip,create_time,isDelete,device_type,drift_state) values ('" + serverId + "','" + c.ClusterId + "','" + localConfig.ServerName + "','" + serverId + "','" + (serverIp + ":" + strconv.Itoa(syncdb.DefaultBindPort)) + "','" + fmtTimeStr + "',0,'" + config.Server.DeviceType + "','master')" |
| | | if err = tx.Exec(sql).Error; err != nil { |
| | | return false |
| | | return err |
| | | } |
| | | |
| | | tx.Commit() |
| | | return true |
| | | |
| | | return nil |
| | | } |
| | | |
| | | func (c *Cluster) UpdateClusterName(clusterName string, virtualIp string) bool { |