Queue and track database and mailbox recovery jobs
This commit is contained in:
1 parent
337a078ffe
commit
e13adc0d4e
6 files changed
+304
-7
No files matched your search
@@ -0,0 +1,117 @@
|
|||||||
|
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 .models import ComponentRestoreJob, HostingMailbox, WebsiteRestoreJob
|
||||||
|
from .resource_requests import lock_hosting_account
|
||||||
|
|
||||||
|
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),
|
||||||
|
)
|
||||||
|
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 backup.state != "configured" or not current:
|
||||||
|
return {}
|
||||||
|
archive = AccountBackup.from_manifest(backup.manifest)
|
||||||
|
if archive.website.account_id != backup.account_id:
|
||||||
|
return {}
|
||||||
|
if kind == RestoreKind.DATABASE:
|
||||||
|
keys = {database.suffix for database in archive.databases}
|
||||||
|
else:
|
||||||
|
keys = {mailbox.address for mailbox in archive.mailboxes or ()}
|
||||||
|
return {key: spec for key, spec in current.items() if key in keys}
|
||||||
|
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def queue_website_restore(
|
||||||
|
backup, *, restore_id, filename, replace, operator_username
|
||||||
|
):
|
||||||
|
account = lock_hosting_account(backup.account_id, customer_id=None)
|
||||||
|
if not backup.can_restore_website(
|
||||||
|
WebSite(account.pk, account.primary_domain)
|
||||||
|
):
|
||||||
|
raise ValidationError(
|
||||||
|
"Choose a completed backup of the current website."
|
||||||
|
)
|
||||||
|
|
||||||
|
_queue_restore(
|
||||||
|
WebsiteRestoreJob.objects,
|
||||||
|
backup,
|
||||||
|
restore_id,
|
||||||
|
operator_username,
|
||||||
|
{"filename": filename, "replace": replace},
|
||||||
|
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
|
||||||
|
):
|
||||||
|
account = lock_hosting_account(backup.account_id, customer_id=None)
|
||||||
|
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, "resource": resource},
|
||||||
|
ComponentRestoreJob.objects.filter(
|
||||||
|
backup__account=account,
|
||||||
|
kind=kind,
|
||||||
|
resource=resource,
|
||||||
|
finished_at__isnull=True,
|
||||||
|
),
|
||||||
|
"Wait for the current restore of this target to finish.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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."
|
||||||
|
)
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
# Generated by Django 5.2.19 on 2026-11-21 13:50
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
import django.utils.timezone
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [
|
||||||
|
("hosting", "0026_website_restore_jobs"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="ComponentRestoreJob",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.BigAutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"requested_at",
|
||||||
|
models.DateTimeField(default=django.utils.timezone.now),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"last_dispatched_at",
|
||||||
|
models.DateTimeField(blank=True, null=True),
|
||||||
|
),
|
||||||
|
("started_at", models.DateTimeField(blank=True, null=True)),
|
||||||
|
("finished_at", models.DateTimeField(blank=True, null=True)),
|
||||||
|
("problem", models.TextField(blank=True)),
|
||||||
|
("restore_id", models.UUIDField(editable=False, unique=True)),
|
||||||
|
(
|
||||||
|
"kind",
|
||||||
|
models.CharField(
|
||||||
|
choices=[
|
||||||
|
("database", "Database"),
|
||||||
|
("mailbox", "Mailbox"),
|
||||||
|
],
|
||||||
|
max_length=8,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("resource", models.CharField(max_length=254)),
|
||||||
|
("operator_username", models.CharField(max_length=150)),
|
||||||
|
(
|
||||||
|
"backup",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="component_restores",
|
||||||
|
to="hosting.accountbackupjob",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
"permissions": [
|
||||||
|
(
|
||||||
|
"restore_account_database",
|
||||||
|
"Can restore an account database",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"restore_account_mailbox",
|
||||||
|
"Can restore an account mailbox",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
"indexes": [
|
||||||
|
models.Index(
|
||||||
|
fields=["finished_at", "requested_at"],
|
||||||
|
name="hosting_com_finishe_521d95_idx",
|
||||||
|
)
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -481,6 +481,35 @@ class WebsiteRestoreJob(ProvisioningJob):
|
|||||||
indexes = [models.Index(fields=("finished_at", "requested_at"))]
|
indexes = [models.Index(fields=("finished_at", "requested_at"))]
|
||||||
|
|
||||||
|
|
||||||
|
class ComponentRestoreJob(ProvisioningJob):
|
||||||
|
class Kind(models.TextChoices):
|
||||||
|
DATABASE = "database", "Database"
|
||||||
|
MAILBOX = "mailbox", "Mailbox"
|
||||||
|
|
||||||
|
backup = models.ForeignKey(
|
||||||
|
AccountBackupJob,
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
related_name="component_restores",
|
||||||
|
)
|
||||||
|
restore_id = models.UUIDField(unique=True, editable=False)
|
||||||
|
kind = models.CharField(max_length=8, choices=Kind)
|
||||||
|
resource = models.CharField(max_length=254)
|
||||||
|
operator_username = models.CharField(max_length=150)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
indexes = [models.Index(fields=("finished_at", "requested_at"))]
|
||||||
|
permissions = [
|
||||||
|
("restore_account_database", "Can restore an account database"),
|
||||||
|
("restore_account_mailbox", "Can restore an account mailbox"),
|
||||||
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def target_name(self):
|
||||||
|
if self.kind == self.Kind.DATABASE:
|
||||||
|
return DatabaseSpec(self.backup.account_id, self.resource).name
|
||||||
|
return self.resource
|
||||||
|
|
||||||
|
|
||||||
class AccountBackupSchedule(models.Model):
|
class AccountBackupSchedule(models.Model):
|
||||||
class Frequency(models.TextChoices):
|
class Frequency(models.TextChoices):
|
||||||
DAILY = "daily", "Daily"
|
DAILY = "daily", "Daily"
|
||||||
|
|||||||
@@ -120,6 +120,16 @@ def restore_local_website(request):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def restore_local_component(request):
|
||||||
|
apply_on_local_node(
|
||||||
|
request,
|
||||||
|
RESTORE_SOCKET,
|
||||||
|
"backup node",
|
||||||
|
"component restore",
|
||||||
|
timeout=RESTORE_TIMEOUT_SECONDS + 15,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def encode_manifest(manifest):
|
def encode_manifest(manifest):
|
||||||
return json.dumps(manifest).encode("utf-8")
|
return json.dumps(manifest).encode("utf-8")
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ from coulobre.account_backup_request import (
|
|||||||
)
|
)
|
||||||
from coulobre.account_restore_request import (
|
from coulobre.account_restore_request import (
|
||||||
RESTORE_TIMEOUT_SECONDS,
|
RESTORE_TIMEOUT_SECONDS,
|
||||||
|
ComponentRestoreRequest,
|
||||||
WebsiteRestoreRequest,
|
WebsiteRestoreRequest,
|
||||||
)
|
)
|
||||||
from coulobre.dns_node import DnsZone
|
from coulobre.dns_node import DnsZone
|
||||||
@@ -19,8 +20,14 @@ from coulobre.sftp_keys import SFTPKeys
|
|||||||
from coulobre.web_node import WebSite
|
from coulobre.web_node import WebSite
|
||||||
|
|
||||||
from .account_backups import queue_due_account_backups
|
from .account_backups import queue_due_account_backups
|
||||||
|
from .backup_restores import (
|
||||||
|
available_restore_resources,
|
||||||
|
current_restore_resources,
|
||||||
|
enabled_component_restores,
|
||||||
|
)
|
||||||
from .models import (
|
from .models import (
|
||||||
AccountBackupJob,
|
AccountBackupJob,
|
||||||
|
ComponentRestoreJob,
|
||||||
DatabaseProvisioning,
|
DatabaseProvisioning,
|
||||||
DnsPublication,
|
DnsPublication,
|
||||||
MailboxProvisioning,
|
MailboxProvisioning,
|
||||||
@@ -40,6 +47,7 @@ from .node_client import (
|
|||||||
provision_local_database,
|
provision_local_database,
|
||||||
provision_local_mailbox,
|
provision_local_mailbox,
|
||||||
publish_local_zone,
|
publish_local_zone,
|
||||||
|
restore_local_component,
|
||||||
restore_local_website,
|
restore_local_website,
|
||||||
set_local_site_storage_limit,
|
set_local_site_storage_limit,
|
||||||
update_local_mailbox_quota,
|
update_local_mailbox_quota,
|
||||||
@@ -49,8 +57,12 @@ from .sftp import site_is_active
|
|||||||
|
|
||||||
REDISPATCH_AFTER = timedelta(minutes=5)
|
REDISPATCH_AFTER = timedelta(minutes=5)
|
||||||
ACCOUNT_BACKUP_LEASE = timedelta(seconds=BACKUP_TIMEOUT_SECONDS + 60)
|
ACCOUNT_BACKUP_LEASE = timedelta(seconds=BACKUP_TIMEOUT_SECONDS + 60)
|
||||||
WEBSITE_RESTORE_LEASE = timedelta(seconds=RESTORE_TIMEOUT_SECONDS + 60)
|
ACCOUNT_RESTORE_LEASE = timedelta(seconds=RESTORE_TIMEOUT_SECONDS + 60)
|
||||||
ACCOUNT_BACKUP_QUEUE = "coulobre-account-backups"
|
ACCOUNT_BACKUP_QUEUE = "coulobre-account-backups"
|
||||||
|
UNCONFIRMED_RESTORE = (
|
||||||
|
"Could not confirm the restore on the local backup node. "
|
||||||
|
"Check the destination before requesting another restore."
|
||||||
|
)
|
||||||
WEB_ACTIVATION_QUEUE = "coulobre-web-activation"
|
WEB_ACTIVATION_QUEUE = "coulobre-web-activation"
|
||||||
WEB_HTTPS_QUEUE = "coulobre-web-https"
|
WEB_HTTPS_QUEUE = "coulobre-web-https"
|
||||||
WEB_SFTP_QUEUE = "coulobre-web-sftp"
|
WEB_SFTP_QUEUE = "coulobre-web-sftp"
|
||||||
@@ -172,14 +184,26 @@ def dispatch_account_backups():
|
|||||||
WebsiteRestoreJob,
|
WebsiteRestoreJob,
|
||||||
restore_website,
|
restore_website,
|
||||||
ACCOUNT_BACKUP_QUEUE,
|
ACCOUNT_BACKUP_QUEUE,
|
||||||
lease=WEBSITE_RESTORE_LEASE,
|
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,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def dispatch_provisioning_jobs(model, task, queue, *, lease=REDISPATCH_AFTER):
|
def dispatch_provisioning_jobs(
|
||||||
|
model, task, queue, *, lease=REDISPATCH_AFTER, **scope
|
||||||
|
):
|
||||||
now = timezone.now()
|
now = timezone.now()
|
||||||
stale_before = now - REDISPATCH_AFTER
|
stale_before = now - REDISPATCH_AFTER
|
||||||
waiting = model.objects.filter(finished_at__isnull=True)
|
waiting = model.objects.filter(finished_at__isnull=True, **scope)
|
||||||
pending = (
|
pending = (
|
||||||
waiting.filter(
|
waiting.filter(
|
||||||
Q(last_dispatched_at__isnull=True)
|
Q(last_dispatched_at__isnull=True)
|
||||||
@@ -393,9 +417,8 @@ def restore_website(job_id):
|
|||||||
WebsiteRestoreJob.objects.select_related("backup__account"),
|
WebsiteRestoreJob.objects.select_related("backup__account"),
|
||||||
job_id,
|
job_id,
|
||||||
apply_website_restore,
|
apply_website_restore,
|
||||||
"Could not confirm the restore on the local backup node. "
|
UNCONFIRMED_RESTORE,
|
||||||
"Check the destination before requesting another restore.",
|
lease=ACCOUNT_RESTORE_LEASE,
|
||||||
lease=WEBSITE_RESTORE_LEASE,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -415,6 +438,38 @@ def apply_website_restore(job):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@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."
|
||||||
|
)
|
||||||
|
restore_local_component(
|
||||||
|
ComponentRestoreRequest(
|
||||||
|
job.restore_id, backup.backup_id, backup.account_id, target
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def run_provisioning_job(
|
def run_provisioning_job(
|
||||||
jobs, job_id, apply, connection_problem, *, lease=REDISPATCH_AFTER
|
jobs, job_id, apply, connection_problem, *, lease=REDISPATCH_AFTER
|
||||||
):
|
):
|
||||||
|
|||||||
@@ -136,6 +136,14 @@ LOCAL_WEBSITE_RESTORE = (
|
|||||||
LOCAL_ACCOUNT_BACKUPS
|
LOCAL_ACCOUNT_BACKUPS
|
||||||
and os.environ.get("COULOBRE_LOCAL_WEBSITE_RESTORE") == "1"
|
and os.environ.get("COULOBRE_LOCAL_WEBSITE_RESTORE") == "1"
|
||||||
)
|
)
|
||||||
|
LOCAL_DATABASE_RESTORE = (
|
||||||
|
LOCAL_ACCOUNT_BACKUPS
|
||||||
|
and os.environ.get("COULOBRE_LOCAL_DATABASE_RESTORE") == "1"
|
||||||
|
)
|
||||||
|
LOCAL_MAILBOX_RESTORE = (
|
||||||
|
LOCAL_ACCOUNT_BACKUPS
|
||||||
|
and os.environ.get("COULOBRE_LOCAL_MAILBOX_RESTORE") == "1"
|
||||||
|
)
|
||||||
CELERY_BEAT_SCHEDULE = {
|
CELERY_BEAT_SCHEDULE = {
|
||||||
"dispatch-web-provisioning-plans": {
|
"dispatch-web-provisioning-plans": {
|
||||||
"task": "coulobre.hosting.tasks.dispatch_web_plans",
|
"task": "coulobre.hosting.tasks.dispatch_web_plans",
|
||||||
|
|||||||
Reference in new issue
Block a user