diff --git a/resource.py b/resource.py index 16bff8ec9d09933f48b43b7828d579af02dc69a4..9bb2b0adfe1cb9113064a17cbae1ee99ba4f15de 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'} @@ -95,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)) @@ -140,16 +140,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 +412,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 +427,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 +800,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"} @@ -820,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: @@ -930,6 +950,14 @@ def update_resource_attributes(rsc_id, data): # #修改operation操作属性 if "actions" in data: action = data["actions"] #list + #删除之前的所有操作属性 + op_list = get_all_ops(rsc_id) + if op_list: + cmd_del_head = "pcs resource op delete " + str(rsc_id) + for op in op_list: + cmd_del = cmd_del_head + " " + str(op) + status, output = common_utils.run_cmd(cmd_del) + cmd_in = "pcs resource update " + str(rsc_id) + " op" for ops in action: name = str(ops["name"]) @@ -1161,6 +1189,63 @@ 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": + + + data["id"] = str(rsc_id) + return data def get_all_rsc_metas(): @@ -1303,7 +1388,7 @@ def get_rsc_meta_attributes(category): if category == "primitive": return {'action': True, 'data': retjson} - + if category == "clone": retjson['interleave'] = {} retjson['interleave']['content'] = {}