2026-11-21 00:34:37 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
from django.db import connection, transaction
|
|
|
|
|
from django.db.models import F
|
|
|
|
|
|
|
|
|
|
from coulobre.mail_node import MailboxSpec
|
|
|
|
|
|
2026-11-21 13:32:08 +01:00
|
|
|
from .models import (
|
2026-11-21 18:55:41 +01:00
|
|
|
DatabaseProvisioning,
|
2026-11-21 13:32:08 +01:00
|
|
|
HostingAccount,
|
|
|
|
|
HostingMailbox,
|
|
|
|
|
HostingMailDomain,
|
|
|
|
|
MailboxProvisioning,
|
2026-11-21 14:10:34 +01:00
|
|
|
SiteStorageLimit,
|
2026-11-21 13:40:50 +01:00
|
|
|
SiteStorageUsage,
|
2026-11-21 18:55:41 +01:00
|
|
|
WebHTTPS,
|
2026-11-21 13:32:08 +01:00
|
|
|
)
|
2026-11-21 00:34:37 +01:00
|
|
|
|
|
|
|
|
|
2026-11-21 00:43:04 +01:00
|
|
|
def lock_hosting_account(account_id, customer_id):
|
2026-11-21 00:34:37 +01:00
|
|
|
accounts = HostingAccount.objects.filter(pk=account_id)
|
2026-11-21 00:43:04 +01:00
|
|
|
if customer_id is not None:
|
|
|
|
|
accounts = accounts.filter(customer_id=customer_id)
|
2026-11-21 00:34:37 +01:00
|
|
|
if connection.vendor == "sqlite":
|
2026-11-21 02:12:32 +01:00
|
|
|
# Lock writes before reading the account and resource counts.
|
2026-11-21 00:34:37 +01:00
|
|
|
accounts.update(hosting_plan=F("hosting_plan"))
|
|
|
|
|
return accounts.select_for_update().get()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic
|
2026-11-21 00:43:04 +01:00
|
|
|
def create_database_request(account_id, suffix, *, customer_id=None):
|
|
|
|
|
account = lock_hosting_account(account_id, customer_id)
|
2026-11-21 19:40:19 +01:00
|
|
|
require_open_account(account)
|
2026-11-21 00:34:37 +01:00
|
|
|
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
|
|
|
|
|
):
|
2026-11-21 02:12:32 +01:00
|
|
|
raise ValidationError(
|
|
|
|
|
"This hosting account has reached its database limit."
|
|
|
|
|
)
|
2026-11-21 00:34:37 +01:00
|
|
|
return databases.create(suffix=suffix)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic
|
2026-11-21 00:43:04 +01:00
|
|
|
def create_mailbox_request(account_id, local_part, *, customer_id=None):
|
|
|
|
|
account = lock_hosting_account(account_id, customer_id)
|
2026-11-21 19:40:19 +01:00
|
|
|
require_open_account(account)
|
2026-11-21 00:34:37 +01:00
|
|
|
try:
|
|
|
|
|
spec = MailboxSpec(account.primary_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:
|
2026-11-21 02:12:32 +01:00
|
|
|
raise ValidationError(
|
|
|
|
|
"This mail domain belongs to another hosting account."
|
|
|
|
|
)
|
2026-11-21 00:34:37 +01:00
|
|
|
if mail_domain.mailboxes.filter(local_part=spec.local_part).exists():
|
|
|
|
|
raise ValidationError(
|
|
|
|
|
{"local_part": "This mailbox has already been requested."}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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:
|
2026-11-21 02:12:32 +01:00
|
|
|
raise ValidationError(
|
|
|
|
|
"This hosting account has reached its mailbox limit."
|
|
|
|
|
)
|
2026-11-21 13:02:06 +01:00
|
|
|
return mail_domain.mailboxes.create(
|
|
|
|
|
local_part=spec.local_part,
|
|
|
|
|
quota_mib=plan.mailbox_quota_mib if plan is not None else None,
|
|
|
|
|
)
|
2026-11-21 13:32:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
|
def queue_mailbox_provisioning(mailbox):
|
2026-11-21 18:55:41 +01:00
|
|
|
account = lock_hosting_account(
|
|
|
|
|
mailbox.mail_domain.account_id, customer_id=None
|
|
|
|
|
)
|
|
|
|
|
require_active_services(account)
|
2026-11-21 13:32:08 +01:00
|
|
|
MailboxProvisioning.objects.queue(mailbox=mailbox)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 13:40:50 +01:00
|
|
|
@transaction.atomic
|
|
|
|
|
def queue_site_storage_measurement(account_id):
|
|
|
|
|
account = lock_hosting_account(account_id, customer_id=None)
|
2026-11-21 19:40:19 +01:00
|
|
|
require_open_account(account)
|
2026-11-21 13:40:50 +01:00
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 14:10:34 +01:00
|
|
|
@transaction.atomic
|
|
|
|
|
def queue_site_storage_limit(account_id, limit_mib):
|
|
|
|
|
account = lock_hosting_account(account_id, customer_id=None)
|
2026-11-21 19:40:19 +01:00
|
|
|
require_open_account(account)
|
2026-11-21 14:10:34 +01:00
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 13:32:08 +01:00
|
|
|
@transaction.atomic
|
|
|
|
|
def change_mailbox_quota(mailbox, quota_mib):
|
2026-11-21 18:55:41 +01:00
|
|
|
account = lock_hosting_account(
|
|
|
|
|
mailbox.mail_domain.account_id, customer_id=None
|
|
|
|
|
)
|
|
|
|
|
require_active_services(account)
|
2026-11-21 13:32:08 +01:00
|
|
|
current = HostingMailbox.objects.get(pk=mailbox.pk)
|
2026-11-21 19:23:01 +01:00
|
|
|
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."
|
|
|
|
|
)
|
2026-11-21 13:32:08 +01:00
|
|
|
if current.quota_mib == quota_mib:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
jobs = MailboxProvisioning.objects.filter(mailbox=current)
|
|
|
|
|
job = jobs.select_for_update().first()
|
|
|
|
|
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)
|
2026-11-21 18:55:41 +01:00
|
|
|
|
|
|
|
|
|
2026-11-21 19:40:19 +01:00
|
|
|
def require_open_account(account):
|
|
|
|
|
if account.retired_at is not None:
|
|
|
|
|
raise ValidationError("This hosting account is retired.")
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 18:55:41 +01:00
|
|
|
def require_active_services(account):
|
2026-11-21 19:40:19 +01:00
|
|
|
require_open_account(account)
|
2026-11-21 18:55:41 +01:00
|
|
|
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)
|
|
|
|
|
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)
|