From b32db058f0128ec0e9cee258a51baffdf37e406d Mon Sep 17 00:00:00 2001 From: forest Date: Sun, 21 Feb 2021 14:22:14 -0600 Subject: [PATCH] fix http session being used inside hub model --- capsulflask/hub_model.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/capsulflask/hub_model.py b/capsulflask/hub_model.py index 840cefb..b9fd1ee 100644 --- a/capsulflask/hub_model.py +++ b/capsulflask/hub_model.py @@ -8,7 +8,6 @@ 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 @@ -51,21 +50,22 @@ 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 synchronous_operation(self, hosts: List[OnlineHost], email: str, payload: str) -> List[HTTPResult]: + return self.generic_operation(hosts, email, payload, True)[1] - def asynchronous_operation(self, hosts: List[OnlineHost], payload: str) -> Tuple[int, List[HTTPResult]]: - return self.generic_operation(hosts, payload, False) + def asynchronous_operation(self, hosts: List[OnlineHost], email: str, payload: str) -> Tuple[int, List[HTTPResult]]: + return self.generic_operation(hosts, email, payload, False) - 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") + def generic_operation(self, hosts: List[OnlineHost], email: str, payload: str, immediate_mode: bool) -> Tuple[int, List[HTTPResult]]: + url_path = "/spoke/operation" operation_id = None 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) url_path = f"/spoke/operation/{operation_id}" + 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) @@ -123,7 +123,7 @@ class CapsulFlaskHub(VirtualizationInterface): 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)) - results = self.synchronous_operation(online_hosts, payload) + results = self.synchronous_operation(online_hosts, None, payload) for result in results: try: result_body = json.loads(result.body) @@ -140,7 +140,7 @@ class CapsulFlaskHub(VirtualizationInterface): host = get_model().host_of_capsul(id) if host is not None: 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: try: result_body = json.loads(result.body) @@ -154,7 +154,7 @@ class CapsulFlaskHub(VirtualizationInterface): def list_ids(self) -> list: online_hosts = get_model().get_online_hosts() payload = json.dumps(dict(type="list_ids")) - results = self.synchronous_operation(online_hosts, payload) + results = self.synchronous_operation(online_hosts, None, payload) to_return = [] for i in range(len(results)): host = online_hosts[i] @@ -193,7 +193,7 @@ class CapsulFlaskHub(VirtualizationInterface): memory_mb=memory_mb, 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] results = op[1] number_of_assigned = 0 @@ -226,7 +226,7 @@ class CapsulFlaskHub(VirtualizationInterface): host = get_model().host_of_capsul(id) if host is not None: 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 = "" for result in results: try: @@ -247,7 +247,7 @@ class CapsulFlaskHub(VirtualizationInterface): host = get_model().host_of_capsul(id) if host is not None: 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 = "" for result in results: try: