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:
parent
1c91761921
commit
d422ff4c60
3 changed files with 55 additions and 38 deletions
|
@ -24,7 +24,8 @@ def RunAnalyse(clusters_conf, generalconf, delay=300):
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
play.run()
|
""" Instances types availables: lxc/qemu/all"""
|
||||||
|
play.run("all")
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
|
|
||||||
class Core:
|
class Core:
|
||||||
|
@ -173,14 +174,14 @@ class Core:
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def delete_instance(self, vmid, node, cluster, instancetype="lxc"):
|
def delete_instance(self, vmid, instancetype="lxc"):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
""" Find node/cluster informations from vmid """
|
""" 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 """
|
""" 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_url = cluster_informations["url"]
|
||||||
proxmox_cluster_port = cluster_informations["port"]
|
proxmox_cluster_port = cluster_informations["port"]
|
||||||
|
@ -190,7 +191,7 @@ class Core:
|
||||||
proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]),
|
proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]),
|
||||||
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
|
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
|
||||||
""" LOAD PROXMOX """
|
""" LOAD PROXMOX """
|
||||||
proxmox = Proxmox(node)
|
proxmox = Proxmox(instance_informations['node'])
|
||||||
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
|
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
|
||||||
int(proxmox_cluster_port)),
|
int(proxmox_cluster_port)),
|
||||||
proxmox_cluster_user,
|
proxmox_cluster_user,
|
||||||
|
@ -198,10 +199,10 @@ class Core:
|
||||||
|
|
||||||
result = proxmox.delete_instance("{0}:{1}".format(proxmox_cluster_url,
|
result = proxmox.delete_instance("{0}:{1}".format(proxmox_cluster_url,
|
||||||
int(proxmox_cluster_port)),
|
int(proxmox_cluster_port)),
|
||||||
node, instancetype, vmid)
|
instance_informations['node'], instancetype, vmid)
|
||||||
|
|
||||||
if result['result'] == "OK":
|
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'])
|
self.mongo.update_system_free_ip(instance_informations['ip'])
|
||||||
|
|
||||||
except IndexError as ierror:
|
except IndexError as ierror:
|
||||||
|
@ -213,13 +214,13 @@ class Core:
|
||||||
|
|
||||||
return result
|
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 """
|
""" Find node/cluster informations from vmid """
|
||||||
try:
|
try:
|
||||||
#instance_informations = self.mongo.get_instance(vmid, node, cluster)
|
instance_informations = self.mongo.get_instance(vmid)
|
||||||
|
|
||||||
""" Find cluster informations from node """
|
""" 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_url = cluster_informations["url"]
|
||||||
proxmox_cluster_port = cluster_informations["port"]
|
proxmox_cluster_port = cluster_informations["port"]
|
||||||
|
@ -229,7 +230,7 @@ class Core:
|
||||||
proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]),
|
proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]),
|
||||||
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
|
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
|
||||||
""" LOAD PROXMOX """
|
""" LOAD PROXMOX """
|
||||||
proxmox = Proxmox(node)
|
proxmox = Proxmox(instance_informations['node'])
|
||||||
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
|
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
|
||||||
int(proxmox_cluster_port)),
|
int(proxmox_cluster_port)),
|
||||||
proxmox_cluster_user,
|
proxmox_cluster_user,
|
||||||
|
@ -237,7 +238,7 @@ class Core:
|
||||||
|
|
||||||
result = proxmox.status_instance("{0}:{1}".format(proxmox_cluster_url,
|
result = proxmox.status_instance("{0}:{1}".format(proxmox_cluster_url,
|
||||||
int(proxmox_cluster_port)),
|
int(proxmox_cluster_port)),
|
||||||
node,
|
instance_informations['node'],
|
||||||
instancetype,
|
instancetype,
|
||||||
vmid, action)
|
vmid, action)
|
||||||
|
|
||||||
|
@ -250,13 +251,13 @@ class Core:
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def info_instance(self, vmid, node, cluster, instancetype="lxc"):
|
def info_instance(self, vmid, instancetype="lxc"):
|
||||||
""" Find node/cluster informations from vmid """
|
""" Find node/cluster informations from vmid """
|
||||||
try:
|
try:
|
||||||
#instance_informations = self.mongo.get_instance(vmid, node, cluster)
|
instance_informations = self.mongo.get_instance(vmid)
|
||||||
|
|
||||||
""" Find cluster informations from node """
|
""" 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_url = cluster_informations["url"]
|
||||||
proxmox_cluster_port = cluster_informations["port"]
|
proxmox_cluster_port = cluster_informations["port"]
|
||||||
|
@ -266,7 +267,7 @@ class Core:
|
||||||
proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]),
|
proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]),
|
||||||
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
|
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
|
||||||
""" LOAD PROXMOX """
|
""" LOAD PROXMOX """
|
||||||
proxmox = Proxmox(node)
|
proxmox = Proxmox(instance_informations['node'])
|
||||||
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
|
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
|
||||||
int(proxmox_cluster_port)),
|
int(proxmox_cluster_port)),
|
||||||
proxmox_cluster_user,
|
proxmox_cluster_user,
|
||||||
|
@ -274,7 +275,7 @@ class Core:
|
||||||
|
|
||||||
result = proxmox.get_config("{0}:{1}".format(proxmox_cluster_url,
|
result = proxmox.get_config("{0}:{1}".format(proxmox_cluster_url,
|
||||||
int(proxmox_cluster_port)),
|
int(proxmox_cluster_port)),
|
||||||
node,
|
instance_informations['node'],
|
||||||
instancetype,
|
instancetype,
|
||||||
vmid)
|
vmid)
|
||||||
|
|
||||||
|
@ -287,13 +288,13 @@ class Core:
|
||||||
|
|
||||||
return result
|
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 """
|
""" Find node/cluster informations from vmid """
|
||||||
try:
|
try:
|
||||||
#instance_informations = self.mongo.get_instance(vmid, node, cluster)
|
instance_informations = self.mongo.get_instance(vmid)
|
||||||
|
|
||||||
""" Find cluster informations from node """
|
""" 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_url = cluster_informations["url"]
|
||||||
proxmox_cluster_port = cluster_informations["port"]
|
proxmox_cluster_port = cluster_informations["port"]
|
||||||
|
@ -304,7 +305,7 @@ class Core:
|
||||||
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
|
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
|
||||||
|
|
||||||
""" LOAD PROXMOX """
|
""" LOAD PROXMOX """
|
||||||
proxmox = Proxmox(node)
|
proxmox = Proxmox(instance_informations['node'])
|
||||||
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
|
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
|
||||||
int(proxmox_cluster_port)),
|
int(proxmox_cluster_port)),
|
||||||
proxmox_cluster_user,
|
proxmox_cluster_user,
|
||||||
|
@ -312,12 +313,12 @@ class Core:
|
||||||
|
|
||||||
result = proxmox.resize_instance("{0}:{1}".format(proxmox_cluster_url,
|
result = proxmox.resize_instance("{0}:{1}".format(proxmox_cluster_url,
|
||||||
int(proxmox_cluster_port)),
|
int(proxmox_cluster_port)),
|
||||||
node,
|
instance_informations['node'],
|
||||||
instancetype,
|
instancetype,
|
||||||
vmid, data)
|
vmid, data)
|
||||||
|
|
||||||
if result['result'] == "OK":
|
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:
|
except IndexError as ierror:
|
||||||
result = {
|
result = {
|
||||||
|
|
|
@ -28,6 +28,7 @@ class Crawler:
|
||||||
self.mongo.client = self.mongo.connect()
|
self.mongo.client = self.mongo.connect()
|
||||||
self.mongo.db = self.mongo.client.db
|
self.mongo.db = self.mongo.client.db
|
||||||
|
|
||||||
|
""" Instances types availables: lxc/qemu/all"""
|
||||||
def run(self, instancetype="lxc"):
|
def run(self, instancetype="lxc"):
|
||||||
insert_time = time.time()
|
insert_time = time.time()
|
||||||
|
|
||||||
|
@ -50,9 +51,16 @@ class Crawler:
|
||||||
nodes_list = proxmox.get_nodes("{0}:{1}".format(cluster["url"], int(cluster["port"])))
|
nodes_list = proxmox.get_nodes("{0}:{1}".format(cluster["url"], int(cluster["port"])))
|
||||||
if nodes_list["result"] == "OK":
|
if nodes_list["result"] == "OK":
|
||||||
for value_nodes_list in nodes_list["value"]["data"]:
|
for value_nodes_list in nodes_list["value"]["data"]:
|
||||||
|
list_instances = ""
|
||||||
""" TOTAL COUNT CPU and RAM allocate"""
|
""" TOTAL COUNT CPU and RAM allocate"""
|
||||||
list_instances = proxmox.get_instance("{0}:{1}".format(cluster["url"], int(cluster["port"])),
|
if(instancetype == "all"):
|
||||||
value_nodes_list["node"], instancetype)["value"]
|
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 key_list_instances, value_list_instances in list_instances.items():
|
||||||
for instance in value_list_instances:
|
for instance in value_list_instances:
|
||||||
|
@ -63,7 +71,6 @@ class Crawler:
|
||||||
# si non existante, alors il s'agit d'une instance manuelle
|
# si non existante, alors il s'agit d'une instance manuelle
|
||||||
instance["commandid"] = "000000"
|
instance["commandid"] = "000000"
|
||||||
self.mongo.insert_instance(instance)
|
self.mongo.insert_instance(instance)
|
||||||
# Si elle existe déjà, on l'update:
|
# Si elle existe deja, on l'update:
|
||||||
else:
|
else:
|
||||||
self.mongo.update_instance(instance, instance["vmid"], instance["node"], instance["cluster"])
|
self.mongo.update_instance(instance, instance["vmid"], instance["node"], instance["cluster"])
|
||||||
return
|
|
|
@ -211,18 +211,27 @@ class MongoDB:
|
||||||
def insert_instance(self, data):
|
def insert_instance(self, data):
|
||||||
return self.db[self.collection_instance].insert(data)
|
return self.db[self.collection_instance].insert(data)
|
||||||
|
|
||||||
def update_instance(self, data, vmid, node, cluster):
|
def update_instance(self, data, vmid, node=None, cluster=None):
|
||||||
return self.db[self.collection_instance].update(
|
if node and cluster:
|
||||||
{"vmid": int(vmid), "node": node, "cluster": cluster}, {'$set': data}, upsert=False
|
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):
|
def delete_instance(self, vmid, node=None, cluster=None):
|
||||||
self.db[self.collection_instance].remove({"vmid": int(vmid), "node": node, "cluster": cluster})
|
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:
|
try:
|
||||||
return json.loads(dumps(
|
if node and cluster:
|
||||||
self.db[self.collection_instance].find_one(
|
return json.loads(dumps(
|
||||||
{"vmid": int(vmid), "node": node, "cluster": cluster})))
|
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:
|
except BaseException as serr:
|
||||||
raise ("MongoDB error on {0}:{1} ({2})".format(self.server, self.port, serr))
|
raise ("MongoDB error on {0}:{1} ({2})".format(self.server, self.port, serr))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue