From 898f5af57e3563bea7933e81f773a69fa56beee3 Mon Sep 17 00:00:00 2001 From: "thomas.guiseppin" Date: Fri, 27 Oct 2017 17:56:52 +0200 Subject: [PATCH] Add error managment --- code/scripts/main/api/v1/api.py | 135 ++++++++++-------- code/scripts/main/core/core.py | 16 ++- code/scripts/main/core/modules/mod_proxmox.py | 70 ++++++--- 3 files changed, 136 insertions(+), 85 deletions(-) diff --git a/code/scripts/main/api/v1/api.py b/code/scripts/main/api/v1/api.py index 8bf8a0a..5308a84 100644 --- a/code/scripts/main/api/v1/api.py +++ b/code/scripts/main/api/v1/api.py @@ -25,15 +25,9 @@ class Cluster: def GET(self, cluster=None): try: if cluster: - result = { - "result": "OK", - "value": core.get_cluster(cluster) - } + result = core.get_cluster(cluster) else: - result = { - "result": "OK", - "value": core.get_cluster() - } + result = core.get_cluster() except BaseException as e: result = { "result": "ERROR", @@ -45,10 +39,7 @@ class Cluster: def POST(self): try: data = json.loads(web.data().decode('utf-8')) - result = { - "result": "OK", - "value": core.insert_cluster(data) - } + result = core.insert_cluster(data) except BaseException as e: result = { "result": "ERROR", @@ -60,10 +51,7 @@ class Cluster: def PUT(self, cluster): try: data = json.loads(web.data().decode('utf-8')) - result = { - "result": "OK", - "value": core.change_cluster(cluster, data) - } + result = core.change_cluster(cluster, data) except BaseException as e: result = { "result": "ERROR", @@ -74,10 +62,7 @@ class Cluster: def DELETE(self, cluster): try: - result = { - "result": "OK", - "value": core.delete_cluste(cluster) - } + result = core.delete_cluste(cluster) except BaseException as e: result = { "result": "ERROR", @@ -90,57 +75,89 @@ class Cluster: # Ajouter le retour d'erreur des requetes foireuses class Instance: def GET(self, vmid=None, status=None): - if status: - """ GET INSTANCE STATUS """ - return core.status_instance(vmid, status) - elif vmid: - """ GET INSTANCE INFORMATION """ - return core.info_instance(vmid) + try: + if status: + """ GET INSTANCE STATUS """ + result = core.status_instance(vmid, status) + elif vmid: + """ GET INSTANCE INFORMATION """ + result = core.info_instance(vmid) + except BaseException as e: + result = { + "result": "ERROR", + "type": "PYTHON - API", + "value": "{0} {1}".format("Invalid request:", e) + } + return result def POST(self, vmid=None, status=None): - if vmid: - """ GET INSTANCE INFORMATION """ - return core.status_instance(vmid, status) - else: - """ CREATE NEWS INSTANCES""" - count = json.loads(web.data().decode('utf-8'))["count"] + try: + if vmid: + """ GET INSTANCE INFORMATION """ + result = core.status_instance(vmid, status) + else: + """ CREATE NEWS INSTANCES""" + count = json.loads(web.data().decode('utf-8'))["count"] - """ GENERATE UNIQ COMMAND ID """ - randtext = ''.join(random.choice('0123456789ABCDEF') for i in range(8)) - command_id = "{0}_{1}_{2}".format(time.time(), count, randtext) + """ GENERATE UNIQ COMMAND ID """ + randtext = ''.join(random.choice('0123456789ABCDEF') for i in range(8)) + command_id = "{0}_{1}_{2}".format(time.time(), count, randtext) - """ LOAD CLUSTER CONFIGURATIONS """ - select = Analyse(core.clusters_conf, generalconf) - sorted_nodes = dict(select.set_attribution(count)) + """ LOAD CLUSTER CONFIGURATIONS """ + select = Analyse(core.clusters_conf, generalconf) + sorted_nodes = dict(select.set_attribution(count)) - """ START ALL Thread """ - for target, count in sorted_nodes.items(): - # Limit to 5 instance per block - thci = threading.Thread(name="Insert Instance", - target=core.insert_instance, - args=(target, str(count), command_id,)) + """ START ALL Thread """ + for target, count in sorted_nodes.items(): + # Limit to 5 instance per block + thci = threading.Thread(name="Insert Instance", + target=core.insert_instance, + args=(target, str(count), command_id,)) - thci.start() + thci.start() - """ Wait all results of Thread from redis messages queue. Valid or not """ - timeout = 2 * int(generalconf["deploy"]["concurrencydeploy"]) * int(generalconf["deploy"]["delayrounddeploy"]) - for t in range(timeout): - time.sleep(1) - try: - if len(ast.literal_eval(Lredis.get_message(command_id))) == int(count): - break - except BaseException as err: - print("Value not found", err) + """ Wait all results of Thread from redis messages queue. Valid or not """ + timeout = 2 * int(generalconf["deploy"]["concurrencydeploy"]) * int(generalconf["deploy"]["delayrounddeploy"]) + for t in range(timeout): + time.sleep(1) + try: + if len(ast.literal_eval(Lredis.get_message(command_id))) == int(count): + break + except BaseException as err: + print("Value not found", err) - """ Return messages """ - return ast.literal_eval(Lredis.get_message(command_id)) + """ Return messages """ + return ast.literal_eval(Lredis.get_message(command_id)) + except BaseException as e: + result = { + "result": "ERROR", + "type": "PYTHON - API", + "value": "{0} {1}".format("Invalid request:", e) + } + return result def PUT(self, vmid): - data = json.loads(web.data().decode('utf-8')) - return core.change_instance(vmid, data) + try: + data = json.loads(web.data().decode('utf-8')) + result = core.change_instance(vmid, data) + except BaseException as e: + result = { + "result": "ERROR", + "type": "PYTHON - API", + "value": "{0} {1}".format("Invalid request:", e) + } + return result def DELETE(self, vmid): - return core.delete_instance(vmid) + try: + result = core.delete_instance(vmid) + except BaseException as e: + result = { + "result": "ERROR", + "type": "PYTHON - API", + "value": "{0} {1}".format("Invalid request:", e) + } + return result class ThreadAPI(threading.Thread): diff --git a/code/scripts/main/core/core.py b/code/scripts/main/core/core.py index eb58056..1c76a71 100644 --- a/code/scripts/main/core/core.py +++ b/code/scripts/main/core/core.py @@ -307,8 +307,8 @@ class Core: "type": "PROXMOX - VALUES", "value": "{0} is not a valid VMID: {1}".format(vmid, ierror) } - - return result + # Voir comment return ça proprement + return """ ####################### @@ -330,9 +330,17 @@ class Core: data["password"] = base64.b64encode(pcrypt(data["password"], self.generalconf["keys"]["key_pvt"])["data"]).decode('utf-8') new_cluster = self.mongo.insert_new_cluster(data) else: - new_cluster = {"value": "{0}".format("Duplicate entry, please delete the current cluster or update it")} + new_cluster = { + "value": "{0}".format("Duplicate entry, please delete the current cluster or update it") + "result": "ERROR", + "type": "PROXMOX - VALUES" + } else: - new_cluster = {"value": "{1} {0}".format(testdata, "Invalid or miss paramettrer")} + new_cluster = { + "value": "{1} {0}".format(testdata, "Invalid or miss paramettrer") + "result": "ERROR", + "type": "PROXMOX - VALUES" + } return new_cluster diff --git a/code/scripts/main/core/modules/mod_proxmox.py b/code/scripts/main/core/modules/mod_proxmox.py index d8834d7..d65f241 100644 --- a/code/scripts/main/core/modules/mod_proxmox.py +++ b/code/scripts/main/core/modules/mod_proxmox.py @@ -40,23 +40,27 @@ class Proxmox: self.ticket = self.socket.post(request, params=params, verify=False, timeout=5) if self.ticket.status_code == 200: - result = {"result": "OK", - "value": self.ticket.json() - } + result = { + "result": "OK", + "value": self.ticket.json() + } self.PVEAuthCookie = {'PVEAuthCookie': self.ticket.json()['data']['ticket']} self.csrf = {'CSRFPreventionToken': self.ticket.json()['data']['CSRFPreventionToken']} else: - result = {"result": "ERROR", - "target": "{0}".format(request), - "type": "PROXMOX - STATUS CODE", - "value": "Error nodes informations. Bad HTTP Status code : " - "{0} -- {1}".format(self.ticket.status_code, self.ticket.text) - } + result = { + "result": "ERROR", + "target": "{0}".format(request), + "type": "PROXMOX - STATUS CODE", + "value": "Error nodes informations. Bad HTTP Status code : " + "{0} -- {1}".format(self.ticket.status_code, self.ticket.text) + } except (TypeError, ValueError, requests.exceptions.RequestException) as e: - result = {"result": "ERROR", - "target": "{0}".format(url), - "type": "PYTHON", - "value": "Cannot get ticket session {0} ({1})".format(url, e)} + result = { + "result": "ERROR", + "target": "{0}".format(url), + "type": "PYTHON", + "value": "Cannot get ticket session {0} ({1})".format(url, e) + } return result @@ -104,10 +108,18 @@ class Proxmox: request = "https://{0}/api2/json/nodes/{1}/status".format(url, nodename) try: self.status = self.socket.get(request, cookies=self.PVEAuthCookie, verify=False, timeout=5).json() - result = {"result": "OK"} + result = { + "result": "OK", + "value": self.status + + } except (TypeError, ValueError, requests.exceptions.RequestException) as e: - result = {"result": "ERROR", "target": "[{3}]", - "value": "Cannot get node information for {0} ({1})".format(url, e, nodename)} + result = { + "result": "ERROR", + "target": "{0}".format(nodename), + "type": "PYTHON - ERROR", + "value": "Cannot get node information for {0} ({1})".format(url, e) + } return result @@ -120,10 +132,17 @@ class Proxmox: request = "https://{0}/api2/json/nodes/{1}/storage".format(url, nodename) try: self.storage = self.socket.get(request, cookies=self.PVEAuthCookie, verify=False, timeout=5).json() - result = {"result": "OK"} + result = { + "result": "OK", + "value": self.storage + } except (TypeError, ValueError, requests.exceptions.RequestException) as e: - result = {"result": "ERROR", "target": "[{3}]", - "value": "Cannot get storage information for {0} ({1})".format(url, e, nodename)} + result = { + "result": "ERROR", + "type": "PYTHON - ERROR", + "target": "{0}".format(nodename), + "value": "Cannot get storage information for {0} ({1})".format(url, e) + } return result @@ -137,10 +156,17 @@ class Proxmox: request = "https://{0}/api2/json/nodes/{1}/storage/{2}/content".format(url, nodename, sto_id) try: self.disks = self.socket.get(request, cookies=self.PVEAuthCookie, verify=False, timeout=5).json() - result = {"result": "OK"} + result = { + "result": "OK", + "value": self.disks + } except (TypeError, ValueError, requests.exceptions.RequestException) as e: - result = {"result": "ERROR", "target": "[{3}]", - "value": "Cannot get disks information for {0} ({1})".format(url, e, nodename)} + result = { + "result": "ERROR", + "type": "PYTHON - ERROR", + "target": "{0}".format(nodename), + "value": "Cannot get disks information for {0} ({1})".format(url, e) + } return result