fix http session being used inside hub model

This commit is contained in:
forest 2021-02-21 14:22:14 -06:00
parent 70628039c4
commit b32db058f0

View File

@ -8,7 +8,6 @@ from typing import List, Tuple
import aiohttp import aiohttp
from flask import current_app from flask import current_app
from flask import session
from time import sleep from time import sleep
from os.path import join from os.path import join
from subprocess import run from subprocess import run
@ -51,21 +50,22 @@ class MockHub(VirtualizationInterface):
class CapsulFlaskHub(VirtualizationInterface): class CapsulFlaskHub(VirtualizationInterface):
def synchronous_operation(self, hosts: List[OnlineHost], payload: str) -> List[HTTPResult]: def synchronous_operation(self, hosts: List[OnlineHost], email: str, payload: str) -> List[HTTPResult]:
return self.generic_operation(hosts, payload, True)[1] return self.generic_operation(hosts, email, payload, True)[1]
def asynchronous_operation(self, hosts: List[OnlineHost], payload: str) -> Tuple[int, List[HTTPResult]]: def asynchronous_operation(self, hosts: List[OnlineHost], email: str, payload: str) -> Tuple[int, List[HTTPResult]]:
return self.generic_operation(hosts, payload, False) return self.generic_operation(hosts, email, payload, False)
def generic_operation(self, hosts: List[OnlineHost], email: str, payload: str, immediate_mode: bool) -> Tuple[int, List[HTTPResult]]:
def generic_operation(self, hosts: List[OnlineHost], payload: str, immediate_mode: bool) -> Tuple[int, List[HTTPResult]]:
email = session["account"]
if not email or email == "":
raise ValueError("generic_operation was called but user was not logged in")
url_path = "/spoke/operation" url_path = "/spoke/operation"
operation_id = None operation_id = None
if not immediate_mode: if not immediate_mode:
if not email or email == "":
raise ValueError("can't create_operation in the db cuz no email was provided")
operation_id = get_model().create_operation(hosts, email, payload) operation_id = get_model().create_operation(hosts, email, payload)
url_path = f"/spoke/operation/{operation_id}" url_path = f"/spoke/operation/{operation_id}"
authorization_header = f"Bearer {current_app.config['HUB_TOKEN']}" authorization_header = f"Bearer {current_app.config['HUB_TOKEN']}"
results = current_app.config["HTTP_CLIENT"].do_multi_http_sync(hosts, url_path, payload, authorization_header=authorization_header) results = current_app.config["HTTP_CLIENT"].do_multi_http_sync(hosts, url_path, payload, authorization_header=authorization_header)
@ -123,7 +123,7 @@ class CapsulFlaskHub(VirtualizationInterface):
def capacity_avaliable(self, additional_ram_bytes): def capacity_avaliable(self, additional_ram_bytes):
online_hosts = get_model().get_online_hosts() online_hosts = get_model().get_online_hosts()
payload = json.dumps(dict(type="capacity_avaliable", additional_ram_bytes=additional_ram_bytes)) payload = json.dumps(dict(type="capacity_avaliable", additional_ram_bytes=additional_ram_bytes))
results = self.synchronous_operation(online_hosts, payload) results = self.synchronous_operation(online_hosts, None, payload)
for result in results: for result in results:
try: try:
result_body = json.loads(result.body) result_body = json.loads(result.body)
@ -140,7 +140,7 @@ class CapsulFlaskHub(VirtualizationInterface):
host = get_model().host_of_capsul(id) host = get_model().host_of_capsul(id)
if host is not None: if host is not None:
payload = json.dumps(dict(type="get", id=id, get_ssh_host_keys=get_ssh_host_keys)) payload = json.dumps(dict(type="get", id=id, get_ssh_host_keys=get_ssh_host_keys))
results = self.synchronous_operation([host], payload) results = self.synchronous_operation([host], None, payload)
for result in results: for result in results:
try: try:
result_body = json.loads(result.body) result_body = json.loads(result.body)
@ -154,7 +154,7 @@ class CapsulFlaskHub(VirtualizationInterface):
def list_ids(self) -> list: def list_ids(self) -> list:
online_hosts = get_model().get_online_hosts() online_hosts = get_model().get_online_hosts()
payload = json.dumps(dict(type="list_ids")) payload = json.dumps(dict(type="list_ids"))
results = self.synchronous_operation(online_hosts, payload) results = self.synchronous_operation(online_hosts, None, payload)
to_return = [] to_return = []
for i in range(len(results)): for i in range(len(results)):
host = online_hosts[i] host = online_hosts[i]
@ -193,7 +193,7 @@ class CapsulFlaskHub(VirtualizationInterface):
memory_mb=memory_mb, memory_mb=memory_mb,
ssh_authorized_keys=ssh_authorized_keys, ssh_authorized_keys=ssh_authorized_keys,
)) ))
op = self.asynchronous_operation(online_hosts, payload) op = self.asynchronous_operation(online_hosts, email, payload)
operation_id = op[0] operation_id = op[0]
results = op[1] results = op[1]
number_of_assigned = 0 number_of_assigned = 0
@ -226,7 +226,7 @@ class CapsulFlaskHub(VirtualizationInterface):
host = get_model().host_of_capsul(id) host = get_model().host_of_capsul(id)
if host is not None: if host is not None:
payload = json.dumps(dict(type="destroy", email=email, id=id)) payload = json.dumps(dict(type="destroy", email=email, id=id))
results = self.synchronous_operation([host], payload) results = self.synchronous_operation([host], email, payload)
result_json_string = "<no response from host>" result_json_string = "<no response from host>"
for result in results: for result in results:
try: try:
@ -247,7 +247,7 @@ class CapsulFlaskHub(VirtualizationInterface):
host = get_model().host_of_capsul(id) host = get_model().host_of_capsul(id)
if host is not None: if host is not None:
payload = json.dumps(dict(type="vm_state_command", email=email, id=id, command=command)) payload = json.dumps(dict(type="vm_state_command", email=email, id=id, command=command))
results = self.synchronous_operation([host], payload) results = self.synchronous_operation([host], email, payload)
result_json_string = "<no response from host>" result_json_string = "<no response from host>"
for result in results: for result in results:
try: try: