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

@ -24,7 +24,8 @@ def RunAnalyse(clusters_conf, generalconf, delay=300):
while True:
play.run()
""" Instances types availables: lxc/qemu/all"""
play.run("all")
time.sleep(delay)
class Core:
@ -173,14 +174,14 @@ class Core:
return
def delete_instance(self, vmid, node, cluster, instancetype="lxc"):
def delete_instance(self, vmid, instancetype="lxc"):
try:
""" Find node/cluster informations from vmid """
# instance_informations = self.mongo.get_instance(vmid, node, cluster)
instance_informations = self.mongo.get_instance(vmid)
""" Find cluster informations from node """
cluster_informations = self.mongo.get_clusters_conf(cluster)["value"]
cluster_informations = self.mongo.get_clusters_conf(instance_informations['cluster'])["value"]
proxmox_cluster_url = cluster_informations["url"]
proxmox_cluster_port = cluster_informations["port"]
@ -190,7 +191,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(node)
proxmox = Proxmox(instance_informations['node'])
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
proxmox_cluster_user,
@ -198,10 +199,10 @@ class Core:
result = proxmox.delete_instance("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
node, instancetype, vmid)
instance_informations['node'], instancetype, vmid)
if result['result'] == "OK":
self.mongo.delete_instance(vmid, node, cluster)
self.mongo.delete_instance(vmid)
self.mongo.update_system_free_ip(instance_informations['ip'])
except IndexError as ierror:
@ -213,13 +214,13 @@ class Core:
return result
def status_instance(self, vmid, node, cluster, action, instancetype="lxc"):
def status_instance(self, vmid, action, instancetype="lxc"):
""" Find node/cluster informations from vmid """
try:
#instance_informations = self.mongo.get_instance(vmid, node, cluster)
instance_informations = self.mongo.get_instance(vmid)
""" Find cluster informations from node """
cluster_informations = self.mongo.get_clusters_conf(cluster)["value"]
cluster_informations = self.mongo.get_clusters_conf(instance_informations['cluster'])["value"]
proxmox_cluster_url = cluster_informations["url"]
proxmox_cluster_port = cluster_informations["port"]
@ -229,7 +230,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(node)
proxmox = Proxmox(instance_informations['node'])
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
proxmox_cluster_user,
@ -237,7 +238,7 @@ class Core:
result = proxmox.status_instance("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
node,
instance_informations['node'],
instancetype,
vmid, action)
@ -250,13 +251,13 @@ class Core:
return result
def info_instance(self, vmid, node, cluster, instancetype="lxc"):
def info_instance(self, vmid, instancetype="lxc"):
""" Find node/cluster informations from vmid """
try:
#instance_informations = self.mongo.get_instance(vmid, node, cluster)
instance_informations = self.mongo.get_instance(vmid)
""" Find cluster informations from node """
cluster_informations = self.mongo.get_clusters_conf(cluster)["value"]
cluster_informations = self.mongo.get_clusters_conf(instance_informations['cluster'])["value"]
proxmox_cluster_url = cluster_informations["url"]
proxmox_cluster_port = cluster_informations["port"]
@ -266,7 +267,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(node)
proxmox = Proxmox(instance_informations['node'])
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
proxmox_cluster_user,
@ -274,7 +275,7 @@ class Core:
result = proxmox.get_config("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
node,
instance_informations['node'],
instancetype,
vmid)
@ -287,13 +288,13 @@ class Core:
return result
def change_instance(self, vmid, node, cluster, data, instancetype="lxc"):
def change_instance(self, vmid, data, instancetype="lxc"):
""" Find node/cluster informations from vmid """
try:
#instance_informations = self.mongo.get_instance(vmid, node, cluster)
instance_informations = self.mongo.get_instance(vmid)
""" Find cluster informations from node """
cluster_informations = self.mongo.get_clusters_conf(cluster)["value"]
cluster_informations = self.mongo.get_clusters_conf(instance_informations['cluster'])["value"]
proxmox_cluster_url = cluster_informations["url"]
proxmox_cluster_port = cluster_informations["port"]
@ -304,7 +305,7 @@ class Core:
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
""" LOAD PROXMOX """
proxmox = Proxmox(node)
proxmox = Proxmox(instance_informations['node'])
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
proxmox_cluster_user,
@ -312,12 +313,12 @@ class Core:
result = proxmox.resize_instance("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
node,
instance_informations['node'],
instancetype,
vmid, data)
if result['result'] == "OK":
self.mongo.update_instance(data, vmid, instance_informations['node'], instance_informations['cluster'])
self.mongo.update_instance(data, vmid)
except IndexError as ierror:
result = {

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))