318 lines
10 KiB
Python
318 lines
10 KiB
Python
import hashlib
|
|
import json
|
|
from collections import defaultdict
|
|
|
|
from django.db import IntegrityError, transaction
|
|
|
|
from coulobre.directadmin_payload import database_suffix
|
|
from coulobre.domain_names import canonical_domain
|
|
|
|
from .models import (
|
|
DirectAdminImport,
|
|
HostingAccount,
|
|
HostingDatabase,
|
|
HostingDnsZone,
|
|
HostingMailbox,
|
|
HostingMailDomain,
|
|
WebsiteDomain,
|
|
)
|
|
from .resource_requests import new_database_spec
|
|
|
|
|
|
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)
|
|
|
|
source_domain = domains.get(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": source_domain.get("mailboxes", []),
|
|
"source_data": {
|
|
"website_files": source_domain.get("website_files", 0),
|
|
"mail_files": source_domain.get("mail_files", 0),
|
|
"dns_zone": source_domain.get("dns_zone", False),
|
|
},
|
|
"conflicts": conflicts,
|
|
"problems": problems,
|
|
"manual_review": review,
|
|
}
|
|
|
|
|
|
def plan_database_requests(username, sources):
|
|
by_suffix = defaultdict(list)
|
|
review = []
|
|
for source in sources:
|
|
suffix = database_suffix(username, source)
|
|
if suffix is None:
|
|
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}
|
|
)
|
|
alias_owner = (
|
|
WebsiteDomain.objects.filter(domain=domain, is_primary=False)
|
|
.values_list("account_id", flat=True)
|
|
.first()
|
|
)
|
|
if alias_owner is not None:
|
|
conflicts.append(
|
|
{
|
|
"kind": "website_alias",
|
|
"domain": domain,
|
|
"account_id": alias_owner,
|
|
}
|
|
)
|
|
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}
|
|
|
|
databases = [
|
|
new_database_spec(account, request["suffix"])
|
|
for request in plan["database_requests"]
|
|
]
|
|
HostingDatabase.objects.bulk_create(
|
|
HostingDatabase(
|
|
account=account,
|
|
suffix=spec.suffix,
|
|
account_label=spec.account_label,
|
|
)
|
|
for spec in databases
|
|
)
|
|
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:
|
|
# Another process may have claimed the domain during the 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):
|
|
databases = {
|
|
database.suffix: database for database in account.databases.all()
|
|
}
|
|
return plan | {
|
|
"state": state,
|
|
"account": {
|
|
"id": account.pk,
|
|
"name": account.name,
|
|
"primary_domain": account.primary_domain,
|
|
},
|
|
"database_requests": [
|
|
request | {"database": str(databases[request["suffix"]])}
|
|
for request in plan["database_requests"]
|
|
],
|
|
"conflicts": [],
|
|
}
|