From ab90bfe7c79d0f3f151787066edbdbed4086db27 Mon Sep 17 00:00:00 2001 From: Zixian Zhou Date: Fri, 12 Jun 2020 20:18:50 +0800 Subject: [PATCH] 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