From ab90bfe7c79d0f3f151787066edbdbed4086db27 Mon Sep 17 00:00:00 2001 From: Zixian Zhou Date: Fri, 12 Jun 2020 20:18:50 +0800 Subject: [PATCH 1/7] 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/7] 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/7] 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/7] 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 From a1b44f216563d5d3061f34ce4f89d664d2bc7e0f Mon Sep 17 00:00:00 2001 From: ZiXian Zhou Date: Mon, 15 Jun 2020 11:27:01 +0800 Subject: [PATCH 5/7] Add get_nodes and nodes_id and nodes_action --- .node.py.swp | Bin 0 -> 16384 bytes node.py | 59 ++++++++++++++++++++++++--------------------------- 2 files changed, 28 insertions(+), 31 deletions(-) create mode 100644 .node.py.swp diff --git a/.node.py.swp b/.node.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..79cd38515bd18b8114791a2821e6ea2308dd5624 GIT binary patch literal 16384 zcmeI2ONi3uDidNnh^?CdTYT}Stz1Pm%mgoT6a+G(b1W?N>uC;bTE zuplHzf+0vG(L^+es05>Hj0qBn@#4*c1icz#oMkbb)ac2_zq+cXr}x2bU`0%-$!~kQ z`>U_M{&&?^UDa&q+`M)j?ap@*Yzqikdv4^p!Mhicze=-+?|C6VJ=Hd7#ige*BfGf6 z4hvg{>)9+{+c9MkZJ-s<3e1E8QCMp4p1sgnR?O0-u9JAOij1Y49Xi3s!?p&;~~46LKCL2Of9;{CykFfe%0(tObj~9MHIx zkT1dOpbUBe1xvs@@Z&r}&VXaU2Nrk)ECfGe@!}kK2OI|n0Rs;M1N?R~A*0}Ha1!hS zo54!Z1(twcZ-O5%3_b#f!FI3!{Ba{R18;-ZzyNp_+z%Fkx#0V`guDq3g9t1Ki@~3B z2>BSC00+P`U^)053ooa@TVNm91nvdDV8P~n@CMio)`KM=evYl}jLNO(Ra<^mJ=csv zrxN6ggRHpKw4!h@8wJdFtt!jatRUFt`SvoRil{TRhdOR83TZY&M~6?3e(`E!-@A?B zmo6OJQ=pl|Ub&jke)QA#8i!6d?VS>}ilO7Vw5Nw=d=`Y3A7%=1-E9~5A8fob+>8U) z!YIgjQOJYnp}4E$R{Y2{i&Z;o)QSNuR-zzeJ`F;zMh&`x^8a$;wA@mv==G)ok6KC4 z*mHE`!{b*a0R+8D4QR9eYu6M~4FRp7ao}V#is?55{@iKgqJV2}*)dLrbn9!{De-^$ z>|o=>{tHL0xaR5AE3H(mW2*F782K)(XGA~A6zCeO5-^&h8Rq+*j}vCrSY^4c7t)gB z+B6(w#T;?edXb;y2PW@9 zHY?FG3r&6^Fhxx2YEB$3D^QBWEVuFT_xVvAzHNmTqOWJ1T7Z3AUu{SP&Txw&HtuGe z4=I7cT^I#JhT6OSaRv=6r^4)|Mxp@HnqzYXk~RLgKoz}Lkw0#ZD@PGB1@3pHPM(T)!Y|B?UUZ`kk?T5c*_g;WrDLlL{yTAVITujXb1RO^c$+jrbO$48 z>(ZsDhRCC_yQH2O3U{N(kQ3hVbi#!VVv3XO#P>8!*=Fc=)>(Y~zv8>O#^Q@;JO1@& zfQJ-!y-Au~(x_6x$s%z|$s_eY8iu~JB??(qcImNQUbKRV>}3;ac~?ZkR$XDD0^(ZQ zKF*pvzl{yXPTTRcfDS!@CBKr`mgJy_m)qDl;C@$ESe3b9@Ys$%tK19wY=%3^AP*yC zRV0YZux(;AyiB=37Ic*@0;^emz&1ilx2#5>yzLbuUazd4*TpxLuAA=wD&;p{0mN{n za<+Vu$l9H8yy7@F<)G$QO}tUl_I4|1-(IbVp(-8#c>Yg~{bF!}9EM>Kkzxl(94!8% zQZ6;(7GxAk{Jo`V!)*=6=A(De)!l96(pN;txZ08tnmr`3tv5>zB`1YzKFAmao?V+Z z;)1I4!Aa|s=_Bzds*$5(f9$V0{PG%dq#g^=R`M~`T4%Xd8PgA^TJ!MoEf0x2#7u?6 zW~GW1Y*j`93j$1o#1&|drcljS9oNBL*|fA6<&0ah&}S?!n@k^mQ)&ftb)T}~!?OC_ zI`JWV{(l7X@NWU1|6>{)Z=Ya3|1#(UPk^~#4&ZbA@1*^CUPfmKr5gX&Ra$2CoSa*5~iyo_37 zY0m4_k~m+5$2!ESLCY<(c!fwU^9E6|h-th0(vV)^9Oq6g*%( literal 0 HcmV?d00001 diff --git a/node.py b/node.py index a5072ee..bfb3ebf 100644 --- a/node.py +++ b/node.py @@ -22,14 +22,11 @@ import run ] } """ -#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} + return {'action': False , 'error': output} new_nodes = parseString(output).documentElement for nodes in new_nodes.getElementsByTagName('nodes'): for node in nodes.getElementsByTagName('node'): @@ -70,52 +67,52 @@ def get_node_info(): status_json['is_dc'] = is_dc data.append(status_json) if data != []: - return {"action":True, "data":data} + return {"action":True , "data":data} else: - return {"action":False, "data":"Get node failed!"} + return {"action":False , "data":"Get node failed!"} -def get_nodeid_info(nodeid): +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!"} + if nameid == {}: + return {'action': False, "data": "nodeid NOGet failed!"} else: - return {'action': True, 'data': {'ips': nodeip}} + return {'action': True, 'data': {'ips': nodeip }} -def get_nodes_action(node_id,action): +def get_nodes_action(node_id, action): nodeid = str(node_id) #节点未开启 - if nodeid == {} or nodeid == None: - return {'action': False, 'error': _("Cannot find the")} + 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) + 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) + 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) + 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"} + 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"} + return {"action" : True , "info" : "Change node status success"} if __name__ == '__main__': get_node_info() -- Gitee From 1b1a5c2a977b8c3a79149af444c0fb170a1beedc Mon Sep 17 00:00:00 2001 From: ZiXian Zhou Date: Mon, 15 Jun 2020 13:51:25 +0800 Subject: [PATCH 6/7] Add node --- node.py | 86 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/node.py b/node.py index bfb3ebf..f908d31 100644 --- a/node.py +++ b/node.py @@ -23,53 +23,53 @@ import run } """ 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" + 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 = "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) + 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} + return {"action":True , "data":data} else: - return {"action":False , "data":"Get node failed!"} + return {"action":False , "data":"Get node failed!"} def get_nodeid_info( nodeid ): nameid = str(nodeid) -- Gitee From a9c204e6c28d54dfffa75df43c683de45b812349 Mon Sep 17 00:00:00 2001 From: ZiXian Zhou Date: Mon, 15 Jun 2020 14:17:33 +0800 Subject: [PATCH 7/7] Add node --- node.py | 8 ++++---- run.py | 3 --- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/node.py b/node.py index f908d31..5d22490 100644 --- a/node.py +++ b/node.py @@ -66,10 +66,10 @@ def get_node_info(): 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!"} + if data != []: + return {"action":True , "data":data} + else: + return {"action":False , "data":"Get node failed!"} def get_nodeid_info( nodeid ): nameid = str(nodeid) diff --git a/run.py b/run.py index 625a47d..789d645 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 -- Gitee