Files
coulobre/src/coulobre/hosting/resource_requests.py
T

203 lines
6.7 KiB
Python
Raw Normal View History

from django.core.exceptions import ValidationError
from django.db import connection, transaction
from django.db.models import F
from coulobre.mail_node import MailboxSpec
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()
@transaction.atomic
def create_database_request(account_id, suffix, *, customer_id=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."
)
return databases.create(suffix=suffix)
@transaction.atomic
def create_mailbox_request(account_id, local_part, *, customer_id=None):
account = lock_hosting_account(account_id, customer_id)
require_open_account(account)
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:
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."}
)
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."
)
return mail_domain.mailboxes.create(
local_part=spec.local_part,
quota_mib=plan.mailbox_quota_mib if plan is not None else None,
)
@transaction.atomic
def queue_mailbox_provisioning(mailbox):
account = lock_hosting_account(
mailbox.mail_domain.account_id, customer_id=None
)
require_active_services(account)
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):
account = lock_hosting_account(
mailbox.mail_domain.account_id, customer_id=None
)
require_active_services(account)
current = HostingMailbox.objects.get(pk=mailbox.pk)
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."
)
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)
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)
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)