diff --git a/docs/deployment.md b/docs/deployment.md index c51642e..4488a8e 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -608,6 +608,15 @@ in-place procedure for custom service unit changes and for environments missing a dependency required by the target version. From 0.11 onward, the updater checks installed dependencies before stopping panel services. +Markdown release notes require `markdown-it-py` in the panel's virtual +environment. To use the web updater for the first release with this feature, +install the dependency before starting the update. The in-place procedure +installs it with the panel package. + +```sh +sudo /opt/coulobre/bin/python -m pip install 'markdown-it-py>=4.3,<5' +``` + The panel and its workers pause during the upgrade. Refresh **Updates** after the panel returns. The latest update status names the recovery directory in `/var/lib/coulobre-update/`. The root-only directory holds the previous diff --git a/pyproject.toml b/pyproject.toml index b12797f..cda5670 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ dependencies = [ "cryptography>=50", "dnspython>=2.9.2,<3", "idna>=3.15,<4", + "markdown-it-py>=4.3,<5", "qrcode>=8.2,<9", ] diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 63b5bdf..fc48ad4 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -452,6 +452,55 @@ h2 { overflow-wrap: anywhere; } +.update-release { + padding: 0.75rem 0.9rem; + border: 1px solid var(--line); + border-radius: 0.5rem; +} + +.update-release > summary { + cursor: pointer; + font-weight: 680; +} + +.release-notes { + max-width: 80ch; + margin-top: 0.9rem; +} + +.release-notes :is(h1, h2, h3, h4, h5, h6) { + margin: 1rem 0 0.25rem; + font-size: 1rem; + line-height: 1.3; +} + +.release-notes :is(p, ul, ol, pre, blockquote) { + margin: 0.5rem 0; +} + +.release-notes :is(ul, ol) { + padding-inline-start: 1.35rem; +} + +.release-notes pre { + padding: 0.7rem 0.85rem; + border-radius: 0.4rem; + background: var(--code-bg); + color: var(--code-text); + font-size: 0.85rem; + white-space: pre-wrap; +} + +.update-release-form { + margin-top: 0.9rem; + padding-top: 0.85rem; + border-top: 1px solid var(--line); +} + +.update-release-form .button { + justify-self: start; +} + .account-toolbar { justify-content: space-between; } diff --git a/src/coulobre/hosting/templates/hosting/updates.html b/src/coulobre/hosting/templates/hosting/updates.html index 3ad9920..0f8ebd9 100644 --- a/src/coulobre/hosting/templates/hosting/updates.html +++ b/src/coulobre/hosting/templates/hosting/updates.html @@ -1,4 +1,5 @@ {% extends "hosting/base.html" %} +{% load release_notes %} {% block title %}Updates · Coulobre Panel{% endblock %} {% block content %}
@@ -44,11 +45,13 @@

Compatible releases

{% for release in releases %} -
+
{{ release.version }}{% if release.prerelease %} · Prerelease{% endif %} - {{ release.notes|linebreaks }} + {% if release.notes %} +
{{ release.notes|release_notes_html }}
+ {% endif %} {% if may_apply and release.version in applyable_versions and update_status.state != "running" and update_status.state != "recovery_required" %} -
+ {% csrf_token %} diff --git a/src/coulobre/hosting/templatetags/__init__.py b/src/coulobre/hosting/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/coulobre/hosting/templatetags/release_notes.py b/src/coulobre/hosting/templatetags/release_notes.py new file mode 100644 index 0000000..023d1db --- /dev/null +++ b/src/coulobre/hosting/templatetags/release_notes.py @@ -0,0 +1,29 @@ +from urllib.parse import urlsplit + +from django import template +from django.utils.safestring import mark_safe +from markdown_it import MarkdownIt + +register = template.Library() + + +class ReleaseNotesMarkdown(MarkdownIt): + def validateLink(self, url): # noqa: N802 - markdown-it-py hook + try: + link = urlsplit(url) + return ( + link.scheme in {"https", "http"} + and bool(link.hostname) + and link.username is None + and link.password is None + ) + except ValueError: + return False + + +markdown = ReleaseNotesMarkdown("js-default").disable("image") + + +@register.filter +def release_notes_html(notes): + return mark_safe(markdown.render(notes)) diff --git a/src/coulobre/hosting/test_update_views.py b/src/coulobre/hosting/test_update_views.py new file mode 100644 index 0000000..ec7a0bd --- /dev/null +++ b/src/coulobre/hosting/test_update_views.py @@ -0,0 +1,56 @@ +from unittest.mock import patch + +from django.contrib.auth import get_user_model +from django.core.cache import cache +from django.test import TestCase +from django.urls import reverse + +from coulobre.release_updates import Release + + +class UpdateViewTests(TestCase): + @classmethod + def setUpTestData(cls): + cls.admin = get_user_model().objects.create_superuser("operator") + + def test_catalog_renders_markdown_and_escapes_untrusted_content(self): + checksum = "a" * 64 + notes = f"""## Changes + +- **Upgrade** from the previous release. +- Follow the [deployment guide](https://example.test/docs#upgrade). + +```text +{checksum} coulobre_panel-0.11.1rc1.tar.gz +``` + + + +[Unsafe](javascript:invalid) [Local action](/updates/) + +![Remote image](https://example.test/image.png) +""" + release = Release("0.11.1rc1", notes, True, checksum) + self.client.force_login(self.admin) + cache.clear() + self.addCleanup(cache.clear) + path = "coulobre.hosting.update_views" + with ( + patch(f"{path}.SOCKET", **{"is_socket.return_value": True}), + patch(f"{path}.inspect_linked_nodes", return_value={"nodes": []}), + patch(f"{path}.available_releases", return_value=[release]), + ): + response = self.client.get(reverse("panel:updates")) + + self.assertContains(response, "

Changes

", html=True) + self.assertContains(response, "Upgrade", html=True) + self.assertContains(response, '
')
+        self.assertContains(response, f"{checksum}  coulobre_panel-")
+        self.assertContains(
+            response, 'href="https://example.test/docs#upgrade"'
+        )
+        self.assertContains(response, "<script>")
+        self.assertNotContains(response, "