From 374408f8c7a6b05709437bfaa659a4b1439dbc5e Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Thu, 19 Nov 2026 21:35:59 +0100 Subject: [PATCH] Add an isolated PHP smoke test for hosting plans --- docs/development.md | 23 ++++++ pyproject.toml | 1 + src/coulobre/hosting/models.py | 8 ++ src/coulobre/hosting/tasks.py | 4 +- .../templates/hosting/apache-vhost.conf | 6 +- .../templates/hosting/php-fpm-pool.conf | 11 +++ .../templates/hosting/plan_detail.html | 7 +- .../hosting/templates/hosting/smoke-index.php | 5 ++ src/coulobre/hosting/test_web_node.py | 73 +++++++++++++++++++ src/coulobre/hosting/urls.py | 5 ++ src/coulobre/hosting/views.py | 22 ++++++ src/coulobre/hosting/web_node.py | 35 +++++++++ tests/web-node/Dockerfile | 11 +++ tests/web-node/smoke.sh | 48 ++++++++++++ 14 files changed, 253 insertions(+), 6 deletions(-) create mode 100644 src/coulobre/hosting/templates/hosting/php-fpm-pool.conf create mode 100644 src/coulobre/hosting/templates/hosting/smoke-index.php create mode 100644 src/coulobre/hosting/test_web_node.py create mode 100644 src/coulobre/hosting/web_node.py create mode 100644 tests/web-node/Dockerfile create mode 100644 tests/web-node/smoke.sh diff --git a/docs/development.md b/docs/development.md index 485f810..c154fc9 100644 --- a/docs/development.md +++ b/docs/development.md @@ -48,6 +48,29 @@ to RabbitMQ. After a broker outage, queued requests resume when the worker and scheduler reconnect. Plans that remain incomplete five minutes after dispatch are eligible for delivery again. +## Run an isolated PHP test site + +Open a completed preview for the account's current domain and select **Download +test bundle**. The archive contains the exact Apache preview, a matching PHP-FPM +pool, site metadata, and a small PHP test page. It is intended for the isolated +Ubuntu check below. + +Run the test with Docker from the repository root. Set the bundle path to the +downloaded tar archive. + +```sh +docker build -t coulobre-web-smoke tests/web-node +bundle="$PWD/coulobre-web-plan-1.tar" +docker run --rm --network none \ + --mount "type=bind,src=$bundle,dst=/test/site.tar,readonly" \ + coulobre-web-smoke +``` + +The Ubuntu container installs a system user for the site, checks the Apache and +PHP-FPM configurations, and makes an HTTP request with the account's primary +domain. A passing check confirms PHP execution through the generated socket. +The container uses its own filesystem and loopback interface. + ## MariaDB Create a dedicated MariaDB database and application user. Install the MariaDB or diff --git a/pyproject.toml b/pyproject.toml index 87dbfc6..a7366e3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,7 @@ mariadb = ["mysqlclient>=2.2.1,<3"] "templates/*.html", "templates/hosting/*.conf", "templates/hosting/*.html", + "templates/hosting/*.php", ] [tool.ruff] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 31ad7d5..23eb19a 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -28,5 +28,13 @@ class WebProvisioningPlan(models.Model): def for_account(cls, account): return cls(account=account, primary_domain=account.primary_domain) + @property + def can_export(self): + return ( + self.completed_at is not None + and bool(self.apache_config) + and self.primary_domain == self.account.primary_domain + ) + def __str__(self): return str(self.primary_domain) diff --git a/src/coulobre/hosting/tasks.py b/src/coulobre/hosting/tasks.py index 9d902c0..a403f47 100644 --- a/src/coulobre/hosting/tasks.py +++ b/src/coulobre/hosting/tasks.py @@ -6,6 +6,7 @@ from django.template.loader import render_to_string from django.utils import timezone from .models import WebProvisioningPlan +from .web_node import web_site_configuration REDISPATCH_AFTER = timedelta(minutes=5) @@ -37,7 +38,6 @@ def render_web_plan(plan_id): return apache_config = render_to_string( - "hosting/apache-vhost.conf", - {"account_id": str(plan.account_id), "domain": plan.primary_domain}, + "hosting/apache-vhost.conf", web_site_configuration(plan) ) pending.update(apache_config=apache_config, completed_at=timezone.now()) diff --git a/src/coulobre/hosting/templates/hosting/apache-vhost.conf b/src/coulobre/hosting/templates/hosting/apache-vhost.conf index 720a0c1..adeef4c 100644 --- a/src/coulobre/hosting/templates/hosting/apache-vhost.conf +++ b/src/coulobre/hosting/templates/hosting/apache-vhost.conf @@ -1,14 +1,14 @@ ServerName {{ domain }} - DocumentRoot "/srv/coulobre/web/{{ account_id }}/public" + DocumentRoot "{{ document_root }}" - + AllowOverride None Options None Require all granted - SetHandler "proxy:unix:/run/php/coulobre-{{ account_id }}.sock|fcgi://localhost/" + SetHandler "proxy:unix:{{ socket_path }}|fcgi://localhost/" diff --git a/src/coulobre/hosting/templates/hosting/php-fpm-pool.conf b/src/coulobre/hosting/templates/hosting/php-fpm-pool.conf new file mode 100644 index 0000000..29127f9 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/php-fpm-pool.conf @@ -0,0 +1,11 @@ +[{{ service_user }}] +user = {{ service_user }} +group = {{ service_user }} +listen = {{ socket_path }} +listen.owner = www-data +listen.group = www-data +listen.mode = 0660 +pm = ondemand +pm.max_children = 2 +clear_env = yes +security.limit_extensions = .php diff --git a/src/coulobre/hosting/templates/hosting/plan_detail.html b/src/coulobre/hosting/templates/hosting/plan_detail.html index 6d2d155..b801c09 100644 --- a/src/coulobre/hosting/templates/hosting/plan_detail.html +++ b/src/coulobre/hosting/templates/hosting/plan_detail.html @@ -21,7 +21,12 @@ {% endif %} {% if plan.completed_at %}
-

