From c6a7f3678d26721bc33616efc2b5cdf5a563203c Mon Sep 17 00:00:00 2001 From: Tlams Date: Thu, 8 Feb 2018 18:46:44 +0000 Subject: [PATCH] Clusters functions --- code/scripts/main/core/core.py | 10 ++--- code/scripts/main/core/modules/mod_analyst.py | 12 ++++++ .../scripts/main/core/modules/mod_database.py | 41 ++++++++++++++++--- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/code/scripts/main/core/core.py b/code/scripts/main/core/core.py index 092a4fd..a01c9ca 100644 --- a/code/scripts/main/core/core.py +++ b/code/scripts/main/core/core.py @@ -105,7 +105,7 @@ class Core: elif dest == "storages": resultmbrequest = self.mongo.get_storage(date, cluster, node) elif dest == "clusters": - resultmbrequest = self.mongo.get_clusters_conf(date, cluster) + resultmbrequest = self.mongo.get_clusters(date, cluster) else: resultmbrequest = json_decode({"value": "Bad request"}) @@ -392,12 +392,12 @@ class Core: ####################### """ - def get_cluster(self, cluster=None): + def get_cluster_conf(self, cluster=None): """ Find cluster informations from node """ cluster_informations = self.mongo.get_clusters_conf(cluster)["value"] return cluster_informations - def insert_cluster(self, data): + def insert_cluster_conf(self, data): testdata = valid_cluster_data(data) if not testdata: @@ -420,12 +420,12 @@ class Core: return new_cluster - def change_cluster(self, cluster, data): + def change_cluster_conf(self, cluster, data): cluster_update = self.mongo.update_cluster_conf(cluster, data) return cluster_update - def delete_cluster(self, cluster): + def delete_cluster_conf(self, cluster): """ Find cluster informations from node """ cluster_delete = self.mongo.delete_cluster_conf(cluster) return cluster_delete diff --git a/code/scripts/main/core/modules/mod_analyst.py b/code/scripts/main/core/modules/mod_analyst.py index f0a3e46..5146cee 100644 --- a/code/scripts/main/core/modules/mod_analyst.py +++ b/code/scripts/main/core/modules/mod_analyst.py @@ -73,9 +73,21 @@ class Analyse: proxmox = Proxmox("Analyse") proxmox.get_ticket("{0}:{1}".format(cluster["url"], int(cluster["port"])), proxmox_cluster_user, proxmox_cluster_pwd) + """ + ############## + # CLUSTERS # + ############## + """ + """ Get excluded nodes """ exclude_nodes = cluster["exclude_nodes"] + """ UPDATE CLUSTER STATUS """ + clusters_status = proxmox.get_clusters("{0}:{1}".format(cluster["url"], int(cluster["port"]))) + clusters_status["date"] = int(insert_time) + clusters_status["cluster"] = cluster["name"] + self.mongo.insert_cluster(instance) + """ UPDATE NODES LIST """ nodes_list = proxmox.get_nodes("{0}:{1}".format(cluster["url"], int(cluster["port"]))) if nodes_list["result"] == "OK": diff --git a/code/scripts/main/core/modules/mod_database.py b/code/scripts/main/core/modules/mod_database.py index 6391f7f..78cc514 100644 --- a/code/scripts/main/core/modules/mod_database.py +++ b/code/scripts/main/core/modules/mod_database.py @@ -54,6 +54,7 @@ class MongoDB: self.collection_instance = "instances" self.collection_nodes = "nodes" self.collection_clusters = "clusters" + self.collection_clusters_conf = "clusters_conf" self.collection_storages = "storages" self.collection_disks = "disks" self.collection_datekey = "dates" @@ -92,17 +93,47 @@ class MongoDB: """ CLUSTER """ + def get_clusters(self, date, cluster): + try: + if not cluster: + result = { + "result": "OK", + "value": json.loads( + dumps(self.db[self.collection_clusters].find({'date': date, 'grata': str(grata)}))) + } + else: + result = { + "result": "OK", + "value": json.loads( + dumps(self.db[self.collection_clusters].find_one( + {'$and': [{'date': date, 'cluster': cluster}]}))) + } + + except BaseException as serr: + result = { + "result": "ERROR", + "value": "MongoDB error on {0}:{1} ({2})".format(self.server, self.port, serr) + } + return result + + def insert_clusters(self, data): + try: + return self.db[self.collection_clusters].insert(data) + except BaseException as serr: + raise ("MongoDB error on {0}:{1} ({2})".format(self.server, self.port, serr)) + + def get_clusters_conf(self, cluster=None): try: if cluster: result = { "result": "OK", - "value": json.loads(dumps(self.db[self.collection_clusters].find_one({"name": cluster}))) + "value": json.loads(dumps(self.db[self.collection_clusters_conf].find_one({"name": cluster}))) } else: result = { "result": "OK", - "value": json.loads(dumps(self.db[self.collection_clusters].find({}))) + "value": json.loads(dumps(self.db[self.collection_clusters_conf].find({}))) } except BaseException as e: result = { @@ -113,7 +144,7 @@ class MongoDB: def insert_clusters_conf(self, data): try: - self.db[self.collection_clusters].insert(data) + self.db[self.collection_clusters_conf].insert(data) result = { "result": "OK", "value": "{0} is now available".format(data["name"]) @@ -127,7 +158,7 @@ class MongoDB: def update_cluster_conf(self, cluster, data): try: - self.db[self.collection_clusters].update({"vmid": str(cluster)}, {'$set': data}, upsert=False) + self.db[self.collection_clusters_conf].update({"vmid": str(cluster)}, {'$set': data}, upsert=False) result = { "result": "OK", "value": "{0} has been updated".format(data["name"]) @@ -141,7 +172,7 @@ class MongoDB: def delete_cluster_conf(self, cluster): try: - self.db[self.collection_clusters].remove({"cluster": str(cluster)}) + self.db[self.collection_clusters_conf].remove({"cluster": str(cluster)}) result = { "result": "OK", "value": "{0} has been deleted".format(cluster)