Skip to content

Commit

Permalink
feat: add server versioning check
Browse files Browse the repository at this point in the history
  • Loading branch information
hlouzada committed Nov 6, 2024
1 parent acfee6e commit 6a9fe49
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
59 changes: 38 additions & 21 deletions spyder/plugins/remoteclient/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import socket

import asyncssh
from packaging.version import Version

from spyder.api.translations import _
from spyder.config.base import get_debug_level
Expand All @@ -26,7 +27,10 @@
from spyder.plugins.remoteclient.api.ssh import SpyderSSHClient
from spyder.plugins.remoteclient.utils.installation import (
get_installer_command,
get_server_version_command,
SERVER_ENV,
SPYDER_REMOTE_MAX_VERSION,
SPYDER_REMOTE_MIN_VERSION
)


Expand Down Expand Up @@ -99,6 +103,10 @@ def __emit_connection_status(self, status, message):
)
)

def __emit_version_mismatch(self, version):
if self._plugin is not None:
self._plugin.sig_version_mismatch.emit(version)

@property
def _api_token(self):
return self._server_info.get("token")
Expand Down Expand Up @@ -409,36 +417,43 @@ async def __start_remote_server(self):
return False

async def ensure_server_installed(self) -> bool:
"""Ensure remote server is installed."""
if not await self.check_server_installed():
return await self.install_remote_server()

return True

async def check_server_installed(self) -> bool:
"""Check if remote server is installed."""
"""Check remote server version."""
if not self.ssh_is_connected:
self._logger.error("SSH connection is not open")
return False
return ""

commnad = get_server_version_command(self.options["platform"])

try:
await self._ssh_connection.run(
self.CHECK_SERVER_COMMAND, check=True
output = await self._ssh_connection.run(
commnad, check=True
)
except asyncssh.ProcessError as err:
self._logger.warning(
f"spyder-remote-server is not installed: {err.stderr}"
)
return False
# Server is not installed
self._logger.warning(f"Error checking server version: {err.stderr}")
return await self.install_remote_server()
except asyncssh.TimeoutError:
self._logger.error("Checking server version timed out")
return False

version = Version(output.stdout.splitlines()[-1].strip())

if version >= Version(SPYDER_REMOTE_MAX_VERSION):
self._logger.error(
"Checking if spyder-remote-server is installed timed out"
f"Server version mismatch: {version} is greater than "
f"the maximum supported version {SPYDER_REMOTE_MAX_VERSION}"
)
self.__emit_version_mismatch(version)
return False

self._logger.debug(
f"spyder-remote-server is installed on {self.peer_host}"
)
if version < Version(SPYDER_REMOTE_MIN_VERSION):
self._logger.error(
f"Server version mismatch: {version} is lower than "
f"the minimum supported version {SPYDER_REMOTE_MIN_VERSION}"
)
return await self.install_remote_server()

self._logger.info(f"Supported Server version: {version}")

return True

Expand Down Expand Up @@ -468,8 +483,10 @@ async def __install_remote_server(self):
self._logger.debug(
f"Installing spyder-remote-server on {self.peer_host}"
)
command = get_installer_command(self.options["platform"])
if not command:

try:
command = get_installer_command(self.options["platform"])
except NotImplementedError as e:
self._logger.error(
f"Cannot install spyder-remote-server on "
f"{self.options['platform']} automatically. Please install it "
Expand Down
3 changes: 3 additions & 0 deletions spyder/plugins/remoteclient/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

# Third-party imports
from qtpy.QtCore import Signal, Slot
from packaging.version import Version

# Local imports
from spyder.api.asyncdispatcher import AsyncDispatcher
Expand Down Expand Up @@ -66,6 +67,8 @@ class RemoteClient(SpyderPluginV2):
sig_connection_lost = Signal(str)
sig_connection_status_changed = Signal(dict)

sig_version_mismatch = Signal(Version)

_sig_kernel_started = Signal(object, dict)

def __init__(self, *args, **kwargs):
Expand Down
17 changes: 15 additions & 2 deletions spyder/plugins/remoteclient/utils/installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@
SERVER_ENTRY_POINT = "spyder-server"
SERVER_ENV = "spyder-remote"
PACKAGE_NAME = "spyder-remote-services"
PACKAGE_VERSION = "0.1.3"

SPYDER_REMOTE_MIN_VERSION = "0.1.3"
SPYDER_REMOTE_MAX_VERSION = '1.0.0'
SPYDER_REMOTE_VERSION = (
f'>={SPYDER_REMOTE_MIN_VERSION},<{SPYDER_REMOTE_MAX_VERSION}'
)

ENCODING = "utf-8"

SCRIPT_URL = (
f"https://raw.githubusercontent.com/spyder-ide/{PACKAGE_NAME}/master/scripts"
)


def get_installer_command(platform: str) -> str:
if platform == "win":
raise NotImplementedError("Windows is not supported yet")
Expand All @@ -28,5 +34,12 @@ def get_installer_command(platform: str) -> str:

return (
f'"${{SHELL}}" <(curl -L {SCRIPT_URL}/installer.sh) '
f'"{PACKAGE_VERSION}" "{SPYDER_KERNELS_VERSION}"'
f'"{SPYDER_REMOTE_VERSION}" "{SPYDER_KERNELS_VERSION}"'
)


def get_server_version_command(platform: str) -> str:
return (
f"${{HOME}}/.local/bin/micromamba run -n {SERVER_ENV} python -c "
"'import spyder_remote_services as sprs; print(sprs.__version__)'"
)

0 comments on commit 6a9fe49

Please sign in to comment.