From 14d836ee01529bafdd30ece4916cfbd3f0c04c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AD=90=E8=B4=A4?= <2367744612@qq.com> Date: Wed, 24 Jun 2020 16:04:10 +0800 Subject: [PATCH 1/2] get_node_list --- config.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- run.py | 5 +---- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/config.py b/config.py index e869f5e..f205562 100644 --- a/config.py +++ b/config.py @@ -43,10 +43,71 @@ from collections import OrderedDict } ''' -def get_hb_info(data): - print("11111111") +def get_hb_info(): + """ + GET /api/v1/haclusters/1/configs +{ +    "action":true, +    "data":{         +            "hbaddrs1":[ +                { +                    "ip":"10.1.167.86", +                    "nodeid": "ha1"                + }, +                { +                    "ip":"10.1.167.81", +                    "nodeid": "ha2" +                } +            ], +        +            "hbaddrs2":[p +                { +                    "ip":"10.1.167.93", +                    "nodeid": "ha1" +                }, +                { +                    "ip":"10.1.167.92", +                    "nodeid": "ha2" +                } +            ], +            "hbaddrs2_enabled": 1 +  +} + """ + cmd_str = "crm_node -l" #获取node节点信息 + status, output = common_utils.run_cmd(cmd_str) + node_data = str(output).split("\n") + data = {} + hbaddrs1 = [] + hbaddrs2 = [] + nid = [] + for i in node_data: + ip1 = {} + ip2 = {} + node = i.split(" ") + # print(node) + nid.append(node[0]) + ip1['nodeid'] = node[1] + ip2['nodeid'] = node[1] + for i in nid: + status, output = common_utils.run_cmd("corosync-cfgtool -a" + str(i)) + ip = str(output).split(" ")#ip + if len(ip) <= 1: + ip1['ip'] = ip[0] + if len(ip) >= 1: + ip1['ip'] = ip[0] + ip2['ip'] = ip[1] + hbaddrs1.append(ip1) + hbaddrs2.append(ip2) + data["hbaddrs1"] = hbaddrs1 + data["hbaddrs2"] = hbaddrs2 + data["hbaddrs2_enabled"] = 1 + if status != 0: + return {'action': False, 'error': _(output)} + else: + return {'action': True, "data": data} + - def edit_hb_info(data): if data == {} or data is None: return {'action': False, 'error': _('No input data')} diff --git a/run.py b/run.py index 22867b0..4b212d7 100644 --- a/run.py +++ b/run.py @@ -140,10 +140,7 @@ def create_app(test_config=None): @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)) -- Gitee From 226c467d24c5f91d6d0be8897a3c7cb2f9cdfce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AD=90=E8=B4=A4?= <2367744612@qq.com> Date: Wed, 24 Jun 2020 16:47:47 +0800 Subject: [PATCH 2/2] get-nodes_list and create_resource_target-role_stopped --- resource.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/resource.py b/resource.py index 2f18d4b..4d41664 100644 --- a/resource.py +++ b/resource.py @@ -1035,7 +1035,7 @@ def update_resource_attributes(rsc_id, data): status, output = common_utils.run_cmd(cmd_str) if "instance_attributes" in data: inst = data["instance_attributes"] - cmd_str = "crm_resource --resource " + str(rsc_id) + " --set-parameter " + cmd_str = "crm_resource --resource " + str(rsc_id) + " --set-parameter " for key in inst.keys(): value = inst[key] cmd = cmd_str + str(key) + " --parameter-value " + str(value) @@ -1187,6 +1187,8 @@ def create_resource(data): "id":"test1" } """ + # 设置元属性值 +# if data == {} or data is None: return {'action': False, 'error': _('No input data')} rsc_id = str(data["id"]) @@ -1199,17 +1201,24 @@ def create_resource(data): rsc_class = " class=\"" + str(data["class"]) + "\"" rsc_type = " type=\"" + str(data["type"]) + "\"" cmd = "cibadmin --create -o resources --xml-text '<" + crm = "crm_resource -m -r " + role = " --set-parameter target-role --parameter-value stopped" + # crm_resource -m -r xcv --set-parameter target-role --parameter-value stopped if "provider" in data: provider = " provider=\"" + str(data["provider"]) + "\"" cmd_str = cmd + cate + rsc_id_str + rsc_class + rsc_type + provider + ">'" + cmd_value = crm + rsc_id + role status, output = common_utils.run_cmd(cmd_str) + status, output = common_utils.run_cmd(cmd_value) if status != 0: return {'action': False, 'error': _(output)} else: update_resource_attributes(rsc_id, data) else: cmd_str = cmd + cate + rsc_id_str + rsc_class + rsc_type + ">'" + cmd_value = crm + rsc_id + role status, output = common_utils.run_cmd(cmd_str) + status, output = common_utils.run_cmd(cmd_value) if status != 0: return {'action': False, 'error': _(output)} else: -- Gitee