forked from 3wordchant/capsul-flask
create capsul is working
This commit is contained in:
@ -8,6 +8,7 @@ from typing import List, Tuple
|
||||
|
||||
import aiohttp
|
||||
from flask import current_app
|
||||
from flask import session
|
||||
from time import sleep
|
||||
from os.path import join
|
||||
from subprocess import run
|
||||
@ -38,14 +39,27 @@ class MockHub(VirtualizationInterface):
|
||||
|
||||
class CapsulFlaskHub(VirtualizationInterface):
|
||||
|
||||
def synchronous_operation(self, hosts: List[OnlineHost], payload: str) -> List[HTTPResult]:
|
||||
return self.generic_operation(hosts, payload, True)[1]
|
||||
|
||||
def asynchronous_operation(self, hosts: List[OnlineHost], payload: str) -> Tuple[int, List[HTTPResult]]:
|
||||
return self.generic_operation(hosts, payload, False)
|
||||
|
||||
def generic_operation(self, hosts: List[OnlineHost], payload: str, immediate_mode: bool) -> Tuple[int, List[HTTPResult]]:
|
||||
operation_id = get_model().create_operation(hosts, payload)
|
||||
email = session["account"]
|
||||
if not email or email == "":
|
||||
raise ValueError("generic_operation was called but user was not logged in")
|
||||
url_path = "/spoke/operation"
|
||||
operation_id = None
|
||||
if not immediate_mode:
|
||||
operation_id = get_model().create_operation(hosts, email, payload)
|
||||
url_path = f"/spoke/operation/{operation_id}"
|
||||
authorization_header = f"Bearer {current_app.config['HUB_TOKEN']}"
|
||||
results = current_app.config["HTTP_CLIENT"].make_requests_sync(hosts, "/spoke/operation", payload, authorization_header=authorization_header)
|
||||
results = current_app.config["HTTP_CLIENT"].make_requests_sync(hosts, url_path, payload, authorization_header=authorization_header)
|
||||
|
||||
for i in range(len(hosts)):
|
||||
host = hosts[i]
|
||||
result = results[i]
|
||||
task_result = None
|
||||
assignment_status = "pending"
|
||||
if result.status_code == -1:
|
||||
assignment_status = "no_response_from_host"
|
||||
@ -64,8 +78,6 @@ class CapsulFlaskHub(VirtualizationInterface):
|
||||
assignment_status = "invalid_response_from_host"
|
||||
error_message = ""
|
||||
try:
|
||||
if immediate_mode:
|
||||
task_result = result.body
|
||||
result_body = json.loads(result.body)
|
||||
result_is_json = True
|
||||
result_is_dict = isinstance(result_body, dict)
|
||||
@ -87,16 +99,16 @@ class CapsulFlaskHub(VirtualizationInterface):
|
||||
error_message: {error_message}
|
||||
"""
|
||||
)
|
||||
|
||||
get_model().update_host_operation(host.id, operation_id, assignment_status, task_result)
|
||||
|
||||
if not immediate_mode:
|
||||
get_model().update_host_operation(host.id, operation_id, assignment_status, None)
|
||||
|
||||
return results
|
||||
return (operation_id, results)
|
||||
|
||||
def capacity_avaliable(self, additional_ram_bytes):
|
||||
online_hosts = get_model().get_online_hosts()
|
||||
payload = json.dumps(dict(type="capacity_avaliable", additional_ram_bytes=additional_ram_bytes))
|
||||
op = self.generic_operation(online_hosts, payload, True)
|
||||
results = op[1]
|
||||
results = self.synchronous_operation(online_hosts, payload)
|
||||
for result in results:
|
||||
try:
|
||||
result_body = json.loads(result.body)
|
||||
@ -108,13 +120,12 @@ class CapsulFlaskHub(VirtualizationInterface):
|
||||
return False
|
||||
|
||||
|
||||
async def get(self, id) -> VirtualMachine:
|
||||
def get(self, id) -> VirtualMachine:
|
||||
validate_capsul_id(id)
|
||||
host = get_model().host_of_capsul(id)
|
||||
if host is not None:
|
||||
payload = json.dumps(dict(type="get", id=id))
|
||||
op = self.generic_operation([host], payload, True)
|
||||
results = op[1]
|
||||
results = self.synchronous_operation([host], payload)
|
||||
for result in results:
|
||||
try:
|
||||
result_body = json.loads(result.body)
|
||||
@ -128,9 +139,7 @@ class CapsulFlaskHub(VirtualizationInterface):
|
||||
def list_ids(self) -> list:
|
||||
online_hosts = get_model().get_online_hosts()
|
||||
payload = json.dumps(dict(type="list_ids"))
|
||||
op = self.generic_operation(online_hosts, payload, False)
|
||||
operation_id = op[0]
|
||||
results = op[1]
|
||||
results = self.synchronous_operation(online_hosts, payload)
|
||||
to_return = []
|
||||
for i in range(len(results)):
|
||||
host = online_hosts[i]
|
||||
@ -145,11 +154,8 @@ class CapsulFlaskHub(VirtualizationInterface):
|
||||
to_return.append(id)
|
||||
except:
|
||||
all_valid = False
|
||||
if all_valid:
|
||||
get_model().update_host_operation(host.id, operation_id, None, result.body)
|
||||
else:
|
||||
if not all_valid:
|
||||
result_json_string = json.dumps({"error_message": "invalid capsul id returned"})
|
||||
get_model().update_host_operation(host.id, operation_id, None, result_json_string)
|
||||
current_app.logger.error(f"""error reading ids for list_ids operation {operation_id}, host {host.id}""")
|
||||
else:
|
||||
result_json_string = json.dumps({"error_message": "invalid response, missing 'ids' list"})
|
||||
@ -173,7 +179,7 @@ class CapsulFlaskHub(VirtualizationInterface):
|
||||
memory_mb=memory_mb,
|
||||
ssh_public_keys=ssh_public_keys,
|
||||
))
|
||||
op = self.generic_operation(online_hosts, payload, False)
|
||||
op = self.asynchronous_operation(online_hosts, payload)
|
||||
operation_id = op[0]
|
||||
results = op[1]
|
||||
number_of_assigned = 0
|
||||
@ -206,8 +212,7 @@ class CapsulFlaskHub(VirtualizationInterface):
|
||||
host = get_model().host_of_capsul(id)
|
||||
if host is not None:
|
||||
payload = json.dumps(dict(type="destroy", id=id))
|
||||
op = self.generic_operation([host], payload, True)
|
||||
results = op[1]
|
||||
results = self.synchronous_operation([host], payload)
|
||||
result_json_string = "<no response from host>"
|
||||
for result in results:
|
||||
try:
|
||||
|
Reference in New Issue
Block a user