diff --git a/config.py b/config.py index e869f5eeeecb8cb7cfea4dcee87ccbbc189679fe..06741a2cd29c1a75363d357564ef2e3335501943 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 f4cef399221cd41ee075e71c3f1e51308ed0ef69..2529b2c95319e9a895e0487f8f3e3fe29af140dd 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,10 +47,12 @@ 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} -class HAPublic: +class HAPublic(): def level_init(self): self.level_score = [] # list(self.level_score) nodeinfo = node.get_node_info() @@ -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 = {} @@ -406,14 +409,17 @@ 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 + 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,9 +825,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: @@ -1188,6 +1214,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 +1413,7 @@ def get_rsc_meta_attributes(category): if category == "primitive": return {'action': True, 'data': retjson} - + if category == "clone": retjson['interleave'] = {} retjson['interleave']['content'] = {} diff --git a/run.py b/run.py index 1f8544b910bb5f18313ea5e53ffd6517b92f5d1f..a34bbb7663193573ff4e1e1dab97476c337d277c 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))