From 9746bd8a74fa2e4e87f78b1ccb9f8343d0278626 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Thu, 19 Nov 2026 22:40:47 +0100 Subject: [PATCH 1/3] Add MariaDB database requests and deployment manifests --- docs/development.md | 18 +++ pyproject.toml | 1 + src/coulobre/database_node.py | 72 ++++++++++ .../migrations/0006_hostingdatabase.py | 59 +++++++++ src/coulobre/hosting/models.py | 31 +++++ src/coulobre/hosting/static/hosting/panel.css | 4 + .../templates/hosting/account_detail.html | 29 +++++ .../hosting/templates/hosting/base.html | 2 +- .../templates/hosting/database_form.html | 16 +++ src/coulobre/hosting/test_databases.py | 123 ++++++++++++++++++ src/coulobre/hosting/urls.py | 10 ++ src/coulobre/hosting/views.py | 61 ++++++++- 12 files changed, 418 insertions(+), 8 deletions(-) create mode 100644 src/coulobre/database_node.py create mode 100644 src/coulobre/hosting/migrations/0006_hostingdatabase.py create mode 100644 src/coulobre/hosting/templates/hosting/database_form.html create mode 100644 src/coulobre/hosting/test_databases.py diff --git a/docs/development.md b/docs/development.md index 907f516..802cc5f 100644 --- a/docs/development.md +++ b/docs/development.md @@ -45,6 +45,24 @@ and node deployment files. The local database defaults to `db.sqlite3`. Set `COULOBRE_SQLITE_PATH` to use a different file. Keep `COULOBRE_DEBUG` for local development. +## Request a MariaDB database + +Open a hosting account and select **Request database**. Choose a short suffix +such as `wordpress`. The panel derives the full database name from the account +ID and suffix, so two accounts can each request a database named `wordpress`. + +The account page shows the requested databases. Operators can download a +versioned deployment manifest containing the account ID and suffix. Resellers +can view the requested database names on their own assigned accounts. The +manifest describes the database to provision on a MariaDB node. + +Use the installed node command to validate the manifest and display the derived +database and login names. + +```sh +coulobre-database-node inspect coulobre-database-1.json +``` + ## Request an Apache configuration preview Start RabbitMQ and set `COULOBRE_BROKER_URL` to its AMQP URL. The development diff --git a/pyproject.toml b/pyproject.toml index 77c8fcc..2be9202 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ dependencies = [ ] [project.scripts] +coulobre-database-node = "coulobre.database_node:main" coulobre-web-node = "coulobre.web_node:main" [project.optional-dependencies] diff --git a/src/coulobre/database_node.py b/src/coulobre/database_node.py new file mode 100644 index 0000000..756a8a1 --- /dev/null +++ b/src/coulobre/database_node.py @@ -0,0 +1,72 @@ +import argparse +import json +import re +from dataclasses import dataclass +from pathlib import Path + +MANIFEST_VERSION = 1 +MANIFEST_KIND = "mariadb_database" +DATABASE_SUFFIX_PATTERN = r"\A[a-z][a-z0-9_]{0,23}\Z" +INVALID_SUFFIX = ( + "Use 1 to 24 lowercase letters, digits, or underscores. Start with a letter." +) + + +@dataclass(frozen=True) +class DatabaseSpec: + account_id: int + suffix: str + + def __post_init__(self): + if type(self.account_id) is not int or not 1 <= self.account_id <= 2**63 - 1: + raise ValueError("The account ID must be a positive 64-bit integer.") + if not isinstance(self.suffix, str) or not re.fullmatch( + DATABASE_SUFFIX_PATTERN, self.suffix + ): + raise ValueError(INVALID_SUFFIX) + + @property + def name(self): + return f"coulobre_{self.account_id}_{self.suffix}" + + def manifest(self): + return { + "version": MANIFEST_VERSION, + "kind": MANIFEST_KIND, + "account_id": self.account_id, + "suffix": self.suffix, + } + + @classmethod + def from_manifest(cls, data): + if not isinstance(data, dict) or data.keys() != { + "version", + "kind", + "account_id", + "suffix", + }: + raise ValueError("Expected a MariaDB database manifest.") + if type(data["version"]) is not int or data["version"] != MANIFEST_VERSION: + raise ValueError("Unsupported database manifest version.") + if data["kind"] != MANIFEST_KIND: + raise ValueError("Expected a MariaDB database manifest.") + return cls(data["account_id"], data["suffix"]) + + +def main(argv=None): + parser = argparse.ArgumentParser(prog="coulobre-database-node") + commands = parser.add_subparsers(dest="command", required=True) + inspect = commands.add_parser( + "inspect", help="Inspect a database deployment manifest." + ) + inspect.add_argument("manifest", type=Path) + args = parser.parse_args(argv) + + try: + payload = json.loads(args.manifest.read_text(encoding="utf-8")) + database = DatabaseSpec.from_manifest(payload) + except (OSError, ValueError) as error: + parser.exit(1, f"coulobre-database-node: {error}\n") + + print(json.dumps({"database": database.name, "user": database.name}, indent=2)) + return 0 diff --git a/src/coulobre/hosting/migrations/0006_hostingdatabase.py b/src/coulobre/hosting/migrations/0006_hostingdatabase.py new file mode 100644 index 0000000..d630161 --- /dev/null +++ b/src/coulobre/hosting/migrations/0006_hostingdatabase.py @@ -0,0 +1,59 @@ +# Generated by Django 5.2.19 on 2026-11-19 21:37 + +import django.core.validators +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0005_reseller_account_limit_reseller_hosting_plans"), + ] + + operations = [ + migrations.CreateModel( + name="HostingDatabase", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "suffix", + models.CharField( + help_text="Use 1 to 24 lowercase letters, digits, or underscores. Start with a letter.", + max_length=24, + validators=[ + django.core.validators.RegexValidator( + "\\A[a-z][a-z0-9_]{0,23}\\Z", + "Use 1 to 24 lowercase letters, digits, or underscores. Start with a letter.", + ) + ], + verbose_name="database suffix", + ), + ), + ( + "account", + models.ForeignKey( + on_delete=django.db.models.deletion.PROTECT, + related_name="databases", + to="hosting.hostingaccount", + ), + ), + ], + options={ + "ordering": ("suffix",), + "constraints": [ + models.UniqueConstraint( + fields=("account", "suffix"), + name="hosting_database_account_suffix_unique", + ) + ], + }, + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 9336f68..7cd8cbb 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -1,7 +1,10 @@ from django.conf import settings +from django.core.validators import RegexValidator from django.db import models from django.urls import reverse +from coulobre.database_node import DATABASE_SUFFIX_PATTERN, INVALID_SUFFIX, DatabaseSpec + from .fields import DomainNameField @@ -51,6 +54,34 @@ class HostingAccount(models.Model): return str(self.primary_domain) +class HostingDatabase(models.Model): + account = models.ForeignKey( + HostingAccount, on_delete=models.PROTECT, related_name="databases" + ) + suffix = models.CharField( + "database suffix", + max_length=24, + validators=[RegexValidator(DATABASE_SUFFIX_PATTERN, INVALID_SUFFIX)], + help_text=INVALID_SUFFIX, + ) + + class Meta: + ordering = ("suffix",) + constraints = [ + models.UniqueConstraint( + fields=("account", "suffix"), + name="hosting_database_account_suffix_unique", + ) + ] + + @property + def spec(self): + return DatabaseSpec(self.account_id, self.suffix) + + def __str__(self): + return self.spec.name + + class WebProvisioningPlan(models.Model): account = models.ForeignKey(HostingAccount, on_delete=models.CASCADE) primary_domain = DomainNameField(max_length=253) diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 986d254..14e5bfa 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -237,6 +237,10 @@ h2 { box-shadow: 0 2px 5px #1a2e2010; } +.main > section.surface + section.surface { + margin-top: 1.5rem; +} + .button { display: inline-flex; min-height: 2.55rem; diff --git a/src/coulobre/hosting/templates/hosting/account_detail.html b/src/coulobre/hosting/templates/hosting/account_detail.html index 248e494..e210b86 100644 --- a/src/coulobre/hosting/templates/hosting/account_detail.html +++ b/src/coulobre/hosting/templates/hosting/account_detail.html @@ -35,4 +35,33 @@ {% include "hosting/plan_table.html" %} {% endif %} + {% if is_reseller or perms.hosting.view_hostingdatabase %} +
+
+

MariaDB database requests

+ {% if not is_reseller and perms.hosting.add_hostingdatabase %} + Request database + {% endif %} +
+ {% if databases %} +
+ + {% if not is_reseller %}{% endif %} + + {% for database in databases %} + + + {% if not is_reseller %} + + {% endif %} + + {% endfor %} + +
DatabaseDeployment
{{ database }}Download manifest
+
+ {% else %} +

No databases requested yet.

+ {% endif %} +
+ {% endif %} {% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/base.html b/src/coulobre/hosting/templates/hosting/base.html index 4b8b73a..efde25d 100644 --- a/src/coulobre/hosting/templates/hosting/base.html +++ b/src/coulobre/hosting/templates/hosting/base.html @@ -18,7 +18,7 @@