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/api/v1/api.py

255 lines
7.8 KiB
Python
Raw Normal View History

2017-10-21 20:04:42 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import threading
import web
from core.core import *
from core.modules.mod_analyst import *
import json
import time
import random
import ast
2017-10-29 17:00:00 +00:00
def cooktheticket(ticket, action, target):
if ticket == "aaa":
return True
else:
return False
2017-10-21 20:04:42 +00:00
class Auth:
def POST(self):
# '{"username":"fff", "password":"azerty"}'
data = json.loads(web.data().decode('utf-8'))
print(data["username"])
# Test Login
# If true generate an ticket
2017-10-29 17:00:00 +00:00
# use date and ip
i = web.input(ticket='aaa')
web.setcookie('ticket', i.ticket, 3600)
2017-10-21 20:04:42 +00:00
return
2017-10-29 17:00:00 +00:00
2018-02-04 22:10:18 +00:00
""" CLASS MONGO CACHE """
class General_Search:
2018-02-07 19:00:08 +00:00
def GET(self, query, id):
try:
return core.generalsearch(query, id)
except BaseException as e:
result = {
"result": "ERROR",
"type": "PYTHON - API",
"value": "{0} {1}".format("Invalid request:", e)
}
return result
2018-02-04 22:10:18 +00:00
class QueryCache_Infra:
def GET(self, dest, date, cluster=None, node=None, vmid=None):
try:
result = core.generalquerycacheinfra(dest, date, cluster, node, vmid)
except BaseException as e:
result = {
"result": "ERROR",
"type": "PYTHON - API",
"value": "{0} {1}".format("Invalid request:", e)
}
return result
class Static_Nodes:
def GET(self, date, cluster=None, node=None):
if node and cluster:
return core.generalsearch('{ "date": {0}, "cluster": {1}, "node": {2} }'.format(date, cluster, node))
elif cluster:
return core.generalsearch('{ "date": {0}, "cluster": {1} }'.format(date, cluster))
else:
return core.generalsearch('{ "date": {0}}'.format(date))
class Dates:
def GET(self):
return core.generalsearch('{ "_id": {id} }'.format(id=nodeid))
""" CLASS DIRECT """
2017-10-21 20:04:42 +00:00
class Cluster:
def GET(self, cluster=None):
2017-10-25 18:45:10 +00:00
try:
if cluster:
2018-02-09 12:17:50 +00:00
result = core.get_clusters_conf(cluster)
2017-10-25 18:45:10 +00:00
else:
2018-02-09 12:17:50 +00:00
result = core.get_clusters_conf()
2017-10-25 18:45:10 +00:00
except BaseException as e:
2017-10-26 15:59:16 +00:00
result = {
"result": "ERROR",
"type": "PYTHON - API",
"value": "{0} {1}".format("Invalid request:", e)
}
2017-10-25 18:45:10 +00:00
return result
2017-10-21 20:04:42 +00:00
def POST(self):
2017-10-25 18:45:10 +00:00
try:
data = json.loads(web.data().decode('utf-8'))
2018-02-09 12:17:50 +00:00
result = core.insert_clusters_conf(data)
2017-10-25 18:45:10 +00:00
except BaseException as e:
2017-10-26 15:59:16 +00:00
result = {
"result": "ERROR",
"type": "PYTHON - API",
"value": "{0} {1}".format("Invalid request:", e)
}
2017-10-25 18:45:10 +00:00
return result
2017-10-21 20:04:42 +00:00
def PUT(self, cluster):
2017-10-25 18:45:10 +00:00
try:
data = json.loads(web.data().decode('utf-8'))
2018-02-09 12:17:50 +00:00
result = core.change_clusters_conf(cluster, data)
2017-10-25 18:45:10 +00:00
except BaseException as e:
2017-10-26 15:59:16 +00:00
result = {
"result": "ERROR",
"type": "PYTHON - API",
"value": "{0} {1}".format("Invalid request:", e)
}
2017-10-25 18:45:10 +00:00
return result
2017-10-21 20:04:42 +00:00
def DELETE(self, cluster):
2017-10-25 18:45:10 +00:00
try:
2018-02-09 12:17:50 +00:00
result = core.delete_clusters_conf(cluster)
2017-10-25 18:45:10 +00:00
except BaseException as e:
2017-10-26 15:59:16 +00:00
result = {
"result": "ERROR",
"type": "PYTHON - API",
"value": "{0} {1}".format("Invalid request:", e)
}
2017-10-25 18:45:10 +00:00
return result
2017-10-21 20:04:42 +00:00
class Instance:
2018-02-04 22:10:18 +00:00
def GET(self, vmid, status=None):
2017-10-27 15:56:52 +00:00
try:
if status:
""" GET INSTANCE STATUS """
2018-02-09 12:17:50 +00:00
result = core.status_instances(vmid, status)
2017-10-29 17:00:00 +00:00
else:
2017-10-27 15:56:52 +00:00
""" GET INSTANCE INFORMATION """
2018-02-09 12:17:50 +00:00
result = core.info_instances(vmid)
2017-10-27 15:56:52 +00:00
except BaseException as e:
result = {
"result": "ERROR",
"type": "PYTHON - API",
2017-10-29 17:00:00 +00:00
"value": "Invalid request: {0}".format(e)
2017-10-27 15:56:52 +00:00
}
return result
2017-10-21 20:04:42 +00:00
def POST(self, vmid=None, status=None):
2017-10-27 15:56:52 +00:00
try:
if vmid:
""" GET INSTANCE INFORMATION """
2018-02-09 12:17:50 +00:00
result = core.status_instances(vmid, status)
2017-10-27 15:56:52 +00:00
else:
""" CREATE NEWS INSTANCES"""
count = json.loads(web.data().decode('utf-8'))["count"]
""" GENERATE UNIQ COMMAND ID """
randtext = ''.join(random.choice('0123456789ABCDEF') for i in range(8))
command_id = "{0}_{1}_{2}".format(time.time(), count, randtext)
""" LOAD CLUSTER CONFIGURATIONS """
select = Analyse(core.clusters_conf, generalconf)
sorted_nodes = dict(select.set_attribution(count))
""" START ALL Thread """
2018-02-04 22:10:18 +00:00
for nodeid, count in sorted_nodes.items():
""" Find information by id mongodb"""
realnode = core.generalsearch('{ "_id": {id} }'.format(id=nodeid))
2017-10-27 15:56:52 +00:00
# Limit to 5 instance per block
thci = threading.Thread(name="Insert Instance",
target=core.insert_instance,
2018-02-04 22:10:18 +00:00
args=(realnode["name"], cluster["cluster"], str(count), command_id,))
2017-10-27 15:56:52 +00:00
thci.start()
""" Wait all results of Thread from redis messages queue. Valid or not """
timeout = 2 * int(generalconf["deploy"]["concurrencydeploy"]) * int(generalconf["deploy"]["delayrounddeploy"])
for t in range(timeout):
time.sleep(1)
try:
if len(ast.literal_eval(redis_msg.get_message(command_id))) == int(count):
2017-10-27 15:56:52 +00:00
break
except BaseException as err:
print("Value not found", err)
""" Return messages """
return ast.literal_eval(redis_msg.get_message(command_id))
2017-10-27 15:56:52 +00:00
except BaseException as e:
result = {
"result": "ERROR",
"type": "PYTHON - API",
"value": "{0} {1}".format("Invalid request:", e)
}
return result
2017-10-21 20:04:42 +00:00
def PUT(self, vmid):
2017-10-27 15:56:52 +00:00
try:
data = json.loads(web.data().decode('utf-8'))
2018-02-09 12:17:50 +00:00
result = core.change_instances(vmid, data)
2017-10-27 15:56:52 +00:00
except BaseException as e:
result = {
"result": "ERROR",
"type": "PYTHON - API",
"value": "{0} {1}".format("Invalid request:", e)
}
return result
2017-10-21 20:04:42 +00:00
def DELETE(self, vmid):
2017-10-27 15:56:52 +00:00
try:
2018-02-09 12:17:50 +00:00
result = core.delete_instances(vmid)
2017-10-27 15:56:52 +00:00
except BaseException as e:
result = {
"result": "ERROR",
"type": "PYTHON - API",
"value": "{0} {1}".format("Invalid request:", e)
}
return result
2017-10-21 20:04:42 +00:00
class ThreadAPI(threading.Thread):
#def __init__(self, threadid, name, urls, c, g, r):
def __init__(self, threadid, name, urls, c, g):
2017-10-21 20:04:42 +00:00
""" Pass Global var in this theard."""
global core, generalconf, redis_msg
2017-10-21 20:04:42 +00:00
core = c
generalconf = g
redis_msg = core.redis_msg
2017-10-21 20:04:42 +00:00
""" RUN API """
threading.Thread.__init__(self)
self.threadID = threadid
self.threadName = name
self.app = HttpApi(urls, globals())
self.app.notfound = notfound
def run(self):
print("Start API server...")
self.app.run()
def stop(self):
print("Stop API server...")
self.app.stop()
def notfound():
return web.notfound({"value": "Bad request"})
class HttpApi(web.application):
def run(self, ip="127.0.0.1", port=8080, *middleware):
func = self.wsgifunc(*middleware)
return web.httpserver.runsimple(func, (ip, int(port)))