Plan DirectAdmin imports against the panel database
This commit is contained in:
1 parent
6ff77f3aed
commit
0468db1cd8
6 files changed
+340
No files matched your search
@@ -361,6 +361,28 @@ have an 8 MiB limit per file. Mail and website counts refer to regular
|
||||
files at recognized paths. The report contains names and counts rather
|
||||
than passwords, hashes or file contents.
|
||||
|
||||
### Plan a DirectAdmin import
|
||||
|
||||
On the panel host, use the panel's database settings to preview which
|
||||
account, mailbox requests and database requests can be created.
|
||||
|
||||
```sh
|
||||
python manage.py plan_directadmin_import user.admin.customer.tar.zst
|
||||
```
|
||||
|
||||
The plan uses the source username as the account name and picks the backup's
|
||||
primary domain, or its only discovered domain when there is no default.
|
||||
Use `--primary-domain` to select a different domain from the backup. For
|
||||
a domainless archive, use it to assign the new account a domain.
|
||||
|
||||
The command reads the panel database and reports `blocked` when the chosen
|
||||
domain is already assigned to a hosting account, DNS zone or mail domain,
|
||||
or when a domain still needs to be selected. The report lists database
|
||||
requests using the suffix after the source username and mailbox requests
|
||||
for the selected domain. The `manual_review` list identifies other domains,
|
||||
database names that need a mapping, and data that needs a separate migration,
|
||||
including website files, DNS zones, mail files and SQL dumps.
|
||||
|
||||
## Request an Apache configuration preview
|
||||
|
||||
Start RabbitMQ and set `COULOBRE_BROKER_URL` to its AMQP URL. The development
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
import re
|
||||
from collections import defaultdict
|
||||
|
||||
from coulobre.database_node import DATABASE_SUFFIX_PATTERN
|
||||
from coulobre.domain_names import canonical_domain
|
||||
|
||||
from .models import HostingAccount, HostingDnsZone, HostingMailDomain
|
||||
|
||||
|
||||
def plan_directadmin_import(inventory, primary_domain=None):
|
||||
username = inventory["username"]
|
||||
domains = {entry["name"]: entry for entry in inventory["domains"]}
|
||||
if primary_domain is not None:
|
||||
primary_domain = canonical_domain(primary_domain)
|
||||
if domains and primary_domain not in domains:
|
||||
raise ValueError("The selected primary domain is absent from the backup.")
|
||||
else:
|
||||
primary_domain = inventory["primary_domain"]
|
||||
if primary_domain is None and len(domains) == 1:
|
||||
primary_domain = next(iter(domains))
|
||||
|
||||
databases, review = plan_database_requests(username, inventory["databases"])
|
||||
review.extend(review_backup_data(inventory, primary_domain))
|
||||
account = None
|
||||
conflicts = []
|
||||
problems = []
|
||||
if primary_domain is None:
|
||||
problems.append("Select a primary domain for the hosting account.")
|
||||
else:
|
||||
account = {"name": username, "primary_domain": primary_domain}
|
||||
conflicts = domain_conflicts(primary_domain)
|
||||
|
||||
return {
|
||||
"version": 1,
|
||||
"kind": "directadmin_import_plan",
|
||||
"state": "blocked" if problems or conflicts else "ready",
|
||||
"source_username": username,
|
||||
"source_domains": list(domains),
|
||||
"account": account,
|
||||
"database_requests": databases,
|
||||
"mailbox_requests": domains.get(primary_domain, {}).get("mailboxes", []),
|
||||
"conflicts": conflicts,
|
||||
"problems": problems,
|
||||
"manual_review": review,
|
||||
}
|
||||
|
||||
|
||||
def plan_database_requests(username, sources):
|
||||
by_suffix = defaultdict(list)
|
||||
review = []
|
||||
prefix = f"{username.lower()}_"
|
||||
for source in sources:
|
||||
normalized = source.lower()
|
||||
suffix = normalized.removeprefix(prefix)
|
||||
if normalized == suffix or not re.fullmatch(DATABASE_SUFFIX_PATTERN, suffix):
|
||||
review.append(
|
||||
{
|
||||
"kind": "database_name",
|
||||
"source": source,
|
||||
"reason": "unsupported_suffix",
|
||||
}
|
||||
)
|
||||
continue
|
||||
by_suffix[suffix].append(source)
|
||||
|
||||
requests = []
|
||||
for suffix, names in sorted(by_suffix.items()):
|
||||
if len(names) == 1:
|
||||
requests.append({"source": names[0], "suffix": suffix})
|
||||
else:
|
||||
review.append(
|
||||
{
|
||||
"kind": "database_name",
|
||||
"sources": names,
|
||||
"reason": "duplicate_suffix",
|
||||
}
|
||||
)
|
||||
return requests, review
|
||||
|
||||
|
||||
def review_backup_data(inventory, primary_domain):
|
||||
entries = []
|
||||
for domain in inventory["domains"]:
|
||||
name = domain["name"]
|
||||
if primary_domain is not None and name != primary_domain:
|
||||
entries.append(
|
||||
{
|
||||
"kind": "additional_domain",
|
||||
"domain": name,
|
||||
"mailboxes": domain["mailboxes"],
|
||||
}
|
||||
)
|
||||
if domain["dns_zone"]:
|
||||
entries.append({"kind": "dns_zone", "domain": name})
|
||||
for field in ("website_files", "mail_files", "unrecognized_mailbox_entries"):
|
||||
if domain[field]:
|
||||
entries.append({"kind": field, "domain": name, "count": domain[field]})
|
||||
|
||||
if inventory["databases"]:
|
||||
entries.append({"kind": "database_dumps", "names": inventory["databases"]})
|
||||
if inventory["home_archive"]:
|
||||
entries.append({"kind": "nested_home_archive"})
|
||||
if inventory["skipped_non_regular_entries"]:
|
||||
entries.append(
|
||||
{
|
||||
"kind": "special_archive_entries",
|
||||
"count": inventory["skipped_non_regular_entries"],
|
||||
}
|
||||
)
|
||||
return entries
|
||||
|
||||
|
||||
def domain_conflicts(domain):
|
||||
sources = (
|
||||
("hosting_account", HostingAccount, "primary_domain", "pk"),
|
||||
("dns_zone", HostingDnsZone, "domain", "account_id"),
|
||||
("mail_domain", HostingMailDomain, "domain", "account_id"),
|
||||
)
|
||||
conflicts = []
|
||||
for kind, model, domain_field, owner_field in sources:
|
||||
owner = (
|
||||
model.objects.filter(**{domain_field: domain})
|
||||
.values_list(owner_field, flat=True)
|
||||
.first()
|
||||
)
|
||||
if owner is not None:
|
||||
conflicts.append({"kind": kind, "domain": domain, "account_id": owner})
|
||||
return conflicts
|
||||
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,25 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
from coulobre.directadmin import inspect_backup
|
||||
from coulobre.hosting.directadmin_import import plan_directadmin_import
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Preview a DirectAdmin account import against the current panel database."
|
||||
|
||||
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"])
|
||||
plan = plan_directadmin_import(inventory, options["primary_domain"])
|
||||
except (OSError, ValueError) as error:
|
||||
raise CommandError(str(error)) from None
|
||||
self.stdout.write(json.dumps(plan, indent=2))
|
||||
@@ -0,0 +1,165 @@
|
||||
import json
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from django.core.management import CommandError, call_command
|
||||
from django.test import TestCase
|
||||
|
||||
from .models import (
|
||||
HostingAccount,
|
||||
HostingDatabase,
|
||||
HostingDnsZone,
|
||||
HostingMailbox,
|
||||
HostingMailDomain,
|
||||
)
|
||||
from .test_directadmin import make_tar
|
||||
|
||||
|
||||
class DirectAdminImportPlanTests(TestCase):
|
||||
def setUp(self):
|
||||
directory = Path(self.enterContext(TemporaryDirectory()))
|
||||
self.backup = directory / "user.admin.client.tar"
|
||||
self.entries = [
|
||||
("backup/client_site.sql", "database dump"),
|
||||
("backup/client_Store.sql", ""),
|
||||
("backup/client_store.sql", ""),
|
||||
("backup/client_9.sql", ""),
|
||||
("backup/other_wp.sql", ""),
|
||||
("backup/example.net/email/passwd", "support:private-mail-hash:10\n"),
|
||||
("backup/other.example/email/passwd", "billing:private-other-hash:10\n"),
|
||||
("backup/example.net/example.net.db", "zone file"),
|
||||
("domains/example.net/public_html/index.php", "website data"),
|
||||
("imap/example.net/support/Maildir/cur/message", "mail data"),
|
||||
]
|
||||
|
||||
def plan(self, entries=None, *, primary_domain=None):
|
||||
if entries is None:
|
||||
entries = [
|
||||
("backup/user.conf", "username=client\ndomain=Example.Net.\n"),
|
||||
*self.entries,
|
||||
]
|
||||
self.backup.write_bytes(make_tar(entries))
|
||||
output = StringIO()
|
||||
call_command(
|
||||
"plan_directadmin_import",
|
||||
self.backup,
|
||||
primary_domain=primary_domain,
|
||||
stdout=output,
|
||||
)
|
||||
return json.loads(output.getvalue())
|
||||
|
||||
def test_command_reports_account_requests_and_manual_work_without_writing(self):
|
||||
plan = self.plan()
|
||||
|
||||
self.assertEqual(plan["state"], "ready")
|
||||
self.assertEqual(
|
||||
plan["account"], {"name": "client", "primary_domain": "example.net"}
|
||||
)
|
||||
self.assertEqual(plan["source_domains"], ["example.net", "other.example"])
|
||||
self.assertEqual(
|
||||
plan["database_requests"], [{"source": "client_site", "suffix": "site"}]
|
||||
)
|
||||
self.assertEqual(plan["mailbox_requests"], ["support@example.net"])
|
||||
for entry in (
|
||||
{
|
||||
"kind": "database_name",
|
||||
"sources": ["client_Store", "client_store"],
|
||||
"reason": "duplicate_suffix",
|
||||
},
|
||||
{
|
||||
"kind": "database_name",
|
||||
"source": "client_9",
|
||||
"reason": "unsupported_suffix",
|
||||
},
|
||||
{
|
||||
"kind": "database_name",
|
||||
"source": "other_wp",
|
||||
"reason": "unsupported_suffix",
|
||||
},
|
||||
{"kind": "dns_zone", "domain": "example.net"},
|
||||
{"kind": "website_files", "domain": "example.net", "count": 1},
|
||||
{"kind": "mail_files", "domain": "example.net", "count": 1},
|
||||
{
|
||||
"kind": "additional_domain",
|
||||
"domain": "other.example",
|
||||
"mailboxes": ["billing@other.example"],
|
||||
},
|
||||
):
|
||||
with self.subTest(entry=entry):
|
||||
self.assertIn(entry, plan["manual_review"])
|
||||
self.assertFalse(HostingAccount.objects.exists())
|
||||
self.assertFalse(HostingDatabase.objects.exists())
|
||||
self.assertFalse(HostingMailDomain.objects.exists())
|
||||
self.assertFalse(HostingMailbox.objects.exists())
|
||||
|
||||
def test_existing_account_blocks_the_plan(self):
|
||||
account = HostingAccount.objects.create(
|
||||
name="Existing", primary_domain="example.net"
|
||||
)
|
||||
|
||||
plan = self.plan()
|
||||
|
||||
self.assertEqual(plan["state"], "blocked")
|
||||
self.assertEqual(
|
||||
plan["conflicts"],
|
||||
[
|
||||
{
|
||||
"kind": "hosting_account",
|
||||
"domain": "example.net",
|
||||
"account_id": account.pk,
|
||||
}
|
||||
],
|
||||
)
|
||||
self.assertEqual(HostingAccount.objects.count(), 1)
|
||||
|
||||
def test_existing_dns_and_mail_ownership_block_a_reused_domain(self):
|
||||
owner = HostingAccount.objects.create(
|
||||
name="Renamed", primary_domain="renamed.example"
|
||||
)
|
||||
HostingDnsZone.objects.create(
|
||||
account=owner,
|
||||
domain="example.net",
|
||||
nameservers=["ns1.provider.example"],
|
||||
responsible_name="hostmaster.provider.example",
|
||||
)
|
||||
HostingMailDomain.objects.create(account=owner, domain="example.net")
|
||||
|
||||
plan = self.plan()
|
||||
|
||||
self.assertEqual(plan["state"], "blocked")
|
||||
self.assertEqual(
|
||||
plan["conflicts"],
|
||||
[
|
||||
{"kind": "dns_zone", "domain": "example.net", "account_id": owner.pk},
|
||||
{
|
||||
"kind": "mail_domain",
|
||||
"domain": "example.net",
|
||||
"account_id": owner.pk,
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
def test_multiple_domains_without_a_default_require_a_selection(self):
|
||||
entries = [("backup/user.conf", "username=client\n"), *self.entries]
|
||||
|
||||
blocked = self.plan(entries)
|
||||
self.assertEqual(blocked["state"], "blocked")
|
||||
self.assertIsNone(blocked["account"])
|
||||
self.assertTrue(blocked["problems"])
|
||||
|
||||
selected = self.plan(entries, primary_domain="Other.Example.")
|
||||
self.assertEqual(selected["state"], "ready")
|
||||
self.assertEqual(selected["account"]["primary_domain"], "other.example")
|
||||
self.assertEqual(selected["mailbox_requests"], ["billing@other.example"])
|
||||
|
||||
with self.assertRaisesRegex(CommandError, "absent from the backup"):
|
||||
self.plan(entries, primary_domain="absent.example")
|
||||
|
||||
def test_single_discovered_domain_or_explicit_new_domain_can_be_used(self):
|
||||
account = ("backup/user.conf", "username=client\n")
|
||||
discovered = self.plan([account, ("backup/domains.list", "example.net\n")])
|
||||
assigned = self.plan([account], primary_domain="new.example")
|
||||
|
||||
self.assertEqual(discovered["account"]["primary_domain"], "example.net")
|
||||
self.assertEqual(assigned["account"]["primary_domain"], "new.example")
|
||||
Reference in new issue
Block a user