From 1c9176192103920804418bce04e45571fa3ca9a9 Mon Sep 17 00:00:00 2001 From: Tlams Date: Sat, 3 Feb 2018 17:51:50 +0000 Subject: [PATCH] Fix duplicate vmid issue --- code/scripts/main/core/core.py | 44 +++++++++---------- code/scripts/main/core/modules/mod_crawler.py | 4 +- .../scripts/main/core/modules/mod_database.py | 12 ++--- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/code/scripts/main/core/core.py b/code/scripts/main/core/core.py index ec5f174..8fe996f 100644 --- a/code/scripts/main/core/core.py +++ b/code/scripts/main/core/core.py @@ -173,14 +173,14 @@ class Core: return - def delete_instance(self, vmid, instancetype="lxc"): + def delete_instance(self, vmid, node, cluster, instancetype="lxc"): try: """ Find node/cluster informations from vmid """ - instance_informations = self.mongo.get_instance(vmid) + # instance_informations = self.mongo.get_instance(vmid, node, cluster) """ Find cluster informations from node """ - cluster_informations = self.mongo.get_clusters_conf(instance_informations['cluster'])["value"] + cluster_informations = self.mongo.get_clusters_conf(cluster)["value"] proxmox_cluster_url = cluster_informations["url"] proxmox_cluster_port = cluster_informations["port"] @@ -190,7 +190,7 @@ class Core: proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]), self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8') """ LOAD PROXMOX """ - proxmox = Proxmox(instance_informations['node']) + proxmox = Proxmox(node) proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), proxmox_cluster_user, @@ -198,10 +198,10 @@ class Core: result = proxmox.delete_instance("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), - instance_informations['node'], instancetype, vmid) + node, instancetype, vmid) if result['result'] == "OK": - self.mongo.delete_instance(vmid) + self.mongo.delete_instance(vmid, node, cluster) self.mongo.update_system_free_ip(instance_informations['ip']) except IndexError as ierror: @@ -213,13 +213,13 @@ class Core: return result - def status_instance(self, vmid, action, instancetype="lxc"): + def status_instance(self, vmid, node, cluster, action, instancetype="lxc"): """ Find node/cluster informations from vmid """ try: - instance_informations = self.mongo.get_instance(vmid) + #instance_informations = self.mongo.get_instance(vmid, node, cluster) """ Find cluster informations from node """ - cluster_informations = self.mongo.get_clusters_conf(instance_informations['cluster'])["value"] + cluster_informations = self.mongo.get_clusters_conf(cluster)["value"] proxmox_cluster_url = cluster_informations["url"] proxmox_cluster_port = cluster_informations["port"] @@ -229,7 +229,7 @@ class Core: proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]), self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8') """ LOAD PROXMOX """ - proxmox = Proxmox(instance_informations['node']) + proxmox = Proxmox(node) proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), proxmox_cluster_user, @@ -237,7 +237,7 @@ class Core: result = proxmox.status_instance("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), - instance_informations['node'], + node, instancetype, vmid, action) @@ -250,13 +250,13 @@ class Core: return result - def info_instance(self, vmid, instancetype="lxc"): + def info_instance(self, vmid, node, cluster, instancetype="lxc"): """ Find node/cluster informations from vmid """ try: - instance_informations = self.mongo.get_instance(vmid) + #instance_informations = self.mongo.get_instance(vmid, node, cluster) """ Find cluster informations from node """ - cluster_informations = self.mongo.get_clusters_conf(instance_informations['cluster'])["value"] + cluster_informations = self.mongo.get_clusters_conf(cluster)["value"] proxmox_cluster_url = cluster_informations["url"] proxmox_cluster_port = cluster_informations["port"] @@ -266,7 +266,7 @@ class Core: proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]), self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8') """ LOAD PROXMOX """ - proxmox = Proxmox(instance_informations['node']) + proxmox = Proxmox(node) proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), proxmox_cluster_user, @@ -274,7 +274,7 @@ class Core: result = proxmox.get_config("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), - instance_informations['node'], + node, instancetype, vmid) @@ -287,13 +287,13 @@ class Core: return result - def change_instance(self, vmid, data, instancetype="lxc"): + def change_instance(self, vmid, node, cluster, data, instancetype="lxc"): """ Find node/cluster informations from vmid """ try: - instance_informations = self.mongo.get_instance(vmid) + #instance_informations = self.mongo.get_instance(vmid, node, cluster) """ Find cluster informations from node """ - cluster_informations = self.mongo.get_clusters_conf(instance_informations['cluster'])["value"] + cluster_informations = self.mongo.get_clusters_conf(cluster)["value"] proxmox_cluster_url = cluster_informations["url"] proxmox_cluster_port = cluster_informations["port"] @@ -304,7 +304,7 @@ class Core: self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8') """ LOAD PROXMOX """ - proxmox = Proxmox(instance_informations['node']) + proxmox = Proxmox(node) proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), proxmox_cluster_user, @@ -312,12 +312,12 @@ class Core: result = proxmox.resize_instance("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), - instance_informations['node'], + node, instancetype, vmid, data) if result['result'] == "OK": - self.mongo.update_instance(vmid, data) + self.mongo.update_instance(data, vmid, instance_informations['node'], instance_informations['cluster']) except IndexError as ierror: result = { diff --git a/code/scripts/main/core/modules/mod_crawler.py b/code/scripts/main/core/modules/mod_crawler.py index 1d2f6d4..3935526 100644 --- a/code/scripts/main/core/modules/mod_crawler.py +++ b/code/scripts/main/core/modules/mod_crawler.py @@ -59,11 +59,11 @@ class Crawler: instance["cluster"] = cluster["name"] instance["node"] = value_nodes_list["node"] # Test si l'instance existe - if not self.mongo.get_instance(instance["vmid"], instance["node"]): + if not self.mongo.get_instance(instance["vmid"], instance["node"], instance["cluster"]): # si non existante, alors il s'agit d'une instance manuelle instance["commandid"] = "000000" self.mongo.insert_instance(instance) # Si elle existe déjà, on l'update: else: - self.mongo.update_instance(instance, instance["vmid"], instance["node"]) + self.mongo.update_instance(instance, instance["vmid"], instance["node"], instance["cluster"]) return \ No newline at end of file diff --git a/code/scripts/main/core/modules/mod_database.py b/code/scripts/main/core/modules/mod_database.py index 8bf0b62..19d1750 100644 --- a/code/scripts/main/core/modules/mod_database.py +++ b/code/scripts/main/core/modules/mod_database.py @@ -211,18 +211,18 @@ class MongoDB: def insert_instance(self, data): return self.db[self.collection_instance].insert(data) - def update_instance(self, data, vmid, node): + def update_instance(self, data, vmid, node, cluster): return self.db[self.collection_instance].update( - {"vmid": int(vmid), "node": node}, {'$set': data}, upsert=False + {"vmid": int(vmid), "node": node, "cluster": cluster}, {'$set': data}, upsert=False ) - def delete_instance(self, vmid, node): - self.db[self.collection_instance].remove({"vmid": int(vmid), "node": node}) + def delete_instance(self, vmid, node, cluster): + self.db[self.collection_instance].remove({"vmid": int(vmid), "node": node, "cluster": cluster}) - def get_instance(self, vmid, node): + def get_instance(self, vmid, node, cluster): try: return json.loads(dumps( self.db[self.collection_instance].find_one( - {"vmid": int(vmid), "node": node}))) + {"vmid": int(vmid), "node": node, "cluster": cluster}))) except BaseException as serr: raise ("MongoDB error on {0}:{1} ({2})".format(self.server, self.port, serr))