From 630a719a820be7508ab301c09868003f5f5c7a98 Mon Sep 17 00:00:00 2001 From: ZiXian Zhou Date: Thu, 18 Jun 2020 15:32:10 +0800 Subject: [PATCH 1/4] get all resource --- resource.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- run.py | 13 +++++++------ 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/resource.py b/resource.py index e1486e7..60689dd 100644 --- a/resource.py +++ b/resource.py @@ -1,9 +1,50 @@ -''' import common_utils -def get_resouce_add(): - status, output = common_utils.run_cmd("crm_resource --list-standards") - print(output) -''' +import xmltodict +from xml.dom.minidom import parseString +import xml.etree.ElementTree as ET +from collections import OrderedDict +def get_rsc_metas(rsc_class, rsc_type, rsc_provider): + # item = {'rsc_class':'ocf', 'rsc_type':'IPaddr', 'rsc_provider': 'common'} + data = {} + prop = [] + actions = [] + class_name = str(rsc_class) + type_name = str(rsc_type) + provider_name = str(rsc_provider) + # crm_resource --show-metadata ocf:heartbeat:IPaddr + status, output = common_utils.run_cmd("crm_resource --show-metadata " + class_name + ":" + type_name + ":" + provider_name + "") + etree = ET.fromstring(output)#封装 + data_name = etree.get("name", "")#找跟目录下的name + data['name'] = data_name + parameter = etree.findall("./parameters/parameter")#parameter路径 + for i in parameter: + parameters={ + "name": i.get("name", ""), + "required": i.get("required", ""), + "unique": i.get("unique", "") + } + content = i.find("content") + cnt={ + "default": content.get("default", ""), + "type": content.get("type", "") + } + parameters['content'] = cnt + parameters['shortdesc'] = i.find("shortdesc").text.replace("\n", " ") + parameters['longdesc'] = i.find("longdesc").text.replace("\n", " ") + prop.append(parameters) + for action in etree.findall("./actions/action"): + act = {} + for key,value in action.items(): + act[str(key)] = str(value) + actions.append(act) + data['version'] = etree.find('version').text + data['longdesc'] = etree.find('longdesc').text.replace("\n", " ") + data['shortdesc'] = etree.find('shortdesc').text.replace("\n", " ") + data['parameters'] = prop + data['actions'] = actions + return {'action': True, "data": data} + + diff --git a/run.py b/run.py index 20a5832..6c40829 100644 --- a/run.py +++ b/run.py @@ -76,7 +76,7 @@ def create_app(test_config=None): user_name = session.get('user_name') if user_name: return render_template('index.html') - redirect('/login') + return redirect('/login') @app.route('/login') def set_login_html(): @@ -101,11 +101,12 @@ def create_app(test_config=None): def nodes_action(node_id, action): if request.method == 'PUT': return json.dumps(node.get_nodes_action(node_id, action)) - # #获取资源创建 - # @app.route('/api/' + API_VERSION + '/haclusters/1/metas/', methods=['GET', 'POST', 'PUT']) - # def get_resource_creation(): - # if request.method == 'GET': - # return json.dumps(resource.get_resouce_add()) + #获取资源创建 + @app.route('/api/' + API_VERSION + '/haclusters/1/metas//', methods=['GET', 'POST', 'PUT']) + @app.route('/api/' + API_VERSION + '/haclusters/1/metas///', methods=['GET', 'POST', 'PUT']) + def get_rsc_meta(rsc_class, rsc_type, rsc_provider=None): + if request.method == 'GET': + return json.dumps(resource.get_rsc_metas(rsc_class, rsc_type, rsc_provider)) return app -- Gitee From 487729e11cda3867093d484f021f46eebba3e41a Mon Sep 17 00:00:00 2001 From: ZiXian Zhou Date: Thu, 18 Jun 2020 15:36:46 +0800 Subject: [PATCH 2/4] get all resource --- resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resource.py b/resource.py index 60689dd..ae80d11 100644 --- a/resource.py +++ b/resource.py @@ -34,7 +34,7 @@ def get_rsc_metas(rsc_class, rsc_type, rsc_provider): prop.append(parameters) for action in etree.findall("./actions/action"): act = {} - for key,value in action.items(): + for key, value in action.items(): act[str(key)] = str(value) actions.append(act) data['version'] = etree.find('version').text -- Gitee From 9457b15a4e625c517eb538ef93fdc91e94461b45 Mon Sep 17 00:00:00 2001 From: ZiXian Zhou Date: Thu, 18 Jun 2020 17:28:04 +0800 Subject: [PATCH 3/4] get all resource --- resource.py | 464 ---------------------------------------------------- 1 file changed, 464 deletions(-) diff --git a/resource.py b/resource.py index 43dd1ab..ae80d11 100644 --- a/resource.py +++ b/resource.py @@ -1,6 +1,5 @@ import common_utils import xmltodict -<<<<<<< HEAD from xml.dom.minidom import parseString import xml.etree.ElementTree as ET from collections import OrderedDict @@ -46,471 +45,8 @@ def get_rsc_metas(rsc_class, rsc_type, rsc_provider): return {'action': True, "data": data} -======= -import json -import node -from xml.dom.minidom import parseString -from xml.dom.minidom import getDOMImplementation -import xml.etree.ElementTree as ET ->>>>>>> 3abe5f71dfab22c8d5e52b0f22d658d3c552dea8 - - -class HAPublic: - def level_init(self): - self.level_score = [] # list(self.level_score) - nodeinfo = node.get_node_info() - node_num = len(nodeinfo['data']) - max = node_num - 1 - for i in range(max): - self.level_score.append(str(16000 - 1000 * i)) - - return - - def turn_score_to_level(self, score): - self.level_init() - if score in ["20000"]: # ["INFINITY", "+INFINITY", "infinity", "+infinity"]: - return "Master Node" - - if score in ["-INFINITY", "-infinity"]: - return ("No Run Node") - - if score not in self.level_score: - return score - level = 1 - for s in self.level_score: - if s == score: - return ("Slave %d") % (level) - level = level + 1 - - return score - - def turn_level_to_score(self, level): - self.level_init() - if level == "Master Node": - return "20000" # "INFINITY" - - if level == ("No Run Node"): - return "-INFINITY" - - if ("Slave ") not in level: - return level - else: - index = int(level[len(str("Slave ")):]) - return self.level_score[index - 1] - - 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"]: - return str("INFINITY") - return str(int(score1) + int(score2)) - - -web_manager = HAPublic() - -# 获取顶级显示的资源 -def get_top_rsc(): - status, stdout = common_utils.run_cmd("cibadmin --query --scope resources") - json_str = json.dumps(xmltodict.parse(stdout)) - res_json = json.loads(json_str)['resources'] - top_rsc = [] - if "clone" in res_json: - clone = res_json["clone"] - if not isinstance(clone, list): - top_rsc.append(clone['@id']) - else: - for rsc in clone: - top_rsc.append(rsc['@id']) - - if "group" in res_json: - group = res_json["group"] - if not isinstance(group, list): - top_rsc.append(group['@id']) - else: - for rsc in group: - top_rsc.append(rsc['@id']) - - if "primitive" in res_json: - primitive = res_json["primitive"] - if not isinstance(primitive, list): - top_rsc.append(primitive['@id']) - else: - for rsc in primitive: - top_rsc.append(rsc['@id']) - return top_rsc - -def get_all_constraints(): - - data = {} - status, stdout = common_utils.run_cmd("cibadmin -Q") - xml_nodes = parseString(stdout).documentElement - for rsc_id in get_top_rsc(): - rsc = rsc_id.split(":")[0] - data[rsc] = {} - data[rsc]["before_rscs"] = [] - data[rsc]["after_rscs"] = [] - data[rsc]["same_node"] = [] - data[rsc]["diff_node"] = [] - data[rsc]['location'] = [] - - constraints = xml_nodes.getElementsByTagName('constraints')[0] - ############location############ - for location in constraints.getElementsByTagName('rsc_location'): - if location.getAttribute('id').startswith('cli-prefer-'): - continue - node = location.getAttribute('node') - rsc_id = location.getAttribute('rsc') - score = location.getAttribute('score') - location_single = {} - location_single['node'] = node - location_single['level'] = web_manager.turn_score_to_level(score) - # location_list.append(location_single) - if rsc_id in data.keys(): - data[rsc_id]["location"].append(location_single) - - ########order######## - for node in constraints.getElementsByTagName('rsc_order'): - first_rsc_id = node.getAttribute("first") - then_rsc_id = node.getAttribute('then') - try: - score = node.getAttribute('score') - if score is None or len(score) == 0: - score = 'infinity' - if score not in ["INFINITY", "+INFINITY", "infinity", "+infinity"]: - continue - except: - pass - if first_rsc_id in data.keys(): - data[first_rsc_id]["after_rscs"].append({'id': then_rsc_id}) - - if then_rsc_id in data.keys(): - data[then_rsc_id]["before_rscs"].append({'id': first_rsc_id}) - - ##########colocation############### - for node in constraints.getElementsByTagName('rsc_colocation'): - first_rsc_id = node.getAttribute("rsc") - with_rsc_id = node.getAttribute('with-rsc') - try: - score = node.getAttribute('score') - if score in ["INFINITY", "+INFINITY", "infinity", "+infinity"]: - rsc = {} - rsc['rsc'] = first_rsc_id - rsc['with_rsc'] = with_rsc_id - data[first_rsc_id]["same_node"].append(rsc) - data[with_rsc_id]["same_node"].append(rsc) - elif score in ["-INFINITY", "-infinity"]: - rsc = {} - rsc['rsc'] = first_rsc_id - rsc['with_rsc'] = with_rsc_id - data[first_rsc_id]["diff_node"].append(rsc) - data[with_rsc_id]["diff_node"].append(rsc) - except: - pass - - constraints = [] - for rsc_id in data.keys(): - constraint = {} - constraint['id'] = rsc_id - colocation = {} - colocation['same_node'] = data[rsc_id]["same_node"] - colocation['diff_node'] = data[rsc_id]["diff_node"] - colocation['same_node_num'] = len(colocation['same_node']) - colocation['diff_node_num'] = len(colocation['diff_node']) - order = {} - order['before_rscs'] = data[rsc_id]["before_rscs"] - order['after_rscs'] = data[rsc_id]["after_rscs"] - order['before_rscs_num'] = len(order['before_rscs']) - order['after_rscs_num'] = len(order['after_rscs']) - constraint['location'] = data[rsc_id]["location"] - constraint['colocation'] = colocation - constraint['order'] = order - constraints.append(constraint) - return {'action': True, 'data': constraints} - -#获取可迁移资源 -def get_all_migrate_rscs(): - status, stdout = common_utils.run_cmd("cibadmin -Q") - xml_nodes = parseString(stdout).documentElement - rsc_locations = {} - for rsc_location in xml_nodes.getElementsByTagName("rsc_location"): - id = str(rsc_location.getAttribute("id")) - rsc_locations[id] = rsc_location - migrate_ids = {} - rsc_ids = [] - for id in rsc_locations: - for prefix in ["cli-prefer-", "cli-standby-"]: - if id.startswith(prefix): - split_id = id.split(prefix) - if len(split_id) > 1: - rsc_id = split_id[1] - if not rsc_id in migrate_ids: - migrate_ids[rsc_id] = [] - migrate_ids[rsc_id].append(id) - rsc_list = [] - if migrate_ids == {}: - rsc_list = [] - else: - rsc_list = list(migrate_ids.keys())[:] - return rsc_list - -# 获取资源的类型 -def get_rsc_type(rsc_id): - cmd = "cibadmin --query --scope resources|grep 'id=\""+ str(rsc_id) + "\"'" - status, output = common_utils.run_cmd(cmd) - type_str = output.strip() - if status == 0: - rsc_type = type_str.split(' ')[0].replace('<',"") - return rsc_type - - return None - -# 获取资源的运行状态 -def get_rsc_status(rsc_id): - return - -# status_messesge 资源的状态信息,如果资源出现异常时,会出现该错误信息 -# status 资源运行状态 -# running_node 运行节点 - -def get_resource_info(): - - constraints = get_all_constraints() # /get_all_constraints - if not constraints['action']: - return json.dumps(constraints) - re_all_rsc_status = {} - re_all_rsc_status['data'] = [] - print(constraints) - for constraint in constraints['data']: - print(constraint) - rsc_id = str(constraint['id']) - print(rsc_id) - # 添加是否允许回迁操作 - migrate_rscs = get_all_migrate_rscs() - if rsc_id in migrate_rscs: - constraint['allow_unmigrate'] = True - else: - constraint['allow_unmigrate'] = False - type = get_rsc_type(rsc_id) - constraint['type'] = type - print(constraints) - - -def resource_action(rsc_id, action, data): - #单个资源操作 - """ - operations: start stop delete cleanup - Example input: - {} - """ - cmd = "crm_resource --resource " - if str(action) == "start": - cmd_str = cmd + str(rsc_id) + " --set-parameter target-role --meta --parameter-value started" - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} - return {"action": True, "info":"Action on resource success"} - if str(action) == "stop": - cmd_str = cmd + str(rsc_id) + " --set-parameter target-role --meta --parameter-value stopped" - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} - return {"action": True, "info":"Action on resource success"} - if str(action) == "delete": - #通过id查询category - if get_rsc_category(rsc_id) == "group": - cmd_str = "crm_resource -D -r " + str(rsc_id) + " -t group" - else: - cmd_str = "crm_resource --resource " + str(rsc_id) + " --delete -t primitive" - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} - return {"action": True, "info":"Action on resource success"} - if str(action) == "cleanup": - cmd_str = cmd + str(rsc_id) + " --clear" - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} - return {"action": True, "info":"Action on resource success"} - - """ - Example input: - migrate: - {"is_force": true, "to_node": "ns187", "period": "PYMDTHM3S"} - """ - if str(action) == "migrate": - #迁移节点 - duration = "" - 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 - status, output = common_utils.run_cmd(cmd_str) - if status != 0: - return {'action': False, 'error': _(output)} - return {"action": True, "info":"Action on resource success"} - - """ - unmigrate: - {"rsc_id": "kk1", "is_all_rscs": false} - """ - #!!!需要改写获取migrate列表的函数!!! - 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" - - """ - location: - {"node_level": [{"node": "ns187", "level": "Master Node"}, {"node": "ns188", "level": "Slave 1"}]} - """ - if str(action) == "location": - mn = "" - sn = "" - for item in data["node_level"]: - 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"} - - """ - colocation: - {"same_node": ["test1234"],"diff_node": ["group_tomcat"]} - """ - if str(action) == "colocation": - sn = str(data["same_node"]) - dn = str(data["diff_node"]) - #协同节点 - 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)} - #!!!需要添加:非协同节点!!! - - - """ - {"before_rscs": ["test4","test5"],"after_rscs": ["group1"]} - """ - #!!!需要添加关于考虑约束条件的语句!!! - if str(action) == "order": - br = data["before_rscs"] - ar = data["after_rscs"] - rsc_id = str(rsc_id) - for i in 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 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)} - - -#获取资源category(primitive, group, clone) -def get_rsc_category(rsc_id): - cmd_str = "crm_resource --resource " + str(rsc_id) + " --query-xml" - status, output = common_utils.run_cmd(cmd_str) #输出为xml格式 - if status != 0: - return {'action': False, 'error': _(output)} - #format - xml_data = output.split(":\n") - xml = xml_data[1] - etree = ET.fromstring(xml) - ct = str(etree.tag) - return ct - - -def unmigrate_rsc(is_all_rscs, rsc_id): - # 保留标签 is_all_rscs = False - - # if rsc_id not in get_all_rsc(): - # return {'action': False, 'error': _("Cannot find the %s: %s") % (_("resource"), str(rsc_id))} - - rsc_locations = {} - for rsc_location in get_all_migrate_rscs(): - id = str(rsc_location.getAttribute("id")) - rsc_locations[id] = rsc_location - migrate_ids = {} - status = "" - output = "" - for id in rsc_locations: - for prefix in ["cli-prefer-", "cli-standby-"]: - if id.startswith(prefix): - split_id = id.split(prefix) - if len(split_id) > 1: - tmp_rsc_id = split_id[1] - if not tmp_rsc_id in migrate_ids: - migrate_ids[tmp_rsc_id] = [] - migrate_ids[tmp_rsc_id].append(id) - if is_all_rscs: - for rsc_id in migrate_ids.keys(): - for id in migrate_ids[rsc_id]: - xml_str = rsc_locations[id].toxml().replace("\n", "") - status, output = common_utils.run_cmd("cib_delete\nconstraints\n%s" % (str(xml_str))) - else: - if rsc_id not in migrate_ids.keys(): - return {'action': True, 'info': "Unmigrate resource success"} - for id in migrate_ids[rsc_id]: - xml_str = rsc_locations[id].toxml().replace("\n", "") - status, output = common_utils.run_cmd("cib_delete\nconstraints\n%s" % (str(xml_str))) - - if status != 0: - return {'action': False, 'error': _(output)} - else: - return {'action': True, 'info': _('Unmigrate resource success')} - - -def get_rsc_metas(): - #获取支持的资源代理标准 - standards = [] - status, output = common_utils.run_cmd("crm_resource --list-standards") - standards = str(output).split("\n") - print(standards) - data = {} - res = {} - for st in standards: - if st == "ocf": - #获取providers列表 - status, output = common_utils.run_cmd("crm_resource --list-ocf-providers") - pvds = [] - pvds = str(output).split("\n") - #根据providers列出所有资源 - for p in pvds: - status, output = common_utils.run_cmd("crm_resource --list-agents ocf:heartbeat") - ag = [] - ag = str(output).split("\n") - res[str(p)] = ag - data["ocf"] = res - #lsb目前没有 - elif st == "lsb": - continue - else: - print(st) - status, output = common_utils.run_cmd("crm_resource --list-agents " + st) - la = [] - la = str(output).split("\n") - print(la) - data[st] = la - return data - -def get_rsc_meta_attributes(): - return -if __name__ == '__main__': - print(get_resource_info()) - #get_top_rsc() - # get_rsc_type("jjj") -- Gitee From 513a989183aa490e442af24f7bbb1a4a0c1f172e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AD=90=E8=B4=A4?= <2367744612@qq.com> Date: Thu, 18 Jun 2020 17:46:12 +0800 Subject: [PATCH 4/4] update run.py. --- run.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/run.py b/run.py index c443ec4..199dc7c 100644 --- a/run.py +++ b/run.py @@ -190,17 +190,12 @@ def create_app(test_config=None): def nodes_action(node_id, action): if request.method == 'PUT': return json.dumps(node.get_nodes_action(node_id, action)) -<<<<<<< HEAD #获取资源创建 @app.route('/api/' + API_VERSION + '/haclusters/1/metas//', methods=['GET', 'POST', 'PUT']) @app.route('/api/' + API_VERSION + '/haclusters/1/metas///', methods=['GET', 'POST', 'PUT']) def get_rsc_meta(rsc_class, rsc_type, rsc_provider=None): if request.method == 'GET': return json.dumps(resource.get_rsc_metas(rsc_class, rsc_type, rsc_provider)) -======= - ->>>>>>> 3abe5f71dfab22c8d5e52b0f22d658d3c552dea8 - return app if __name__ == '__main__': -- Gitee