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
|
2018-02-08 14:30:00 +00:00
|
|
|
|
import hashlib
|
2017-10-25 17:44:15 +00:00
|
|
|
|
|
2018-04-27 15:24:31 +00:00
|
|
|
|
def RunAnalyse(clusters_conf, generalconf, logger):
|
|
|
|
|
play = Analyse(clusters_conf, generalconf, logger)
|
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")
|
2018-04-27 15:24:31 +00:00
|
|
|
|
time.sleep(int(generalconf["analyst"]["walker"]))
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
class Core:
|
2018-02-08 14:30:00 +00:00
|
|
|
|
# def __init__(self, generalconf, Lredis):
|
2018-04-27 15:24:31 +00:00
|
|
|
|
def __init__(self, generalconf, logger):
|
2018-04-28 11:45:19 +00:00
|
|
|
|
self.purge = False
|
2017-10-21 20:04:42 +00:00
|
|
|
|
self.generalconf = generalconf
|
2018-04-27 15:24:31 +00:00
|
|
|
|
self.logger = logger
|
|
|
|
|
self.logger.write({"thread":threading.get_ident() ,"result": "INFO", "type": "HYPERPROXMOX",
|
|
|
|
|
"value": "Start Core process"})
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
""" LOAD MONGODB """
|
2018-04-27 15:24:31 +00:00
|
|
|
|
self.logger.write({"thread": threading.get_ident(), "result": "INFO", "type": "HYPERPROXMOX",
|
|
|
|
|
"value": "MongoDB connection"})
|
2017-10-21 20:04:42 +00:00
|
|
|
|
self.mongo = MongoDB(generalconf["mongodb"]["ip"])
|
|
|
|
|
self.mongo.client = self.mongo.connect()
|
|
|
|
|
|
|
|
|
|
""" LOAD REDIS """
|
2018-04-27 15:24:31 +00:00
|
|
|
|
self.logger.write({"thread": threading.get_ident(),"result": "INFO", "type": "HYPERPROXMOX",
|
|
|
|
|
"value": "Redis connection"})
|
2018-02-08 14:30:00 +00:00
|
|
|
|
self.redis_msg = Redis_wrapper(generalconf["redis"]["ip"],
|
2018-02-09 14:28:11 +00:00
|
|
|
|
generalconf["redis"]["port"], 0)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-08 14:30:00 +00:00
|
|
|
|
self.redis_cache = Redis_wrapper(generalconf["redis"]["ip"],
|
2018-02-09 14:28:11 +00:00
|
|
|
|
generalconf["redis"]["port"], 3)
|
|
|
|
|
|
|
|
|
|
self.redis_msg.connect()
|
|
|
|
|
self.redis_cache.connect()
|
2018-02-08 14:30:00 +00:00
|
|
|
|
|
2018-02-11 18:40:05 +00:00
|
|
|
|
if self.mongo.client and self.redis_msg.connect() and self.redis_cache.connect():
|
2017-10-21 20:04:42 +00:00
|
|
|
|
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
|
|
|
|
|
2018-02-08 16:21:58 +00:00
|
|
|
|
""" Clean previous lockers """
|
2018-04-27 15:24:31 +00:00
|
|
|
|
self.logger.write({"thread": threading.get_ident(), "result": "INFO", "type": "HYPERPROXMOX",
|
|
|
|
|
"value": "Clean Locker"})
|
2018-02-08 16:21:58 +00:00
|
|
|
|
locker = Locker()
|
2018-02-09 12:17:50 +00:00
|
|
|
|
locker.unlock(generalconf["analyst"]["walker_lock"], "startup")
|
2018-02-08 16:21:58 +00:00
|
|
|
|
|
2017-10-21 20:04:42 +00:00
|
|
|
|
thc = threading.Thread(name="Update statistics",
|
|
|
|
|
target=RunAnalyse,
|
2018-04-27 15:24:31 +00:00
|
|
|
|
args=(self.clusters_conf, self.generalconf, self.logger))
|
2017-10-21 20:04:42 +00:00
|
|
|
|
thc.start()
|
2018-02-11 18:40:05 +00:00
|
|
|
|
else:
|
|
|
|
|
exit(1)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
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-09 14:28:11 +00:00
|
|
|
|
def getkey(self, keytype):
|
|
|
|
|
if keytype == "all":
|
|
|
|
|
return self.mongo.get_all_datekey()
|
|
|
|
|
elif keytype == "last":
|
|
|
|
|
return self.mongo.get_last_datekey()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-02-08 14:30:00 +00:00
|
|
|
|
""" Test Redis Cache """
|
2018-02-09 14:28:11 +00:00
|
|
|
|
hash_object = hashlib.md5("{0}-{1}-{2}-{3}-{4}".format(dest, date, cluster, node, vmid).encode('utf-8'))
|
2018-02-08 14:30:00 +00:00
|
|
|
|
hash_hex = hash_object.hexdigest()
|
|
|
|
|
|
2018-02-13 16:42:47 +00:00
|
|
|
|
cache = self.redis_cache.get_message(hash_hex)
|
2018-02-08 14:30:00 +00:00
|
|
|
|
|
2018-04-28 11:45:19 +00:00
|
|
|
|
if cache is None or self.generalconf["logger"]["logs_level"] == 5 or self.purge:
|
2018-02-08 14:30:00 +00:00
|
|
|
|
if dest == "instances":
|
2018-02-09 12:17:50 +00:00
|
|
|
|
resultmbrequest = self.mongo.get_instances(date, cluster, node, vmid)
|
2018-02-08 14:30:00 +00:00
|
|
|
|
elif dest == "nodes":
|
2018-02-09 12:17:50 +00:00
|
|
|
|
resultmbrequest = self.mongo.get_nodes(date, cluster, node)
|
2018-02-08 18:09:30 +00:00
|
|
|
|
elif dest == "disks":
|
2018-02-09 12:17:50 +00:00
|
|
|
|
resultmbrequest = self.mongo.get_disks(date, cluster, node, vmid)
|
2018-02-08 18:09:30 +00:00
|
|
|
|
elif dest == "storages":
|
2018-02-09 12:17:50 +00:00
|
|
|
|
resultmbrequest = self.mongo.get_storages(date, cluster, node)
|
2018-02-08 14:30:00 +00:00
|
|
|
|
elif dest == "clusters":
|
2018-02-08 18:46:44 +00:00
|
|
|
|
resultmbrequest = self.mongo.get_clusters(date, cluster)
|
2018-02-08 14:30:00 +00:00
|
|
|
|
else:
|
|
|
|
|
resultmbrequest = json_decode({"value": "Bad request"})
|
|
|
|
|
|
2018-02-08 16:21:58 +00:00
|
|
|
|
self.redis_cache.insert_message(hash_hex, resultmbrequest, 3600)
|
2018-02-08 14:30:00 +00:00
|
|
|
|
return resultmbrequest
|
|
|
|
|
else:
|
2018-02-13 16:42:47 +00:00
|
|
|
|
return json.loads(cache.replace("'", "\"").replace("None", "\"\""))
|
2018-02-04 22:10:18 +00:00
|
|
|
|
|
|
|
|
|
|
2017-10-21 20:04:42 +00:00
|
|
|
|
"""
|
|
|
|
|
#######################
|
|
|
|
|
# INSTANCE MANAGEMENT #
|
|
|
|
|
#######################
|
|
|
|
|
"""
|
2018-02-09 12:17:50 +00:00
|
|
|
|
def insert_instances(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-09 12:17:50 +00:00
|
|
|
|
nodes_informations = self.mongo.get_nodes_informations((int(lastkeyvalid["value"])), node, cluster)
|
|
|
|
|
clusters_informations = self.mongo.get_clusters_conf(cluster)["value"]
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox_clusters_url = clusters_informations["url"]
|
|
|
|
|
proxmox_clusters_port = clusters_informations["port"]
|
|
|
|
|
proxmox_clusters_user = pdecrypt(base64.b64decode(clusters_informations["user"]),
|
2018-04-28 12:26:32 +00:00
|
|
|
|
self.generalconf["keys"]["key_pvt"])["value"].decode('utf-8')
|
2017-10-26 15:59:16 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox_clusters_pwd = pdecrypt(base64.b64decode(clusters_informations["password"]),
|
2018-04-28 12:26:32 +00:00
|
|
|
|
self.generalconf["keys"]["key_pvt"])["value"].decode('utf-8')
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox_template = clusters_informations["template"]
|
|
|
|
|
proxmox_storages_disk = clusters_informations["storages_disk"]
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
""" LOAD PROXMOX """
|
2018-02-04 22:10:18 +00:00
|
|
|
|
proxmox = Proxmox(node)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox.get_ticket("{0}:{1}".format(proxmox_clusters_url,
|
|
|
|
|
int(proxmox_clusters_port)),
|
|
|
|
|
proxmox_clusters_user,
|
|
|
|
|
proxmox_clusters_pwd)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
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"""
|
2018-02-09 12:17:50 +00:00
|
|
|
|
next_instances_id = int(get_info_system["instances_number"]+1)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
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"]:
|
2018-02-09 12:17:50 +00:00
|
|
|
|
get_instances_ip = get_info_system["IP_current"]
|
|
|
|
|
next_ip = iter_iprange(get_instances_ip, '172.16.255.250', step=1)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
# 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,
|
2018-02-09 12:17:50 +00:00
|
|
|
|
'vmid': next_instances_id,
|
|
|
|
|
'storage': proxmox_storages_disk,
|
2017-10-21 20:04:42 +00:00
|
|
|
|
'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 """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
result_new = proxmox.create_instances("{0}:{1}".format(proxmox_clusters_url,
|
|
|
|
|
int(proxmox_clusters_port)), node, instancetype,
|
2017-10-21 20:04:42 +00:00
|
|
|
|
data)
|
2017-10-29 17:00:00 +00:00
|
|
|
|
""" Get first digest """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
digest_init = proxmox.get_config("{0}:{1}".format(proxmox_clusters_url,
|
|
|
|
|
int(proxmox_clusters_port)),
|
|
|
|
|
node, instancetype, next_instances_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 """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
self.mongo.update_system_instances_id(next_instances_id)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
""" INCREMENT INSTANCE IP IN DATABASE """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
self.mongo.update_system_instances_ip(ip)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
""" INSERT THIS NEW SERVER IN DATABASE """
|
|
|
|
|
data["commandid"] = command_id
|
2018-02-09 12:17:50 +00:00
|
|
|
|
data["cluster"] = nodes_informations["cluster"]
|
2017-10-21 20:04:42 +00:00
|
|
|
|
data["node"] = target
|
|
|
|
|
data["ip"] = ip
|
2018-02-08 14:30:00 +00:00
|
|
|
|
data["date"] = lastkeyvalid["value"]
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
self.mongo.insert_instances(data)
|
2017-10-29 17:00:00 +00:00
|
|
|
|
""" Limit creation DDOS based on digest """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
while digest_init == proxmox.get_config("{0}:{1}".format(proxmox_clusters_url,
|
|
|
|
|
int(proxmox_clusters_port)),
|
|
|
|
|
node, instancetype, next_instances_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-09 12:17:50 +00:00
|
|
|
|
def delete_instances(self, vmid, instancetype="lxc"):
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
""" Find node/cluster informations from vmid """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
instances_informations = self.mongo.get_instances(vmid)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
""" Find cluster informations from node """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
clusters_informations = self.mongo.get_clusters_conf(instances_informations['cluster'])["value"]
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox_clusters_url = clusters_informations["url"]
|
|
|
|
|
proxmox_clusters_port = clusters_informations["port"]
|
|
|
|
|
proxmox_clusters_user = pdecrypt(base64.b64decode(clusters_informations["user"]),
|
2018-04-28 12:26:32 +00:00
|
|
|
|
self.generalconf["keys"]["key_pvt"])["value"].decode('utf-8')
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox_clusters_pwd = pdecrypt(base64.b64decode(clusters_informations["password"]),
|
2018-04-28 12:26:32 +00:00
|
|
|
|
self.generalconf["keys"]["key_pvt"])["value"].decode('utf-8')
|
2017-10-21 20:04:42 +00:00
|
|
|
|
""" LOAD PROXMOX """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox = Proxmox(instances_informations['node'])
|
|
|
|
|
proxmox.get_ticket("{0}:{1}".format(proxmox_clusters_url,
|
|
|
|
|
int(proxmox_clusters_port)),
|
|
|
|
|
proxmox_clusters_user,
|
|
|
|
|
proxmox_clusters_pwd)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
result = proxmox.delete_instances("{0}:{1}".format(proxmox_clusters_url,
|
|
|
|
|
int(proxmox_clusters_port)),
|
|
|
|
|
instances_informations['node'], instancetype, vmid)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
if result['result'] == "OK":
|
2018-02-09 12:17:50 +00:00
|
|
|
|
self.mongo.delete_instances(vmid)
|
|
|
|
|
self.mongo.update_system_free_ip(instances_informations['ip'])
|
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-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
2018-02-18 17:52:44 +00:00
|
|
|
|
def status_instances(self, id, action):
|
2017-10-21 20:04:42 +00:00
|
|
|
|
""" Find node/cluster informations from vmid """
|
|
|
|
|
try:
|
2018-02-18 17:52:44 +00:00
|
|
|
|
instances_informations = self.mongo.generalmongosearch("instances", id)["value"]
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
""" Find cluster informations from node """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
clusters_informations = self.mongo.get_clusters_conf(instances_informations['cluster'])["value"]
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-18 17:52:44 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox_clusters_url = clusters_informations["url"]
|
|
|
|
|
proxmox_clusters_port = clusters_informations["port"]
|
|
|
|
|
proxmox_clusters_user = pdecrypt(base64.b64decode(clusters_informations["user"]),
|
2018-04-28 12:26:32 +00:00
|
|
|
|
self.generalconf["keys"]["key_pvt"])["value"].decode('utf-8')
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox_clusters_pwd = pdecrypt(base64.b64decode(clusters_informations["password"]),
|
2018-04-28 12:26:32 +00:00
|
|
|
|
self.generalconf["keys"]["key_pvt"])["value"].decode('utf-8')
|
2017-10-21 20:04:42 +00:00
|
|
|
|
""" LOAD PROXMOX """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox = Proxmox(instances_informations['node'])
|
|
|
|
|
proxmox.get_ticket("{0}:{1}".format(proxmox_clusters_url,
|
|
|
|
|
int(proxmox_clusters_port)),
|
|
|
|
|
proxmox_clusters_user,
|
|
|
|
|
proxmox_clusters_pwd)
|
|
|
|
|
|
|
|
|
|
result = proxmox.status_instances("{0}:{1}".format(proxmox_clusters_url,
|
|
|
|
|
int(proxmox_clusters_port)),
|
|
|
|
|
instances_informations['node'],
|
2018-02-18 17:52:44 +00:00
|
|
|
|
instances_informations['type'],
|
|
|
|
|
instances_informations['vmid'], action)
|
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-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
def info_instances(self, vmid, instancetype="lxc"):
|
2017-10-21 20:04:42 +00:00
|
|
|
|
""" Find node/cluster informations from vmid """
|
|
|
|
|
try:
|
2018-02-09 12:17:50 +00:00
|
|
|
|
instances_informations = self.mongo.get_instances(vmid)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
""" Find cluster informations from node """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
clusters_informations = self.mongo.get_clusters_conf(instances_informations['cluster'])["value"]
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox_clusters_url = clusters_informations["url"]
|
|
|
|
|
proxmox_clusters_port = clusters_informations["port"]
|
|
|
|
|
proxmox_clusters_user = pdecrypt(base64.b64decode(clusters_informations["user"]),
|
2018-04-28 12:26:32 +00:00
|
|
|
|
self.generalconf["keys"]["key_pvt"])["value"].decode('utf-8')
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox_clusters_pwd = pdecrypt(base64.b64decode(clusters_informations["password"]),
|
2018-04-28 12:26:32 +00:00
|
|
|
|
self.generalconf["keys"]["key_pvt"])["value"].decode('utf-8')
|
2017-10-21 20:04:42 +00:00
|
|
|
|
""" LOAD PROXMOX """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox = Proxmox(instances_informations['node'])
|
|
|
|
|
proxmox.get_ticket("{0}:{1}".format(proxmox_clusters_url,
|
|
|
|
|
int(proxmox_clusters_port)),
|
|
|
|
|
proxmox_clusters_user,
|
|
|
|
|
proxmox_clusters_pwd)
|
|
|
|
|
|
|
|
|
|
result = proxmox.get_config("{0}:{1}".format(proxmox_clusters_url,
|
|
|
|
|
int(proxmox_clusters_port)),
|
|
|
|
|
instances_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-09 12:17:50 +00:00
|
|
|
|
def change_instances(self, vmid, data, instancetype="lxc"):
|
2017-10-21 20:04:42 +00:00
|
|
|
|
""" Find node/cluster informations from vmid """
|
|
|
|
|
try:
|
2018-02-09 12:17:50 +00:00
|
|
|
|
instances_informations = self.mongo.get_instances(vmid)
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
""" Find cluster informations from node """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
clusters_informations = self.mongo.get_clusters_conf(instances_informations['cluster'])["value"]
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox_clusters_url = clusters_informations["url"]
|
|
|
|
|
proxmox_clusters_port = clusters_informations["port"]
|
|
|
|
|
proxmox_clusters_user = pdecrypt(base64.b64decode(clusters_informations["user"]),
|
2018-04-28 12:26:32 +00:00
|
|
|
|
self.generalconf["keys"]["key_pvt"])["value"].decode('utf-8')
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox_clusters_pwd = pdecrypt(base64.b64decode(clusters_informations["password"]),
|
2018-04-28 12:26:32 +00:00
|
|
|
|
self.generalconf["keys"]["key_pvt"])["value"].decode('utf-8')
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
|
|
|
|
""" LOAD PROXMOX """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
proxmox = Proxmox(instances_informations['node'])
|
|
|
|
|
proxmox.get_ticket("{0}:{1}".format(proxmox_clusters_url,
|
|
|
|
|
int(proxmox_clusters_port)),
|
|
|
|
|
proxmox_clusters_user,
|
|
|
|
|
proxmox_clusters_pwd)
|
|
|
|
|
|
2018-03-20 15:45:33 +00:00
|
|
|
|
result = proxmox.change_instances("{0}:{1}".format(proxmox_clusters_url,
|
2018-02-09 12:17:50 +00:00
|
|
|
|
int(proxmox_clusters_port)),
|
|
|
|
|
instances_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-09 12:17:50 +00:00
|
|
|
|
self.mongo.update_instances(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 #
|
|
|
|
|
#######################
|
|
|
|
|
"""
|
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
def get_clusters_conf(self, cluster=None):
|
2018-02-13 16:42:47 +00:00
|
|
|
|
""" Test Redis Cache """
|
|
|
|
|
hash_object = hashlib.md5("{0}-{1}".format("administration", cluster).encode('utf-8'))
|
|
|
|
|
hash_hex = hash_object.hexdigest()
|
|
|
|
|
|
|
|
|
|
cache = self.redis_cache.get_message(hash_hex)
|
|
|
|
|
|
2018-04-28 16:04:26 +00:00
|
|
|
|
if cache is None or self.generalconf["logger"]["logs_level"] == 5:
|
2018-02-13 16:42:47 +00:00
|
|
|
|
clusters_informations = self.mongo.get_clusters_conf(cluster)
|
|
|
|
|
self.redis_cache.insert_message(hash_hex, clusters_informations, 500)
|
|
|
|
|
return clusters_informations
|
|
|
|
|
else:
|
|
|
|
|
return json.loads(cache.replace("'", "\""))
|
|
|
|
|
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
def insert_clusters_conf(self, data):
|
|
|
|
|
testdata = valid_clusters_data(data)
|
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"]:
|
2018-04-28 12:26:32 +00:00
|
|
|
|
data["user"] = base64.b64encode(pcrypt(data["user"], self.generalconf["keys"]["key_pvt"])["value"]).decode('utf-8')
|
|
|
|
|
data["password"] = base64.b64encode(pcrypt(data["password"], self.generalconf["keys"]["key_pvt"])["value"]).decode('utf-8')
|
2018-02-08 18:16:03 +00:00
|
|
|
|
new_cluster = self.mongo.insert_clusters_conf(data)
|
2017-10-25 18:47:26 +00:00
|
|
|
|
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 = {
|
2018-04-28 16:04:26 +00:00
|
|
|
|
"value": "{1} {0}".format(testdata, "Invalid or missing parameter"),
|
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
|
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
def change_clusters_conf(self, cluster, data):
|
|
|
|
|
clusters_update = self.mongo.update_clusters_conf(cluster, data)
|
|
|
|
|
return clusters_update
|
2017-10-29 17:00:00 +00:00
|
|
|
|
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
def delete_clusters_conf(self, cluster):
|
2017-10-29 17:00:00 +00:00
|
|
|
|
""" Find cluster informations from node """
|
2018-02-09 12:17:50 +00:00
|
|
|
|
clusters_delete = self.mongo.delete_clusters_conf(cluster)
|
|
|
|
|
return clusters_delete
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-04-27 15:24:31 +00:00
|
|
|
|
"""
|
|
|
|
|
#######################
|
|
|
|
|
# DATA MANAGEMENT #
|
|
|
|
|
#######################
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def managedata(self, json):
|
|
|
|
|
if json["action"] == "purge":
|
2018-04-28 11:45:19 +00:00
|
|
|
|
purgedata = {}
|
2018-04-27 15:24:31 +00:00
|
|
|
|
if json["type"] == "strict":
|
2018-04-28 11:45:19 +00:00
|
|
|
|
listdate = self.mongo.get_all_datekey()["value"]
|
|
|
|
|
self.logger.write({"thread": threading.get_ident(), "result": "INFO", "type": "HYPERPROXMOX",
|
|
|
|
|
"value": "Starting purge process"})
|
|
|
|
|
self.purge = True # Purge Lock
|
|
|
|
|
totalinstances, totalnodes, totaldisks, totalstorares, totalclusters, totaldates = 0, 0, 0, 0, 0, 0
|
|
|
|
|
for date in listdate:
|
|
|
|
|
if int(date['date']) <= int(json["date"]):
|
|
|
|
|
for instance in self.generalquerycacheinfra("instances", date['date'])["value"]:
|
|
|
|
|
purgedata[instance["_id"]["$oid"]] = self.mongo.generalmongodelete("instances", instance["_id"]["$oid"])
|
|
|
|
|
totalinstances += 1
|
|
|
|
|
|
|
|
|
|
for node in self.generalquerycacheinfra("nodes", date['date'])["value"]:
|
|
|
|
|
purgedata[node["_id"]["$oid"]] = self.mongo.generalmongodelete("nodes", node["_id"]["$oid"])
|
|
|
|
|
totalnodes += 1
|
|
|
|
|
|
|
|
|
|
for storage in self.generalquerycacheinfra("storages", date['date'])["value"]:
|
|
|
|
|
purgedata[storage["_id"]["$oid"]] = self.mongo.generalmongodelete("storages", storage["_id"]["$oid"])
|
|
|
|
|
totalstorares += 1
|
|
|
|
|
|
|
|
|
|
for disk in self.generalquerycacheinfra("disks", date['date'])["value"]:
|
|
|
|
|
purgedata[disk["_id"]["$oid"]] = self.mongo.generalmongodelete("disks", disk["_id"]["$oid"])
|
|
|
|
|
totaldisks += 1
|
|
|
|
|
|
|
|
|
|
for cluster in self.generalquerycacheinfra("clusters", date['date'])["value"]:
|
|
|
|
|
purgedata[cluster["_id"]["$oid"]] = self.mongo.generalmongodelete("clusters", cluster["_id"]["$oid"])
|
|
|
|
|
totalclusters += 1
|
|
|
|
|
|
|
|
|
|
purgedata[date["_id"]["$oid"]] = self.mongo.generalmongodelete("dates", date["_id"]["$oid"])
|
|
|
|
|
totaldates += 1
|
|
|
|
|
|
|
|
|
|
self.purge = False # Purge UnLock
|
|
|
|
|
valuedelete = {
|
|
|
|
|
"Instances": totalinstances, "Disks": totaldisks, "Nodes": totalnodes,
|
|
|
|
|
"Storages": totalstorares, "Dates": totaldates, "Clusters": totalclusters
|
|
|
|
|
}
|
|
|
|
|
self.logger.write({"thread": threading.get_ident(), "result": "INFO", "type": "HYPERPROXMOX",
|
|
|
|
|
"value": "{0} entries in your database deleted".format(valuedelete)})
|
|
|
|
|
self.logger.write({"thread": threading.get_ident(), "result": "INFO", "type": "HYPERPROXMOX",
|
|
|
|
|
"value": "Purge process terminated"})
|
|
|
|
|
data = {
|
|
|
|
|
"value": "{0} entries in your database deleted".format(valuedelete),
|
|
|
|
|
"result": "OK",
|
2018-04-27 15:24:31 +00:00
|
|
|
|
"type": "HYPERPROXMOX"
|
|
|
|
|
}
|
|
|
|
|
else:
|
2018-04-28 11:45:19 +00:00
|
|
|
|
data = {
|
|
|
|
|
"value": "Bad request",
|
|
|
|
|
"result": "ERROR",
|
2018-04-27 15:24:31 +00:00
|
|
|
|
"type": "HYPERPROXMOX"
|
|
|
|
|
}
|
2018-04-28 11:45:19 +00:00
|
|
|
|
else:
|
|
|
|
|
data = {
|
|
|
|
|
"value": "Bad request",
|
|
|
|
|
"result": "ERROR",
|
|
|
|
|
"type": "HYPERPROXMOX"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data
|
2017-10-21 20:04:42 +00:00
|
|
|
|
|
2018-02-04 22:10:18 +00:00
|
|
|
|
"""
|
|
|
|
|
#######################
|
|
|
|
|
# STORAGES MANAGEMENT #
|
|
|
|
|
#######################
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
#######################
|
|
|
|
|
# NODES MANAGEMENT #
|
|
|
|
|
#######################
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-09 12:17:50 +00:00
|
|
|
|
def valid_clusters_data(data):
|
2018-02-23 13:59:36 +00:00
|
|
|
|
key_required = ["name", "url", "port", "user", "password", "template", "storage_disk", "weight", "exclude_nodes", "groups"]
|
2017-10-21 20:04:42 +00:00
|
|
|
|
result = []
|
|
|
|
|
for key in key_required:
|
|
|
|
|
if key not in data:
|
|
|
|
|
result.append(key)
|
|
|
|
|
return result
|