diff --git a/src/coulobre/hosting/operator_tools.py b/src/coulobre/hosting/operator_tools.py index 3cb5198..d3ff691 100644 --- a/src/coulobre/hosting/operator_tools.py +++ b/src/coulobre/hosting/operator_tools.py @@ -113,6 +113,12 @@ class ServerUpdatesView(OperatorAuthorization, FormView): def latest(self): return self.package_jobs().filter(actor=self.request.user).first() + @cached_property + def apt_status(self): + if self.target and DEB_WHEEL.is_dir(): + return inspect_linked_nodes() + return {} + def preview(self, job): if ( job is not None @@ -126,8 +132,17 @@ class ServerUpdatesView(OperatorAuthorization, FormView): def get_form_kwargs(self): kwargs = super().get_form_kwargs() + preview = self.preview(self.latest) + status = self.apt_status.get("apt_status", {}) + if ( + preview is not None + and status.get("state") == "succeeded" + and preview.result.get("target_version") + == status.get("target_version") + ): + preview = None return kwargs | { - "preview": self.preview(self.latest), + "preview": preview, "target": self.target, } @@ -135,7 +150,6 @@ class ServerUpdatesView(OperatorAuthorization, FormView): latest = self.latest apt_release = self.target == "coulobre" apt_managed = DEB_WHEEL.is_dir() - apt = inspect_linked_nodes() if apt_release and apt_managed else {} return super().get_context_data( available=Path(SOCKET).is_socket(), enabled=settings.SERVER_PACKAGE_UPDATES, @@ -144,8 +158,8 @@ class ServerUpdatesView(OperatorAuthorization, FormView): jobs=self.package_jobs().select_related("actor")[:20], apt_release=apt_release, apt_managed=apt_managed, - apt_status=apt.get("apt_status", {}), - panel_status=apt.get("status", {}), + apt_status=self.apt_status.get("apt_status", {}), + panel_status=self.apt_status.get("status", {}), **kwargs, ) diff --git a/src/coulobre/hosting/templates/hosting/server_updates.html b/src/coulobre/hosting/templates/hosting/server_updates.html index 7e36963..c599d1a 100644 --- a/src/coulobre/hosting/templates/hosting/server_updates.html +++ b/src/coulobre/hosting/templates/hosting/server_updates.html @@ -38,12 +38,13 @@

No package updates are available.

{% elif form.preview %}

{{ form.preview.result.count }} package update{{ form.preview.result.count|pluralize }} ready to review.

+ {% elif apt_status.state == 'succeeded' %} +

Coulobre {{ apt_status.target_version }} was installed through APT.

{% elif latest %}

The package review expired. Check again for the current list.

{% else %}

{% if apt_release %}Check the configured APT repository for a newer Coulobre package.{% else %}Check for available operating system and service package updates.{% endif %}

{% endif %} - {% if apt_status.state == 'succeeded' %}

Coulobre {{ apt_status.target_version }} was installed through APT.

{% endif %} {% if form.errors %}{% endif %} {% if available and enabled and apt_status.state != 'running' and apt_status.state != 'recovery_required' and panel_status.state != 'recovery_required' %} diff --git a/src/coulobre/hosting/test_operator_workspaces.py b/src/coulobre/hosting/test_operator_workspaces.py index edbe1b1..4887752 100644 --- a/src/coulobre/hosting/test_operator_workspaces.py +++ b/src/coulobre/hosting/test_operator_workspaces.py @@ -300,7 +300,9 @@ class ServerPackageTests(TestCase): ) as updater, ): package.is_dir.return_value = True - self.assertContains(self.client.post(url, data), "Review") + self.assertContains( + self.client.post(url, data), "review a current" + ) updater.assert_not_called() preview.target = "coulobre" preview.result["target_version"] = "0.13.0~rc6" diff --git a/src/coulobre/local_update.py b/src/coulobre/local_update.py index 0397be5..427d9b2 100644 --- a/src/coulobre/local_update.py +++ b/src/coulobre/local_update.py @@ -512,7 +512,7 @@ def handle_apt_update(peer, request): raise UpdateError("An APT update is already running.") from error if read_status(APT_STATUS).get("state") == "running": raise UpdateError("Check the interrupted APT update on the host.") - with locked_root_directory(OPERATOR_STATE): + with locked_root_directory(OPERATOR_STATE, blocking=False): if display_status().get("state") in { "running", "recovery_required", @@ -521,7 +521,11 @@ def handle_apt_update(peer, request): "Resolve the previous update before retrying." ) try: - plan = apt_plan(coulobre=True) + # APT extracts the signed index during simulation. + with TemporaryDirectory(dir=STATE) as preview_directory: + plan = apt_plan( + coulobre=True, temp_directory=preview_directory + ) except ValueError as error: raise UpdateError(str(error)) from error target = plan["target_version"] @@ -671,10 +675,11 @@ def main(): try: handle_request(peer) except (OSError, ValueError, UpdateError) as error: - message = ( - str(error) - if isinstance(error, UpdateError) - else "Could not accept the update request." - ) + if isinstance(error, UpdateError): + message = str(error) + elif isinstance(error, BlockingIOError): + message = "Wait for the current operator action to finish." + else: + message = "Could not accept the update request." with suppress(OSError): reply(peer, accepted=False, message=message) diff --git a/src/coulobre/node_files.py b/src/coulobre/node_files.py index 4505a45..84d1800 100644 --- a/src/coulobre/node_files.py +++ b/src/coulobre/node_files.py @@ -74,13 +74,16 @@ def replace_configuration(path, content, *, mode=0o644): @contextmanager -def locked_root_directory(path, *, shared=False): +def locked_root_directory(path, *, shared=False, blocking=True): import fcntl require_root_directory(path) fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) try: - fcntl.flock(fd, fcntl.LOCK_SH if shared else fcntl.LOCK_EX) + mode = fcntl.LOCK_SH if shared else fcntl.LOCK_EX + if not blocking: + mode |= fcntl.LOCK_NB + fcntl.flock(fd, mode) yield fd finally: os.close(fd) diff --git a/src/coulobre/operator_node.py b/src/coulobre/operator_node.py index da74693..b83489f 100644 --- a/src/coulobre/operator_node.py +++ b/src/coulobre/operator_node.py @@ -86,7 +86,14 @@ class OperatorRequest: } -def command(*args, timeout=90): +def command(*args, timeout=90, temp_directory=None): + env = { + "PATH": "/usr/sbin:/usr/bin", + "LC_ALL": "C", + "DEBIAN_FRONTEND": "noninteractive", + } + if temp_directory is not None: + env["TMPDIR"] = temp_directory try: result = subprocess.run( args, @@ -94,11 +101,7 @@ def command(*args, timeout=90): capture_output=True, timeout=timeout, check=False, - env={ - "PATH": "/usr/sbin:/usr/bin", - "LC_ALL": "C", - "DEBIAN_FRONTEND": "noninteractive", - }, + env=env, ) except subprocess.TimeoutExpired: raise ValueError( @@ -111,13 +114,19 @@ def command(*args, timeout=90): return result.stdout.decode("utf-8", errors="replace") -def apt_plan(*, coulobre=False): +def apt_plan(*, coulobre=False, temp_directory=None): selection = ( ("--only-upgrade", "install", PANEL_PACKAGE) if coulobre else ("upgrade",) ) - output = command(APT, "--simulate", "--no-remove", *selection) + output = command( + APT, + "--simulate", + "--no-remove", + *selection, + temp_directory=temp_directory, + ) lines = [line for line in output.splitlines() if line.startswith("Inst ")] panel = next( (