Import DirectAdmin account metadata and hosting requests
This commit is contained in:
1 parent
0468db1cd8
commit
1a29340ebf
6 files changed
+351
-3
No files matched your search
@@ -383,6 +383,27 @@ for the selected domain. The `manual_review` list identifies other domains,
|
|||||||
database names that need a mapping, and data that needs a separate migration,
|
database names that need a mapping, and data that needs a separate migration,
|
||||||
including website files, DNS zones, mail files and SQL dumps.
|
including website files, DNS zones, mail files and SQL dumps.
|
||||||
|
|
||||||
|
### Import DirectAdmin account metadata
|
||||||
|
|
||||||
|
Apply the panel database migrations, then run the import on the panel host
|
||||||
|
with the backup and the same primary domain selection used for the preview.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
python manage.py import_directadmin user.admin.customer.tar.zst
|
||||||
|
```
|
||||||
|
|
||||||
|
The import checks domain ownership again and creates the new hosting account,
|
||||||
|
supported database requests and selected domain's mailbox requests in one
|
||||||
|
transaction. The JSON report includes the new account ID and the MariaDB
|
||||||
|
names derived from the database requests. Use the account page to download
|
||||||
|
deployment manifests for the nodes. The `manual_review` list records the
|
||||||
|
website, DNS, mail and SQL data that needs a separate migration.
|
||||||
|
|
||||||
|
Replaying the same account and request selection reports `already_imported`
|
||||||
|
for the original account. A domain conflict or a change to the original
|
||||||
|
account's domain or requests can block the import. A blocked import prints
|
||||||
|
its report and exits with code `2`.
|
||||||
|
|
||||||
## Request an Apache configuration preview
|
## Request an Apache configuration preview
|
||||||
|
|
||||||
Start RabbitMQ and set `COULOBRE_BROKER_URL` to its AMQP URL. The development
|
Start RabbitMQ and set `COULOBRE_BROKER_URL` to its AMQP URL. The development
|
||||||
|
|||||||
@@ -1,10 +1,21 @@
|
|||||||
|
import hashlib
|
||||||
|
import json
|
||||||
import re
|
import re
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from coulobre.database_node import DATABASE_SUFFIX_PATTERN
|
from django.db import IntegrityError, transaction
|
||||||
|
|
||||||
|
from coulobre.database_node import DATABASE_SUFFIX_PATTERN, DatabaseSpec
|
||||||
from coulobre.domain_names import canonical_domain
|
from coulobre.domain_names import canonical_domain
|
||||||
|
|
||||||
from .models import HostingAccount, HostingDnsZone, HostingMailDomain
|
from .models import (
|
||||||
|
DirectAdminImport,
|
||||||
|
HostingAccount,
|
||||||
|
HostingDatabase,
|
||||||
|
HostingDnsZone,
|
||||||
|
HostingMailbox,
|
||||||
|
HostingMailDomain,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def plan_directadmin_import(inventory, primary_domain=None):
|
def plan_directadmin_import(inventory, primary_domain=None):
|
||||||
@@ -126,3 +137,125 @@ def domain_conflicts(domain):
|
|||||||
if owner is not None:
|
if owner is not None:
|
||||||
conflicts.append({"kind": kind, "domain": domain, "account_id": owner})
|
conflicts.append({"kind": kind, "domain": domain, "account_id": owner})
|
||||||
return conflicts
|
return conflicts
|
||||||
|
|
||||||
|
|
||||||
|
def import_directadmin_account(inventory, primary_domain=None):
|
||||||
|
plan = import_report(plan_directadmin_import(inventory, primary_domain))
|
||||||
|
if plan["account"] is None:
|
||||||
|
return plan
|
||||||
|
|
||||||
|
fingerprint = import_fingerprint(plan)
|
||||||
|
previous = previous_import(plan, fingerprint)
|
||||||
|
if previous is not None:
|
||||||
|
return previous
|
||||||
|
if plan["state"] == "blocked":
|
||||||
|
return plan
|
||||||
|
|
||||||
|
try:
|
||||||
|
with transaction.atomic():
|
||||||
|
account = HostingAccount.objects.create(**plan["account"])
|
||||||
|
conflicts = [
|
||||||
|
conflict
|
||||||
|
for conflict in domain_conflicts(account.primary_domain)
|
||||||
|
if conflict["account_id"] != account.pk
|
||||||
|
]
|
||||||
|
if conflicts:
|
||||||
|
transaction.set_rollback(True)
|
||||||
|
return plan | {"state": "blocked", "conflicts": conflicts}
|
||||||
|
|
||||||
|
HostingDatabase.objects.bulk_create(
|
||||||
|
HostingDatabase(account=account, suffix=request["suffix"])
|
||||||
|
for request in plan["database_requests"]
|
||||||
|
)
|
||||||
|
if plan["mailbox_requests"]:
|
||||||
|
mail_domain = HostingMailDomain.objects.create(
|
||||||
|
account=account, domain=account.primary_domain
|
||||||
|
)
|
||||||
|
HostingMailbox.objects.bulk_create(
|
||||||
|
HostingMailbox(
|
||||||
|
mail_domain=mail_domain, local_part=address.split("@", 1)[0]
|
||||||
|
)
|
||||||
|
for address in plan["mailbox_requests"]
|
||||||
|
)
|
||||||
|
DirectAdminImport.objects.create(account=account, fingerprint=fingerprint)
|
||||||
|
except IntegrityError:
|
||||||
|
# A different process may have claimed the domain while the plan was read.
|
||||||
|
refreshed = import_report(plan_directadmin_import(inventory, primary_domain))
|
||||||
|
previous = previous_import(refreshed, fingerprint)
|
||||||
|
if previous is not None:
|
||||||
|
return previous
|
||||||
|
if refreshed["state"] == "blocked":
|
||||||
|
return refreshed
|
||||||
|
raise
|
||||||
|
|
||||||
|
return completed_import(plan, account, "imported")
|
||||||
|
|
||||||
|
|
||||||
|
def import_report(plan):
|
||||||
|
return plan | {"kind": "directadmin_import_result"}
|
||||||
|
|
||||||
|
|
||||||
|
def import_fingerprint(plan):
|
||||||
|
identity = {
|
||||||
|
key: plan[key] for key in ("account", "database_requests", "mailbox_requests")
|
||||||
|
}
|
||||||
|
payload = json.dumps(identity, sort_keys=True).encode("utf-8")
|
||||||
|
return hashlib.sha256(payload).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
def previous_import(plan, fingerprint):
|
||||||
|
saved = (
|
||||||
|
DirectAdminImport.objects.select_related("account")
|
||||||
|
.filter(fingerprint=fingerprint)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
if saved is None:
|
||||||
|
return None
|
||||||
|
account = saved.account
|
||||||
|
domain = plan["account"]["primary_domain"]
|
||||||
|
conflicts = [
|
||||||
|
conflict
|
||||||
|
for conflict in plan["conflicts"]
|
||||||
|
if conflict["account_id"] != account.pk
|
||||||
|
]
|
||||||
|
if conflicts:
|
||||||
|
return plan | {"state": "blocked", "conflicts": conflicts}
|
||||||
|
if account.primary_domain != domain:
|
||||||
|
return plan | {
|
||||||
|
"state": "blocked",
|
||||||
|
"problems": ["The imported account now uses a different primary domain."],
|
||||||
|
}
|
||||||
|
|
||||||
|
suffixes = {request["suffix"] for request in plan["database_requests"]}
|
||||||
|
mailboxes = {address.split("@", 1)[0] for address in plan["mailbox_requests"]}
|
||||||
|
if not suffixes.issubset(account.databases.values_list("suffix", flat=True)) or (
|
||||||
|
mailboxes
|
||||||
|
and not mailboxes.issubset(
|
||||||
|
HostingMailbox.objects.filter(
|
||||||
|
mail_domain__account=account, mail_domain__domain=domain
|
||||||
|
).values_list("local_part", flat=True)
|
||||||
|
)
|
||||||
|
):
|
||||||
|
return plan | {
|
||||||
|
"state": "blocked",
|
||||||
|
"problems": [
|
||||||
|
"The imported account is missing one or more original requests."
|
||||||
|
],
|
||||||
|
}
|
||||||
|
return completed_import(plan, account, "already_imported")
|
||||||
|
|
||||||
|
|
||||||
|
def completed_import(plan, account, state):
|
||||||
|
return plan | {
|
||||||
|
"state": state,
|
||||||
|
"account": {
|
||||||
|
"id": account.pk,
|
||||||
|
"name": account.name,
|
||||||
|
"primary_domain": account.primary_domain,
|
||||||
|
},
|
||||||
|
"database_requests": [
|
||||||
|
request | {"database": DatabaseSpec(account.pk, request["suffix"]).name}
|
||||||
|
for request in plan["database_requests"]
|
||||||
|
],
|
||||||
|
"conflicts": [],
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
|
from django.db import IntegrityError
|
||||||
|
|
||||||
|
from coulobre.directadmin import inspect_backup
|
||||||
|
from coulobre.hosting.directadmin_import import import_directadmin_account
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = (
|
||||||
|
"Create a hosting account and its supported requests from a DirectAdmin backup."
|
||||||
|
)
|
||||||
|
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument("backup", type=Path)
|
||||||
|
parser.add_argument(
|
||||||
|
"--primary-domain", help="Select the domain for the new account."
|
||||||
|
)
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
try:
|
||||||
|
inventory = inspect_backup(options["backup"])
|
||||||
|
result = import_directadmin_account(inventory, options["primary_domain"])
|
||||||
|
except (OSError, ValueError) as error:
|
||||||
|
raise CommandError(str(error)) from None
|
||||||
|
except IntegrityError:
|
||||||
|
raise CommandError("The panel database rejected the import.") from None
|
||||||
|
self.stdout.write(json.dumps(result, indent=2))
|
||||||
|
if result["state"] == "blocked":
|
||||||
|
raise CommandError(
|
||||||
|
"The import is blocked. Review the report.", returncode=2
|
||||||
|
)
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
# Generated by Django 5.2.19 on 2026-11-20 22:30
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [
|
||||||
|
("hosting", "0009_hostingdnszone_mail_records"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="DirectAdminImport",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.BigAutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("fingerprint", models.CharField(max_length=64, unique=True)),
|
||||||
|
(
|
||||||
|
"account",
|
||||||
|
models.OneToOneField(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="directadmin_import",
|
||||||
|
to="hosting.hostingaccount",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -90,6 +90,13 @@ class HostingDatabase(models.Model):
|
|||||||
return self.spec.name
|
return self.spec.name
|
||||||
|
|
||||||
|
|
||||||
|
class DirectAdminImport(models.Model):
|
||||||
|
account = models.OneToOneField(
|
||||||
|
HostingAccount, on_delete=models.CASCADE, related_name="directadmin_import"
|
||||||
|
)
|
||||||
|
fingerprint = models.CharField(max_length=64, unique=True)
|
||||||
|
|
||||||
|
|
||||||
class HostingDnsZone(models.Model):
|
class HostingDnsZone(models.Model):
|
||||||
account = models.ForeignKey(
|
account = models.ForeignKey(
|
||||||
HostingAccount, on_delete=models.PROTECT, related_name="dns_zones"
|
HostingAccount, on_delete=models.PROTECT, related_name="dns_zones"
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ import json
|
|||||||
from io import StringIO
|
from io import StringIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
from django.core.management import CommandError, call_command
|
from django.core.management import CommandError, call_command
|
||||||
|
from django.db import IntegrityError
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from .models import (
|
from .models import (
|
||||||
|
DirectAdminImport,
|
||||||
HostingAccount,
|
HostingAccount,
|
||||||
HostingDatabase,
|
HostingDatabase,
|
||||||
HostingDnsZone,
|
HostingDnsZone,
|
||||||
@@ -33,13 +36,16 @@ class DirectAdminImportPlanTests(TestCase):
|
|||||||
("imap/example.net/support/Maildir/cur/message", "mail data"),
|
("imap/example.net/support/Maildir/cur/message", "mail data"),
|
||||||
]
|
]
|
||||||
|
|
||||||
def plan(self, entries=None, *, primary_domain=None):
|
def write_backup(self, entries):
|
||||||
if entries is None:
|
if entries is None:
|
||||||
entries = [
|
entries = [
|
||||||
("backup/user.conf", "username=client\ndomain=Example.Net.\n"),
|
("backup/user.conf", "username=client\ndomain=Example.Net.\n"),
|
||||||
*self.entries,
|
*self.entries,
|
||||||
]
|
]
|
||||||
self.backup.write_bytes(make_tar(entries))
|
self.backup.write_bytes(make_tar(entries))
|
||||||
|
|
||||||
|
def plan(self, entries=None, *, primary_domain=None):
|
||||||
|
self.write_backup(entries)
|
||||||
output = StringIO()
|
output = StringIO()
|
||||||
call_command(
|
call_command(
|
||||||
"plan_directadmin_import",
|
"plan_directadmin_import",
|
||||||
@@ -49,6 +55,12 @@ class DirectAdminImportPlanTests(TestCase):
|
|||||||
)
|
)
|
||||||
return json.loads(output.getvalue())
|
return json.loads(output.getvalue())
|
||||||
|
|
||||||
|
def run_import(self, entries=None):
|
||||||
|
self.write_backup(entries)
|
||||||
|
self.import_output = StringIO()
|
||||||
|
call_command("import_directadmin", self.backup, stdout=self.import_output)
|
||||||
|
return json.loads(self.import_output.getvalue())
|
||||||
|
|
||||||
def test_command_reports_account_requests_and_manual_work_without_writing(self):
|
def test_command_reports_account_requests_and_manual_work_without_writing(self):
|
||||||
plan = self.plan()
|
plan = self.plan()
|
||||||
|
|
||||||
@@ -163,3 +175,108 @@ class DirectAdminImportPlanTests(TestCase):
|
|||||||
|
|
||||||
self.assertEqual(discovered["account"]["primary_domain"], "example.net")
|
self.assertEqual(discovered["account"]["primary_domain"], "example.net")
|
||||||
self.assertEqual(assigned["account"]["primary_domain"], "new.example")
|
self.assertEqual(assigned["account"]["primary_domain"], "new.example")
|
||||||
|
|
||||||
|
def test_import_creates_planned_requests_and_reuses_the_same_account_on_replay(
|
||||||
|
self,
|
||||||
|
):
|
||||||
|
first = self.run_import()
|
||||||
|
replay = self.run_import()
|
||||||
|
|
||||||
|
self.assertEqual(first["kind"], "directadmin_import_result")
|
||||||
|
self.assertEqual(first["state"], "imported")
|
||||||
|
self.assertEqual(replay, first | {"state": "already_imported"})
|
||||||
|
account = HostingAccount.objects.get()
|
||||||
|
self.assertEqual(
|
||||||
|
first["account"],
|
||||||
|
{
|
||||||
|
"id": account.pk,
|
||||||
|
"name": "client",
|
||||||
|
"primary_domain": "example.net",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
database = account.databases.get()
|
||||||
|
self.assertEqual(
|
||||||
|
first["database_requests"],
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"source": "client_site",
|
||||||
|
"suffix": "site",
|
||||||
|
"database": database.spec.name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
account.mail_domains.get().mailboxes.get().spec.address,
|
||||||
|
"support@example.net",
|
||||||
|
)
|
||||||
|
self.assertEqual(first["mailbox_requests"], ["support@example.net"])
|
||||||
|
self.assertEqual(DirectAdminImport.objects.get().account_id, account.pk)
|
||||||
|
self.assertFalse(HostingDnsZone.objects.exists())
|
||||||
|
self.assertNotIn("private-mail-hash", self.import_output.getvalue())
|
||||||
|
|
||||||
|
def test_import_refuses_to_merge_with_an_existing_hosting_account(self):
|
||||||
|
existing = HostingAccount.objects.create(
|
||||||
|
name="Existing", primary_domain="example.net"
|
||||||
|
)
|
||||||
|
|
||||||
|
with self.assertRaises(CommandError) as error:
|
||||||
|
self.run_import()
|
||||||
|
|
||||||
|
self.assertEqual(error.exception.returncode, 2)
|
||||||
|
result = json.loads(self.import_output.getvalue())
|
||||||
|
self.assertEqual(result["state"], "blocked")
|
||||||
|
self.assertEqual(result["conflicts"][0]["account_id"], existing.pk)
|
||||||
|
self.assertEqual(HostingAccount.objects.get().name, "Existing")
|
||||||
|
self.assertFalse(DirectAdminImport.objects.exists())
|
||||||
|
self.assertFalse(HostingDatabase.objects.exists())
|
||||||
|
|
||||||
|
def test_failed_import_rolls_back_all_requests(self):
|
||||||
|
with (
|
||||||
|
patch(
|
||||||
|
"coulobre.hosting.directadmin_import.HostingMailbox.objects.bulk_create",
|
||||||
|
side_effect=IntegrityError("Interrupted while saving mailboxes."),
|
||||||
|
),
|
||||||
|
self.assertRaisesRegex(CommandError, "database rejected the import"),
|
||||||
|
):
|
||||||
|
self.run_import()
|
||||||
|
|
||||||
|
for model in (
|
||||||
|
HostingAccount,
|
||||||
|
HostingDatabase,
|
||||||
|
HostingMailDomain,
|
||||||
|
HostingMailbox,
|
||||||
|
DirectAdminImport,
|
||||||
|
):
|
||||||
|
with self.subTest(model=model.__name__):
|
||||||
|
self.assertFalse(model.objects.exists())
|
||||||
|
self.assertEqual(self.run_import()["state"], "imported")
|
||||||
|
|
||||||
|
def test_conflict_discovered_during_import_rolls_back_the_new_account(self):
|
||||||
|
conflict = {"kind": "dns_zone", "domain": "example.net", "account_id": 99}
|
||||||
|
with (
|
||||||
|
patch(
|
||||||
|
"coulobre.hosting.directadmin_import.domain_conflicts",
|
||||||
|
side_effect=[[], [conflict]],
|
||||||
|
),
|
||||||
|
self.assertRaises(CommandError) as error,
|
||||||
|
):
|
||||||
|
self.run_import()
|
||||||
|
|
||||||
|
self.assertEqual(error.exception.returncode, 2)
|
||||||
|
self.assertEqual(
|
||||||
|
json.loads(self.import_output.getvalue())["conflicts"], [conflict]
|
||||||
|
)
|
||||||
|
self.assertFalse(HostingAccount.objects.exists())
|
||||||
|
|
||||||
|
def test_replay_reports_missing_requests_without_restoring_them(self):
|
||||||
|
self.run_import()
|
||||||
|
HostingDatabase.objects.get().delete()
|
||||||
|
|
||||||
|
with self.assertRaises(CommandError) as error:
|
||||||
|
self.run_import()
|
||||||
|
|
||||||
|
self.assertEqual(error.exception.returncode, 2)
|
||||||
|
result = json.loads(self.import_output.getvalue())
|
||||||
|
self.assertIn("missing one or more original requests", result["problems"][0])
|
||||||
|
self.assertFalse(HostingDatabase.objects.exists())
|
||||||
|
self.assertEqual(HostingAccount.objects.count(), 1)
|
||||||
Reference in new issue
Block a user