From c2b966658dea1edd6b91258e9a26a118e1b83ede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AD=90=E8=B4=A4?= <2367744612@qq.com> Date: Tue, 23 Jun 2020 10:35:36 +0800 Subject: [PATCH 1/7] get_loation --- resource.py | 166 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 127 insertions(+), 39 deletions(-) diff --git a/resource.py b/resource.py index f4cef39..38837e2 100644 --- a/resource.py +++ b/resource.py @@ -95,8 +95,7 @@ class HAPublic: def add_score(self, score1, score2): if score1 in ["-INFINITY", "-infinity"] or score2 in ["-INFINITY", "-infinity"]: return str("-INFINITY") - if score1 in ["INFINITY", "+INFINITY", "infinity", "+infinity"] or score2 in ["INFINITY", "+INFINITY", - "infinity", "+infinity"]: + if score1 in ["INFINITY", "+INFINITY", "infinity", "+infinity"] or score2 in ["INFINITY", "+INFINITY","infinity", "+infinity"]: return str("INFINITY") return str(int(score1) + int(score2)) @@ -140,16 +139,16 @@ def get_sub_rscs(rsc_id): primitive_info["id"] = rsc_primitives["@id"] pri_id = primitive_info["id"] if pri_id in rsc_status: - primitive_info["status"] = rsc_status[pri_id]["status"] - primitive_info["running_node"] = rsc_status[pri_id]["running_node"] + primitive_info["status"] = rsc_status[pri_id]["status"] + primitive_info["running_node"] = rsc_status[pri_id]["running_node"] else: - primitive_info["status"] = "Stopped" - primitive_info["running_node"] = [] - primitive_info["status_message"] = "" - primitive_info["svc"] = rsc_primitives["@type"] - primitive_info["type"] = "primitive" - primitive_info["running_node"] = [] - sub_sub_rsc.append(primitive_info) + primitive_info["status"] = "Stopped" + primitive_info["running_node"] = [] + primitive_info["status_message"] = "" + primitive_info["svc"] = rsc_primitives["@type"] + primitive_info["type"] = "primitive" + primitive_info["running_node"] = [] + sub_sub_rsc.append(primitive_info) else: for primitive in rsc_primitives: primitive_info = {} @@ -412,8 +411,8 @@ def get_rsc_type(rsc_id): return None def get_rsc_status(rsc_info): - ''' - { + ''' + { "@id":"test3", "@resource_agent":"ocf::pacemaker:Dummy", "@role":"Started", @@ -427,35 +426,33 @@ def get_rsc_status(rsc_info): "node":{ "@name":"ha3", "@id":"1", - "@cached":"false" - } + "@cached":"false"} } - ''' - if rsc_info['@managed'] == "false": - return "Unmanaged" + ''' + if rsc_info['@managed'] == "false": + return "Unmanaged" - if rsc_info['@failed'] == "true": - return "Failed" + if rsc_info['@failed'] == "true": + return "Failed" - return "Running" + return "Running" # 获取资源的运行状态 def get_all_rsc_status(): ''' infos = { - 0:_('Running'), - 1:_('Not Running'), - 2:_('Unmanaged'), - 3:_('Failed'), - 4:_('Stop Failed'), - 5:_('running (Master)'), - 6:_('running (Slave)')} - - rsc_info = { - "hj1": {"status": 0 , "status_message": "test", running_node: []} - "hj2": {"status": 0 , "status_message": "test", running_node: []} - } + 0:_('Running'), + 1:_('Not Running'), + 2:_('Unmanaged'), + 3:_('Failed'), + 4:_('Stop Failed'), + 5:_('running (Master)'), + 6:_('running (Slave)')} + rsc_info = { + "hj1": {"status": 0 , "status_message": "test", running_node: []} + "hj2": {"status": 0 , "status_message": "test", running_node: []} + } ''' rsc_info = {} @@ -802,14 +799,15 @@ def resource_action(rsc_id, action, data): #迁移节点 duration = "" to_node = "" + cmd_head = "crm_resource --resource " + to_node = str(data["to_node"]) #迁移到指定节点 if "period" in data.keys() and data["period"] != "PYMDT0HMS": duration = str(data["period"]) #持续时间 - to_node = str(data["to_node"]) #迁移到指定节点 - cmd_head = "crm_resource --resource " - #cmd_str = cmd_head + str(rsc_id) + " --move -N " + to_node + " -u " + duration - cmd_str = cmd_head + str(rsc_id) + " --move -N " + to_node - print(cmd_str) + cmd_str = cmd_head + str(rsc_id) + " --move -N " + to_node + " -u " + duration + else: + cmd_str = cmd_head + str(rsc_id) + " --move -N " + to_node status, output = common_utils.run_cmd(cmd_str) + print(cmd_str) if status != 0: return {'action': False, 'error': _(output)} return {"action": True, "info":"Action on resource success"} @@ -1188,6 +1186,96 @@ def unmigrate_rsc(is_all_rscs, rsc_id): return {'action': False, 'error': _(output)} else: return {'action': True, 'info': _('Unmigrate resource success')} +#获取约束条件 +def get_constraints_by_id(rsc_id, action): + """ + { + "action":true, + "data":{ + "before_rscs":[ + + ], + "rsc_id":"c-1", + "after_rscs":[ + + ] + } + } + """ + cmd_str = "" + output = "" + data = {} + split = [] + if str(action) == "order": + cmd_str = "pcs constraint order show" + """ + Ordering Constraints: + start c-pri-1 then start c-pri-2 (kind:Mandatory) + start c-pri-2 then start c-pri-3 (kind:Mandatory) + """ + status, output = common_utils.run_cmd(cmd_str) + if status != 0: + return {'action': False, 'error': _(output)} + #去掉第一行并且按行存入list + out_str = str(output).replace("Ordering Constraints:\n", "").split("\n") + for item in out_str: + #读取每条约束条件语句 + #去掉首尾空格 + tmp = item.strip().split(" ") + split.append(tmp) + after_rscs = [] + before_rscs = [] + for item in split: + #[['start', 'c-pri-1', 'then', 'start', 'c-pri-2', '(kind:Mandatory)'], ['start', 'c-pri-2', 'then', 'start', 'c-pri-3', '(kind:Mandatory)']] + #取1和4 + if item[1] == str(rsc_id): + after_rscs.append(item[4]) + if item[4] == str(rsc_id): + before_rscs.append(item[1]) + data["before_rscs"] = before_rscs + data["after_rscs"] = after_rscs + # if str(action) == "colocation": + # cmd_str = "pcs constraint colocation show" + + + if str(action) == "location": + cmd_str = "pcs constraint location show " + """ + 1Resource: qqq + Enabled on: ha1 (score:INFINITY) (role: Started) + Resource: scsavs + 2Constraint: cli-prefer-scsavs + Rule: boolean-op=and score=INFINITY + Expression: #uname eq string ha2 + Expression: date lt 2020-06-23 08:59:24 +08:00 + """ + status, output = common_utils.run_cmd(cmd_str) + if status != 0: + return {'action': False, 'error': _(output)} + #去掉第一行并且按行存入list + out_str = str(output).replace("Ordering Constraints:\n", "").split("\n") + for i in out_str: + #读取每条约束条件语句 + #去掉首尾空格 + tmp = i.strip().split(" ") + split.append(tmp) + print(split) + after_rscs = [] + before_rscs = [] + for i in split: + #[['start', 'c-pri-1', 'then', 'start', 'c-pri-2', '(kind:Mandatory)'], ['start', 'c-pri-2', 'then', 'start', 'c-pri-3', '(kind:Mandatory)']] + #取1和4 + print(i[1]) + # if i[1] == str(rsc_id): + # after_rscs.append(i[4]) + # if i[4] == str(rsc_id): + # before_rscs.append(i[1]) + # data["before_rscs"] = before_rscs + # data["after_rscs"] = after_rscs + # print(data) + + data["id"] = str(rsc_id) + return data def get_all_rsc_metas(): @@ -1330,7 +1418,7 @@ def get_rsc_meta_attributes(category): if category == "primitive": return {'action': True, 'data': retjson} - + if category == "clone": retjson['interleave'] = {} retjson['interleave']['content'] = {} -- Gitee From e937999248565a38d9f5b9a6f9e04c5551c7dd55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AD=90=E8=B4=A4?= <2367744612@qq.com> Date: Tue, 23 Jun 2020 10:38:15 +0800 Subject: [PATCH 2/7] get_loation --- resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resource.py b/resource.py index 38837e2..d9640b4 100644 --- a/resource.py +++ b/resource.py @@ -1239,7 +1239,7 @@ def get_constraints_by_id(rsc_id, action): if str(action) == "location": - cmd_str = "pcs constraint location show " + cmd_str = "pcs constraint location --full" """ 1Resource: qqq Enabled on: ha1 (score:INFINITY) (role: Started) -- Gitee From c1928c1868533acb02d726f8c667028beac5e959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AD=90=E8=B4=A4?= <2367744612@qq.com> Date: Tue, 23 Jun 2020 15:25:19 +0800 Subject: [PATCH 3/7] migrate and unmmigrate --- resource.py | 65 ++++++++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/resource.py b/resource.py index d9640b4..5eb4c8f 100644 --- a/resource.py +++ b/resource.py @@ -6,6 +6,7 @@ from xml.dom.minidom import parseString from xml.dom.minidom import getDOMImplementation import xml.etree.ElementTree as ET from collections import OrderedDict +import xmltodict def get_rsc_metas(rsc_class, rsc_type, rsc_provider): # item = {'rsc_class':'ocf', 'rsc_type':'IPaddr', 'rsc_provider': 'common'} @@ -818,9 +819,30 @@ def resource_action(rsc_id, action, data): """ if str(action) == "unmigrate": #迁离当前节点 - is_all_rscs = data["is_all_rscs"] - return unmigrate_rsc(is_all_rscs, rsc_id) - #cmd_str = "crm_resource --resource " + str(data["rsc_id"]) + " --move" + cmd_str = "cibadmin --query --scope constraints" + status, output = common_utils.run_cmd(cmd_str) + etree = ET.fromstring(output)#封装 + rsc_name = etree.findall("./rsc_location") + rsc_data = [] + for i in rsc_name: + rsc = { + "rsc" : i.get("rsc", ""), + "rsc_id" : i.get("id","") + } + rsc_data.append(rsc) + if rsc_id == rsc["rsc"]: + print(rsc_id) + print(rsc["rsc"]) + # pcs constraint location delete + location_id = str(rsc["rsc_id"]) + cmd_str= "pcs constraint location delete " + location_id + status, output = common_utils.run_cmd(cmd_str) + print(cmd_str) + # else: + # return {'action': False, 'error': "rsc_id unfound"} + if status != 0: + return {'action': False, 'error': _(output)} + return {"action": True, "info":"Action on resource success"} """ location: @@ -1238,41 +1260,8 @@ def get_constraints_by_id(rsc_id, action): # cmd_str = "pcs constraint colocation show" - if str(action) == "location": - cmd_str = "pcs constraint location --full" - """ - 1Resource: qqq - Enabled on: ha1 (score:INFINITY) (role: Started) - Resource: scsavs - 2Constraint: cli-prefer-scsavs - Rule: boolean-op=and score=INFINITY - Expression: #uname eq string ha2 - Expression: date lt 2020-06-23 08:59:24 +08:00 - """ - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} - #去掉第一行并且按行存入list - out_str = str(output).replace("Ordering Constraints:\n", "").split("\n") - for i in out_str: - #读取每条约束条件语句 - #去掉首尾空格 - tmp = i.strip().split(" ") - split.append(tmp) - print(split) - after_rscs = [] - before_rscs = [] - for i in split: - #[['start', 'c-pri-1', 'then', 'start', 'c-pri-2', '(kind:Mandatory)'], ['start', 'c-pri-2', 'then', 'start', 'c-pri-3', '(kind:Mandatory)']] - #取1和4 - print(i[1]) - # if i[1] == str(rsc_id): - # after_rscs.append(i[4]) - # if i[4] == str(rsc_id): - # before_rscs.append(i[1]) - # data["before_rscs"] = before_rscs - # data["after_rscs"] = after_rscs - # print(data) + # if str(action) == "location": + data["id"] = str(rsc_id) return data -- Gitee From 343bf4dae20c8923e1a19286901620c82599ac95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AD=90=E8=B4=A4?= <2367744612@qq.com> Date: Tue, 23 Jun 2020 15:32:54 +0800 Subject: [PATCH 4/7] migrate and unmmigrate --- resource.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/resource.py b/resource.py index 5eb4c8f..e2182cf 100644 --- a/resource.py +++ b/resource.py @@ -96,7 +96,8 @@ class HAPublic: def add_score(self, score1, score2): if score1 in ["-INFINITY", "-infinity"] or score2 in ["-INFINITY", "-infinity"]: return str("-INFINITY") - if score1 in ["INFINITY", "+INFINITY", "infinity", "+infinity"] or score2 in ["INFINITY", "+INFINITY","infinity", "+infinity"]: + if score1 in ["INFINITY", "+INFINITY", "infinity", "+infinity"] or score2 in + ["INFINITY", "+INFINITY", "infinity", "+infinity"]: return str("INFINITY") return str(int(score1) + int(score2)) @@ -412,6 +413,7 @@ def get_rsc_type(rsc_id): return None def get_rsc_status(rsc_info): + ''' { "@id":"test3", @@ -827,7 +829,7 @@ def resource_action(rsc_id, action, data): for i in rsc_name: rsc = { "rsc" : i.get("rsc", ""), - "rsc_id" : i.get("id","") + "rsc_id" : i.get("id", "") } rsc_data.append(rsc) if rsc_id == rsc["rsc"]: -- Gitee From 75ff1a072d073b834d8192a5a9d0aac17e312d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AD=90=E8=B4=A4?= <2367744612@qq.com> Date: Tue, 23 Jun 2020 15:38:50 +0800 Subject: [PATCH 5/7] migrate and unmmigrate --- resource.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/resource.py b/resource.py index e2182cf..f32f3fd 100644 --- a/resource.py +++ b/resource.py @@ -96,8 +96,7 @@ class HAPublic: def add_score(self, score1, score2): if score1 in ["-INFINITY", "-infinity"] or score2 in ["-INFINITY", "-infinity"]: return str("-INFINITY") - if score1 in ["INFINITY", "+INFINITY", "infinity", "+infinity"] or score2 in - ["INFINITY", "+INFINITY", "infinity", "+infinity"]: + if score1 in ["INFINITY", "+INFINITY", "infinity", "+infinity"] or score2 in ["INFINITY", "+INFINITY", "infinity", "+infinity"]: return str("INFINITY") return str(int(score1) + int(score2)) @@ -413,7 +412,7 @@ def get_rsc_type(rsc_id): return None def get_rsc_status(rsc_info): - + ''' { "@id":"test3", -- Gitee From 7a49131ab6f4fd61b73eb9aae7e5ee89b804e5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AD=90=E8=B4=A4?= <2367744612@qq.com> Date: Tue, 23 Jun 2020 15:54:13 +0800 Subject: [PATCH 6/7] migrate and unmmigrate --- resource.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/resource.py b/resource.py index f32f3fd..2886601 100644 --- a/resource.py +++ b/resource.py @@ -47,6 +47,8 @@ def get_rsc_metas(rsc_class, rsc_type, rsc_provider): data['shortdesc'] = etree.find('shortdesc').text.replace("\n", " ") data['parameters'] = prop data['actions'] = actions + if status != 0: + return {'action': False, 'error': _(output)} return {'action': True, "data": data} @@ -96,7 +98,8 @@ class HAPublic: def add_score(self, score1, score2): if score1 in ["-INFINITY", "-infinity"] or score2 in ["-INFINITY", "-infinity"]: return str("-INFINITY") - if score1 in ["INFINITY", "+INFINITY", "infinity", "+infinity"] or score2 in ["INFINITY", "+INFINITY", "infinity", "+infinity"]: + if score1 in ["INFINITY", "+INFINITY", "infinity", "+infinity"] or score2 in ["INFINITY", + "+INFINITY", "infinity", "+infinity"]: return str("INFINITY") return str(int(score1) + int(score2)) @@ -411,9 +414,11 @@ def get_rsc_type(rsc_id): return None + def get_rsc_status(rsc_info): - ''' + + """ { "@id":"test3", "@resource_agent":"ocf::pacemaker:Dummy", @@ -430,7 +435,7 @@ def get_rsc_status(rsc_info): "@id":"1", "@cached":"false"} } - ''' + """ if rsc_info['@managed'] == "false": return "Unmanaged" @@ -827,8 +832,8 @@ def resource_action(rsc_id, action, data): rsc_data = [] for i in rsc_name: rsc = { - "rsc" : i.get("rsc", ""), - "rsc_id" : i.get("id", "") + "rsc": i.get("rsc", ""), + "rsc_id": i.get("id", "") } rsc_data.append(rsc) if rsc_id == rsc["rsc"]: -- Gitee From dc09798ea77371144136bc323e1ff31ff3915ebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AD=90=E8=B4=A4?= <2367744612@qq.com> Date: Tue, 23 Jun 2020 17:08:15 +0800 Subject: [PATCH 7/7] add unmigrate --- config.py | 15 ++++++++++++--- resource.py | 4 ++-- run.py | 5 ++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/config.py b/config.py index e869f5e..06741a2 100644 --- a/config.py +++ b/config.py @@ -43,10 +43,19 @@ from collections import OrderedDict } ''' -def get_hb_info(data): - print("11111111") +def get_hb_info(): + print("1111") + # cmd_str = "crm_node -l" + # status, output = common_utils.run_cmd(cmd_str) + # print(output) + # aa = str(output).split("\n") + # print(aa) + # cmd_ip = "corosync-cfgtool -a" + + + + - def edit_hb_info(data): if data == {} or data is None: return {'action': False, 'error': _('No input data')} diff --git a/resource.py b/resource.py index 2886601..2529b2c 100644 --- a/resource.py +++ b/resource.py @@ -52,7 +52,7 @@ def get_rsc_metas(rsc_class, rsc_type, rsc_provider): return {'action': True, "data": data} -class HAPublic: +class HAPublic(): def level_init(self): self.level_score = [] # list(self.level_score) nodeinfo = node.get_node_info() @@ -409,7 +409,7 @@ def get_rsc_type(rsc_id): status, output = common_utils.run_cmd(cmd) type_str = output.strip() if status == 0: - rsc_type = type_str.split(' ')[0].replace('<',"") + rsc_type = type_str.split(' ')[0].replace('<', "") return rsc_type return None diff --git a/run.py b/run.py index 1f8544b..a34bbb7 100644 --- a/run.py +++ b/run.py @@ -136,14 +136,13 @@ def create_app(test_config=None): else: return - # config ops + # config ops @app.route('/api/' + API_VERSION + '/haclusters/1/configs', methods=['GET', 'POST', 'PUT']) def config_ops(): if request.method == 'GET': #data = '{"action":true,"data":{ "hbaddrs1":[{"ip":"10.1.167.86","nodeid": "ha1"},{"ip":"10.1.167.81","nodeid": "ha2"},{"ip":"10.1.167.83","nodeid": "ha3"}],"hbaddrs2":[{"ip":"10.1.167.93","nodeid": "ha1"},{"ip":"10.1.167.92","nodeid": "ha2"},{"ip":"10.1.167.84","nodeid": "ha3"}], "hbaddrs2_enabled": 1}}' - data = '{"action":true,"data":{ "hbaddrs1":[{"ip":"10.1.167.86","nodeid": "ha1"},{"ip":"10.1.167.81","nodeid": "ha2"}],"hbaddrs2":[{"ip":"10.1.167.93","nodeid": "ha1"},{"ip":"10.1.167.92","nodeid": "ha2"}], "hbaddrs2_enabled": 1}}' #data = '{"action":true,"data":{ "hbaddrs1":[{"ip":"10.1.167.86","nodeid": "ha1"},{"ip":"10.1.167.81","nodeid": "ha2"}]}}' - return data + return json.dumps(config.get_hb_info()) if request.method == 'POST': data = request.json return json.dumps(config.edit_hb_info(data)) -- Gitee