1
0
Fork 0
mirror of https://github.com/ThomasGsp/HyperProxmox.git synced 2025-02-13 11:42:13 +00:00
HyperProxmox/code/scripts/main/core/core.py

428 lines
17 KiB
Python
Raw Normal View History

2017-10-21 20:04:42 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Author: Tlams
Langage: Python
Minimum version require: 3.4
"""
from core.modules.mod_proxmox import *
from core.modules.mod_database import *
from core.modules.mod_analyst import *
from core.modules.mod_access import *
from core.libs.hcrypt import *
from netaddr import iter_iprange
import threading
import time
2017-10-25 17:44:15 +00:00
import base64
2017-10-21 20:04:42 +00:00
def RunAnalyse(clusters_conf, generalconf, delay=300):
2018-02-04 22:10:18 +00:00
play = Analyse(clusters_conf, generalconf)
2017-11-15 11:35:34 +00:00
2017-10-21 20:04:42 +00:00
while True:
2018-02-03 18:56:28 +00:00
""" Instances types availables: lxc/qemu/all"""
play.run("all")
2017-10-21 20:04:42 +00:00
time.sleep(delay)
class Core:
def __init__(self, generalconf, Lredis):
self.generalconf = generalconf
self.Lredis = Lredis
""" LOAD MONGODB """
self.mongo = MongoDB(generalconf["mongodb"]["ip"])
self.mongo.client = self.mongo.connect()
""" LOAD REDIS """
self.redis_msg = Lredis
if self.mongo.client and self.redis_msg.connect():
self.mongo.db = self.mongo.client.db
""" Others """
# value
self.concurrencydeploy = generalconf["deploy"]["concurrencydeploy"]
# in seconds
self.delayrounddeploy = generalconf["deploy"]["delayrounddeploy"]
""" RUN THE ANALYZER IN DEDICATED THEARD"""
2017-10-26 15:59:16 +00:00
self.clusters_conf = self.mongo.get_clusters_conf()["value"]
2017-10-21 20:04:42 +00:00
thc = threading.Thread(name="Update statistics",
target=RunAnalyse,
args=(self.clusters_conf, self.generalconf))
thc.start()
2018-02-04 22:10:18 +00:00
"""
#######################
# GENERAL FUNCTIONS #
#######################
"""
2018-02-07 17:59:47 +00:00
def is_json(selmyjson):
2018-02-04 22:10:18 +00:00
try:
2018-02-07 17:59:47 +00:00
json.loads(myjson)
except ValueError as e:
2018-02-04 22:10:18 +00:00
return False
return True
2018-02-07 18:59:54 +00:00
def generalsearch(self, collection, id):
try:
return self.mongo.generalmongosearch(collection, str(id))
except:
2018-02-04 22:10:18 +00:00
return json_decode({"value": "Bad request"})
2018-02-07 17:59:47 +00:00
def generalquerycacheinfra(self, dest, date, cluster=None, node=None, vmid=None):
2018-02-04 22:10:18 +00:00
if dest == "instances":
return self.mongo.get_instance(date, cluster, node, vmid)
elif dest == "nodes":
return self.mongo.get_nodes_informations(date, cluster, node)
elif dest == "clusters":
return self.mongo.get_clusters_conf(date, cluster)
else:
json_decode({"value": "Bad request"})
2017-10-21 20:04:42 +00:00
"""
#######################
# INSTANCE MANAGEMENT #
#######################
"""
2018-02-04 22:10:18 +00:00
def insert_instance(self, node, cluster, count=1, command_id=000000, instancetype="lxc"):
2017-10-21 20:04:42 +00:00
""" Find cluster informations from node """
lastkeyvalid = self.mongo.get_last_datekey()
2018-02-04 22:10:18 +00:00
node_informations = self.mongo.get_nodes_informations((int(lastkeyvalid["value"])), node, cluster)
cluster_informations = self.mongo.get_clusters_conf(cluster)["value"]
2017-10-21 20:04:42 +00:00
proxmox_cluster_url = cluster_informations["url"]
proxmox_cluster_port = cluster_informations["port"]
2017-10-29 17:00:00 +00:00
proxmox_cluster_user = pdecrypt(base64.b64decode(cluster_informations["user"]),
2017-10-26 15:59:16 +00:00
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
2017-10-29 17:00:00 +00:00
proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]),
2017-10-26 15:59:16 +00:00
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
2017-10-21 20:04:42 +00:00
proxmox_template = cluster_informations["template"]
proxmox_storage_disk = cluster_informations["storage_disk"]
""" LOAD PROXMOX """
2018-02-04 22:10:18 +00:00
proxmox = Proxmox(node)
2017-10-21 20:04:42 +00:00
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
proxmox_cluster_user,
proxmox_cluster_pwd)
returnlistresult = []
currentcount = 0
for c in range(0, int(count)):
if currentcount == self.concurrencydeploy:
time.sleep(self.delayrounddeploy)
currentcount = 0
currentcount = currentcount + 1
get_info_system = self.mongo.get_system_info()
""" FIND NEXT INSTANCE ID AVAILABLE AND INCREMENT IT"""
next_instance_id = int(get_info_system["instances_number"]+1)
2017-11-18 20:07:58 +00:00
""" TEST THIS ID """
2017-10-21 20:04:42 +00:00
""" FIND LAST LAST IP USE AND INCREMENT IT"""
if not get_info_system["IP_free"]:
get_instance_ip = get_info_system["IP_current"]
next_ip = iter_iprange(get_instance_ip, '172.16.255.250', step=1)
# Revoir pour un truc plus clean ....
next(next_ip)
ip = str(next(next_ip))
else:
ip = str(get_info_system["IP_free"][0])
self.mongo.update_system_delete_ip(ip)
2017-10-29 17:00:00 +00:00
# insert check duplicate entry
2017-10-21 20:04:42 +00:00
""" INSTANCE DEFINITION """
data = {
'ostemplate': proxmox_template,
'vmid': next_instance_id,
'storage': proxmox_storage_disk,
'cores': 1,
'cpulimit': 1,
'cpuunits': 512,
'arch': "amd64",
'memory': 256,
'description': command_id,
'onboot': 0,
'swap': 256,
'ostype': 'debian',
'net0': 'name=eth0,bridge=vmbr1,ip={0}/16,gw=172.16.1.254'.format(ip),
'ssh-public-keys': get_info_system["sshpublickey"]
}
""" INSTANCE INSERTION """
result_new = proxmox.create_instance("{0}:{1}".format(proxmox_cluster_url,
2018-02-04 22:10:18 +00:00
int(proxmox_cluster_port)), node, instancetype,
2017-10-21 20:04:42 +00:00
data)
2017-10-29 17:00:00 +00:00
""" Get first digest """
digest_init = proxmox.get_config("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
2018-02-04 22:10:18 +00:00
node, instancetype, next_instance_id)['value']['data']['digest']
2017-10-29 17:00:00 +00:00
2017-10-21 20:04:42 +00:00
""" VERIFY THE RESULT BY PROXMOX STATUS REQUEST CODE """
if result_new['result'] == "OK":
""" INCREMENT INSTANCE ID IN DATABASE """
self.mongo.update_system_instance_id(next_instance_id)
""" INCREMENT INSTANCE IP IN DATABASE """
self.mongo.update_system_instance_ip(ip)
""" INSERT THIS NEW SERVER IN DATABASE """
data["commandid"] = command_id
data["cluster"] = node_informations["cluster"]
data["node"] = target
data["ip"] = ip
self.mongo.insert_instance(data)
2017-10-29 17:00:00 +00:00
""" Limit creation DDOS based on digest """
while digest_init == proxmox.get_config("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
2018-02-04 22:10:18 +00:00
node, instancetype, next_instance_id)['value']['data']['digest']:
2017-10-29 17:00:00 +00:00
time.sleep(5)
2017-10-21 20:04:42 +00:00
returnlistresult.append(result_new)
""" SEND MESSAGE IN REDIS """
self.redis_msg.insert_message(command_id, returnlistresult)
return
2018-02-03 18:56:28 +00:00
def delete_instance(self, vmid, instancetype="lxc"):
2017-10-21 20:04:42 +00:00
try:
""" Find node/cluster informations from vmid """
2018-02-03 18:56:28 +00:00
instance_informations = self.mongo.get_instance(vmid)
2017-10-21 20:04:42 +00:00
""" Find cluster informations from node """
2018-02-03 18:56:28 +00:00
cluster_informations = self.mongo.get_clusters_conf(instance_informations['cluster'])["value"]
2017-10-21 20:04:42 +00:00
proxmox_cluster_url = cluster_informations["url"]
proxmox_cluster_port = cluster_informations["port"]
2017-10-29 17:00:00 +00:00
proxmox_cluster_user = pdecrypt(base64.b64decode(cluster_informations["user"]),
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
2017-10-21 20:04:42 +00:00
2017-10-29 17:00:00 +00:00
proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]),
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
2017-10-21 20:04:42 +00:00
""" LOAD PROXMOX """
2018-02-03 18:56:28 +00:00
proxmox = Proxmox(instance_informations['node'])
2017-10-21 20:04:42 +00:00
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
proxmox_cluster_user,
proxmox_cluster_pwd)
result = proxmox.delete_instance("{0}:{1}".format(proxmox_cluster_url,
2017-11-15 11:35:34 +00:00
int(proxmox_cluster_port)),
2018-02-03 18:56:28 +00:00
instance_informations['node'], instancetype, vmid)
2017-10-21 20:04:42 +00:00
if result['result'] == "OK":
2018-02-03 18:56:28 +00:00
self.mongo.delete_instance(vmid)
2017-10-21 20:04:42 +00:00
self.mongo.update_system_free_ip(instance_informations['ip'])
2017-10-26 15:59:16 +00:00
except IndexError as ierror:
result = {
"result": "ERROR",
"type": "PROXMOX - VALUES",
"value": "{0} is not a valid VMID: {1}".format(vmid, ierror)
}
2017-10-21 20:04:42 +00:00
return result
2018-02-03 18:56:28 +00:00
def status_instance(self, vmid, action, instancetype="lxc"):
2017-10-21 20:04:42 +00:00
""" Find node/cluster informations from vmid """
try:
2018-02-03 18:56:28 +00:00
instance_informations = self.mongo.get_instance(vmid)
2017-10-21 20:04:42 +00:00
""" Find cluster informations from node """
2018-02-03 18:56:28 +00:00
cluster_informations = self.mongo.get_clusters_conf(instance_informations['cluster'])["value"]
2017-10-21 20:04:42 +00:00
proxmox_cluster_url = cluster_informations["url"]
proxmox_cluster_port = cluster_informations["port"]
2017-10-29 17:00:00 +00:00
proxmox_cluster_user = pdecrypt(base64.b64decode(cluster_informations["user"]),
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
2017-10-21 20:04:42 +00:00
2017-10-29 17:00:00 +00:00
proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]),
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
2017-10-21 20:04:42 +00:00
""" LOAD PROXMOX """
2018-02-03 18:56:28 +00:00
proxmox = Proxmox(instance_informations['node'])
2017-10-21 20:04:42 +00:00
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
proxmox_cluster_user,
proxmox_cluster_pwd)
result = proxmox.status_instance("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
2018-02-03 18:56:28 +00:00
instance_informations['node'],
2017-11-15 11:35:34 +00:00
instancetype,
2017-10-21 20:04:42 +00:00
vmid, action)
2017-10-26 15:59:16 +00:00
except IndexError as ierror:
result = {
"result": "ERROR",
"type": "PROXMOX - VALUES",
"value": "{0} is not a valid VMID: {1}".format(vmid, ierror)
}
2017-10-21 20:04:42 +00:00
return result
2018-02-03 18:56:28 +00:00
def info_instance(self, vmid, instancetype="lxc"):
2017-10-21 20:04:42 +00:00
""" Find node/cluster informations from vmid """
try:
2018-02-03 18:56:28 +00:00
instance_informations = self.mongo.get_instance(vmid)
2017-10-21 20:04:42 +00:00
""" Find cluster informations from node """
2018-02-03 18:56:28 +00:00
cluster_informations = self.mongo.get_clusters_conf(instance_informations['cluster'])["value"]
2017-10-21 20:04:42 +00:00
proxmox_cluster_url = cluster_informations["url"]
proxmox_cluster_port = cluster_informations["port"]
2017-10-29 17:00:00 +00:00
proxmox_cluster_user = pdecrypt(base64.b64decode(cluster_informations["user"]),
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
2017-10-21 20:04:42 +00:00
2017-10-29 17:00:00 +00:00
proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]),
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
2017-10-21 20:04:42 +00:00
""" LOAD PROXMOX """
2018-02-03 18:56:28 +00:00
proxmox = Proxmox(instance_informations['node'])
2017-10-21 20:04:42 +00:00
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
proxmox_cluster_user,
proxmox_cluster_pwd)
result = proxmox.get_config("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
2018-02-03 18:56:28 +00:00
instance_informations['node'],
2017-11-15 11:35:34 +00:00
instancetype,
2017-10-21 20:04:42 +00:00
vmid)
2017-10-26 15:59:16 +00:00
except IndexError as ierror:
result = {
"result": "ERROR",
"type": "PROXMOX - VALUES",
"value": "{0} is not a valid VMID: {1}".format(vmid, ierror)
}
2017-10-21 20:04:42 +00:00
return result
2018-02-03 18:56:28 +00:00
def change_instance(self, vmid, data, instancetype="lxc"):
2017-10-21 20:04:42 +00:00
""" Find node/cluster informations from vmid """
try:
2018-02-03 18:56:28 +00:00
instance_informations = self.mongo.get_instance(vmid)
2017-10-21 20:04:42 +00:00
""" Find cluster informations from node """
2018-02-03 18:56:28 +00:00
cluster_informations = self.mongo.get_clusters_conf(instance_informations['cluster'])["value"]
2017-10-21 20:04:42 +00:00
proxmox_cluster_url = cluster_informations["url"]
proxmox_cluster_port = cluster_informations["port"]
2017-10-29 17:00:00 +00:00
proxmox_cluster_user = pdecrypt(base64.b64decode(cluster_informations["user"]),
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
2017-10-21 20:04:42 +00:00
2017-10-29 17:00:00 +00:00
proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster_informations["password"]),
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
2017-10-21 20:04:42 +00:00
""" LOAD PROXMOX """
2018-02-03 18:56:28 +00:00
proxmox = Proxmox(instance_informations['node'])
2017-10-21 20:04:42 +00:00
proxmox.get_ticket("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
proxmox_cluster_user,
proxmox_cluster_pwd)
result = proxmox.resize_instance("{0}:{1}".format(proxmox_cluster_url,
int(proxmox_cluster_port)),
2018-02-03 18:56:28 +00:00
instance_informations['node'],
2017-11-15 11:35:34 +00:00
instancetype,
2017-10-21 20:04:42 +00:00
vmid, data)
if result['result'] == "OK":
2018-02-03 18:56:28 +00:00
self.mongo.update_instance(data, vmid)
2017-10-21 20:04:42 +00:00
2017-10-26 15:59:16 +00:00
except IndexError as ierror:
result = {
"result": "ERROR",
"type": "PROXMOX - VALUES",
"value": "{0} is not a valid VMID: {1}".format(vmid, ierror)
}
2017-10-29 17:00:00 +00:00
return result
2017-10-21 20:04:42 +00:00
"""
#######################
# CLUSTERS MANAGEMENT #
#######################
"""
def get_cluster(self, cluster=None):
""" Find cluster informations from node """
2017-10-26 15:59:16 +00:00
cluster_informations = self.mongo.get_clusters_conf(cluster)["value"]
2017-10-21 20:04:42 +00:00
return cluster_informations
def insert_cluster(self, data):
testdata = valid_cluster_data(data)
2017-10-25 18:47:26 +00:00
2017-10-21 20:04:42 +00:00
if not testdata:
2017-10-26 15:59:16 +00:00
if not self.mongo.get_clusters_conf(data["name"])["value"]:
2017-10-25 18:47:26 +00:00
data["user"] = base64.b64encode(pcrypt(data["user"], self.generalconf["keys"]["key_pvt"])["data"]).decode('utf-8')
data["password"] = base64.b64encode(pcrypt(data["password"], self.generalconf["keys"]["key_pvt"])["data"]).decode('utf-8')
new_cluster = self.mongo.insert_new_cluster(data)
else:
2017-10-27 15:56:52 +00:00
new_cluster = {
2017-10-29 17:00:00 +00:00
"value": "{0}".format("Duplicate entry, please delete the current cluster or update it"),
2017-10-27 15:56:52 +00:00
"result": "ERROR",
"type": "PROXMOX - VALUES"
}
2017-10-21 20:04:42 +00:00
else:
2017-10-27 15:56:52 +00:00
new_cluster = {
2017-10-29 17:00:00 +00:00
"value": "{1} {0}".format(testdata, "Invalid or miss paramettrer"),
2017-10-27 15:56:52 +00:00
"result": "ERROR",
"type": "PROXMOX - VALUES"
}
2017-10-25 18:47:26 +00:00
2017-10-21 20:04:42 +00:00
return new_cluster
def change_cluster(self, cluster, data):
2017-10-29 17:00:00 +00:00
cluster_update = self.mongo.update_cluster(cluster, data)
return cluster_update
2017-10-21 20:04:42 +00:00
def delete_cluster(self, cluster):
2017-10-29 17:00:00 +00:00
""" Find cluster informations from node """
cluster_delete = self.mongo.delete_cluster(cluster)
return cluster_delete
2017-10-21 20:04:42 +00:00
2018-02-04 22:10:18 +00:00
"""
#######################
# STORAGES MANAGEMENT #
#######################
"""
"""
#######################
# NODES MANAGEMENT #
#######################
"""
2017-10-21 20:04:42 +00:00
def valid_cluster_data(data):
key_required = ["name", "url", "port", "user", "password", "template", "storage_disk", "weight", "exclude_nodes"]
result = []
for key in key_required:
if key not in data:
result.append(key)
return result