From 19f944268a81d62e167a37281494936bed2891e4 Mon Sep 17 00:00:00 2001 From: bizhiyuan Date: Thu, 12 Dec 2024 17:50:19 +0800 Subject: [PATCH] add EditHeartbeat functions --- locale/zh_CN/LC_MESSAGES/ha-api.po | 26 ++++++++++- models/heartbeat.go | 73 ++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/locale/zh_CN/LC_MESSAGES/ha-api.po b/locale/zh_CN/LC_MESSAGES/ha-api.po index 6b360ee..fdfdcdf 100644 --- a/locale/zh_CN/LC_MESSAGES/ha-api.po +++ b/locale/zh_CN/LC_MESSAGES/ha-api.po @@ -228,4 +228,28 @@ msgid "Exception occurred while deleting the heartbeat. Please check the logs an msgstr "心跳删除出现异常,请检查日志以及原心跳状态" msgid "Deletion heartbeat failed, the heartbeat ID does not exist" -msgstr "心跳删除失败, 该心跳ID不存在, 请检查日志" \ No newline at end of file +msgstr "心跳删除失败, 该心跳ID不存在, 请检查日志" + +msgid "The maximum number of heartbeats in the cluster has been reached and cannot be added further." +msgstr "集群心跳路数已达到最大支持路数,无法继续添加" + +msgid "All network heartbeats currently in a disconnected state and cannot continue to add heartbeat." +msgstr "集群当前所有网络心跳都处于断开状态,无法继续添加" + +msgid "Exception occurred while adding heartbeats, either the IP was conflicted with the original heartbeat" +msgstr "心跳添加出现异常, 新添加心跳IP或和原心跳冲突" + +msgid "Add heartbeat success" +msgstr "心跳添加成功" + +msgid "The cluster has not been created." +msgstr "集群未创建,请先创建集群" + +msgid "The heartbeat to be edited exceeds the maximum number of heartbeats" +msgstr "要编辑的心跳路数超过了集群支持的网络心跳路数容量" + +msgid "Edit heartbeat success" +msgstr "心跳编辑成功" + +msgid "Failed to synchronize corosync.conf file" +msgstr "同步corosync配置文件失败" \ No newline at end of file diff --git a/models/heartbeat.go b/models/heartbeat.go index 742743d..c94a141 100644 --- a/models/heartbeat.go +++ b/models/heartbeat.go @@ -474,6 +474,79 @@ func SyncCorosyncConf() error { return err } +func EditHeartbeat(hbInfo HBInfo) utils.GeneralResponse { + maxNum := 7 + // 当前环境中的心跳信息 + linksInfo, _ := ExtractHbInfoFromConf() + hbIds := utils.Keys[string, []string](linksInfo) + if len(hbIds) == 0 { + return utils.GeneralResponse{ + Action: false, + Error: gettext.Gettext("The cluster has not been created."), + } + } + // 要进行编辑的心跳的信息 + hbInfoList, idsEdited := ExtractHbInfo(hbInfo.Data) + idsNum := len(hbInfoList) + if idsNum > maxNum { + return utils.GeneralResponse{ + Action: false, + Error: gettext.Gettext("The heartbeat to be edited exceeds the maximum number of heartbeats"), + } + } + clusterStatus := GetClusterStatus() + if clusterStatus == 0 { + connectedNetIds := GetConnectedNetLinksId() + leftConnectedIds := utils.DifferenceSlice(connectedNetIds, idsEdited) + if len(hbInfoList) < len(linksInfo) && len(leftConnectedIds) > 0 { + var linksStr strings.Builder + for _, id := range idsEdited { + linksStr.WriteString(" ") + linksStr.WriteString(id) + } + // 基于心跳编号删除要编辑的心跳 + DeletLinks(linksStr.String()) + + // 添加新的心跳内容 + for _, x := range hbInfoList { + AddLink(x, "") + } + return utils.GeneralResponse{ + Action: true, + Info: gettext.Gettext("Edit heartbeat success"), + } + } + } + utils.RunCommand(utils.CmdStopCluster + " --force") + + // 修改配置文件 + conf, _ := utils.GetCorosyncConfig() + for _, nodeInfo := range hbInfo.Data { + for _, node := range conf.NodeList { + if nodeInfo["name"] == node["name"] { + for _, id := range idsEdited { + ringStr := "ring" + id + "_addr" + node[ringStr] = nodeInfo[ringStr] + } + } + } + } + utils.SetCorosyncConfig(conf, settings.CorosyncConfFile) + if err := SyncCorosyncConf(); err != nil { + return utils.GeneralResponse{ + Action: false, + Info: gettext.Gettext("Failed to synchronize corosync.conf file"), + } + } + // 重新启动集群 + utils.RunCommand(utils.CmdStartCluster) + + return utils.GeneralResponse{ + Action: true, + Info: gettext.Gettext("Edit heartbeat success"), + } +} + func GetCurrentLinkIds() []string { linksInfo, _ := ExtractHbInfoFromConf() ids := make([]string, 0, len(linksInfo)) -- Gitee