217 lines
6.9 KiB
Python
217 lines
6.9 KiB
Python
from django.conf import settings
|
|
from django.core.exceptions import ValidationError
|
|
from django.db import transaction
|
|
|
|
from coulobre.account_backup import AccountBackup
|
|
from coulobre.web_node import WebSite
|
|
|
|
from .independent_websites import Operation, active_websites
|
|
from .models import ComponentRestoreJob, HostingMailbox, WebsiteRestoreJob
|
|
from .resource_requests import lock_hosting_account, require_active_services
|
|
|
|
RestoreKind = ComponentRestoreJob.Kind
|
|
|
|
|
|
def enabled_component_restores():
|
|
return {
|
|
kind
|
|
for kind, enabled in (
|
|
(RestoreKind.DATABASE, settings.LOCAL_DATABASE_RESTORE),
|
|
(
|
|
RestoreKind.MAILBOX,
|
|
settings.LOCAL_MAILBOX_RESTORE
|
|
or settings.REMOTE_MAILBOX_RESTORE,
|
|
),
|
|
)
|
|
if enabled
|
|
}
|
|
|
|
|
|
def current_restore_resources(account, kind):
|
|
if kind == RestoreKind.DATABASE:
|
|
return {db.suffix: db.spec for db in account.databases.all()}
|
|
|
|
mailboxes = HostingMailbox.objects.filter(
|
|
mail_domain__account=account
|
|
).select_related("mail_domain")
|
|
resources = (mailbox.spec for mailbox in mailboxes)
|
|
return {mailbox.address: mailbox for mailbox in resources}
|
|
|
|
|
|
def available_restore_resources(backup, kind, current):
|
|
if not backup.archive_is_restorable or not current:
|
|
return {}
|
|
archive = AccountBackup.from_manifest(backup.manifest)
|
|
if archive.website.account_id != backup.account_id:
|
|
return {}
|
|
if kind == RestoreKind.DATABASE:
|
|
databases = {
|
|
database.suffix: database for database in archive.databases
|
|
}
|
|
return {
|
|
key: spec
|
|
for key, spec in current.items()
|
|
if databases.get(key) == spec
|
|
}
|
|
keys = {mailbox.address for mailbox in archive.mailboxes or ()}
|
|
return {key: spec for key, spec in current.items() if key in keys}
|
|
|
|
|
|
def current_website_restore_targets(account):
|
|
if account.retired_at is not None:
|
|
return {}
|
|
sites = [WebSite(account.pk, account.primary_domain)]
|
|
sites.extend(
|
|
website.spec
|
|
for website in active_websites(account).exclude(
|
|
operation=Operation.RETIRE, finished_at__isnull=True
|
|
)
|
|
)
|
|
return {site.node_key: site for site in sites}
|
|
|
|
|
|
def available_website_restore_targets(backup, current=None):
|
|
if current is None:
|
|
current = current_website_restore_targets(backup.account)
|
|
return {
|
|
key: site
|
|
for key, site in current.items()
|
|
if backup.can_restore_website(site)
|
|
}
|
|
|
|
|
|
def require_no_wordpress_update(account):
|
|
if account.wordpress_jobs.filter(
|
|
action__in=("update", "imp-promote"), finished_at__isnull=True
|
|
).exists():
|
|
raise ValidationError(
|
|
"Wait for the current WordPress update to finish."
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def queue_website_restore(
|
|
backup,
|
|
*,
|
|
restore_id,
|
|
filename,
|
|
replace,
|
|
operator_username,
|
|
site_key=None,
|
|
customer_id=None,
|
|
):
|
|
account = lock_hosting_account(backup.account_id, customer_id=customer_id)
|
|
require_active_services(account)
|
|
require_no_wordpress_update(account)
|
|
backup.refresh_from_db()
|
|
if customer_id is not None and backup.export_customer_id != customer_id:
|
|
raise ValidationError("This backup is no longer available.")
|
|
backup.account = account
|
|
targets = available_website_restore_targets(backup)
|
|
site = targets.get(site_key or str(account.pk))
|
|
if site is None:
|
|
raise ValidationError(
|
|
"Choose a completed backup of the current website."
|
|
)
|
|
|
|
_queue_restore(
|
|
WebsiteRestoreJob.objects,
|
|
backup,
|
|
restore_id,
|
|
operator_username,
|
|
{
|
|
"filename": filename,
|
|
"requested_customer_id": customer_id,
|
|
"replace": replace,
|
|
"site_manifest": site.manifest()
|
|
if site.site_id is not None
|
|
else None,
|
|
},
|
|
WebsiteRestoreJob.objects.filter(
|
|
backup__account=account, finished_at__isnull=True
|
|
),
|
|
"Wait for the current website restore to finish.",
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def queue_component_restore(
|
|
backup, *, restore_id, kind, resource, operator_username, customer_id=None
|
|
):
|
|
account = lock_hosting_account(backup.account_id, customer_id=customer_id)
|
|
require_active_services(account)
|
|
if kind == RestoreKind.DATABASE:
|
|
require_no_wordpress_update(account)
|
|
backup.refresh_from_db()
|
|
if customer_id is not None and backup.export_customer_id != customer_id:
|
|
raise ValidationError("This backup is no longer available.")
|
|
targets = available_restore_resources(
|
|
backup, kind, current_restore_resources(account, kind)
|
|
)
|
|
if resource not in targets:
|
|
raise ValidationError("This restore target is no longer available.")
|
|
|
|
_queue_restore(
|
|
ComponentRestoreJob.objects,
|
|
backup,
|
|
restore_id,
|
|
operator_username,
|
|
{
|
|
"kind": kind,
|
|
"requested_customer_id": customer_id,
|
|
"resource": resource,
|
|
"remote_mail": kind == RestoreKind.MAILBOX
|
|
and settings.REMOTE_MAILBOX_RESTORE,
|
|
},
|
|
ComponentRestoreJob.objects.filter(
|
|
backup__account=account,
|
|
kind=kind,
|
|
resource=resource,
|
|
finished_at__isnull=True,
|
|
),
|
|
"Wait for the current restore of this target to finish.",
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def retry_remote_mail_restore(job, operator_username):
|
|
account = lock_hosting_account(job.backup.account_id, customer_id=None)
|
|
require_active_services(account)
|
|
job.refresh_from_db()
|
|
targets = available_restore_resources(
|
|
job.backup,
|
|
RestoreKind.MAILBOX,
|
|
current_restore_resources(account, RestoreKind.MAILBOX),
|
|
)
|
|
if not job.can_retry_remote or job.resource not in targets:
|
|
raise ValidationError("This mailbox restore can no longer be retried.")
|
|
jobs = ComponentRestoreJob.objects.filter(
|
|
backup__account=account,
|
|
kind=RestoreKind.MAILBOX,
|
|
resource=job.resource,
|
|
)
|
|
if jobs.filter(finished_at__isnull=True).exists():
|
|
raise ValidationError("Wait for the current restore of this target.")
|
|
ComponentRestoreJob.objects.requeue(
|
|
jobs.filter(pk=job.pk), operator_username=operator_username
|
|
)
|
|
|
|
|
|
def _queue_restore(
|
|
jobs, backup, restore_id, operator_username, target, pending, busy
|
|
):
|
|
details = {
|
|
"backup_id": backup.pk,
|
|
"operator_username": operator_username,
|
|
**target,
|
|
}
|
|
job = jobs.filter(restore_id=restore_id).first()
|
|
if job is None:
|
|
if pending.exists():
|
|
raise ValidationError(busy)
|
|
job, _ = jobs.get_or_create(restore_id=restore_id, defaults=details)
|
|
if any(getattr(job, field) != value for field, value in details.items()):
|
|
raise ValidationError(
|
|
"Reopen the restore form to submit a new request."
|
|
)
|