Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it a bit more user friendly when you can't log in #383

Merged
merged 5 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions spinnman/spalloc/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
from logging import getLogger
import re
import requests
from json.decoder import JSONDecodeError
from typing import Dict, Tuple, cast, Optional
import websocket # type: ignore
from spinn_utilities.log import FormatAdapter
from spinn_utilities.typing.json import JsonObject
from .utils import clean_url
from spinnman.exceptions import SpallocException
from .utils import clean_url

logger = FormatAdapter(getLogger(__name__))
#: The name of the session cookie issued by Spring Security
Expand Down Expand Up @@ -250,8 +251,18 @@ def renew(self) -> JsonObject:
data=form, timeout=10)
logger.debug("POST {} returned {}",
self.__login_submit_url, r.status_code)
self._session_id = r.cookies[_SESSION_COOKIE]
# We don't need to follow that redirect
try:
self._session_id = r.cookies[_SESSION_COOKIE]
except KeyError as e:
try:
json_error = r.json()
if 'message' in json_error:
error = json_error['message']
else:
error = str(json_error)
except JSONDecodeError:
error = r.raw
raise SpallocException(f"Unable to login: {error}") from e

# Step three: get the basic service data and new CSRF token
obj: JsonObject = self.get(self.__srv_base).json()
Expand Down
28 changes: 13 additions & 15 deletions spinnman/spalloc/spalloc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,47 +571,45 @@ def __proxy_url(self) -> Optional[str]:
except KeyError:
return None

def __init_proxy(self) -> _ProxyReceiver:
def __init_proxy(self) -> Tuple[_ProxyReceiver, WebSocket]:
if self.__proxy_handle is None or not self.__proxy_handle.connected:
if self.__proxy_url is None:
raise ValueError("no proxy available")
self.__proxy_handle = self._websocket(
self.__proxy_url, origin=get_hostname(self._url))
self.__proxy_thread = _ProxyReceiver(self.__proxy_handle)
self.__proxy_ping = _ProxyPing(self.__proxy_handle)
assert self.__proxy_handle is not None
assert self.__proxy_thread is not None
return self.__proxy_thread
return self.__proxy_thread, self.__proxy_handle

@overrides(SpallocJob.connect_to_board)
def connect_to_board(
self, x: int, y: int,
port: int = SCP_SCAMP_PORT) -> SpallocSCPConnection:
proxy = self.__init_proxy()
return _ProxiedSCAMPConnection(
self.__proxy_handle, proxy, int(x), int(y), int(port))
proxy, ws = self.__init_proxy()
return _ProxiedSCAMPConnection(ws, proxy, int(x), int(y), int(port))

@overrides(SpallocJob.connect_for_booting)
def connect_for_booting(self) -> SpallocBootConnection:
proxy = self.__init_proxy()
return _ProxiedBootConnection(self.__proxy_handle, proxy)
proxy, ws = self.__init_proxy()
return _ProxiedBootConnection(ws, proxy)

@overrides(SpallocJob.open_eieio_connection)
def open_eieio_connection(self, x: int, y: int) -> SpallocEIEIOConnection:
proxy = self.__init_proxy()
proxy, ws = self.__init_proxy()
return _ProxiedEIEIOConnection(
self.__proxy_handle, proxy, int(x), int(y), SCP_SCAMP_PORT)
ws, proxy, int(x), int(y), SCP_SCAMP_PORT)

@overrides(SpallocJob.open_eieio_listener_connection)
def open_eieio_listener_connection(self) -> SpallocEIEIOListener:
proxy = self.__init_proxy()
return _ProxiedEIEIOListener(
self.__proxy_handle, proxy, self.get_connections())
proxy, ws = self.__init_proxy()
return _ProxiedEIEIOListener(ws, proxy, self.get_connections())

@overrides(SpallocJob.open_udp_listener_connection)
def open_udp_listener_connection(self) -> UDPConnection:
proxy = self.__init_proxy()
return _ProxiedUDPListener(
self.__proxy_handle, proxy, self.get_connections())
proxy, ws = self.__init_proxy()
return _ProxiedUDPListener(ws, proxy, self.get_connections())

@overrides(SpallocJob.wait_for_state_change)
def wait_for_state_change(self, old_state: SpallocState,
Expand Down