Update APT-managed Coulobre from the panel #197

Merged
parkel merged 2 commits from feature/web-apt-updates into main 2026-11-24 15:11:36 +01:00
6 changed files with 57 additions and 23 deletions
Showing only changes of commit 48de3ddbe1 - Show all commits

No files matched your search

+18 -4
View File
@@ -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,
)
@@ -38,12 +38,13 @@
<p>No package updates are available.</p>
{% elif form.preview %}
<p>{{ form.preview.result.count }} package update{{ form.preview.result.count|pluralize }} ready to review.</p>
{% elif apt_status.state == 'succeeded' %}
<p>Coulobre {{ apt_status.target_version }} was installed through APT.</p>
{% elif latest %}
<p>The package review expired. Check again for the current list.</p>
{% else %}
<p>{% if apt_release %}Check the configured APT repository for a newer Coulobre package.{% else %}Check for available operating system and service package updates.{% endif %}</p>
{% endif %}
{% if apt_status.state == 'succeeded' %}<p>Coulobre {{ apt_status.target_version }} was installed through APT.</p>{% endif %}
</div>
{% if form.errors %}<div class="section-text" role="alert">{{ form.errors }}</div>{% endif %}
{% if available and enabled and apt_status.state != 'running' and apt_status.state != 'recovery_required' and panel_status.state != 'recovery_required' %}
@@ -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"
+12 -7
View File
@@ -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)
+5 -2
View File
@@ -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)
+17 -8
View File
@@ -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(
(