diff --git a/resource.py b/resource.py index f4cef399221cd41ee075e71c3f1e51308ed0ef69..b8b4610190a677c035da7432a6463a26ee3ded6e 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'} @@ -46,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} @@ -95,8 +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)) @@ -140,16 +143,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 = {} @@ -411,9 +414,12 @@ 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 +433,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 +806,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,37 +825,47 @@ 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: {"node_level": [{"node": "ns187", "level": "Master Node"}, {"node": "ns188", "level": "Slave 1"}]} """ if str(action) == "location": - #删除所有旧的location约束 - ids = [] - ids = get_rsc_constraint_id(rsc_id, action) - for item in ids: - cmd_str = "pcs constraint location delete " + str(item) - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} - node = "" - score = 0 - #根据主从给节点赋值 + mn = "" + sn = "" for item in data["node_level"]: - if item.get("level", "") == "Master Node": - #master score = 20000 - score = 20000 - elif item.get("level", "") == "Slave 1": - score = 16000 - node = str(item.get("node", "")) - cmd_str = "pcs constraint location " + str(rsc_id) + " prefers " + node + "=" + str(score) - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} + if item["level"] == "Master Node": + mn = str(item["node"]) + else: + sn = str(item["node"]) + cmd_str = "pcs constraint location " + str(rsc_id) + " prefers " + mn + status, output = common_utils.run_cmd(cmd_str) + if status != 0: + return {'action': False, 'error': _(output)} return {"action": True, "info":"Action on resource success"} """ @@ -858,20 +873,17 @@ def resource_action(rsc_id, action, data): {"same_node": ["test1234"],"diff_node": ["group_tomcat"]} """ if str(action) == "colocation": - #删除所有旧约束 - target_id = get_rsc_constraint_id(rsc_id, action) - del_colocation_by_id_action(rsc_id, target_id) - sn = data["same_node"] - dn = data["diff_node"] + sn = str(data["same_node"]) + dn = str(data["diff_node"]) #协同节点 - for item in sn: - cmd_str = "pcs constraint colocation add " + str(rsc_id) + " with " + item + for item in data["same_node"]: + cmd_str = "pcs constraint colocation add " + str(rsc_id) + " with " + sn status, output = common_utils.run_cmd(cmd_str) if status != 0: return {'action': False, 'error': _(output)} #非协同节点 - for item in dn: - cmd_str = "pcs constraint location " + str(rsc_id) + " avoids " + item + " --force" + for item in data["diff_node"]: + cmd_str = "pcs constraint location " + str(rsc_id) + " avoids " + dn status, output = common_utils.run_cmd(cmd_str) if status != 0: return {'action': False, 'error': _(output)} @@ -881,25 +893,19 @@ def resource_action(rsc_id, action, data): {"before_rscs": ["test4","test5"],"after_rscs": ["group1"]} """ if str(action) == "order": - #删除所有之前的order约束 - status, output = common_utils.run_cmd("pcs constraint order delete " + str(rsc_id)) - if status != 0: - return {'action': False, 'error': _(output)} - #提取信息并创建新的order约束 br = data["before_rscs"] ar = data["after_rscs"] rsc_id = str(rsc_id) for i in br: - cmd_br = "pcs constraint order start " + str(i) + " then " + str(rsc_id) - status, output = common_utils.run_cmd(cmd_br) + cmd_str = "pcs constraint order start " + str(i) + " then " + rsc_id + status, output = common_utils.run_cmd(cmd_str) if status != 0: return {'action': False, 'error': _(output)} - for j in ar: - cmd_ar = "pcs constraint order start " + str(rsc_id) + " then " + str(j) - status, output = common_utils.run_cmd(cmd_ar) + for i in ar: + cmd_str = "pcs constraint order start " + rsc_id + " then " + str(i) + status, output = common_utils.run_cmd(cmd_str) if status != 0: return {'action': False, 'error': _(output)} - return {"action": True, "info":"Action on resource success"} def update_resource_attributes(rsc_id, data): @@ -1188,6 +1194,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(): @@ -1330,7 +1393,7 @@ def get_rsc_meta_attributes(category): if category == "primitive": return {'action': True, 'data': retjson} - + if category == "clone": retjson['interleave'] = {} retjson['interleave']['content'] = {} @@ -1371,169 +1434,6 @@ def get_resource_info_by_rsc_id(rsc_id): return ret -#获取单个资源的约束条件(location, colocation, order) -def get_rsc_constraints(rsc_id, action): - data = {} - cmd_str = "cibadmin --query --scope constraints" - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} - etree = ET.fromstring(str(output)) - if str(action) == "order": - et = etree.findall("./rsc_order") - before_class = [] - after_class = [] - for item in et: - first = item.get("first", "") - after = item.get("then", "") - #判断跟rsc_id是否有关系 - if str(first) == str(rsc_id): - # 获取after_class - after_class.append(item.get("then", "")) - if str(after) == str(rsc_id): - #获取before_class - before_class.append(item.get("first", "")) - data["before_rscs"] = before_class - data["after_rscs"] = after_class - data["rsc_id"] = str(rsc_id) - if str(action) == "location": - res = [] - et = etree.findall("./rsc_location") - for item in et: - rsc = item.get("rsc", "") - if str(rsc) == str(rsc_id): - #避开非协同节点约束 - if "score" in item.keys(): - if item.get("score", "") == "-INFINITY": - continue - #获取nodeid - prop = {} - prop["node"] = item.get("node", "") - score = item.get("score", "") - if score == "20000": - prop["level"] = "Master Node" - elif score == "16000": - prop["level"] = "Slave 1" - res.append(prop) - data["node_level"] = res - data["rsc_id"] = str(rsc_id) - if str(action) == "colocation": - #协同节点 - et = etree.findall("./rsc_colocation") - same_node = [] - for item in et: - rsc = item.get("rsc", "") - rsc_with = item.get("with-rsc", "") - if str(rsc) == str(rsc_id): - #获取with-rsc - same_node.append(rsc_with) - if str(rsc_with) == str(rsc): - same_node.append(rsc) - #非协同节点 - et = etree.findall("./rsc_location") - diff_node = [] - for item in et: - #查询rsc - if "rsc" in item.keys() and item.get("rsc") == str(rsc_id): - if "score" in item.keys(): - #如果有score - score = item.get("score", "") - if str(score) == "-INFINITY": - #获取非协同节点 - diff_node.append(item.get("node", "")) - else: - continue - data["same_node"] = same_node - data["diff_node"] = diff_node - data["rsc_id"] = str(rsc_id) - #最终数据格式 - ret = {} - ret["action"] = True - ret["data"] = data - return ret - - -# 按照资源id和action获取相对应的约束条件id(仅location和colocation) -def get_rsc_constraint_id(rsc_id, action): - cmd_str = "cibadmin --query --scope constraints" - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} - etree = ET.fromstring(str(output)) - #按照rsc_id和action获取约束条件id - ids = [] #存储约束条件id - target_id = [] #存储协同节点id - if str(action) == "colocation": - et = etree.findall("./rsc_colocation") - #遍历所有order约束查找含有rsc_id的项并添加进target_id列表里 - #协同节点 - for item in et: - rsc = item.get("rsc", "") - rsc_with = item.get("with-rsc", "") - if str(rsc) == str(rsc_id): - #获取with-rsc - target_id.append(rsc_with) - if str(rsc_with) == str(rsc): - target_id.append(rsc) - #非协同节点 - et = etree.findall("./rsc_location") - for item in et: - #查询rsc - if "rsc" in item.keys() and item.get("rsc") == str(rsc_id): - if "score" in item.keys(): - #如果有score - score = item.get("score", "") - if str(score) == "-INFINITY": - #获取非协同节点 - target_id.append(item.get("node", "")) - else: - continue - return target_id - if str(action) == "location": - et = etree.findall("./rsc_location") - for item in et: - rsc = item.get("rsc", "") - if str(rsc) == str(rsc_id): - #避开非协同节点约束 - if "score" in item.keys() and item.get("score", "") == "-INFINITY": - continue - ids.append(item.get("id", "")) - return ids - return [] - - -#删除source_id的所有colocation约束关系 -def del_colocation_by_id_action(source_id, target_id): - for item in target_id: - #遍历于rsc_id有关的目标节点 - cmd_str = "pcs constraint colocation delete " + str(source_id) + " " + str(item) - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} - #删除非协同节点相关约束条件 - cmd_str = "cibadmin --query --scope constraints" - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} - etree = ET.fromstring(str(output)) - #按照rsc_id和action获取约束条件id - ids = [] #存储约束条件id - et = etree.findall("./rsc_location") - for item in et: - rsc = item.get("rsc", "") - if str(rsc) == str(source_id): - #获取非协同节点约束 - if "score" in item.keys() and item.get("score", "") == "-INFINITY": - ids.append(item.get("id", "")) - for item in ids: - cmd_str = "pcs constraint location delete " + str(item) - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} - - return - - if __name__ == '__main__': #print(get_all_rsc_status()) #print(get_resource_info_by_rsc_id("q11"))