2026-11-22 15:38:27 +01:00
|
|
|
from datetime import datetime, timedelta
|
2026-11-21 15:08:48 +01:00
|
|
|
from uuid import UUID
|
2026-11-19 21:18:37 +01:00
|
|
|
|
|
|
|
|
from celery import shared_task
|
2026-11-21 01:17:01 +01:00
|
|
|
from django.conf import settings
|
2026-11-21 15:32:02 +01:00
|
|
|
from django.db import transaction
|
2026-11-21 19:40:19 +01:00
|
|
|
from django.db.models import Exists, OuterRef, Q
|
2026-11-19 21:18:37 +01:00
|
|
|
from django.utils import timezone
|
|
|
|
|
|
2026-11-21 12:35:24 +01:00
|
|
|
from coulobre.account_backup import AccountBackup
|
|
|
|
|
from coulobre.account_backup_request import (
|
|
|
|
|
BACKUP_TIMEOUT_SECONDS,
|
|
|
|
|
AccountBackupRequest,
|
|
|
|
|
)
|
2026-11-21 15:08:48 +01:00
|
|
|
from coulobre.account_backup_retention import ArchiveExpirationRequest
|
2026-11-21 14:42:38 +01:00
|
|
|
from coulobre.account_restore_request import (
|
|
|
|
|
RESTORE_TIMEOUT_SECONDS,
|
2026-11-21 14:54:51 +01:00
|
|
|
ComponentRestoreRequest,
|
2026-11-21 14:42:38 +01:00
|
|
|
WebsiteRestoreRequest,
|
|
|
|
|
)
|
2026-11-21 01:41:06 +01:00
|
|
|
from coulobre.dns_node import DnsZone
|
2026-11-21 22:18:59 +01:00
|
|
|
from coulobre.mail_archive_transport import UNCONFIRMED_REMOTE_RESTORE
|
2026-11-21 16:04:33 +01:00
|
|
|
from coulobre.php_pool_settings import PhpPoolSettings
|
2026-11-21 10:56:47 +01:00
|
|
|
from coulobre.sftp_keys import SFTPKeys
|
2026-11-21 15:32:02 +01:00
|
|
|
from coulobre.web_node import WebAlias, WebSite
|
2026-11-19 21:44:15 +01:00
|
|
|
|
2026-11-21 14:26:38 +01:00
|
|
|
from .account_backups import queue_due_account_backups
|
2026-11-21 19:40:19 +01:00
|
|
|
from .account_retirement import apply_retirement_job, finalize_retirement
|
2026-11-21 14:54:51 +01:00
|
|
|
from .backup_restores import (
|
2026-11-21 22:18:59 +01:00
|
|
|
RestoreKind,
|
2026-11-21 14:54:51 +01:00
|
|
|
available_restore_resources,
|
2026-11-22 12:06:10 +01:00
|
|
|
available_website_restore_targets,
|
2026-11-21 14:54:51 +01:00
|
|
|
current_restore_resources,
|
|
|
|
|
enabled_component_restores,
|
|
|
|
|
)
|
2026-11-21 15:08:48 +01:00
|
|
|
from .backup_retention import plan_backup_expirations
|
2026-11-22 12:06:10 +01:00
|
|
|
from .independent_websites import active_websites, apply_website_operation
|
2026-11-21 22:04:15 +01:00
|
|
|
from .mail_dns_setup import sync_mail_dns
|
2026-11-22 11:03:26 +01:00
|
|
|
from .mail_policies import apply_policy, mail_policy_available
|
2026-11-21 01:41:06 +01:00
|
|
|
from .models import (
|
2026-11-21 15:08:48 +01:00
|
|
|
AccountBackupExpiration,
|
2026-11-21 12:35:24 +01:00
|
|
|
AccountBackupJob,
|
2026-11-21 19:40:19 +01:00
|
|
|
AccountRetirement,
|
|
|
|
|
AccountRetirementJob,
|
2026-11-21 18:55:41 +01:00
|
|
|
AccountServiceJob,
|
2026-11-21 14:54:51 +01:00
|
|
|
ComponentRestoreJob,
|
2026-11-21 01:41:06 +01:00
|
|
|
DatabaseProvisioning,
|
|
|
|
|
DnsPublication,
|
2026-11-21 22:04:15 +01:00
|
|
|
HostingMailDomain,
|
2026-11-22 12:06:10 +01:00
|
|
|
IndependentWebsite,
|
2026-11-21 01:51:42 +01:00
|
|
|
MailboxProvisioning,
|
2026-11-22 11:03:26 +01:00
|
|
|
MailRoutingPublication,
|
|
|
|
|
MailVacationPublication,
|
2026-11-22 16:14:55 +01:00
|
|
|
OffsiteBackupTransfer,
|
2026-11-22 11:30:58 +01:00
|
|
|
SignInWindow,
|
2026-11-21 16:04:33 +01:00
|
|
|
SitePhpSettings,
|
2026-11-21 23:57:07 +01:00
|
|
|
SitePhpVersion,
|
2026-11-21 10:56:47 +01:00
|
|
|
SiteSFTP,
|
2026-11-21 14:10:34 +01:00
|
|
|
SiteStorageLimit,
|
2026-11-21 13:40:50 +01:00
|
|
|
SiteStorageUsage,
|
2026-11-21 01:41:06 +01:00
|
|
|
WebActivation,
|
2026-11-21 10:31:32 +01:00
|
|
|
WebHTTPS,
|
2026-11-21 01:41:06 +01:00
|
|
|
WebProvisioningPlan,
|
2026-11-21 15:32:02 +01:00
|
|
|
WebsiteAlias,
|
2026-11-21 14:42:38 +01:00
|
|
|
WebsiteRestoreJob,
|
2026-11-22 15:38:27 +01:00
|
|
|
WebsiteScheduledJob,
|
2026-11-21 01:41:06 +01:00
|
|
|
)
|
|
|
|
|
from .node_client import (
|
2026-11-21 22:18:59 +01:00
|
|
|
NodeReplyError,
|
2026-11-21 01:41:06 +01:00
|
|
|
activate_local_site,
|
2026-11-21 12:35:24 +01:00
|
|
|
create_local_account_backup,
|
2026-11-21 10:31:32 +01:00
|
|
|
enable_local_https,
|
2026-11-21 15:08:48 +01:00
|
|
|
expire_local_account_backup,
|
2026-11-21 13:40:50 +01:00
|
|
|
measure_local_site_storage,
|
2026-11-21 01:41:06 +01:00
|
|
|
provision_local_database,
|
2026-11-21 01:51:42 +01:00
|
|
|
provision_local_mailbox,
|
2026-11-21 22:04:15 +01:00
|
|
|
provision_signing_mailbox,
|
2026-11-21 01:41:06 +01:00
|
|
|
publish_local_zone,
|
2026-11-21 14:54:51 +01:00
|
|
|
restore_local_component,
|
2026-11-21 14:42:38 +01:00
|
|
|
restore_local_website,
|
2026-11-21 14:10:34 +01:00
|
|
|
set_local_site_storage_limit,
|
2026-11-21 13:32:08 +01:00
|
|
|
update_local_mailbox_quota,
|
2026-11-21 16:04:33 +01:00
|
|
|
update_local_php_settings,
|
2026-11-21 23:57:07 +01:00
|
|
|
update_local_php_version,
|
2026-11-21 10:56:47 +01:00
|
|
|
update_local_sftp,
|
2026-11-21 15:32:02 +01:00
|
|
|
update_local_website_alias,
|
2026-11-21 01:41:06 +01:00
|
|
|
)
|
2026-11-22 16:14:55 +01:00
|
|
|
from .offsite_backups import (
|
|
|
|
|
MAX_ATTEMPTS as MAX_OFFSITE_ATTEMPTS,
|
|
|
|
|
)
|
|
|
|
|
from .offsite_backups import (
|
|
|
|
|
OFFSITE_BACKUP_LEASE,
|
|
|
|
|
OFFSITE_BACKUP_QUEUE,
|
|
|
|
|
finish_interrupted_transfers,
|
|
|
|
|
run_offsite_transfer,
|
|
|
|
|
transfer_scope,
|
|
|
|
|
)
|
2026-11-22 11:30:58 +01:00
|
|
|
from .password_reset import send_customer_reset_email
|
2026-11-21 23:57:07 +01:00
|
|
|
from .php_settings import plan_php_versions
|
2026-11-21 18:55:41 +01:00
|
|
|
from .service_changes import apply_service_change
|
2026-11-21 10:56:47 +01:00
|
|
|
from .sftp import site_is_active
|
2026-11-22 11:30:58 +01:00
|
|
|
from .sign_in_limits import current_window
|
2026-11-22 15:38:27 +01:00
|
|
|
from .website_jobs import (
|
|
|
|
|
JOB_QUEUE,
|
|
|
|
|
claim_website_job,
|
|
|
|
|
due_website_jobs,
|
|
|
|
|
recover_interrupted_runs,
|
|
|
|
|
run_website_job,
|
|
|
|
|
save_website_job_result,
|
|
|
|
|
)
|
|
|
|
|
from .website_jobs import (
|
|
|
|
|
Result as WebsiteJobResult,
|
|
|
|
|
)
|
2026-11-19 21:18:37 +01:00
|
|
|
|
|
|
|
|
REDISPATCH_AFTER = timedelta(minutes=5)
|
2026-11-21 12:35:24 +01:00
|
|
|
ACCOUNT_BACKUP_LEASE = timedelta(seconds=BACKUP_TIMEOUT_SECONDS + 60)
|
2026-11-21 14:54:51 +01:00
|
|
|
ACCOUNT_RESTORE_LEASE = timedelta(seconds=RESTORE_TIMEOUT_SECONDS + 60)
|
2026-11-21 12:35:24 +01:00
|
|
|
ACCOUNT_BACKUP_QUEUE = "coulobre-account-backups"
|
2026-11-21 14:54:51 +01:00
|
|
|
UNCONFIRMED_RESTORE = (
|
|
|
|
|
"Could not confirm the restore on the local backup node. "
|
|
|
|
|
"Check the destination before requesting another restore."
|
|
|
|
|
)
|
2026-11-21 01:17:01 +01:00
|
|
|
WEB_ACTIVATION_QUEUE = "coulobre-web-activation"
|
2026-11-21 10:31:32 +01:00
|
|
|
WEB_HTTPS_QUEUE = "coulobre-web-https"
|
2026-11-21 10:56:47 +01:00
|
|
|
WEB_SFTP_QUEUE = "coulobre-web-sftp"
|
2026-11-21 01:26:10 +01:00
|
|
|
DATABASE_PROVISIONING_QUEUE = "coulobre-database-provisioning"
|
2026-11-21 01:41:06 +01:00
|
|
|
DNS_PUBLISHING_QUEUE = "coulobre-dns-publishing"
|
2026-11-21 01:51:42 +01:00
|
|
|
MAILBOX_PROVISIONING_QUEUE = "coulobre-mailbox-provisioning"
|
2026-11-19 21:18:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
|
def dispatch_web_plans():
|
|
|
|
|
stale_before = timezone.now() - REDISPATCH_AFTER
|
|
|
|
|
pending_ids = (
|
|
|
|
|
WebProvisioningPlan.objects.filter(completed_at__isnull=True)
|
|
|
|
|
.filter(
|
2026-11-21 02:12:32 +01:00
|
|
|
Q(last_dispatched_at__isnull=True)
|
|
|
|
|
| Q(last_dispatched_at__lte=stale_before)
|
2026-11-19 21:18:37 +01:00
|
|
|
)
|
|
|
|
|
.order_by("requested_at", "pk")
|
|
|
|
|
.values_list("pk", flat=True)[:100]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for plan_id in list(pending_ids):
|
|
|
|
|
render_web_plan.apply_async(args=[plan_id], retry=False)
|
|
|
|
|
WebProvisioningPlan.objects.filter(
|
|
|
|
|
pk=plan_id, completed_at__isnull=True
|
|
|
|
|
).update(last_dispatched_at=timezone.now())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def render_web_plan(plan_id):
|
2026-11-21 02:12:32 +01:00
|
|
|
pending = WebProvisioningPlan.objects.filter(
|
|
|
|
|
pk=plan_id, completed_at__isnull=True
|
|
|
|
|
)
|
2026-11-19 21:18:37 +01:00
|
|
|
plan = pending.first()
|
|
|
|
|
if plan is None:
|
|
|
|
|
return
|
|
|
|
|
|
2026-11-21 02:12:32 +01:00
|
|
|
apache_config = WebSite(plan.account_id, plan.primary_domain).render(
|
|
|
|
|
"apache"
|
|
|
|
|
)
|
2026-11-19 21:18:37 +01:00
|
|
|
pending.update(apache_config=apache_config, completed_at=timezone.now())
|
2026-11-21 01:17:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
|
def dispatch_web_activations():
|
2026-11-21 01:26:10 +01:00
|
|
|
if settings.LOCAL_WEB_ACTIVATION:
|
2026-11-22 12:06:10 +01:00
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
IndependentWebsite,
|
|
|
|
|
provision_independent_website,
|
|
|
|
|
WEB_ACTIVATION_QUEUE,
|
|
|
|
|
condition=~Q(operation=IndependentWebsite.Operation.HTTPS),
|
|
|
|
|
)
|
2026-11-21 01:26:10 +01:00
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
WebActivation, activate_web_plan, WEB_ACTIVATION_QUEUE
|
|
|
|
|
)
|
2026-11-21 15:32:02 +01:00
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
WebsiteAlias,
|
|
|
|
|
update_website_alias,
|
|
|
|
|
WEB_ACTIVATION_QUEUE,
|
|
|
|
|
operation__in=(
|
|
|
|
|
WebsiteAlias.Operation.ACTIVATE,
|
|
|
|
|
WebsiteAlias.Operation.REMOVE,
|
|
|
|
|
),
|
|
|
|
|
)
|
2026-11-21 01:26:10 +01:00
|
|
|
|
2026-11-21 16:04:33 +01:00
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
SitePhpSettings, set_site_php_settings, WEB_ACTIVATION_QUEUE
|
|
|
|
|
)
|
2026-11-21 23:57:07 +01:00
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
SitePhpVersion, set_site_php_version, WEB_ACTIVATION_QUEUE
|
|
|
|
|
)
|
2026-11-21 16:04:33 +01:00
|
|
|
|
2026-11-21 01:26:10 +01:00
|
|
|
|
2026-11-21 13:40:50 +01:00
|
|
|
@shared_task
|
|
|
|
|
def dispatch_site_storage_measurements():
|
|
|
|
|
if settings.LOCAL_WEB_ACTIVATION:
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
SiteStorageUsage, measure_site_storage, WEB_ACTIVATION_QUEUE
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 14:10:34 +01:00
|
|
|
@shared_task
|
|
|
|
|
def dispatch_site_storage_limits():
|
|
|
|
|
if settings.LOCAL_WEB_ACTIVATION:
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
SiteStorageLimit, set_site_storage_limit, WEB_ACTIVATION_QUEUE
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 10:31:32 +01:00
|
|
|
@shared_task
|
|
|
|
|
def dispatch_web_https():
|
|
|
|
|
if settings.LOCAL_WEB_HTTPS:
|
2026-11-22 12:06:10 +01:00
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
IndependentWebsite,
|
|
|
|
|
provision_independent_website,
|
|
|
|
|
WEB_HTTPS_QUEUE,
|
|
|
|
|
operation=IndependentWebsite.Operation.HTTPS,
|
|
|
|
|
)
|
2026-11-21 10:31:32 +01:00
|
|
|
dispatch_provisioning_jobs(WebHTTPS, enable_web_https, WEB_HTTPS_QUEUE)
|
2026-11-21 15:32:02 +01:00
|
|
|
if settings.LOCAL_WEB_ACTIVATION:
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
WebsiteAlias,
|
|
|
|
|
enable_website_alias_https,
|
|
|
|
|
WEB_HTTPS_QUEUE,
|
|
|
|
|
operation=WebsiteAlias.Operation.HTTPS,
|
|
|
|
|
)
|
2026-11-21 10:31:32 +01:00
|
|
|
|
|
|
|
|
|
2026-11-21 10:56:47 +01:00
|
|
|
@shared_task
|
|
|
|
|
def dispatch_web_sftp():
|
|
|
|
|
if settings.LOCAL_WEB_SFTP:
|
|
|
|
|
dispatch_provisioning_jobs(SiteSFTP, update_web_sftp, WEB_SFTP_QUEUE)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 01:26:10 +01:00
|
|
|
@shared_task
|
|
|
|
|
def dispatch_database_provisioning():
|
|
|
|
|
if settings.LOCAL_DATABASE_PROVISIONING:
|
|
|
|
|
dispatch_provisioning_jobs(
|
2026-11-21 02:12:32 +01:00
|
|
|
DatabaseProvisioning,
|
|
|
|
|
provision_database,
|
|
|
|
|
DATABASE_PROVISIONING_QUEUE,
|
2026-11-21 01:26:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 01:41:06 +01:00
|
|
|
@shared_task
|
|
|
|
|
def dispatch_dns_publications():
|
|
|
|
|
if settings.LOCAL_DNS_PUBLISHING:
|
2026-11-21 22:04:15 +01:00
|
|
|
if settings.MANAGE_MAIL_DNS:
|
|
|
|
|
domains = HostingMailDomain.objects.filter(
|
|
|
|
|
dns_sync_requested=True,
|
|
|
|
|
account__services_suspended=False,
|
|
|
|
|
account__retired_at__isnull=True,
|
|
|
|
|
).order_by("pk")[:100]
|
|
|
|
|
for mail_domain in domains:
|
|
|
|
|
sync_mail_dns(mail_domain)
|
2026-11-21 01:41:06 +01:00
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
DnsPublication, publish_dns_zone, DNS_PUBLISHING_QUEUE
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 01:51:42 +01:00
|
|
|
@shared_task
|
|
|
|
|
def dispatch_mailbox_provisioning():
|
|
|
|
|
if settings.LOCAL_MAILBOX_PROVISIONING:
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
MailboxProvisioning, provision_mailbox, MAILBOX_PROVISIONING_QUEUE
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-22 11:03:26 +01:00
|
|
|
if mail_policy_available("routing"):
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
MailRoutingPublication,
|
|
|
|
|
publish_mail_routing,
|
|
|
|
|
MAILBOX_PROVISIONING_QUEUE,
|
|
|
|
|
)
|
|
|
|
|
if mail_policy_available("vacation"):
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
MailVacationPublication,
|
|
|
|
|
publish_mail_vacation,
|
|
|
|
|
MAILBOX_PROVISIONING_QUEUE,
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-21 01:51:42 +01:00
|
|
|
|
2026-11-21 18:55:41 +01:00
|
|
|
@shared_task
|
|
|
|
|
def dispatch_account_service_changes():
|
|
|
|
|
queues = {
|
|
|
|
|
AccountServiceJob.Kind.WEB: WEB_ACTIVATION_QUEUE,
|
|
|
|
|
AccountServiceJob.Kind.SFTP: WEB_SFTP_QUEUE,
|
|
|
|
|
AccountServiceJob.Kind.DATABASE: DATABASE_PROVISIONING_QUEUE,
|
|
|
|
|
AccountServiceJob.Kind.MAILBOX: MAILBOX_PROVISIONING_QUEUE,
|
|
|
|
|
}
|
|
|
|
|
for kind, queue in queues.items():
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
AccountServiceJob, change_service_access, queue, kind=kind
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def change_service_access(job_id):
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
AccountServiceJob.objects.select_related("change"),
|
|
|
|
|
job_id,
|
|
|
|
|
apply_service_change,
|
|
|
|
|
"Could not confirm the service change. Check the node and retry.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 19:40:19 +01:00
|
|
|
@shared_task
|
|
|
|
|
def dispatch_account_retirements():
|
|
|
|
|
kind = AccountRetirementJob.Kind
|
|
|
|
|
queues = {
|
|
|
|
|
kind.ALIAS: WEB_ACTIVATION_QUEUE,
|
|
|
|
|
kind.DATABASE: DATABASE_PROVISIONING_QUEUE,
|
|
|
|
|
kind.MAILBOX: MAILBOX_PROVISIONING_QUEUE,
|
|
|
|
|
kind.DNS: DNS_PUBLISHING_QUEUE,
|
|
|
|
|
}
|
|
|
|
|
for service, queue in queues.items():
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
AccountRetirementJob, retire_account_resource, queue, kind=service
|
|
|
|
|
)
|
|
|
|
|
aliases = AccountRetirementJob.objects.filter(
|
|
|
|
|
retirement_id=OuterRef("retirement_id"), kind=kind.ALIAS
|
|
|
|
|
).filter(Q(finished_at__isnull=True) | ~Q(problem=""))
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
AccountRetirementJob,
|
|
|
|
|
retire_account_resource,
|
|
|
|
|
WEB_ACTIVATION_QUEUE,
|
|
|
|
|
kind=kind.WEB,
|
|
|
|
|
condition=~Exists(aliases),
|
|
|
|
|
)
|
|
|
|
|
unfinished = AccountRetirementJob.objects.filter(
|
|
|
|
|
retirement_id=OuterRef("pk")
|
|
|
|
|
).filter(Q(finished_at__isnull=True) | ~Q(problem=""))
|
|
|
|
|
releasable = (
|
|
|
|
|
AccountRetirement.objects.filter(
|
|
|
|
|
policy=AccountRetirement.Policy.RELEASE, released_at__isnull=True
|
|
|
|
|
)
|
|
|
|
|
.filter(~Exists(unfinished))
|
|
|
|
|
.values_list("account_id", flat=True)
|
|
|
|
|
)
|
|
|
|
|
for account_id in releasable[:100]:
|
|
|
|
|
finalize_retirement(account_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def retire_account_resource(job_id):
|
|
|
|
|
kind = AccountRetirementJob.Kind
|
|
|
|
|
aliases = AccountRetirementJob.objects.filter(
|
|
|
|
|
retirement_id=OuterRef("retirement_id"), kind=kind.ALIAS
|
|
|
|
|
).filter(Q(finished_at__isnull=True) | ~Q(problem=""))
|
|
|
|
|
jobs = AccountRetirementJob.objects.filter(
|
|
|
|
|
~Q(kind=kind.WEB) | ~Exists(aliases)
|
|
|
|
|
)
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
jobs,
|
|
|
|
|
job_id,
|
|
|
|
|
apply_retirement_job,
|
|
|
|
|
"Could not confirm the cleanup on the node. Check the node and retry.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 12:35:24 +01:00
|
|
|
@shared_task
|
|
|
|
|
def dispatch_account_backups():
|
|
|
|
|
if settings.LOCAL_ACCOUNT_BACKUPS:
|
2026-11-21 14:26:38 +01:00
|
|
|
queue_due_account_backups(timezone.now())
|
2026-11-21 12:35:24 +01:00
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
AccountBackupJob,
|
|
|
|
|
create_account_backup,
|
|
|
|
|
ACCOUNT_BACKUP_QUEUE,
|
|
|
|
|
lease=ACCOUNT_BACKUP_LEASE,
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-21 14:42:38 +01:00
|
|
|
if settings.LOCAL_WEBSITE_RESTORE:
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
WebsiteRestoreJob,
|
|
|
|
|
restore_website,
|
|
|
|
|
ACCOUNT_BACKUP_QUEUE,
|
2026-11-21 14:54:51 +01:00
|
|
|
lease=ACCOUNT_RESTORE_LEASE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
kinds = enabled_component_restores()
|
|
|
|
|
if kinds:
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
ComponentRestoreJob,
|
|
|
|
|
restore_backup_component,
|
|
|
|
|
ACCOUNT_BACKUP_QUEUE,
|
|
|
|
|
lease=ACCOUNT_RESTORE_LEASE,
|
|
|
|
|
kind__in=kinds,
|
2026-11-21 14:42:38 +01:00
|
|
|
)
|
|
|
|
|
|
2026-11-21 15:08:48 +01:00
|
|
|
if settings.LOCAL_BACKUP_RETENTION:
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
AccountBackupExpiration,
|
|
|
|
|
expire_account_backup,
|
|
|
|
|
ACCOUNT_BACKUP_QUEUE,
|
|
|
|
|
lease=ACCOUNT_BACKUP_LEASE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
|
def plan_account_backup_expirations():
|
|
|
|
|
if settings.LOCAL_ACCOUNT_BACKUPS and settings.LOCAL_BACKUP_RETENTION:
|
|
|
|
|
plan_backup_expirations(timezone.now())
|
|
|
|
|
|
2026-11-21 12:35:24 +01:00
|
|
|
|
2026-11-22 16:14:55 +01:00
|
|
|
@shared_task
|
|
|
|
|
def dispatch_offsite_backups():
|
|
|
|
|
if settings.LOCAL_ACCOUNT_BACKUPS and settings.OFFSITE_BACKUP_DESTINATIONS:
|
|
|
|
|
now = timezone.now()
|
|
|
|
|
finish_interrupted_transfers(now)
|
|
|
|
|
dispatch_provisioning_jobs(
|
|
|
|
|
OffsiteBackupTransfer,
|
|
|
|
|
transfer_offsite_backup,
|
|
|
|
|
OFFSITE_BACKUP_QUEUE,
|
|
|
|
|
lease=OFFSITE_BACKUP_LEASE,
|
|
|
|
|
attempts__lt=MAX_OFFSITE_ATTEMPTS,
|
|
|
|
|
**transfer_scope(now),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def transfer_offsite_backup(job_id):
|
|
|
|
|
if settings.LOCAL_ACCOUNT_BACKUPS and settings.OFFSITE_BACKUP_DESTINATIONS:
|
|
|
|
|
run_offsite_transfer(job_id)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 14:54:51 +01:00
|
|
|
def dispatch_provisioning_jobs(
|
2026-11-21 19:40:19 +01:00
|
|
|
model, task, queue, *, lease=REDISPATCH_AFTER, condition=None, **scope
|
2026-11-21 14:54:51 +01:00
|
|
|
):
|
2026-11-21 12:35:24 +01:00
|
|
|
now = timezone.now()
|
|
|
|
|
stale_before = now - REDISPATCH_AFTER
|
2026-11-21 14:54:51 +01:00
|
|
|
waiting = model.objects.filter(finished_at__isnull=True, **scope)
|
2026-11-21 19:40:19 +01:00
|
|
|
if condition is not None:
|
|
|
|
|
waiting = waiting.filter(condition)
|
2026-11-21 01:17:01 +01:00
|
|
|
pending = (
|
|
|
|
|
waiting.filter(
|
2026-11-21 02:12:32 +01:00
|
|
|
Q(last_dispatched_at__isnull=True)
|
|
|
|
|
| Q(last_dispatched_at__lte=stale_before)
|
2026-11-21 01:17:01 +01:00
|
|
|
)
|
2026-11-21 12:35:24 +01:00
|
|
|
.filter(Q(started_at__isnull=True) | Q(started_at__lte=now - lease))
|
2026-11-21 01:17:01 +01:00
|
|
|
.order_by("requested_at", "pk")
|
|
|
|
|
.values_list("pk", "requested_at")[:100]
|
|
|
|
|
)
|
2026-11-21 01:26:10 +01:00
|
|
|
for job_id, requested_at in list(pending):
|
|
|
|
|
task.apply_async(args=[job_id], queue=queue, retry=False)
|
|
|
|
|
waiting.filter(pk=job_id, requested_at=requested_at).update(
|
2026-11-21 01:17:01 +01:00
|
|
|
last_dispatched_at=timezone.now()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def activate_web_plan(activation_id):
|
2026-11-21 01:26:10 +01:00
|
|
|
if settings.LOCAL_WEB_ACTIVATION:
|
|
|
|
|
run_provisioning_job(
|
2026-11-21 23:57:07 +01:00
|
|
|
WebActivation.objects.select_related(
|
|
|
|
|
"plan__account__hosting_plan"
|
|
|
|
|
),
|
2026-11-21 01:26:10 +01:00
|
|
|
activation_id,
|
|
|
|
|
apply_web_activation,
|
2026-11-21 02:12:32 +01:00
|
|
|
"Could not complete the request to the local web node. "
|
|
|
|
|
"Check its socket and try again.",
|
2026-11-21 01:26:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_web_activation(activation):
|
|
|
|
|
plan = activation.plan
|
|
|
|
|
if not plan.can_export:
|
|
|
|
|
raise ValueError(
|
2026-11-21 02:12:32 +01:00
|
|
|
"Request a completed Apache preview for the current "
|
|
|
|
|
"account domain."
|
2026-11-21 01:26:10 +01:00
|
|
|
)
|
2026-11-21 16:04:33 +01:00
|
|
|
site = WebSite(plan.account_id, plan.primary_domain)
|
2026-11-21 23:57:07 +01:00
|
|
|
options = (
|
|
|
|
|
PhpPoolSettings.from_manifest(activation.php_settings)
|
|
|
|
|
if activation.php_settings is not None
|
|
|
|
|
else PhpPoolSettings()
|
|
|
|
|
)
|
|
|
|
|
applied, version = activate_local_site(
|
|
|
|
|
site, options, plan_php_versions(plan.account)
|
|
|
|
|
)
|
|
|
|
|
return {"php_settings": applied.manifest(), "applied_php_version": version}
|
2026-11-21 01:26:10 +01:00
|
|
|
|
|
|
|
|
|
2026-11-21 15:32:02 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def update_website_alias(alias_id):
|
|
|
|
|
if settings.LOCAL_WEB_ACTIVATION:
|
|
|
|
|
jobs = WebsiteAlias.objects.select_related(
|
|
|
|
|
"reservation__account"
|
|
|
|
|
).exclude(operation=WebsiteAlias.Operation.HTTPS)
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
jobs,
|
|
|
|
|
alias_id,
|
|
|
|
|
apply_website_alias,
|
|
|
|
|
"Could not contact the local web node. "
|
|
|
|
|
"Check its socket and try again.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_website_alias(job):
|
|
|
|
|
alias = queued_website_alias(job)
|
|
|
|
|
if job.operation == WebsiteAlias.Operation.REMOVE:
|
|
|
|
|
update_local_website_alias(alias, "remove_alias")
|
|
|
|
|
with transaction.atomic():
|
|
|
|
|
reservation = job.reservation
|
|
|
|
|
job.delete()
|
|
|
|
|
reservation.delete()
|
|
|
|
|
return {}
|
|
|
|
|
update_local_website_alias(alias, "apply_alias")
|
|
|
|
|
return {"http_active": True}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def enable_website_alias_https(alias_id):
|
|
|
|
|
if settings.LOCAL_WEB_ACTIVATION and settings.LOCAL_WEB_HTTPS:
|
|
|
|
|
jobs = WebsiteAlias.objects.select_related(
|
|
|
|
|
"reservation__account"
|
|
|
|
|
).filter(operation=WebsiteAlias.Operation.HTTPS)
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
jobs,
|
|
|
|
|
alias_id,
|
|
|
|
|
apply_alias_https,
|
|
|
|
|
"Could not contact the local HTTPS service. "
|
|
|
|
|
"Check its socket and try again.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_alias_https(job):
|
|
|
|
|
enable_local_https(queued_website_alias(job))
|
|
|
|
|
return {"https_active": True}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def queued_website_alias(job):
|
|
|
|
|
account = job.reservation.account
|
|
|
|
|
if (
|
|
|
|
|
job.operation != WebsiteAlias.Operation.REMOVE
|
|
|
|
|
and job.primary_domain != account.primary_domain
|
|
|
|
|
):
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"The primary domain changed before the alias request ran."
|
|
|
|
|
)
|
|
|
|
|
return WebAlias(
|
|
|
|
|
WebSite(account.pk, job.primary_domain), job.reservation.domain
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 16:04:33 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def set_site_php_settings(job_id):
|
|
|
|
|
if settings.LOCAL_WEB_ACTIVATION:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
SitePhpSettings.objects.select_related("account"),
|
|
|
|
|
job_id,
|
|
|
|
|
apply_site_php_settings,
|
|
|
|
|
"Could not contact the local web node. "
|
|
|
|
|
"Check its socket and try again.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_site_php_settings(job):
|
|
|
|
|
if job.domain != job.account.primary_domain:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"Request PHP settings for the current account domain."
|
|
|
|
|
)
|
|
|
|
|
site = WebSite(job.account_id, job.domain)
|
|
|
|
|
options = PhpPoolSettings.from_manifest(job.requested_settings)
|
|
|
|
|
applied = update_local_php_settings(site, options)
|
|
|
|
|
return {
|
|
|
|
|
"applied_settings": applied.manifest(),
|
|
|
|
|
"applied_at": timezone.now(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 23:57:07 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def set_site_php_version(job_id):
|
|
|
|
|
if settings.LOCAL_WEB_ACTIVATION:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
SitePhpVersion.objects.select_related("account__hosting_plan"),
|
|
|
|
|
job_id,
|
|
|
|
|
apply_site_php_version,
|
|
|
|
|
"Could not confirm the PHP change on the web node. "
|
|
|
|
|
"Retry the request.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_site_php_version(job):
|
|
|
|
|
account = job.account
|
|
|
|
|
if account.retired_at or account.services_suspended:
|
|
|
|
|
raise ValueError("The hosting account is suspended or retired.")
|
|
|
|
|
if job.domain != account.primary_domain or not site_is_active(account):
|
|
|
|
|
raise ValueError("Activate the current website before changing PHP.")
|
|
|
|
|
versions = plan_php_versions(account)
|
|
|
|
|
if versions and job.requested_version not in versions:
|
|
|
|
|
raise ValueError("The hosting plan no longer allows this PHP series.")
|
|
|
|
|
site = WebSite(job.account_id, job.domain)
|
|
|
|
|
return {
|
|
|
|
|
"applied_version": update_local_php_version(
|
|
|
|
|
site, job.requested_version
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 13:40:50 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def measure_site_storage(request_id):
|
|
|
|
|
if settings.LOCAL_WEB_ACTIVATION:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
SiteStorageUsage.objects.select_related("account"),
|
|
|
|
|
request_id,
|
|
|
|
|
apply_storage_measurement,
|
|
|
|
|
"Could not contact the local web node. "
|
|
|
|
|
"Check its socket and try again.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_storage_measurement(request):
|
|
|
|
|
if request.domain != request.account.primary_domain:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"Request a measurement for the current account domain."
|
|
|
|
|
)
|
|
|
|
|
used_bytes = measure_local_site_storage(
|
|
|
|
|
WebSite(request.account_id, request.domain)
|
|
|
|
|
)
|
|
|
|
|
return {"used_bytes": used_bytes, "measured_at": timezone.now()}
|
2026-11-21 14:10:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def set_site_storage_limit(request_id):
|
|
|
|
|
if settings.LOCAL_WEB_ACTIVATION:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
SiteStorageLimit.objects.select_related("account"),
|
|
|
|
|
request_id,
|
|
|
|
|
apply_site_storage_limit,
|
|
|
|
|
"Could not contact the local web node. "
|
|
|
|
|
"Check its socket and try again.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_site_storage_limit(request):
|
|
|
|
|
if request.domain != request.account.primary_domain:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"Request a storage limit for the current account domain."
|
|
|
|
|
)
|
|
|
|
|
site = WebSite(request.account_id, request.domain)
|
|
|
|
|
set_local_site_storage_limit(site, request.limit_mib)
|
|
|
|
|
return {"applied_mib": request.limit_mib, "applied_at": timezone.now()}
|
2026-11-21 13:40:50 +01:00
|
|
|
|
|
|
|
|
|
2026-11-21 10:31:32 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def enable_web_https(request_id):
|
|
|
|
|
if settings.LOCAL_WEB_HTTPS:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
WebHTTPS.objects.select_related(
|
|
|
|
|
"plan__account", "plan__activation"
|
|
|
|
|
),
|
|
|
|
|
request_id,
|
|
|
|
|
apply_web_https,
|
|
|
|
|
"Could not contact the local HTTPS service. "
|
|
|
|
|
"Check its socket and try again.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_web_https(provisioning):
|
|
|
|
|
plan = provisioning.plan
|
|
|
|
|
if not plan.can_enable_https:
|
|
|
|
|
raise ValueError("Activate the site for the current domain first.")
|
|
|
|
|
enable_local_https(WebSite(plan.account_id, plan.primary_domain))
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 10:56:47 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def update_web_sftp(request_id):
|
|
|
|
|
if settings.LOCAL_WEB_SFTP:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
SiteSFTP.objects.select_related("account__customer__user"),
|
|
|
|
|
request_id,
|
|
|
|
|
apply_web_sftp,
|
|
|
|
|
"Could not contact the local SFTP service. "
|
|
|
|
|
"Check its socket and try again.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_web_sftp(request):
|
|
|
|
|
account = request.account
|
|
|
|
|
if request.requested_customer_id is not None and (
|
|
|
|
|
request.requested_customer_id != account.customer_id
|
|
|
|
|
or not account.customer.user.is_active
|
|
|
|
|
):
|
|
|
|
|
raise ValueError("The customer no longer has access to this account.")
|
|
|
|
|
if request.public_keys and (
|
|
|
|
|
request.domain != account.primary_domain or not site_is_active(account)
|
|
|
|
|
):
|
|
|
|
|
raise ValueError("Activate the site for the current domain first.")
|
|
|
|
|
site = WebSite(request.account_id, request.domain)
|
|
|
|
|
update_local_sftp(SFTPKeys(site, request.public_keys))
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 01:26:10 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def provision_database(provisioning_id):
|
|
|
|
|
if settings.LOCAL_DATABASE_PROVISIONING:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
DatabaseProvisioning.objects.select_related("database"),
|
|
|
|
|
provisioning_id,
|
|
|
|
|
apply_database_provisioning,
|
2026-11-21 02:12:32 +01:00
|
|
|
"Could not complete the request to the local MariaDB node. "
|
|
|
|
|
"Check its socket and try again.",
|
2026-11-21 01:26:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_database_provisioning(provisioning):
|
|
|
|
|
provision_local_database(provisioning.database.spec)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 01:41:06 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def publish_dns_zone(publication_id):
|
|
|
|
|
if settings.LOCAL_DNS_PUBLISHING:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
DnsPublication.objects.all(),
|
|
|
|
|
publication_id,
|
|
|
|
|
apply_dns_publication,
|
2026-11-21 17:32:52 +01:00
|
|
|
"Could not complete the request to the BIND9 node. "
|
|
|
|
|
"Check the node connection and try again.",
|
2026-11-21 01:41:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_dns_publication(publication):
|
|
|
|
|
zone = DnsZone.from_manifest(publication.manifest)
|
|
|
|
|
publish_local_zone(zone)
|
|
|
|
|
return {"published_serial": zone.serial}
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 01:51:42 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def provision_mailbox(provisioning_id):
|
|
|
|
|
if settings.LOCAL_MAILBOX_PROVISIONING:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
MailboxProvisioning.objects.select_related("mailbox__mail_domain"),
|
|
|
|
|
provisioning_id,
|
|
|
|
|
apply_mailbox_provisioning,
|
2026-11-21 17:32:52 +01:00
|
|
|
"Could not complete the request to the mail node. "
|
|
|
|
|
"Check the node connection and try again.",
|
2026-11-21 01:51:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_mailbox_provisioning(provisioning):
|
2026-11-21 13:32:08 +01:00
|
|
|
if provisioning.quota_update:
|
|
|
|
|
update_local_mailbox_quota(provisioning.mailbox.spec)
|
2026-11-22 17:51:05 +01:00
|
|
|
elif settings.MANAGE_MAIL_DNS or settings.MAIL_NODE_DKIM:
|
2026-11-21 22:04:15 +01:00
|
|
|
public_key = provision_signing_mailbox(provisioning.mailbox.spec)
|
2026-11-22 17:51:05 +01:00
|
|
|
domain = provisioning.mailbox.mail_domain
|
|
|
|
|
if settings.MANAGE_MAIL_DNS:
|
|
|
|
|
sync_mail_dns(domain, public_key=public_key)
|
|
|
|
|
else:
|
|
|
|
|
HostingMailDomain.objects.filter(pk=domain.pk).update(
|
|
|
|
|
dkim_record=public_key
|
|
|
|
|
)
|
2026-11-21 13:32:08 +01:00
|
|
|
else:
|
|
|
|
|
provision_local_mailbox(provisioning.mailbox.spec)
|
2026-11-21 01:51:42 +01:00
|
|
|
|
|
|
|
|
|
2026-11-22 11:03:26 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def publish_mail_routing(job_id):
|
|
|
|
|
if mail_policy_available("routing"):
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
MailRoutingPublication.objects.select_related(
|
|
|
|
|
"mail_domain__account__hosting_plan"
|
|
|
|
|
),
|
|
|
|
|
job_id,
|
|
|
|
|
apply_mail_routing_job,
|
|
|
|
|
"Could not reach the mail node to apply routing. Try again.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_mail_routing_job(job):
|
|
|
|
|
return apply_policy(job, job.mail_domain)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def publish_mail_vacation(job_id):
|
|
|
|
|
if mail_policy_available("vacation"):
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
MailVacationPublication.objects.select_related(
|
|
|
|
|
"mailbox__mail_domain__account"
|
|
|
|
|
),
|
|
|
|
|
job_id,
|
|
|
|
|
apply_mail_vacation_job,
|
|
|
|
|
"Could not reach the mail node to apply replies. Try again.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_mail_vacation_job(job):
|
|
|
|
|
return apply_policy(job, job.mailbox)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 12:35:24 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def create_account_backup(job_id):
|
|
|
|
|
if settings.LOCAL_ACCOUNT_BACKUPS:
|
|
|
|
|
run_provisioning_job(
|
2026-11-21 22:18:59 +01:00
|
|
|
AccountBackupJob.objects.select_related("account"),
|
2026-11-21 12:35:24 +01:00
|
|
|
job_id,
|
|
|
|
|
apply_account_backup,
|
|
|
|
|
"Could not complete the request to the local backup node. "
|
|
|
|
|
"Check its socket and try again.",
|
|
|
|
|
lease=ACCOUNT_BACKUP_LEASE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_account_backup(job):
|
|
|
|
|
backup = AccountBackup.from_manifest(job.manifest)
|
|
|
|
|
if backup.website.account_id != job.account_id:
|
|
|
|
|
raise ValueError("The backup manifest belongs to a different account.")
|
2026-11-22 12:06:10 +01:00
|
|
|
current_sites = {website.spec for website in active_websites(job.account)}
|
|
|
|
|
if set(backup.additional_websites) - current_sites:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"An additional website is no longer available for backup."
|
|
|
|
|
)
|
2026-11-21 22:18:59 +01:00
|
|
|
if backup.mailboxes is not None:
|
|
|
|
|
if job.remote_mail:
|
|
|
|
|
if not settings.REMOTE_ACCOUNT_BACKUP_MAIL:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"Enable remote mailbox backups before trying again."
|
|
|
|
|
)
|
|
|
|
|
current = current_restore_resources(
|
|
|
|
|
job.account, RestoreKind.MAILBOX
|
|
|
|
|
)
|
|
|
|
|
if any(
|
|
|
|
|
mailbox.address not in current for mailbox in backup.mailboxes
|
|
|
|
|
):
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"A mailbox no longer belongs to this account."
|
|
|
|
|
)
|
|
|
|
|
elif not settings.LOCAL_ACCOUNT_BACKUP_MAIL:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"Enable local mailbox backups before trying again."
|
|
|
|
|
)
|
2026-11-21 15:08:48 +01:00
|
|
|
size = create_local_account_backup(
|
2026-11-21 22:18:59 +01:00
|
|
|
AccountBackupRequest(
|
|
|
|
|
job.backup_id, backup, remote_mail=job.remote_mail
|
|
|
|
|
)
|
2026-11-21 15:08:48 +01:00
|
|
|
)
|
|
|
|
|
return {"archive_bytes": size}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UnconfirmedArchiveExpirationError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def expire_account_backup(job_id):
|
|
|
|
|
if settings.LOCAL_ACCOUNT_BACKUPS and settings.LOCAL_BACKUP_RETENTION:
|
|
|
|
|
try:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
AccountBackupExpiration.objects.select_related("backup"),
|
|
|
|
|
job_id,
|
|
|
|
|
apply_backup_expiration,
|
|
|
|
|
"Could not finish archive expiration.",
|
|
|
|
|
lease=ACCOUNT_BACKUP_LEASE,
|
|
|
|
|
)
|
|
|
|
|
except UnconfirmedArchiveExpirationError:
|
|
|
|
|
# The same plan is dispatched again after its lease expires.
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_backup_expiration(job):
|
|
|
|
|
backup = job.backup
|
|
|
|
|
request = ArchiveExpirationRequest(
|
|
|
|
|
backup.account_id,
|
|
|
|
|
backup.backup_id,
|
|
|
|
|
tuple(UUID(value) for value in job.keeper_ids),
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
reason = expire_local_account_backup(request)
|
|
|
|
|
except (OSError, ValueError):
|
|
|
|
|
AccountBackupExpiration.objects.filter(
|
|
|
|
|
pk=job.pk, started_at=job.started_at, finished_at__isnull=True
|
|
|
|
|
).update(
|
|
|
|
|
problem="Could not confirm expiration. The worker will retry."
|
|
|
|
|
)
|
|
|
|
|
raise UnconfirmedArchiveExpirationError from None
|
|
|
|
|
if reason:
|
|
|
|
|
raise ValueError(reason)
|
|
|
|
|
AccountBackupJob.objects.filter(
|
|
|
|
|
pk=backup.pk, expired_at__isnull=True
|
|
|
|
|
).update(expired_at=timezone.now())
|
2026-11-21 12:35:24 +01:00
|
|
|
|
|
|
|
|
|
2026-11-21 14:42:38 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def restore_website(job_id):
|
|
|
|
|
if settings.LOCAL_ACCOUNT_BACKUPS and settings.LOCAL_WEBSITE_RESTORE:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
WebsiteRestoreJob.objects.select_related("backup__account"),
|
|
|
|
|
job_id,
|
|
|
|
|
apply_website_restore,
|
2026-11-21 14:54:51 +01:00
|
|
|
UNCONFIRMED_RESTORE,
|
|
|
|
|
lease=ACCOUNT_RESTORE_LEASE,
|
2026-11-21 14:42:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_website_restore(job):
|
|
|
|
|
account = job.backup.account
|
2026-11-22 12:06:10 +01:00
|
|
|
site = (
|
|
|
|
|
WebSite.from_manifest(job.site_manifest)
|
|
|
|
|
if job.site_manifest is not None
|
|
|
|
|
else WebSite(account.pk, account.primary_domain)
|
|
|
|
|
)
|
|
|
|
|
if (
|
|
|
|
|
available_website_restore_targets(job.backup).get(site.node_key)
|
|
|
|
|
!= site
|
|
|
|
|
):
|
2026-11-21 14:42:38 +01:00
|
|
|
raise ValueError("The backup no longer matches the current website.")
|
|
|
|
|
restore_local_website(
|
|
|
|
|
WebsiteRestoreRequest(
|
|
|
|
|
job.restore_id,
|
|
|
|
|
job.backup.backup_id,
|
|
|
|
|
site,
|
|
|
|
|
job.filename or None,
|
|
|
|
|
job.replace,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-21 14:54:51 +01:00
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def restore_backup_component(job_id):
|
|
|
|
|
kinds = enabled_component_restores()
|
|
|
|
|
if settings.LOCAL_ACCOUNT_BACKUPS and kinds:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
ComponentRestoreJob.objects.filter(kind__in=kinds).select_related(
|
|
|
|
|
"backup__account"
|
|
|
|
|
),
|
|
|
|
|
job_id,
|
|
|
|
|
apply_component_restore,
|
|
|
|
|
UNCONFIRMED_RESTORE,
|
|
|
|
|
lease=ACCOUNT_RESTORE_LEASE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_component_restore(job):
|
|
|
|
|
backup = job.backup
|
|
|
|
|
resources = available_restore_resources(
|
|
|
|
|
backup, job.kind, current_restore_resources(backup.account, job.kind)
|
|
|
|
|
)
|
|
|
|
|
target = resources.get(job.resource)
|
|
|
|
|
if target is None:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"The backup or its restore target is no longer available."
|
|
|
|
|
)
|
2026-11-21 22:18:59 +01:00
|
|
|
if job.kind == RestoreKind.MAILBOX:
|
|
|
|
|
enabled = (
|
|
|
|
|
settings.REMOTE_MAILBOX_RESTORE
|
|
|
|
|
if job.remote_mail
|
|
|
|
|
else settings.LOCAL_MAILBOX_RESTORE
|
2026-11-21 14:54:51 +01:00
|
|
|
)
|
2026-11-21 22:18:59 +01:00
|
|
|
if not enabled:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"Re-enable the requested mailbox restore transport."
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
restore_local_component(
|
|
|
|
|
ComponentRestoreRequest(
|
|
|
|
|
job.restore_id,
|
|
|
|
|
backup.backup_id,
|
|
|
|
|
backup.account_id,
|
|
|
|
|
target,
|
|
|
|
|
remote_mail=job.remote_mail,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
except (OSError, NodeReplyError):
|
|
|
|
|
if job.remote_mail:
|
|
|
|
|
raise ValueError(UNCONFIRMED_REMOTE_RESTORE) from None
|
|
|
|
|
raise
|
2026-11-21 14:54:51 +01:00
|
|
|
|
|
|
|
|
|
2026-11-21 12:35:24 +01:00
|
|
|
def run_provisioning_job(
|
|
|
|
|
jobs, job_id, apply, connection_problem, *, lease=REDISPATCH_AFTER
|
|
|
|
|
):
|
2026-11-21 01:17:01 +01:00
|
|
|
started_at = timezone.now()
|
2026-11-21 01:26:10 +01:00
|
|
|
pending = jobs.filter(pk=job_id, finished_at__isnull=True)
|
2026-11-21 01:17:01 +01:00
|
|
|
claim = pending.filter(
|
2026-11-21 12:35:24 +01:00
|
|
|
Q(started_at__isnull=True) | Q(started_at__lte=started_at - lease)
|
2026-11-21 01:17:01 +01:00
|
|
|
)
|
|
|
|
|
if not claim.update(started_at=started_at):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
running = pending.filter(started_at=started_at)
|
2026-11-21 01:26:10 +01:00
|
|
|
job = running.first()
|
|
|
|
|
if job is None:
|
2026-11-21 01:17:01 +01:00
|
|
|
return
|
|
|
|
|
problem = ""
|
2026-11-21 01:41:06 +01:00
|
|
|
updates = {}
|
2026-11-21 01:26:10 +01:00
|
|
|
try:
|
2026-11-21 01:41:06 +01:00
|
|
|
updates = apply(job) or {}
|
2026-11-21 01:26:10 +01:00
|
|
|
except OSError:
|
|
|
|
|
problem = connection_problem
|
|
|
|
|
except ValueError as error:
|
|
|
|
|
problem = str(error)
|
2026-11-21 01:41:06 +01:00
|
|
|
running.update(finished_at=timezone.now(), problem=problem, **updates)
|
2026-11-22 11:30:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
|
def expire_sign_in_windows():
|
|
|
|
|
SignInWindow.objects.filter(slot__lt=current_window() - 1).delete()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
|
def send_customer_password_reset(email):
|
|
|
|
|
send_customer_reset_email(email)
|
2026-11-22 12:06:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def provision_independent_website(job_id):
|
|
|
|
|
if settings.LOCAL_WEB_ACTIVATION:
|
|
|
|
|
run_provisioning_job(
|
|
|
|
|
IndependentWebsite.objects.select_related(
|
|
|
|
|
"reservation__account__hosting_plan"
|
|
|
|
|
),
|
|
|
|
|
job_id,
|
|
|
|
|
apply_website_operation,
|
|
|
|
|
"Could not complete the request to the web node. Try again.",
|
|
|
|
|
)
|
2026-11-22 15:38:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
|
def dispatch_website_jobs():
|
|
|
|
|
if not settings.WEBSITE_JOBS:
|
|
|
|
|
return
|
|
|
|
|
now = timezone.now()
|
|
|
|
|
recover_interrupted_runs(now)
|
|
|
|
|
due = due_website_jobs(now).values_list("pk", "account_id", "next_run_at")
|
|
|
|
|
for job_id, account_id, next_run in list(due[:100]):
|
|
|
|
|
execute_website_job.apply_async(
|
|
|
|
|
args=[account_id, job_id, next_run.isoformat()],
|
|
|
|
|
queue=JOB_QUEUE,
|
|
|
|
|
expires=90,
|
|
|
|
|
retry=False,
|
|
|
|
|
)
|
|
|
|
|
WebsiteScheduledJob.objects.filter(
|
|
|
|
|
pk=job_id, next_run_at=next_run
|
|
|
|
|
).update(dispatched_at=now)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
|
|
|
|
def execute_website_job(account_id, job_id, scheduled_for):
|
|
|
|
|
if not settings.WEBSITE_JOBS:
|
|
|
|
|
return
|
|
|
|
|
request = claim_website_job(
|
|
|
|
|
account_id, job_id, datetime.fromisoformat(scheduled_for)
|
|
|
|
|
)
|
|
|
|
|
if request is None:
|
|
|
|
|
return
|
|
|
|
|
try:
|
|
|
|
|
result, code = run_website_job(request)
|
|
|
|
|
except (OSError, NodeReplyError):
|
|
|
|
|
result, code = WebsiteJobResult.UNCONFIRMED, None
|
|
|
|
|
except ValueError:
|
|
|
|
|
result, code = WebsiteJobResult.FAILED, None
|
|
|
|
|
save_website_job_result(job_id, request.run_id, result, code)
|
2026-11-22 17:51:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(queue="coulobre-mailbox-provisioning")
|
|
|
|
|
def refresh_mail_queue(domain_id, requested_at):
|
|
|
|
|
from coulobre.mail_queue import QUEUE_NAMES, MailQueueRequest
|
|
|
|
|
|
|
|
|
|
from .node_client import apply_on_local_node
|
|
|
|
|
|
|
|
|
|
domain = HostingMailDomain.objects.filter(
|
|
|
|
|
pk=domain_id,
|
|
|
|
|
account__retired_at__isnull=True,
|
|
|
|
|
queue_requested_at=requested_at,
|
|
|
|
|
).first()
|
|
|
|
|
if domain is None or not settings.LOCAL_MAILBOX_PROVISIONING:
|
|
|
|
|
return
|
|
|
|
|
try:
|
|
|
|
|
report = apply_on_local_node(
|
|
|
|
|
MailQueueRequest(domain.domain),
|
|
|
|
|
"/run/coulobre-mail-apply.sock",
|
|
|
|
|
"mail node",
|
|
|
|
|
"queue summary",
|
|
|
|
|
timeout=20,
|
|
|
|
|
)
|
|
|
|
|
counts = report["counts"]
|
|
|
|
|
if (
|
|
|
|
|
set(counts) != set(QUEUE_NAMES)
|
|
|
|
|
or any(
|
|
|
|
|
type(value) is not int or not 0 <= value <= 20000
|
|
|
|
|
for value in counts.values()
|
|
|
|
|
)
|
|
|
|
|
or type(report["partial"]) is not bool
|
|
|
|
|
):
|
|
|
|
|
raise ValueError
|
|
|
|
|
snapshot = {"counts": counts, "partial": report["partial"]}
|
|
|
|
|
except (KeyError, TypeError, ValueError, OSError):
|
|
|
|
|
snapshot = {"error": "Could not read the queue from the mail node."}
|
|
|
|
|
HostingMailDomain.objects.filter(
|
|
|
|
|
pk=domain_id, queue_requested_at=requested_at
|
|
|
|
|
).update(
|
|
|
|
|
queue_report=snapshot,
|
|
|
|
|
queue_observed_at=timezone.now(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(queue="coulobre-web-activation")
|
|
|
|
|
def collect_website_usage(snapshot_id, requested_at):
|
|
|
|
|
from .models import WebsiteUsage
|
|
|
|
|
from .node_client import apply_on_local_node
|
|
|
|
|
|
|
|
|
|
if not settings.WEBSITE_USAGE:
|
|
|
|
|
return
|
|
|
|
|
snapshot = (
|
|
|
|
|
WebsiteUsage.objects.select_related("account")
|
|
|
|
|
.filter(
|
|
|
|
|
pk=snapshot_id,
|
|
|
|
|
requested_at=requested_at,
|
|
|
|
|
account__retired_at__isnull=True,
|
|
|
|
|
)
|
|
|
|
|
.first()
|
|
|
|
|
)
|
|
|
|
|
if snapshot is None:
|
|
|
|
|
return
|
|
|
|
|
try:
|
|
|
|
|
if snapshot.node_key == str(snapshot.account_id):
|
|
|
|
|
if snapshot.account.primary_domain != snapshot.domain:
|
|
|
|
|
return
|
|
|
|
|
site = WebSite(snapshot.account_id, snapshot.domain)
|
|
|
|
|
else:
|
|
|
|
|
website = (
|
|
|
|
|
active_websites(snapshot.account)
|
|
|
|
|
.filter(
|
|
|
|
|
pk=int(snapshot.node_key.removeprefix("s")),
|
|
|
|
|
domain=snapshot.domain,
|
|
|
|
|
)
|
|
|
|
|
.first()
|
|
|
|
|
)
|
|
|
|
|
if website is None:
|
|
|
|
|
return
|
|
|
|
|
site = website.spec
|
|
|
|
|
response = apply_on_local_node(
|
|
|
|
|
site,
|
|
|
|
|
"/run/coulobre-website-usage.sock",
|
|
|
|
|
"web node",
|
|
|
|
|
"website usage",
|
|
|
|
|
timeout=20,
|
|
|
|
|
)
|
|
|
|
|
report = {
|
|
|
|
|
name: response[name]
|
|
|
|
|
for name in (
|
|
|
|
|
"month",
|
|
|
|
|
"storage_bytes",
|
|
|
|
|
"traffic_bytes",
|
|
|
|
|
"access_lines",
|
|
|
|
|
"error_lines",
|
|
|
|
|
"partial",
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
datetime.strptime(report["month"], "%Y-%m")
|
|
|
|
|
for field in ("storage_bytes", "traffic_bytes"):
|
|
|
|
|
size = report[field]
|
|
|
|
|
if (field != "traffic_bytes" or size is not None) and (
|
|
|
|
|
type(size) is not int or not 0 <= size < 2**63
|
|
|
|
|
):
|
|
|
|
|
raise ValueError
|
|
|
|
|
for field in ("access_lines", "error_lines"):
|
|
|
|
|
lines = report[field]
|
|
|
|
|
if (
|
|
|
|
|
not isinstance(lines, list)
|
|
|
|
|
or len(lines) > 8
|
|
|
|
|
or any(
|
|
|
|
|
not isinstance(line, str) or len(line) > 256
|
|
|
|
|
for line in lines
|
|
|
|
|
)
|
|
|
|
|
):
|
|
|
|
|
raise ValueError
|
|
|
|
|
if type(report["partial"]) is not bool:
|
|
|
|
|
raise ValueError
|
|
|
|
|
except (KeyError, TypeError, ValueError, OSError):
|
|
|
|
|
report = {"error": "Could not measure usage on the web node."}
|
|
|
|
|
WebsiteUsage.objects.filter(
|
|
|
|
|
pk=snapshot.pk, requested_at=requested_at
|
|
|
|
|
).update(
|
|
|
|
|
report=report,
|
|
|
|
|
observed_at=timezone.now(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(queue="coulobre-mailbox-provisioning")
|
|
|
|
|
def collect_mailbox_usage(mailbox_id, requested_at):
|
|
|
|
|
from coulobre.mail_node import MailboxUsageRequest
|
|
|
|
|
|
|
|
|
|
from .models import HostingMailbox
|
|
|
|
|
from .node_client import apply_on_local_node
|
|
|
|
|
|
|
|
|
|
if not settings.LOCAL_MAILBOX_PROVISIONING or not settings.WEBSITE_USAGE:
|
|
|
|
|
return
|
|
|
|
|
mailboxes = HostingMailbox.objects.filter(
|
|
|
|
|
pk=mailbox_id,
|
|
|
|
|
usage_requested_at=requested_at,
|
|
|
|
|
mail_domain__account__retired_at__isnull=True,
|
|
|
|
|
)
|
|
|
|
|
mailbox = mailboxes.select_related("mail_domain").first()
|
|
|
|
|
if mailbox is None:
|
|
|
|
|
return
|
|
|
|
|
try:
|
|
|
|
|
reply = apply_on_local_node(
|
|
|
|
|
MailboxUsageRequest(mailbox.spec),
|
|
|
|
|
"/run/coulobre-mail-apply.sock",
|
|
|
|
|
"mail node",
|
|
|
|
|
"mailbox quota usage",
|
|
|
|
|
timeout=15,
|
|
|
|
|
)
|
|
|
|
|
used = reply["usage_bytes"]
|
|
|
|
|
if type(used) is not int or not 0 <= used < 2**63:
|
|
|
|
|
raise ValueError
|
|
|
|
|
except (KeyError, OSError, ValueError):
|
|
|
|
|
used = None
|
|
|
|
|
mailboxes.update(usage_bytes=used, usage_observed_at=timezone.now())
|