Merge pull request 'Include local mailboxes in backups from the panel' (#29) from feature/panel-mail-account-backups into main
Reviewed-on: #29
This commit was merged in pull request #29.
This commit is contained in:
commit
056858db0d
12 files changed
+224
-21
No files matched your search
@@ -15,3 +15,4 @@ COULOBRE_BROKER_URL=amqp://coulobre:@127.0.0.1:5672/coulobre
|
||||
# COULOBRE_LOCAL_DNS_PUBLISHING=1
|
||||
# COULOBRE_LOCAL_MAILBOX_PROVISIONING=1
|
||||
# COULOBRE_LOCAL_ACCOUNT_BACKUPS=1
|
||||
# COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL=1
|
||||
@@ -0,0 +1,3 @@
|
||||
[Service]
|
||||
Environment=COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL=1
|
||||
ReadWritePaths=-/srv/coulobre-mail
|
||||
@@ -396,6 +396,25 @@ Archives are stored under `/var/lib/coulobre/account-backups/` with root-only
|
||||
access. Retrieve archives and manage their retention on the server. Recovery
|
||||
uses the existing [account bundle commands](development.md#recover-backups-from-the-bundle).
|
||||
|
||||
#### Include mailboxes on the same server
|
||||
|
||||
Complete the [Postfix and Dovecot setup](development.md#apply-a-mailbox-manifest)
|
||||
on the same server. Install the service drop-in so the backup service can
|
||||
create its temporary Maildir copies on the mail volume.
|
||||
|
||||
```sh
|
||||
sudo install -D -m 0644 deploy/systemd/coulobre-account-backup@.service.d/mail.conf \
|
||||
/etc/systemd/system/coulobre-account-backup@.service.d/mail.conf
|
||||
sudo systemctl daemon-reload
|
||||
```
|
||||
|
||||
Set `COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL=1` in `/etc/coulobre/panel.env` and
|
||||
restart `coulobre-panel` and `coulobre-backup-worker`. Operators with
|
||||
permission to view mailbox requests can select **Include mailboxes** when
|
||||
creating an account backup. Provision all listed mailboxes before requesting
|
||||
a backup with mail. For mailboxes on a separate server, use the
|
||||
[bundle workflow for separate nodes](development.md#bundle-backups-from-separate-nodes).
|
||||
|
||||
## Publish local DNS zones from the panel
|
||||
|
||||
Enable the [DNS publishing socket](development.md#enable-local-dns-publishing)
|
||||
|
||||
+4
-2
@@ -902,8 +902,10 @@ each listed database, and the requested mailbox archives. Back up DNS and panel
|
||||
settings separately.
|
||||
|
||||
On a single-server installation, operators can start a backup of the website
|
||||
and databases directly from the account page and view its status there.
|
||||
[Enable account backups in the panel](deployment.md#start-local-account-backups-from-the-panel)
|
||||
and databases directly from the account page and view its status there. If
|
||||
mail is hosted on that server too, enable the **Include mailboxes** option.
|
||||
[Enable account backups in the panel](deployment.md#start-local-account-backups-from-the-panel),
|
||||
[include local mailboxes](deployment.md#include-mailboxes-on-the-same-server)
|
||||
|
||||
If all resources in the manifest are on one server, run the account backup
|
||||
command there as root. To include mail, pass the manifest downloaded with
|
||||
|
||||
@@ -39,12 +39,7 @@ class AccountBackupRequest:
|
||||
backup_id = UUID(data["backup_id"])
|
||||
if str(backup_id) != data["backup_id"]:
|
||||
raise ValueError("Expected a canonical backup ID.")
|
||||
backup = AccountBackup.from_manifest(data["backup"])
|
||||
if backup.mailboxes:
|
||||
raise ValueError(
|
||||
"Use the account backup CLI to include mailboxes."
|
||||
)
|
||||
return cls(backup_id, backup)
|
||||
return cls(backup_id, AccountBackup.from_manifest(data["backup"]))
|
||||
|
||||
def manifest(self):
|
||||
return {
|
||||
@@ -80,6 +75,11 @@ def create_requested_backup(request):
|
||||
|
||||
if os.geteuid() != 0:
|
||||
raise ValueError("Run create-request as root on the backup node.")
|
||||
if (
|
||||
request.backup.mailboxes is not None
|
||||
and os.environ.get("COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL") != "1"
|
||||
):
|
||||
raise ValueError("Enable mailbox backups on the local backup node.")
|
||||
require_root_directory(BACKUP_DIRECTORY)
|
||||
directory = os.open(
|
||||
BACKUP_DIRECTORY, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
|
||||
|
||||
@@ -22,7 +22,11 @@ def account_backup_manifest(account, *, include_mailboxes=False):
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def queue_account_backup(account_id):
|
||||
def queue_account_backup(account_id, *, include_mailboxes=False):
|
||||
account = lock_hosting_account(account_id, customer_id=None)
|
||||
if not account.backup_jobs.filter(finished_at__isnull=True).exists():
|
||||
account.backup_jobs.create(manifest=account_backup_manifest(account))
|
||||
account.backup_jobs.create(
|
||||
manifest=account_backup_manifest(
|
||||
account, include_mailboxes=include_mailboxes
|
||||
)
|
||||
)
|
||||
@@ -392,6 +392,10 @@ class AccountBackupJob(ProvisioningJob):
|
||||
def archive(self):
|
||||
return str(backup_archive(self.account_id, self.backup_id))
|
||||
|
||||
@property
|
||||
def includes_mailboxes(self):
|
||||
return "mailboxes" in self.manifest
|
||||
|
||||
|
||||
class DnsPublication(ProvisioningJob):
|
||||
zone = models.OneToOneField(
|
||||
|
||||
@@ -295,6 +295,8 @@ 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.")
|
||||
if backup.mailboxes is not None and not settings.LOCAL_ACCOUNT_BACKUP_MAIL:
|
||||
raise ValueError("Enable local mailbox backups before trying again.")
|
||||
create_local_account_backup(AccountBackupRequest(job.backup_id, backup))
|
||||
|
||||
|
||||
|
||||
@@ -5,22 +5,29 @@
|
||||
<p class="heading-meta">Includes the website and its MariaDB databases.</p>
|
||||
</div>
|
||||
{% if not account_backup_pending %}
|
||||
<form method="post" action="{% url 'panel:account_backup_create' account.pk %}">
|
||||
<form class="form-actions" method="post" action="{% url 'panel:account_backup_create' account.pk %}">
|
||||
{% csrf_token %}
|
||||
{% if account_backup_allow_mail %}
|
||||
<label><input type="checkbox" name="include_mail" value="1"> Include mailboxes</label>
|
||||
{% endif %}
|
||||
<button class="button primary" type="submit">Create backup</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
</header>
|
||||
{% if account_backup_pending_hidden %}
|
||||
<p class="empty">A backup is already queued or running.</p>
|
||||
{% endif %}
|
||||
{% if account_backup_jobs %}
|
||||
<div class="table-scroll">
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th scope="col">Requested</th><th scope="col">Status</th><th scope="col">Archive</th></tr>
|
||||
<tr><th scope="col">Requested</th><th scope="col">Contents</th><th scope="col">Status</th><th scope="col">Archive</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for job in account_backup_jobs %}
|
||||
<tr>
|
||||
<td>{{ job.requested_at|date:"j M Y, H:i" }} UTC</td>
|
||||
<td>{% if job.includes_mailboxes %}Website, databases, and mail{% else %}Website and databases{% endif %}</td>
|
||||
<td>
|
||||
{% if job.state == "configured" %}
|
||||
Ready
|
||||
@@ -40,7 +47,7 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% else %}
|
||||
{% elif not account_backup_pending_hidden %}
|
||||
<p class="empty">No backups requested.</p>
|
||||
{% endif %}
|
||||
</section>
|
||||
@@ -10,6 +10,7 @@ from uuid import uuid4
|
||||
from zipfile import ZipFile
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.test import SimpleTestCase, TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
@@ -21,7 +22,14 @@ from coulobre.database_node import DEFAULT_SOCKET
|
||||
from coulobre.mail_node import MailboxSpec
|
||||
from coulobre.web_node import WebSite
|
||||
|
||||
from .models import AccountBackupJob, HostingAccount, HostingDatabase, Reseller
|
||||
from .models import (
|
||||
AccountBackupJob,
|
||||
HostingAccount,
|
||||
HostingDatabase,
|
||||
HostingMailbox,
|
||||
HostingMailDomain,
|
||||
Reseller,
|
||||
)
|
||||
from .tasks import (
|
||||
ACCOUNT_BACKUP_LEASE,
|
||||
ACCOUNT_BACKUP_QUEUE,
|
||||
@@ -134,6 +142,116 @@ class PanelAccountBackupTests(TestCase):
|
||||
)
|
||||
self.assertFalse(AccountBackupJob.objects.exists())
|
||||
|
||||
@override_settings(LOCAL_ACCOUNT_BACKUP_MAIL=True)
|
||||
def test_operator_can_include_mailboxes_and_see_the_completed_job(self):
|
||||
domain = HostingMailDomain.objects.create(
|
||||
account=self.account, domain="old.example"
|
||||
)
|
||||
mailbox = HostingMailbox.objects.create(
|
||||
mail_domain=domain, local_part="support"
|
||||
)
|
||||
self.assertContains(
|
||||
self.client.get(self.account.get_absolute_url()),
|
||||
"Include mailboxes",
|
||||
)
|
||||
self.assertRedirects(
|
||||
self.client.post(self.request_url, {"include_mail": "1"}),
|
||||
self.account.get_absolute_url(),
|
||||
)
|
||||
HostingMailbox.objects.create(mail_domain=domain, local_part="later")
|
||||
self.client.post(self.request_url)
|
||||
job = AccountBackupJob.objects.get(account=self.account)
|
||||
|
||||
with patch(
|
||||
"coulobre.hosting.tasks.create_local_account_backup"
|
||||
) as node:
|
||||
create_account_backup(job.pk)
|
||||
|
||||
node.assert_called_once_with(
|
||||
AccountBackupRequest(
|
||||
job.backup_id,
|
||||
AccountBackup(
|
||||
WebSite(self.account.pk, self.account.primary_domain),
|
||||
(self.database.spec,),
|
||||
(mailbox.spec,),
|
||||
),
|
||||
)
|
||||
)
|
||||
page = self.client.get(self.account.get_absolute_url())
|
||||
self.assertContains(page, "Website, databases, and mail")
|
||||
self.assertContains(page, job.archive)
|
||||
|
||||
@override_settings(LOCAL_ACCOUNT_BACKUP_MAIL=False)
|
||||
def test_mail_opt_in_requires_local_mail_backup_to_be_enabled(self):
|
||||
self.assertNotContains(
|
||||
self.client.get(self.account.get_absolute_url()),
|
||||
"Include mailboxes",
|
||||
)
|
||||
|
||||
response = self.client.post(self.request_url, {"include_mail": "1"})
|
||||
self.assertEqual(response.status_code, 404)
|
||||
self.assertFalse(AccountBackupJob.objects.exists())
|
||||
self.assertEqual(self.client.post(self.request_url).status_code, 302)
|
||||
self.assertFalse(AccountBackupJob.objects.get().includes_mailboxes)
|
||||
|
||||
@override_settings(LOCAL_ACCOUNT_BACKUP_MAIL=True)
|
||||
def test_operator_without_mail_permission_cannot_request_or_see_mail_jobs(
|
||||
self,
|
||||
):
|
||||
self.client.post(self.request_url, {"include_mail": "1"})
|
||||
job = AccountBackupJob.objects.get(account=self.account)
|
||||
limited = get_user_model().objects.create_user("limited-operator")
|
||||
limited.user_permissions.set(
|
||||
Permission.objects.filter(
|
||||
content_type__app_label="hosting",
|
||||
codename__in=(
|
||||
"view_hostingaccount",
|
||||
"view_webprovisioningplan",
|
||||
"view_hostingdatabase",
|
||||
"add_accountbackupjob",
|
||||
),
|
||||
)
|
||||
)
|
||||
self.client.force_login(limited)
|
||||
|
||||
pending = self.client.get(self.account.get_absolute_url())
|
||||
self.assertContains(pending, "A backup is already queued or running.")
|
||||
self.assertNotContains(pending, "Create backup")
|
||||
job.finished_at = timezone.now()
|
||||
job.problem = "Could not back up a mailbox at private.example."
|
||||
job.save(update_fields=["finished_at", "problem"])
|
||||
page = self.client.get(self.account.get_absolute_url())
|
||||
self.assertNotContains(page, "private.example")
|
||||
self.assertNotContains(page, "Include mailboxes")
|
||||
self.assertEqual(
|
||||
self.client.post(
|
||||
self.request_url, {"include_mail": "1"}
|
||||
).status_code,
|
||||
403,
|
||||
)
|
||||
self.assertEqual(self.client.post(self.request_url).status_code, 302)
|
||||
self.assertEqual(AccountBackupJob.objects.count(), 2)
|
||||
|
||||
@override_settings(LOCAL_ACCOUNT_BACKUP_MAIL=True)
|
||||
def test_worker_leaves_a_failure_when_mail_is_disabled_after_queueing(
|
||||
self,
|
||||
):
|
||||
self.client.post(self.request_url, {"include_mail": "1"})
|
||||
job = AccountBackupJob.objects.get(account=self.account)
|
||||
|
||||
with (
|
||||
self.settings(LOCAL_ACCOUNT_BACKUP_MAIL=False),
|
||||
patch(
|
||||
"coulobre.hosting.tasks.create_local_account_backup"
|
||||
) as node,
|
||||
):
|
||||
create_account_backup(job.pk)
|
||||
|
||||
node.assert_not_called()
|
||||
job.refresh_from_db()
|
||||
self.assertEqual(job.state, "failed")
|
||||
self.assertIn("Enable local mailbox backups", job.problem)
|
||||
|
||||
|
||||
class AccountBackupRequestTests(SimpleTestCase):
|
||||
def setUp(self):
|
||||
@@ -197,7 +315,7 @@ class AccountBackupRequestTests(SimpleTestCase):
|
||||
self.assertEqual(report["state"], "failed")
|
||||
self.create.assert_not_called()
|
||||
|
||||
def test_panel_worker_rejects_requests_that_include_mailboxes(self):
|
||||
def test_backup_node_requires_mail_backup_opt_in(self):
|
||||
backup = AccountBackup(
|
||||
self.backup.website,
|
||||
(),
|
||||
@@ -205,12 +323,33 @@ class AccountBackupRequestTests(SimpleTestCase):
|
||||
)
|
||||
request = AccountBackupRequest(self.request.backup_id, backup)
|
||||
|
||||
code, report = self.send(request.manifest())
|
||||
with patch.dict(
|
||||
"os.environ", {"COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL": "0"}
|
||||
):
|
||||
code, report = self.send(request.manifest())
|
||||
|
||||
self.assertEqual(code, 1)
|
||||
self.assertIn("Use the account backup CLI", report["problem"])
|
||||
self.assertIn("Enable mailbox backups", report["problem"])
|
||||
self.assertEqual(report["backup_id"], str(request.backup_id))
|
||||
self.create.assert_not_called()
|
||||
|
||||
def test_backup_node_accepts_and_replays_mail_requests_when_enabled(self):
|
||||
backup = AccountBackup(
|
||||
self.backup.website, (), (MailboxSpec("example.net", "sales"),)
|
||||
)
|
||||
request = AccountBackupRequest(self.request.backup_id, backup)
|
||||
archive = self.directory / f"account-123-{request.backup_id}.zip"
|
||||
|
||||
with patch.dict(
|
||||
"os.environ", {"COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL": "1"}
|
||||
):
|
||||
for _ in range(2):
|
||||
code, report = self.send(request.manifest())
|
||||
self.assertEqual(code, 0)
|
||||
self.assertEqual(report["state"], "configured")
|
||||
|
||||
self.create.assert_called_once_with(backup, archive, DEFAULT_SOCKET)
|
||||
|
||||
def test_replay_rejects_an_archive_with_a_different_plan(self):
|
||||
archive = self.directory / f"account-123-{self.request.backup_id}.zip"
|
||||
self.write_zip(
|
||||
|
||||
@@ -250,10 +250,23 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView):
|
||||
jobs = list(
|
||||
self.object.backup_jobs.order_by("-requested_at", "-pk")[:10]
|
||||
)
|
||||
can_view_mailboxes = user.has_perm(VIEW_MAILBOXES)
|
||||
context["show_account_backups"] = True
|
||||
context["account_backup_jobs"] = jobs
|
||||
context["account_backup_pending"] = any(
|
||||
job.finished_at is None for job in jobs
|
||||
visible_jobs = (
|
||||
jobs
|
||||
if can_view_mailboxes
|
||||
else [job for job in jobs if not job.includes_mailboxes]
|
||||
)
|
||||
context["account_backup_jobs"] = visible_jobs
|
||||
pending = next(
|
||||
(job for job in jobs if job.finished_at is None), None
|
||||
)
|
||||
context["account_backup_pending"] = pending is not None
|
||||
context["account_backup_pending_hidden"] = (
|
||||
pending is not None and pending not in visible_jobs
|
||||
)
|
||||
context["account_backup_allow_mail"] = (
|
||||
settings.LOCAL_ACCOUNT_BACKUP_MAIL and can_view_mailboxes
|
||||
)
|
||||
context["manage_sftp"] = (
|
||||
settings.LOCAL_WEB_SFTP
|
||||
@@ -339,8 +352,14 @@ class RequestAccountBackup(OperatorOnlyMixin, PermissionRequiredMixin, View):
|
||||
def post(self, request, pk):
|
||||
if not settings.LOCAL_ACCOUNT_BACKUPS:
|
||||
raise Http404
|
||||
include_mailboxes = request.POST.get("include_mail") == "1"
|
||||
if include_mailboxes:
|
||||
if not settings.LOCAL_ACCOUNT_BACKUP_MAIL:
|
||||
raise Http404
|
||||
if not request.user.has_perm(VIEW_MAILBOXES):
|
||||
return self.handle_no_permission()
|
||||
try:
|
||||
queue_account_backup(pk)
|
||||
queue_account_backup(pk, include_mailboxes=include_mailboxes)
|
||||
except HostingAccount.DoesNotExist:
|
||||
raise Http404 from None
|
||||
return redirect("panel:account", pk=pk)
|
||||
|
||||
@@ -129,6 +129,9 @@ LOCAL_MAILBOX_PROVISIONING = (
|
||||
os.environ.get("COULOBRE_LOCAL_MAILBOX_PROVISIONING") == "1"
|
||||
)
|
||||
LOCAL_ACCOUNT_BACKUPS = os.environ.get("COULOBRE_LOCAL_ACCOUNT_BACKUPS") == "1"
|
||||
LOCAL_ACCOUNT_BACKUP_MAIL = (
|
||||
os.environ.get("COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL") == "1"
|
||||
)
|
||||
CELERY_BEAT_SCHEDULE = {
|
||||
"dispatch-web-provisioning-plans": {
|
||||
"task": "coulobre.hosting.tasks.dispatch_web_plans",
|
||||
|
||||
Reference in new issue
Block a user