298 lines
10 KiB
Python
298 lines
10 KiB
Python
from django.core.exceptions import ValidationError
|
|
from django.db import connection, transaction
|
|
from django.db.models import F
|
|
from django.utils.text import slugify
|
|
|
|
from coulobre.database_node import (
|
|
DATABASE_LABEL_LENGTH,
|
|
DATABASE_NAME_LENGTH,
|
|
DATABASE_SUFFIX_LENGTH,
|
|
DatabaseSpec,
|
|
)
|
|
from coulobre.mail_node import MailboxSpec
|
|
|
|
from .domain_policies import require_managed
|
|
from .models import (
|
|
DatabaseProvisioning,
|
|
HostingAccount,
|
|
HostingMailbox,
|
|
HostingMailDomain,
|
|
MailboxProvisioning,
|
|
SiteStorageLimit,
|
|
SiteStorageUsage,
|
|
WebHTTPS,
|
|
)
|
|
|
|
|
|
def lock_hosting_account(account_id, customer_id):
|
|
accounts = HostingAccount.objects.filter(pk=account_id)
|
|
if customer_id is not None:
|
|
accounts = accounts.filter(customer_id=customer_id)
|
|
if connection.vendor == "sqlite":
|
|
# Lock writes before reading the account and resource counts.
|
|
accounts.update(hosting_plan=F("hosting_plan"))
|
|
return accounts.select_for_update().get()
|
|
|
|
|
|
def customer_mailbox_quota_limit(account):
|
|
plan = account.hosting_plan
|
|
return plan.mailbox_quota_mib if plan else None
|
|
|
|
|
|
def new_database_spec(account, suffix):
|
|
label = slugify(account.name).replace("-", "").replace("_", "")
|
|
if not label or not label[0].isalpha():
|
|
label = f"site{label}"
|
|
available = DATABASE_NAME_LENGTH - DATABASE_SUFFIX_LENGTH
|
|
max_label = min(DATABASE_LABEL_LENGTH, available - len(f"_a{account.pk}_"))
|
|
return DatabaseSpec(account.pk, suffix, label[:max_label])
|
|
|
|
|
|
@transaction.atomic
|
|
def create_database_request(
|
|
account_id,
|
|
suffix,
|
|
*,
|
|
customer_id=None,
|
|
auto_provision=False,
|
|
expected_label=None,
|
|
):
|
|
account = lock_hosting_account(account_id, customer_id)
|
|
require_open_account(account)
|
|
databases = account.databases
|
|
if databases.filter(suffix=suffix).exists():
|
|
raise ValidationError(
|
|
{"suffix": "This account already has a database with that suffix."}
|
|
)
|
|
|
|
plan = account.hosting_plan
|
|
if (
|
|
plan
|
|
and plan.database_limit is not None
|
|
and databases.count() >= plan.database_limit
|
|
):
|
|
raise ValidationError(
|
|
"This hosting account has reached its database limit."
|
|
)
|
|
spec = new_database_spec(account, suffix)
|
|
if expected_label is not None and expected_label != spec.account_label:
|
|
raise ValidationError(
|
|
"The hosting account name changed. Review the database name "
|
|
"and submit the form again."
|
|
)
|
|
database = databases.create(
|
|
suffix=suffix, account_label=spec.account_label
|
|
)
|
|
if auto_provision:
|
|
require_active_services(account)
|
|
DatabaseProvisioning.objects.queue(database=database)
|
|
return database
|
|
|
|
|
|
@transaction.atomic
|
|
def create_mailbox_request(
|
|
account_id,
|
|
local_part,
|
|
*,
|
|
customer_id=None,
|
|
auto_provision=False,
|
|
mail_domain_name=None,
|
|
):
|
|
account = lock_hosting_account(account_id, customer_id)
|
|
require_open_account(account)
|
|
domain = mail_domain_name or account.primary_domain
|
|
if domain != account.primary_domain:
|
|
from .domain_services import allowed_domains
|
|
|
|
if domain not in allowed_domains(account):
|
|
raise ValidationError(
|
|
"This mail domain is outside the hosting account."
|
|
)
|
|
require_managed(account, domain, "mail_managed")
|
|
if account.website_domains.filter(
|
|
domain=domain, mail_alias__isnull=False
|
|
).exists():
|
|
raise ValidationError(
|
|
"Add mailboxes on the primary mail domain, not its alias."
|
|
)
|
|
try:
|
|
spec = MailboxSpec(domain, local_part)
|
|
except ValueError as error:
|
|
raise ValidationError({"local_part": str(error)}) from error
|
|
|
|
mail_domain, _ = HostingMailDomain.objects.get_or_create(
|
|
domain=spec.domain, defaults={"account": account}
|
|
)
|
|
if mail_domain.account_id != account.pk:
|
|
raise ValidationError(
|
|
"This mail domain belongs to another hosting account."
|
|
)
|
|
if mail_domain.mailboxes.filter(local_part=spec.local_part).exists():
|
|
raise ValidationError(
|
|
{"local_part": "This mailbox has already been requested."}
|
|
)
|
|
|
|
routing = getattr(mail_domain, "routing", None)
|
|
if routing and any(
|
|
spec.local_part in settings.get("aliases", {})
|
|
for settings in (routing.manifest, routing.applied_manifest)
|
|
):
|
|
raise ValidationError({"local_part": "Remove the mail alias first."})
|
|
|
|
plan = account.hosting_plan
|
|
if plan and plan.mailbox_limit is not None:
|
|
mailboxes = HostingMailbox.objects.filter(mail_domain__account=account)
|
|
if mailboxes.count() >= plan.mailbox_limit:
|
|
raise ValidationError(
|
|
"This hosting account has reached its mailbox limit."
|
|
)
|
|
mailbox = mail_domain.mailboxes.create(
|
|
local_part=spec.local_part,
|
|
quota_mib=plan.mailbox_quota_mib if plan is not None else None,
|
|
)
|
|
if auto_provision:
|
|
require_active_services(account)
|
|
MailboxProvisioning.objects.queue(mailbox=mailbox)
|
|
return mailbox
|
|
|
|
|
|
@transaction.atomic
|
|
def queue_mailbox_provisioning(mailbox):
|
|
account = lock_hosting_account(
|
|
mailbox.mail_domain.account_id, customer_id=None
|
|
)
|
|
require_managed(account, mailbox.mail_domain.domain, "mail_managed")
|
|
require_active_services(account)
|
|
if not HostingMailbox.objects.filter(
|
|
pk=mailbox.pk, removal__isnull=True
|
|
).exists():
|
|
raise ValidationError("This mailbox is being removed.")
|
|
return MailboxProvisioning.objects.queue(mailbox=mailbox)
|
|
|
|
|
|
@transaction.atomic
|
|
def queue_site_storage_measurement(account_id):
|
|
account = lock_hosting_account(account_id, customer_id=None)
|
|
require_open_account(account)
|
|
job, created = SiteStorageUsage.objects.get_or_create(
|
|
account=account, defaults={"domain": account.primary_domain}
|
|
)
|
|
if not created:
|
|
updates = {"domain": account.primary_domain}
|
|
if job.domain != account.primary_domain:
|
|
updates.update(used_bytes=None, measured_at=None)
|
|
SiteStorageUsage.objects.requeue(
|
|
SiteStorageUsage.objects.filter(pk=job.pk), **updates
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def queue_site_storage_limit(account_id, limit_mib):
|
|
account = lock_hosting_account(account_id, customer_id=None)
|
|
require_open_account(account)
|
|
job, created = SiteStorageLimit.objects.get_or_create(
|
|
account=account,
|
|
defaults={"domain": account.primary_domain, "limit_mib": limit_mib},
|
|
)
|
|
if created:
|
|
return
|
|
if job.finished_at is None:
|
|
if job.domain == account.primary_domain and job.limit_mib == limit_mib:
|
|
return
|
|
raise ValidationError(
|
|
"Wait for the current storage limit request to finish."
|
|
)
|
|
|
|
updates = {"domain": account.primary_domain, "limit_mib": limit_mib}
|
|
if job.domain != account.primary_domain:
|
|
updates.update(applied_mib=None, applied_at=None)
|
|
SiteStorageLimit.objects.requeue(
|
|
SiteStorageLimit.objects.filter(pk=job.pk), **updates
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def change_mailbox_quota(mailbox, quota_mib, *, customer_id=None):
|
|
account = lock_hosting_account(mailbox.mail_domain.account_id, customer_id)
|
|
require_active_services(account)
|
|
current = HostingMailbox.objects.select_related("mail_domain").get(
|
|
pk=mailbox.pk, mail_domain__account=account
|
|
)
|
|
if hasattr(current, "removal"):
|
|
raise ValidationError("This mailbox is being removed.")
|
|
if customer_id is not None:
|
|
require_managed(account, current.mail_domain.domain, "mail_managed")
|
|
limit = customer_mailbox_quota_limit(account)
|
|
if limit is None or quota_mib is None or quota_mib > limit:
|
|
raise ValidationError(
|
|
"Choose a storage limit within your hosting package."
|
|
)
|
|
from .service_credentials import (
|
|
UNCONFIRMED_ROTATION,
|
|
CredentialKind,
|
|
rotation_history,
|
|
)
|
|
|
|
latest = rotation_history(
|
|
account.pk, CredentialKind.MAILBOX, str(current)
|
|
).first()
|
|
if latest is not None and latest.outcome in UNCONFIRMED_ROTATION:
|
|
raise ValidationError(
|
|
"Finish the mailbox password change before changing its quota."
|
|
)
|
|
jobs = MailboxProvisioning.objects.filter(mailbox=current)
|
|
job = jobs.select_for_update().first()
|
|
if customer_id is not None and job is None:
|
|
raise ValidationError("Set up the mailbox before changing its quota.")
|
|
if current.quota_mib == quota_mib:
|
|
if job is not None and job.quota_update and job.state == "failed":
|
|
MailboxProvisioning.objects.requeue(jobs, quota_update=True)
|
|
return True
|
|
return False
|
|
|
|
if job is not None and job.state != "configured":
|
|
raise ValidationError(
|
|
"Complete the pending or failed mailbox request first."
|
|
)
|
|
|
|
current.quota_mib = quota_mib
|
|
current.save(update_fields=["quota_mib"])
|
|
if job is not None:
|
|
MailboxProvisioning.objects.requeue(jobs, quota_update=True)
|
|
return True
|
|
|
|
|
|
def require_open_account(account):
|
|
if account.retired_at is not None:
|
|
raise ValidationError("This hosting account is retired.")
|
|
|
|
|
|
def require_active_services(account):
|
|
require_open_account(account)
|
|
if account.services_suspended:
|
|
raise ValidationError(
|
|
"Resume the hosting account before making this change."
|
|
)
|
|
latest = account.service_changes.first()
|
|
if latest is not None and (
|
|
latest.jobs.filter(finished_at__isnull=True).exists()
|
|
or latest.jobs.exclude(problem="").exists()
|
|
):
|
|
raise ValidationError(
|
|
"Complete the pending or failed service change first."
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def queue_database_provisioning(database):
|
|
account = lock_hosting_account(database.account_id, customer_id=None)
|
|
require_active_services(account)
|
|
return DatabaseProvisioning.objects.queue(database=database)
|
|
|
|
|
|
@transaction.atomic
|
|
def queue_web_https(plan):
|
|
account = lock_hosting_account(plan.account_id, customer_id=None)
|
|
require_active_services(account)
|
|
WebHTTPS.objects.queue(plan=plan)
|