diff --git a/common_utils.py b/common_utils.py index 16e297a2787ec213c43b76de1f89a199c04c4877..915fcdb773f6ed516606074a2b6c0205fa008708 100644 --- a/common_utils.py +++ b/common_utils.py @@ -468,8 +468,13 @@ def get_resource_info_from_xml(cl, et): prop[name] = value if cl == "operations": prop = [] +<<<<<<< HEAD res = {} for item in et.findall("./op"): +======= + for item in et.findall("./op"): + res = {} +>>>>>>> fix update error in primitive rsc for key, value in item.items(): res[str(key)] = str(value) prop.append(res) diff --git a/resource.py b/resource.py index 8e48cc2904a63bf1d3694386b44b29a9f5530564..ce505939f55659867a56cd5d3dcd069b12c64bb7 100644 --- a/resource.py +++ b/resource.py @@ -189,7 +189,7 @@ def get_sub_rscs(rsc_id): sub_rsc["status_message"] = "" sub_rsc["type"] = "primitive" sub_rsc["svc"] = clone_aim["primitive"]["@type"] - + sub_rsc["running_node"] = [] sub_rscs.append(sub_rsc) if rsc_type == "group": @@ -485,8 +485,7 @@ def get_all_rsc_status(): if isinstance(resources_clone, list): for resource in resources_clone: all_run_rscs.append(resource["@id"]) - # 子资源是普通资源 - if "resource" in resource: + if "resource" in resources_clone: sub_resources = resource["resource"] index = 0 clone_run_nodes = [] @@ -777,6 +776,9 @@ def resource_action(rsc_id, action, data): #通过id查询category if get_rsc_category(rsc_id) == "group": cmd_str = "crm_resource -D -r " + str(rsc_id) + " -t group" + elif get_rsc_category(rsc_id) == "clone": + clone_str = str(rsc_id[:-6]) + cmd_str = "pcs resource delete" + " " + clone_str else: cmd_str = "crm_resource --resource " + str(rsc_id) + " --delete -t primitive" status, output = common_utils.run_cmd(cmd_str) @@ -798,11 +800,14 @@ def resource_action(rsc_id, action, data): if str(action) == "migrate": #迁移节点 duration = "" + to_node = "" if "period" in data.keys() and data["period"] != "PYMDT0HMS": duration = str(data["period"]) #持续时间 - to_node = str(data["to_node"]) #迁移到指定节点 + 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 + " -u " + duration + cmd_str = cmd_head + str(rsc_id) + " --move -N " + to_node + print(cmd_str) status, output = common_utils.run_cmd(cmd_str) if status != 0: return {'action': False, 'error': _(output)} @@ -904,17 +909,16 @@ def update_resource_attributes(rsc_id, data): "class":"ocf" } """ + status = "" + output = "" if data == {} or data is None: return {'action': False, 'error': _('No input data')} - if "meta_attributes" in data: meta = data["meta_attributes"] for key in meta.keys(): value = meta[key] cmd_str = "crm_resource -m -r " + str(rsc_id) + " -p " + str(key) + " -v " + str(value) status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} if "instance_attributes" in data: inst = data["instance_attributes"] cmd_str = "crm_resource --resource " + str(rsc_id) + " --set-parameter " @@ -922,49 +926,26 @@ def update_resource_attributes(rsc_id, data): value = inst[key] cmd = cmd_str + str(key) + " --parameter-value " + str(value) status, output = common_utils.run_cmd(cmd) - if status != 0: - return {'action': False, 'error': _(output)} - #修改operation操作属性 + # #修改operation操作属性 if "actions" in data: action = data["actions"] #list - if is_in_op(rsc_id) == False: - # 没有设置过op - cmd_head = "pcs resource op add " + str(rsc_id) - cmd_op = cmd_head - for op in action: - name = str(op["name"]) - cmd_op = cmd_op + " " + name - for key in op.keys(): - #interval, start-delay - if key != "name": - value = op[key] - cmd_op = cmd_op + " " + str(key) + "=" + str(value) - status, output = common_utils.run_cmd(cmd_op) - if status != 0: - return {'action': False, 'error': _(output)} - else: - #已经设置过op - cmd_in = "pcs resource update " + str(rsc_id) + " op" - for op in action: - name = str(op["name"]) - cmd_in = cmd_in + " " + name - - ops = op.items() - if "interval" in ops: - cmd_in = cmd_in + " " + "interval=" + str(ops["interval"]) - if "start-delay" in ops: - cmd_in = cmd_in + " " + "start-delay=" + str(ops["start-delay"]) - if "timeout" in ops: - cmd_in = cmd_in + " " + "timeout=" + str(ops["timeout"]) - if "role" in ops: - cmd_in = cmd_in + " " + "role=" + str(ops["role"]) - if "requires" in ops: - cmd_in = cmd_in + " " + "requires=" + str(ops["requires"]) - if "on-fail" in ops: - cmd_in = cmd_in + " " + "on-fail=" + str(ops["on-fail"]) - status, output = common_utils.run_cmd(cmd_in) - if status != 0: - return {'action': False, 'error': _(output)} + cmd_in = "pcs resource update " + str(rsc_id) + " op" + for ops in action: + name = str(ops["name"]) + cmd_in = cmd_in + " " + name + if "interval" in ops: + cmd_in = cmd_in + " " + "interval=" + str(ops["interval"]) + if "start-delay" in ops: + cmd_in = cmd_in + " " + "start-delay=" + str(ops["start-delay"]) + if "timeout" in ops: + cmd_in = cmd_in + " " + "timeout=" + str(ops["timeout"]) + if "role" in ops: + cmd_in = cmd_in + " " + "role=" + str(ops["role"]) + if "requires" in ops: + cmd_in = cmd_in + " " + "requires=" + str(ops["requires"]) + if "on-fail" in ops: + cmd_in = cmd_in + " " + "on-fail=" + str(ops["on-fail"]) + status, output = common_utils.run_cmd(cmd_in) if data["category"] == "group": #组资源加入新资源 @@ -983,27 +964,29 @@ def update_resource_attributes(rsc_id, data): for r in rscs: cmd_str = cmd_str + " " + str(r) status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} + + if status != 0: + return {'action': False, 'error': _(output)} return {'action': True, 'info': _('Update resource attributes Success')} #根据rsc_id查询资源是否已有op -def is_in_op(rsc_id): - cmd_str = "crm_resource --resource " + str(rsc_id) + " --query-xml" - status, output = common_utils.run_cmd(cmd_str) #输出为xml格式 - #format - xml_data = output.split(":\n") - if 1 > len(xml_data): - return False - xml = xml_data[1] - etree = ET.fromstring(xml) - if etree.findall("./operations/op") is None: - return False - else: - return True - return False +# def is_in_op(rsc_id): +# cmd_str = "crm_resource --resource " + str(rsc_id) + " --query-xml" +# status, output = common_utils.run_cmd(cmd_str) #输出为xml格式 +# #format +# xml_data = output.split(":\n") +# if 1 > len(xml_data): +# return False +# else: +# xml = xml_data[1] +# etree = ET.fromstring(xml) +# if etree.findall("./operations/op") is None: +# return False +# else: +# return True +# return False @@ -1040,20 +1023,32 @@ def create_resource(data): """ if data == {} or data is None: return {'action': False, 'error': _('No input data')} - rsc_id = " id=\"" + str(data["id"]) + "\"" + rsc_id = str(data["id"]) cate = str(data["category"]) #判断category if cate == "primitive": + rsc_id_str = " id=\"" + rsc_id + "\"" + status = "" + output = "" rsc_class = " class=\"" + str(data["class"]) + "\"" rsc_type = " type=\"" + str(data["type"]) + "\"" cmd = "cibadmin --create -o resources --xml-text '<" if "provider" in data: provider = " provider=\"" + str(data["provider"]) + "\"" - cmd_str = cmd + cate + rsc_id + rsc_class + rsc_type + provider + ">'" + cmd_str = cmd + cate + rsc_id_str + rsc_class + rsc_type + provider + ">'" status, output = common_utils.run_cmd(cmd_str) + if status != 0: + return {'action': False, 'error': _(output)} + else: + update_resource_attributes(rsc_id, data) else: - cmd_str = cmd + cate + rsc_id + rsc_class + rsc_type + ">'" + cmd_str = cmd + cate + rsc_id_str + rsc_class + rsc_type + ">'" status, output = common_utils.run_cmd(cmd_str) + if status != 0: + return {'action': False, 'error': _(output)} + else: + update_resource_attributes(rsc_id, data) + elif cate == "group": """ @@ -1070,9 +1065,15 @@ def create_resource(data): } """ rscs = data["rscs"] + rsc_id = str(data["id"]) cmd_str = "pcs resource group add " + rsc_id for r in rscs: cmd_str = cmd_str + " " + str(r) + status, output = common_utils.run_cmd(cmd_str) + if status != 0: + return {'action': False, 'error': _(output)} + else: + update_resource_attributes(rsc_id, data) elif cate == "clone": """ @@ -1088,11 +1089,12 @@ def create_resource(data): """ ori_id = str(data["rsc_id"]) cmd_str = "pcs resource clone " + ori_id - # status, output = common_utils.run_cmd(cmd_str) - # if status != 0: - # return {'action': False, 'error': _(output)} - #更新属性 - update_resource_attributes(rsc_id, data) + status, output = common_utils.run_cmd(cmd_str) + if status != 0: + return {'action': False, 'error': _(output)} + else: + update_resource_attributes(rsc_id, data) + return {"action":True, "info":"Add " + str(cate) + " resource success"}