diff --git a/.node.py.swp b/.node.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..79cd38515bd18b8114791a2821e6ea2308dd5624 Binary files /dev/null and b/.node.py.swp differ diff --git a/node.py b/node.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..53538606a9c8b1120e36a43316e6a458f22fdfcb 100644 --- a/node.py +++ b/node.py @@ -0,0 +1,117 @@ +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 + } + ] +} +""" +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 == {}: + 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 is 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 is None: + return {"action" : False , "error" : "error info"} + else: + return {"action" : True , "info" : "Change node status success"} + +if __name__ == '__main__': + get_node_info() +#123 \ No newline at end of file diff --git a/run.py b/run.py index 5fbc95efbc134a0878f98f89a76ab10c20403e95..789d6456d550ece7a57b22380ce32cada18e375e 100644 --- a/run.py +++ b/run.py @@ -1,6 +1,3 @@ -# -*-coding:utf-8-*- -# coding: iso-8859-15 - import os from flask import Flask from flask import request, Response @@ -11,6 +8,7 @@ import gettext import common_utils import re from flask import jsonify +import node API_VERSION = "v1" app_name = "ha-api" @@ -85,7 +83,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