From 2c4888021f10e29998378f58293c66ef67323b85 Mon Sep 17 00:00:00 2001 From: bizhiyuan Date: Tue, 10 Dec 2024 11:10:44 +0800 Subject: [PATCH] Add DeleteHeartbeat functions --- models/heartbeat.go | 58 +++++++++++++++++++++++++++++++++++++++++++++ utils/command.go | 1 + utils/utils.go | 10 +++++++- 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/models/heartbeat.go b/models/heartbeat.go index 7996b5a..845522d 100644 --- a/models/heartbeat.go +++ b/models/heartbeat.go @@ -344,3 +344,61 @@ func GetRingIdFromIPOffline(ipAddress string) (int, error) { func isValidIPv4(ip string) bool { return net.ParseIP(ip) != nil && strings.Contains(ip, ".") } + +func DeleteHeartbeat(hbInfo HBInfo) utils.GeneralResponse { + hbData := hbInfo.Data + minLinkNum := 1 + hbInfoList, _ := ExtractHbInfo(hbData) + status := GetClusterStatus() + currentLocalHbIds := GetCurrentLinkIds() + var linkId int + var linkIdSet []string + if len(currentLocalHbIds) <= minLinkNum || len(currentLocalHbIds) <= len(hbInfoList) { + return utils.GeneralResponse{ + Action: false, + Error: gettext.Gettext("At least one heartbeat needs to be preserved and cannot be deleted further."), + } + } + + for _, hbInfo := range hbInfoList { + ip := utils.Values(hbInfo) + if status == 0 { + linkId, _ = GetRingIdFromIPOnline(ip[0]) + } else { + linkId, _ = GetRingIdFromIPOnline(ip[0]) + } + + if linkId == -1 { + linkId, _ = GetRingIdFromIPOnline(ip[0]) + if linkId == -1 { + return utils.GeneralResponse{ + Action: false, + Error: gettext.Gettext("Exception occurred while deleting the heartbeat. Please check the logs and the original heartbeat status."), + } + } + } + linkIdSet = append(linkIdSet, strconv.Itoa(linkId)) + } + linkIdSetStr := strings.Join(linkIdSet, " ") + cmd := fmt.Sprintf(utils.CmdDeleteLinks, linkIdSetStr) + if _, err := utils.RunCommand(cmd); err != nil { + return utils.GeneralResponse{ + Action: false, + Error: gettext.Gettext("Deletion heartbeat failed, the heartbeat ID does not exist"), + } + } + + return utils.GeneralResponse{ + Action: true, + Error: gettext.Gettext("Delete heartbeat success"), + } +} + +func GetCurrentLinkIds() []string { + linksInfo, _ := ExtractHbInfoFromConf() + ids := make([]string, 0, len(linksInfo)) + for k := range linksInfo { + ids = append(ids, k) + } + return ids +} diff --git a/utils/command.go b/utils/command.go index 7f89c70..858f37d 100644 --- a/utils/command.go +++ b/utils/command.go @@ -94,6 +94,7 @@ const ( CmdAddLink = "pcs cluster link add %s" CmdAddLinkForce = "pcs cluster link add %s --force" CmdAddLinksWithLinkNum = "pcs cluster link add %s options linknumber=%s --force" + CmdHbStatus = "corosync-cfgtool -n" ) // RunCommand runs the command and get the result diff --git a/utils/utils.go b/utils/utils.go index 3312a49..c499a09 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,6 +1,6 @@ /* * Copyright (c) KylinSoft Co., Ltd. 2024.All rights reserved. - * ha-api licensed under the Mulan Permissive Software License, Version 2. + * ha-api licensed under the Mulan Permissive Software License, Version 2. * See LICENSE file for more details. * Author: yangzhao_kl * Date: Fri Jan 8 20:56:40 2021 +0800 @@ -155,3 +155,11 @@ func Contains(slice []string, str string) bool { } return false } + +func Values(m map[string]string) []string { + values := make([]string, 0, len(m)) + for _, value := range m { + values = append(values, value) + } + return values +} -- Gitee