1
0
Fork 0
mirror of https://github.com/ThomasGsp/HyperProxmox.git synced 2025-03-09 15:40:18 +00:00

Fix duplicate vmid issue

This commit is contained in:
Tlams 2018-02-03 18:56:28 +00:00
parent 1c91761921
commit d422ff4c60
3 changed files with 55 additions and 38 deletions

View file

@ -28,6 +28,7 @@ class Crawler:
self.mongo.client = self.mongo.connect()
self.mongo.db = self.mongo.client.db
""" Instances types availables: lxc/qemu/all"""
def run(self, instancetype="lxc"):
insert_time = time.time()
@ -50,9 +51,16 @@ class Crawler:
nodes_list = proxmox.get_nodes("{0}:{1}".format(cluster["url"], int(cluster["port"])))
if nodes_list["result"] == "OK":
for value_nodes_list in nodes_list["value"]["data"]:
list_instances = ""
""" TOTAL COUNT CPU and RAM allocate"""
list_instances = proxmox.get_instance("{0}:{1}".format(cluster["url"], int(cluster["port"])),
value_nodes_list["node"], instancetype)["value"]
if(instancetype == "all"):
types = ["lxc", "qemu"] # vz...
for type in types:
list_instances.update(proxmox.get_instance("{0}:{1}".format(cluster["url"], int(cluster["port"])),
value_nodes_list["node"], type)["value"])
else:
list_instances = proxmox.get_instance("{0}:{1}".format(cluster["url"], int(cluster["port"])),
value_nodes_list["node"], instancetype)["value"]
for key_list_instances, value_list_instances in list_instances.items():
for instance in value_list_instances:
@ -63,7 +71,6 @@ class Crawler:
# 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:
# Si elle existe deja, on l'update:
else:
self.mongo.update_instance(instance, instance["vmid"], instance["node"], instance["cluster"])
return
self.mongo.update_instance(instance, instance["vmid"], instance["node"], instance["cluster"])

View file

@ -211,18 +211,27 @@ class MongoDB:
def insert_instance(self, data):
return self.db[self.collection_instance].insert(data)
def update_instance(self, data, vmid, node, cluster):
return self.db[self.collection_instance].update(
{"vmid": int(vmid), "node": node, "cluster": cluster}, {'$set': data}, upsert=False
)
def update_instance(self, data, vmid, node=None, cluster=None):
if node and cluster:
return self.db[self.collection_instance].update(
{"vmid": int(vmid), "node": node, "cluster": cluster }, {'$set': data}, upsert=False)
else:
return self.db[self.collection_instance].update({"_id": vmid}, {'$set': data}, upsert=False)
def delete_instance(self, vmid, node, cluster):
self.db[self.collection_instance].remove({"vmid": int(vmid), "node": node, "cluster": cluster})
def delete_instance(self, vmid, node=None, cluster=None):
if node and cluster:
self.db[self.collection_instance].remove({"vmid": int(vmid), "node": node, "cluster": cluster})
else:
self.db[self.collection_instance].remove({"_id": vmid})
def get_instance(self, vmid, node, cluster):
def get_instance(self, vmid, node=None, cluster=None):
try:
return json.loads(dumps(
self.db[self.collection_instance].find_one(
{"vmid": int(vmid), "node": node, "cluster": cluster})))
if node and cluster:
return json.loads(dumps(
self.db[self.collection_instance].find_one(
{"vmid": int(vmid), "node": node, "cluster": cluster})))
else:
return json.loads(dumps(
self.db[self.collection_instance].find_one({"_id": vmid})))
except BaseException as serr:
raise ("MongoDB error on {0}:{1} ({2})".format(self.server, self.port, serr))