Virtual host configuration

+
+

Virtual host configuration

+ {% if plan.can_export %} + Download test bundle + {% endif %} +
{{ plan.apache_config }}
{% else %} diff --git a/src/coulobre/hosting/templates/hosting/smoke-index.php b/src/coulobre/hosting/templates/hosting/smoke-index.php new file mode 100644 index 0000000..f874ac0 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/smoke-index.php @@ -0,0 +1,5 @@ +/", views.PlanDetail.as_view(), name="plan"), + path( + "plans//smoke-bundle/", + views.DownloadWebSmokeBundle.as_view(), + name="download_smoke_bundle", + ), ] diff --git a/src/coulobre/hosting/views.py b/src/coulobre/hosting/views.py index f2f1e77..2c018ff 100644 --- a/src/coulobre/hosting/views.py +++ b/src/coulobre/hosting/views.py @@ -1,11 +1,13 @@ from django.contrib.auth.mixins import PermissionRequiredMixin from django.db.models import Q +from django.http import HttpResponse from django.shortcuts import redirect from django.views import View from django.views.generic import CreateView, DetailView, ListView, UpdateView from django.views.generic.detail import SingleObjectMixin from .models import HostingAccount, WebProvisioningPlan +from .web_node import build_web_smoke_bundle VIEW_ACCOUNTS = "hosting.view_hostingaccount" CHANGE_ACCOUNTS = "hosting.change_hostingaccount" @@ -93,3 +95,23 @@ class PlanDetail(PermissionRequiredMixin, DetailView): permission_required = VIEW_PLANS template_name = "hosting/plan_detail.html" context_object_name = "plan" + + +class DownloadWebSmokeBundle(PlanDetail): + def render_to_response(self, context, **response_kwargs): + plan = self.object + if not plan.can_export: + return HttpResponse( + "Request a completed Apache preview for the current account domain.", + status=409, + content_type="text/plain", + ) + + return HttpResponse( + build_web_smoke_bundle(plan), + content_type="application/x-tar", + headers={ + "Cache-Control": "private, no-store", + "Content-Disposition": f'attachment; filename="coulobre-web-plan-{plan.pk}.tar"', + }, + ) diff --git a/src/coulobre/hosting/web_node.py b/src/coulobre/hosting/web_node.py new file mode 100644 index 0000000..d31c4a3 --- /dev/null +++ b/src/coulobre/hosting/web_node.py @@ -0,0 +1,35 @@ +import json +import tarfile +from io import BytesIO + +from django.template.loader import render_to_string + + +def web_site_configuration(plan): + service_user = f"coulobre-{plan.account_id}" + return { + "account_id": plan.account_id, + "domain": plan.primary_domain, + "service_user": service_user, + "document_root": f"/srv/coulobre/web/{plan.account_id}/public", + "socket_path": f"/run/php/{service_user}.sock", + } + + +def build_web_smoke_bundle(plan): + configuration = web_site_configuration(plan) + files = { + "site.json": json.dumps(configuration, indent=2) + "\n", + "apache.conf": plan.apache_config, + "php-fpm.conf": render_to_string("hosting/php-fpm-pool.conf", configuration), + "public/index.php": render_to_string("hosting/smoke-index.php"), + } + + archive = BytesIO() + with tarfile.open(fileobj=archive, mode="w") as bundle: + for name, text in files.items(): + data = text.encode("utf-8") + member = tarfile.TarInfo(name) + member.size = len(data) + bundle.addfile(member, BytesIO(data)) + return archive.getvalue() diff --git a/tests/web-node/Dockerfile b/tests/web-node/Dockerfile new file mode 100644 index 0000000..486936f --- /dev/null +++ b/tests/web-node/Dockerfile @@ -0,0 +1,11 @@ +FROM ubuntu:26.04 + +RUN apt-get update \ + && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + apache2 curl jq php8.5-fpm \ + && rm -rf /var/lib/apt/lists/* + +COPY smoke.sh /usr/local/bin/coulobre-web-smoke + +ENTRYPOINT ["/bin/sh", "/usr/local/bin/coulobre-web-smoke"] +CMD ["/test/site.tar"] diff --git a/tests/web-node/smoke.sh b/tests/web-node/smoke.sh new file mode 100644 index 0000000..04ec3a2 --- /dev/null +++ b/tests/web-node/smoke.sh @@ -0,0 +1,48 @@ +#!/bin/sh +set -eu + +scratch=$(mktemp -d) +tar --extract --file "$1" --directory "$scratch" --no-same-owner --no-same-permissions +account_id=$(jq -er '.account_id' "$scratch/site.json") +domain=$(jq -er '.domain' "$scratch/site.json") + +case "$account_id" in + ''|*[!0-9]*) echo 'Invalid hosting account ID.' >&2; exit 1 ;; +esac +case "$domain" in + ''|*[!a-z0-9.-]*) echo 'Invalid primary domain.' >&2; exit 1 ;; +esac + +account_dir="/srv/coulobre/web/$account_id" +site_user="coulobre-$account_id" +site_conf="$site_user.conf" + +useradd --system --user-group --no-create-home --home-dir "$account_dir" \ + --shell /usr/sbin/nologin "$site_user" +install -d -m 0755 "$account_dir" /run/php +install -d -m 0750 -o "$site_user" -g www-data "$account_dir/public" +install -m 0640 -o "$site_user" -g www-data \ + "$scratch/public/index.php" "$account_dir/public/index.php" + +install -m 0644 "$scratch/apache.conf" "/etc/apache2/sites-available/$site_conf" +a2enmod proxy_fcgi >/dev/null +a2dissite 000-default >/dev/null +a2ensite "$site_conf" >/dev/null +printf 'ServerName localhost\n' > /etc/apache2/conf-enabled/coulobre-smoke.conf + +printf '[global]\nerror_log = /proc/self/fd/2\ninclude = %s\n' \ + "$scratch/php-fpm.conf" > "$scratch/fpm.conf" +php-fpm8.5 --test --fpm-config "$scratch/fpm.conf" +apache2ctl configtest + +php-fpm8.5 --daemonize --fpm-config "$scratch/fpm.conf" +apache2ctl start + +body=$(curl --fail --silent --show-error --noproxy '*' --max-time 5 \ + --retry 5 --retry-connrefused --header "Host: $domain" http://127.0.0.1/index.php) +if [ "$body" != 'Coulobre PHP-FPM smoke check passed.' ]; then + echo 'The test site did not return the expected PHP response.' >&2 + exit 1 +fi + +printf '%s via %s\n' "$body" "$domain"