From ab90bfe7c79d0f3f151787066edbdbed4086db27 Mon Sep 17 00:00:00 2001 From: Zixian Zhou Date: Fri, 12 Jun 2020 20:18:50 +0800 Subject: [PATCH 1/4] Add interface of get node list and node ip --- node.py | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ run.py | 24 ++++++++++++- 2 files changed, 125 insertions(+), 1 deletion(-) diff --git a/node.py b/node.py index e69de29..2cab990 100644 --- a/node.py +++ b/node.py @@ -0,0 +1,102 @@ +import common_utils +import json +import xml.etree.ElementTree as ET +from xml.dom.minidom import parseString +from flask import session +""" +{ + "action":true, + "data":[ + { + "status":"Master", + "id":"ns187", + "is_dc":true + }, + { + "status":"Not Running/Standby", + "id":"ns188", + "is_dc":false + } + ] +} +""" +tree = ET.ElementTree() +nid = [] +def get_node_info(): + data = [] + status, output = common_utils.run_cmd("crm_mon --as-xml") + if status != 0: + return {'action':False, 'error': output} + new_nodes = parseString(output).documentElement + for nodes in new_nodes.getElementsByTagName('nodes'): + for node in nodes.getElementsByTagName('node'): + name = node.getAttribute('name') + id = node.getAttribute('id') + online = node.getAttribute('online') + standby = node.getAttribute('standby') + is_dc = node.getAttribute('is_dc') + status = "" + if is_dc == "true": + if standby == "true": + if online == "true": + status = "Master/Standby" + else: + status = "Not Running" + + else: + if online == "true": + status = "Master" + else: + status = "Not Running" + else: + if standby == "true": + if online == "true": + status = "Standby" + else: + status = "Not Running" + + else: + if online == "true": + status = "Running" + else: + status = "Not Running" + status_json = {} + status_json['id'] = name + status_json['status'] = status + status_json['is_dc'] = is_dc + data.append(status_json) + #pmrint(data) + id = data[0]["id"] + nid.append(id) + + if data != []: + return {"action":True, "data":data} + else: + return {"action":False, "data":"Get node failed!"} + +def get_nodeid_info(nodeid): + nameid = str(nodeid) + status, output = common_utils.run_cmd("cat /etc/hosts|grep \""+ nameid +"\"|awk -F ' ' '{print $1}'") + nodeip = str(output) + return {'action': True, 'data': {'ips': nodeip}} +''' +def get_nodes_action(node_id,action): + nodeid = str(node_id) + #节点未开启 + if nodeid == None: + return {'action': False, 'error': _("Cannot find the")} + #备用操作 + if action == 'standby': + status,output = common_utils.run_cmd("pcs node standby \""+ nodeid +"\" ") + #不备用操作 + if action == 'unstandby': + status,output = commom_utils.run_cmd("pcs node unstandby \""+nodeid +"\" ") + if action == 'start': + status,output = common_utils.run_cmd("pcs cluster start \""+ nodeid+"\" ") + else: + status,output = common_utils.run_cmd("pcs cluster stop \""+ nodeid+"\" ") + return {'action':True,'data':} + +''' +if __name__ == '__main__': + get_node_info() diff --git a/run.py b/run.py index 5fbc95e..bdbfa94 100644 --- a/run.py +++ b/run.py @@ -11,6 +11,7 @@ import gettext import common_utils import re from flask import jsonify +import node API_VERSION = "v1" app_name = "ha-api" @@ -85,8 +86,29 @@ def create_app(test_config=None): url mapping for index html file, which with @login_required decorated """ return render_template('index.html') - + #获取node + @app.route('/api/'+ API_VERSION + '/haclusters/1/nodes/',methods=['GET','POST','PUT']) + def get_nodes(): + if request.method == 'GET': + return json.dumps( node.get_node_info() ) + else: + return + #根据id取单个node + @app.route('/api/'+ API_VERSION + '/haclusters/1/nodes/',methods=['GET','POST','PUT']) + def get_nodeid(nodeid): + if request.method == 'GET': + return json.dumps( node.get_nodeid_info(nodeid) ) + else: + return + + #node状态修改 + @app.route('/api/'+ API_VERSION + '/haclusters/1/nodes//',methods=['GET','POST','PUT']) + def nodes_action(node_id,action): + if request.method == 'GET': + return json.dumps( node.get_nodes_action(node_id,action) ) + else: + return return app -- Gitee From 05e323c0d888beb21a0edb7a1ab9fcb1b1790bb0 Mon Sep 17 00:00:00 2001 From: Zixian Zhou Date: Sat, 13 Jun 2020 09:36:39 +0800 Subject: [PATCH 2/4] Add interface of get node list and node ip --- node.py | 5 ++++- run.py | 8 ++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/node.py b/node.py index 2cab990..ca476b4 100644 --- a/node.py +++ b/node.py @@ -78,7 +78,10 @@ def get_nodeid_info(nodeid): nameid = str(nodeid) status, output = common_utils.run_cmd("cat /etc/hosts|grep \""+ nameid +"\"|awk -F ' ' '{print $1}'") nodeip = str(output) - return {'action': True, 'data': {'ips': nodeip}} + if nameid == None: + return {'action':False,"data":"nodeid NOGet failed!"} + else: + return {'action': True, 'data': {'ips': nodeip}} ''' def get_nodes_action(node_id,action): nodeid = str(node_id) diff --git a/run.py b/run.py index bdbfa94..900016a 100644 --- a/run.py +++ b/run.py @@ -91,24 +91,20 @@ def create_app(test_config=None): def get_nodes(): if request.method == 'GET': return json.dumps( node.get_node_info() ) - else: - return #根据id取单个node @app.route('/api/'+ API_VERSION + '/haclusters/1/nodes/',methods=['GET','POST','PUT']) def get_nodeid(nodeid): if request.method == 'GET': return json.dumps( node.get_nodeid_info(nodeid) ) - else: - return #node状态修改 @app.route('/api/'+ API_VERSION + '/haclusters/1/nodes//',methods=['GET','POST','PUT']) def nodes_action(node_id,action): if request.method == 'GET': return json.dumps( node.get_nodes_action(node_id,action) ) - else: - return + + return app -- Gitee From 79da17a2e6b0feabaa2e7136d86196d2c3e2c5d8 Mon Sep 17 00:00:00 2001 From: Zixian Zhou Date: Sat, 13 Jun 2020 17:39:51 +0800 Subject: [PATCH 3/4] Add interface of get nodes action --- node.py | 42 +++++++++++++++++++++++++++++------------- run.py | 3 +-- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/node.py b/node.py index ca476b4..2ef22c8 100644 --- a/node.py +++ b/node.py @@ -3,6 +3,8 @@ import json import xml.etree.ElementTree as ET from xml.dom.minidom import parseString from flask import session +import run +#from Manager import Manager """ { "action":true, @@ -20,6 +22,7 @@ from flask import session ] } """ +#manager = Manager() tree = ET.ElementTree() nid = [] def get_node_info(): @@ -60,15 +63,12 @@ def get_node_info(): status = "Running" else: status = "Not Running" + status_json = {} status_json['id'] = name status_json['status'] = status status_json['is_dc'] = is_dc data.append(status_json) - #pmrint(data) - id = data[0]["id"] - nid.append(id) - if data != []: return {"action":True, "data":data} else: @@ -82,24 +82,40 @@ def get_nodeid_info(nodeid): return {'action':False,"data":"nodeid NOGet failed!"} else: return {'action': True, 'data': {'ips': nodeip}} -''' + def get_nodes_action(node_id,action): nodeid = str(node_id) #节点未开启 - if nodeid == None: + if nodeid == {} or nodeid == None: return {'action': False, 'error': _("Cannot find the")} - #备用操作 + #备用 if action == 'standby': status,output = common_utils.run_cmd("pcs node standby \""+ nodeid +"\" ") - #不备用操作 + #不备用 if action == 'unstandby': - status,output = commom_utils.run_cmd("pcs node unstandby \""+nodeid +"\" ") + status,output = common_utils.run_cmd("pcs node unstandby \""+ nodeid +"\" ") + #启动 if action == 'start': - status,output = common_utils.run_cmd("pcs cluster start \""+ nodeid+"\" ") + username = input (' 请输入账号: ') + password = input (' 请输入密码: ') + common_utils.check_auth(username,password) + status,output = common_utils.run_cmd("pcs cluster start \""+ nodeid +"\" ") + #停止 + if action == 'stop': + username = input (' 请输入账号: ') + password = input (' 请输入密码: ') + common_utils.check_auth(username,password) + status,output = common_utils.run_cmd("pcs cluster stop \""+ nodeid +"\" ") + #重启 + if action == 'restart': + username = input (' 请输入账号: ') + password = input (' 请输入密码: ') + common_utils.check_auth(username,password) + status,output = common_utils.run_cmd("pcs cluster restart \""+ nodeid +"\" ") + if action == {} or action == None: + return {"action":False, "error":"error info"} else: - status,output = common_utils.run_cmd("pcs cluster stop \""+ nodeid+"\" ") - return {'action':True,'data':} + return {"action":True, "info":"Change node status success"} -''' if __name__ == '__main__': get_node_info() diff --git a/run.py b/run.py index 900016a..625a47d 100644 --- a/run.py +++ b/run.py @@ -101,10 +101,9 @@ def create_app(test_config=None): #node状态修改 @app.route('/api/'+ API_VERSION + '/haclusters/1/nodes//',methods=['GET','POST','PUT']) def nodes_action(node_id,action): - if request.method == 'GET': + if request.method == 'PUT': return json.dumps( node.get_nodes_action(node_id,action) ) - return app -- Gitee From e52ad6211730b34875c39bde2eaec2e2b3a232d1 Mon Sep 17 00:00:00 2001 From: ZiXian Zhou Date: Sat, 13 Jun 2020 18:04:37 +0800 Subject: [PATCH 4/4] Add get nodes and nodes id and nodes action --- node.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/node.py b/node.py index 2ef22c8..a5072ee 100644 --- a/node.py +++ b/node.py @@ -76,7 +76,7 @@ def get_node_info(): def get_nodeid_info(nodeid): nameid = str(nodeid) - status, output = common_utils.run_cmd("cat /etc/hosts|grep \""+ nameid +"\"|awk -F ' ' '{print $1}'") + status, output = common_utils.run_cmd("cat /etc/hosts|grep "+ nameid +"|awk -F ' ' '{print $1}'") nodeip = str(output) if nameid == None: return {'action':False,"data":"nodeid NOGet failed!"} @@ -90,28 +90,28 @@ def get_nodes_action(node_id,action): return {'action': False, 'error': _("Cannot find the")} #备用 if action == 'standby': - status,output = common_utils.run_cmd("pcs node standby \""+ nodeid +"\" ") + status,output = common_utils.run_cmd("pcs node standby "+ nodeid) #不备用 if action == 'unstandby': - status,output = common_utils.run_cmd("pcs node unstandby \""+ nodeid +"\" ") + status,output = common_utils.run_cmd("pcs node unstandby "+ nodeid) #启动 if action == 'start': username = input (' 请输入账号: ') password = input (' 请输入密码: ') common_utils.check_auth(username,password) - status,output = common_utils.run_cmd("pcs cluster start \""+ nodeid +"\" ") + status,output = common_utils.run_cmd("pcs cluster start "+ nodeid) #停止 if action == 'stop': username = input (' 请输入账号: ') password = input (' 请输入密码: ') common_utils.check_auth(username,password) - status,output = common_utils.run_cmd("pcs cluster stop \""+ nodeid +"\" ") + status,output = common_utils.run_cmd("pcs cluster stop "+ nodeid) #重启 if action == 'restart': username = input (' 请输入账号: ') password = input (' 请输入密码: ') common_utils.check_auth(username,password) - status,output = common_utils.run_cmd("pcs cluster restart \""+ nodeid +"\" ") + status,output = common_utils.run_cmd("pcs cluster restart "+ nodeid) if action == {} or action == None: return {"action":False, "error":"error info"} else: @@ -119,3 +119,4 @@ def get_nodes_action(node_id,action): if __name__ == '__main__': get_node_info() + -- Gitee