Skip to content

Commit

Permalink
Merge pull request spyder-ide#23231 from ccordoba12/improve-remote-cl…
Browse files Browse the repository at this point in the history
…ient-status-and-logging

PR: Improve reporting status and logger messages (Remote client)
  • Loading branch information
ccordoba12 authored Dec 12, 2024
2 parents 6ddc7e5 + 7dfa72c commit c023c4e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
75 changes: 60 additions & 15 deletions spyder/plugins/remoteclient/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ def api_token(self):

@property
def peer_host(self):
if not self.ssh_is_connected:
if self._ssh_connection is not None:
return self._ssh_connection.get_extra_info("peername")[0]
else:
return None

return self._ssh_connection.get_extra_info("peername")[0]

@property
def peer_port(self):
if not self.ssh_is_connected:
Expand Down Expand Up @@ -247,7 +247,7 @@ async def get_server_info(self):
info = json.loads(output.stdout.splitlines()[-1])
except (json.JSONDecodeError, IndexError):
self._logger.debug(
f"Error parsing server info, received: {output.stdout}"
f"Issue parsing server info: {output.stdout}"
)
return None

Expand Down Expand Up @@ -330,6 +330,10 @@ async def __start_remote_server(self):
"""Start remote server."""
if not self.ssh_is_connected:
self._logger.error("SSH connection is not open")
self.__emit_connection_status(
ConnectionStatus.Error,
_("The SSH connection is not open"),
)
return False

if info := await self.get_server_info():
Expand Down Expand Up @@ -391,7 +395,11 @@ async def __start_remote_server(self):
if info is None:
self._logger.error("Faield to get server info")
self.__emit_connection_status(
ConnectionStatus.Error, _("Error getting server info")
ConnectionStatus.Error,
_(
"There was an error when trying to get the remote server "
"information"
),
)
return False

Expand All @@ -410,7 +418,6 @@ async def __start_remote_server(self):
return True

self._logger.error("Error forwarding local port.")

self.__emit_connection_status(
ConnectionStatus.Error,
_("It was not possible to forward the local port"),
Expand All @@ -421,6 +428,10 @@ async def ensure_server_installed(self) -> bool:
"""Check remote server version."""
if not self.ssh_is_connected:
self._logger.error("SSH connection is not open")
self.__emit_connection_status(
ConnectionStatus.Error,
_("The SSH connection is not open"),
)
return ""

commnad = get_server_version_command(self.options["platform"])
Expand All @@ -431,10 +442,16 @@ async def ensure_server_installed(self) -> bool:
)
except asyncssh.ProcessError as err:
# Server is not installed
self._logger.warning(f"Error checking server version: {err.stderr}")
self._logger.warning(
f"Issue checking server version: {err.stderr}"
)
return await self.install_remote_server()
except asyncssh.TimeoutError:
self._logger.error("Checking server version timed out")
self.__emit_connection_status(
ConnectionStatus.Error,
_("The server version check timed out"),
)
return False

version = output.stdout.splitlines()[-1].strip()
Expand All @@ -452,9 +469,10 @@ async def ensure_server_installed(self) -> bool:
return False

if Version(version) < Version(SPYDER_REMOTE_MIN_VERSION):
self._logger.error(
self._logger.warning(
f"Server version mismatch: {version} is lower than "
f"the minimum supported version {SPYDER_REMOTE_MIN_VERSION}"
f"the minimum supported version {SPYDER_REMOTE_MIN_VERSION}. "
f"A more recent version will be installed."
)
return await self.install_remote_server()

Expand Down Expand Up @@ -483,6 +501,10 @@ async def __install_remote_server(self):
"""Install remote server."""
if not self.ssh_is_connected:
self._logger.error("SSH connection is not open")
self.__emit_connection_status(
ConnectionStatus.Error,
_("The SSH connection is not open"),
)
return False

self._logger.debug(
Expand All @@ -491,21 +513,33 @@ async def __install_remote_server(self):

try:
command = get_installer_command(self.options["platform"])
except NotImplementedError as e:
except NotImplementedError:
self._logger.error(
f"Cannot install spyder-remote-server on "
f"{self.options['platform']} automatically. Please install it "
f"manually."
)
self.__emit_connection_status(
status=ConnectionStatus.Error,
message=_("There was an error installing the remote server"),
)
return False

try:
await self._ssh_connection.run(command, check=True)
except asyncssh.ProcessError as err:
self._logger.error(f"Instalation script failed: {err.stderr}")
self._logger.error(f"Installation script failed: {err.stderr}")
self.__emit_connection_status(
status=ConnectionStatus.Error,
message=_("There was an error installing the remote server"),
)
return False
except asyncssh.TimeoutError:
self._logger.error("Instalation script timed out")
self._logger.error("Installation script timed out")
self.__emit_connection_status(
status=ConnectionStatus.Error,
message=_("There was an error installing the remote server"),
)
return False

self._logger.info(
Expand Down Expand Up @@ -555,19 +589,18 @@ async def __create_new_connection(self) -> bool:
_("We're establishing the connection. Please be patient"),
)

conect_kwargs = {
connect_kwargs = {
k: v
for k, v in self.options.items()
if k not in self._extra_options
}
self._logger.debug("Opening SSH connection")
try:
self._ssh_connection = await asyncssh.connect(
**conect_kwargs, client_factory=self.client_factory
**connect_kwargs, client_factory=self.client_factory
)
except (OSError, asyncssh.Error) as e:
self._logger.error(f"Failed to open ssh connection: {e}")

self.__emit_connection_status(
ConnectionStatus.Error,
_("It was not possible to open a connection to this machine"),
Expand All @@ -582,10 +615,18 @@ async def forward_local_port(self):
"""Forward local port."""
if not self.server_port:
self._logger.error("Server port is not set")
self.__emit_connection_status(
status=ConnectionStatus.Error,
message=_("The server port is not set"),
)
return False

if not self.ssh_is_connected:
self._logger.error("SSH connection is not open")
self.__emit_connection_status(
status=ConnectionStatus.Error,
message=_("The SSH connection is not open"),
)
return False

self._logger.debug(
Expand Down Expand Up @@ -645,6 +686,10 @@ async def stop_remote_server(self):

if not self.ssh_is_connected:
self._logger.error("SSH connection is not open")
self.__emit_connection_status(
ConnectionStatus.Error,
_("The SSH connection is not open"),
)
return False

# bug in jupyterhub, need to send SIGINT twice
Expand Down
5 changes: 4 additions & 1 deletion spyder/plugins/remoteclient/widgets/connectionstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ def add_log(self, log: RemoteClientLog):
self._copy_logs_button.setEnabled(True)

formatted_log = (
LOG_LEVEL_TO_FMT_STRING[log["level"]] + " " + log["message"]
# Message
f"<p>{LOG_LEVEL_TO_FMT_STRING[log['level']]} {log['message']}</p>"
# Small vertical space to separate logs
f"<div style='font-size: 2pt;font-weight: normal;'><p></p></div>"
)

# Move cursor so that new logs are always shown at the end
Expand Down

0 comments on commit c023c4e

Please sign in to comment.