diff --git a/node.py b/node.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..a5072eeb45209915f52ae7fb8b3e72d0ec2bae0d 100644 --- a/node.py +++ b/node.py @@ -0,0 +1,122 @@ +import common_utils +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, + "data":[ + { + "status":"Master", + "id":"ns187", + "is_dc":true + }, + { + "status":"Not Running/Standby", + "id":"ns188", + "is_dc":false + } + ] +} +""" +#manager = Manager() +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) + 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) + 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) + #节点未开启 + 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 = 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) + #停止 + 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: + return {"action":True, "info":"Change node status success"} + +if __name__ == '__main__': + get_node_info() + diff --git a/run.py b/run.py index 5fbc95efbc134a0878f98f89a76ab10c20403e95..625a47d04d8243f7279963478f421b266da5f19b 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,7 +86,23 @@ 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() ) + + #根据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) ) + + #node状态修改 + @app.route('/api/'+ API_VERSION + '/haclusters/1/nodes//',methods=['GET','POST','PUT']) + def nodes_action(node_id,action): + if request.method == 'PUT': + return json.dumps( node.get_nodes_action(node_id,action) ) return app