123 lines
4.1 KiB
Python
123 lines
4.1 KiB
Python
from datetime import UTC, datetime, timedelta
|
|||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from django import forms
|
||
|
|
from django.conf import settings
|
||
|
|
from django.contrib import messages
|
||
|
|
from django.contrib.admin.models import CHANGE, LogEntry
|
||
|
|
from django.db import transaction
|
||
|
|
from django.http import Http404
|
||
|
|
from django.shortcuts import redirect
|
||
|
|
from django.utils import timezone
|
||
|
|
from django.views.generic import FormView
|
||
|
|
|
||
|
|
from coulobre.operator_node import SOCKET
|
||
|
|
|
||
|
|
from .account_administration import OperatorAuthorization
|
||
|
|
from .models import SystemBackupJob, SystemBackupSchedule
|
||
|
|
|
||
|
|
|
||
|
|
class SystemBackupScheduleForm(forms.Form):
|
||
|
|
enabled = forms.BooleanField(required=False, label="Run daily")
|
||
|
|
time = forms.TimeField(
|
||
|
|
required=False,
|
||
|
|
label="Daily start time (UTC)",
|
||
|
|
input_formats=["%H:%M"],
|
||
|
|
widget=forms.TimeInput(format="%H:%M", attrs={"type": "time"}),
|
||
|
|
)
|
||
|
|
|
||
|
|
def clean(self):
|
||
|
|
data = super().clean()
|
||
|
|
if data.get("enabled") and not data.get("time"):
|
||
|
|
self.add_error("time", "Choose the daily start time.")
|
||
|
|
return data
|
||
|
|
|
||
|
|
|
||
|
|
def next_system_backup(time, now):
|
||
|
|
candidate = datetime.combine(now.astimezone(UTC).date(), time, tzinfo=UTC)
|
||
|
|
return candidate if candidate > now else candidate + timedelta(days=1)
|
||
|
|
|
||
|
|
|
||
|
|
@transaction.atomic
|
||
|
|
def queue_scheduled_system_backup(now):
|
||
|
|
schedule = (
|
||
|
|
SystemBackupSchedule.objects.select_for_update().filter(pk=1).first()
|
||
|
|
)
|
||
|
|
if (
|
||
|
|
schedule is None
|
||
|
|
or not schedule.enabled
|
||
|
|
or schedule.time is None
|
||
|
|
or schedule.next_run_at is None
|
||
|
|
or schedule.next_run_at > now
|
||
|
|
):
|
||
|
|
return
|
||
|
|
if not SystemBackupJob.objects.filter(finished_at__isnull=True).exists():
|
||
|
|
SystemBackupJob.objects.get_or_create(
|
||
|
|
scheduled_for=schedule.next_run_at
|
||
|
|
)
|
||
|
|
schedule.next_run_at = next_system_backup(schedule.time, now)
|
||
|
|
schedule.save(update_fields=["next_run_at"])
|
||
|
|
|
||
|
|
|
||
|
|
class SystemBackupView(OperatorAuthorization, FormView):
|
||
|
|
form_class = SystemBackupScheduleForm
|
||
|
|
template_name = "hosting/system_backup.html"
|
||
|
|
|
||
|
|
def available(self):
|
||
|
|
return settings.SYSTEM_BACKUPS and Path(SOCKET).is_socket()
|
||
|
|
|
||
|
|
def get_initial(self):
|
||
|
|
schedule = SystemBackupSchedule.objects.filter(pk=1).first()
|
||
|
|
return (
|
||
|
|
{"enabled": schedule.enabled, "time": schedule.time}
|
||
|
|
if schedule
|
||
|
|
else {}
|
||
|
|
)
|
||
|
|
|
||
|
|
def get_context_data(self, **kwargs):
|
||
|
|
return super().get_context_data(
|
||
|
|
available=self.available(),
|
||
|
|
jobs=SystemBackupJob.objects.select_related("actor").order_by(
|
||
|
|
"-pk"
|
||
|
|
)[:30],
|
||
|
|
**kwargs,
|
||
|
|
)
|
||
|
|
|
||
|
|
@transaction.atomic
|
||
|
|
def post(self, request, *args, **kwargs):
|
||
|
|
if not self.available():
|
||
|
|
raise Http404
|
||
|
|
if request.POST.get("action") != "create":
|
||
|
|
return super().post(request, *args, **kwargs)
|
||
|
|
if request.POST.get("confirm") != "yes":
|
||
|
|
messages.error(
|
||
|
|
request, "Confirm that you want to create the backup."
|
||
|
|
)
|
||
|
|
return redirect("panel:system_backup")
|
||
|
|
SystemBackupSchedule.objects.get_or_create(pk=1)
|
||
|
|
SystemBackupSchedule.objects.select_for_update().get(pk=1)
|
||
|
|
if SystemBackupJob.objects.filter(finished_at__isnull=True).exists():
|
||
|
|
messages.error(request, "Wait for the current backup.")
|
||
|
|
else:
|
||
|
|
SystemBackupJob.objects.create(actor=request.user)
|
||
|
|
messages.success(request, "System backup queued.")
|
||
|
|
return redirect("panel:system_backup")
|
||
|
|
|
||
|
|
def form_valid(self, form):
|
||
|
|
data = form.cleaned_data
|
||
|
|
defaults = data | {
|
||
|
|
"next_run_at": next_system_backup(data["time"], timezone.now())
|
||
|
|
if data["enabled"]
|
||
|
|
else None,
|
||
|
|
}
|
||
|
|
schedule, _ = SystemBackupSchedule.objects.update_or_create(
|
||
|
|
pk=1, defaults=defaults
|
||
|
|
)
|
||
|
|
LogEntry.objects.log_actions(
|
||
|
|
self.request.user.pk,
|
||
|
|
[schedule],
|
||
|
|
CHANGE,
|
||
|
|
"Changed system backup schedule.",
|
||
|
|
)
|
||
|
|
messages.success(self.request, "System backup schedule saved.")
|
||
|
|
return redirect("panel:system_backup")
|