Move technical manifest exports out of panel workflows
This commit is contained in:
1 parent
7b486c491b
commit
b58d683f21
14 files changed
+265
-73
No files matched your search
@@ -0,0 +1,83 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
from ...account_backups import account_backup_manifest
|
||||
from ...models import (
|
||||
HostingAccount,
|
||||
HostingDatabase,
|
||||
HostingDnsZone,
|
||||
HostingMailbox,
|
||||
IndependentWebsite,
|
||||
WebProvisioningPlan,
|
||||
)
|
||||
from ...web_node import build_web_smoke_bundle, serialize_deployment_manifest
|
||||
|
||||
RESOURCE_MODELS = {
|
||||
"account-backup": HostingAccount,
|
||||
"database": HostingDatabase,
|
||||
"dns-zone": HostingDnsZone,
|
||||
"mailbox": HostingMailbox,
|
||||
"website": IndependentWebsite,
|
||||
"apache-preview": WebProvisioningPlan,
|
||||
}
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Export a Coulobre provisioning or backup manifest as JSON."
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument("resource", choices=RESOURCE_MODELS)
|
||||
parser.add_argument("id", type=int)
|
||||
parser.add_argument("--include-mail", action="store_true")
|
||||
parser.add_argument("--smoke-bundle", type=Path, metavar="TAR")
|
||||
|
||||
def handle(self, *args, **options):
|
||||
resource = options["resource"]
|
||||
record_id = options["id"]
|
||||
include_mail = options["include_mail"]
|
||||
bundle = options["smoke_bundle"]
|
||||
if include_mail and resource != "account-backup":
|
||||
raise CommandError("--include-mail requires account-backup.")
|
||||
if bundle and resource != "apache-preview":
|
||||
raise CommandError("--smoke-bundle requires apache-preview.")
|
||||
if not 0 < record_id < 2**63:
|
||||
raise CommandError("Enter a positive 64-bit resource ID.")
|
||||
|
||||
model = RESOURCE_MODELS[resource]
|
||||
try:
|
||||
record = model.objects.get(pk=record_id)
|
||||
except model.DoesNotExist as error:
|
||||
raise CommandError(
|
||||
f"No {resource} with ID {record_id}."
|
||||
) from error
|
||||
|
||||
if resource == "apache-preview":
|
||||
if not record.can_export:
|
||||
raise CommandError(
|
||||
"Select a completed preview for the current domain."
|
||||
)
|
||||
if bundle:
|
||||
self.write_bundle(record, bundle)
|
||||
return
|
||||
return serialize_deployment_manifest(record)
|
||||
if resource == "account-backup":
|
||||
if record.retired_at:
|
||||
raise CommandError("Select an active hosting account.")
|
||||
manifest = account_backup_manifest(
|
||||
record, include_mailboxes=include_mail
|
||||
)
|
||||
else:
|
||||
manifest = record.spec.manifest()
|
||||
return json.dumps(manifest, indent=2)
|
||||
|
||||
def write_bundle(self, plan, path):
|
||||
data = build_web_smoke_bundle(plan)
|
||||
try:
|
||||
with path.open("xb") as output:
|
||||
output.write(data)
|
||||
except OSError as error:
|
||||
raise CommandError(
|
||||
f"Could not write the bundle: {error}"
|
||||
) from error
|
||||
@@ -18,7 +18,7 @@
|
||||
{% if is_tenant or perms.hosting.view_hostingdnszone %}<a href="#domains-dns">Domains and DNS</a>{% endif %}
|
||||
{% if is_tenant or perms.hosting.view_hostingmailbox %}<a href="#email">Email</a>{% endif %}
|
||||
{% if is_tenant or perms.hosting.view_hostingdatabase %}<a href="#databases">Databases</a>{% endif %}
|
||||
{% if show_account_backups or export_backup_manifest %}<a href="#backups">Backups</a>{% endif %}
|
||||
{% if show_account_backups %}<a href="#backups">Backups</a>{% endif %}
|
||||
{% if can_change_hosting_services or service_change or can_retire_account or is_operator and perms.hosting.change_hostingaccount %}<a href="#account-management">Account management</a>{% endif %}
|
||||
<a href="#history">History</a>
|
||||
</nav>
|
||||
@@ -174,9 +174,6 @@
|
||||
<button class="button secondary compact" type="submit">Open phpMyAdmin</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
{% if is_operator %}
|
||||
<a class="button secondary compact" href="{% url 'panel:database_manifest' database.pk %}" aria-label="Download manifest for {{ database }}">Download manifest</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if is_operator and manage_database_provisioning and database.provisioning.problem %}
|
||||
<p class="heading-meta">{{ database.provisioning.problem }}</p>
|
||||
@@ -224,7 +221,6 @@
|
||||
{% if is_operator %}
|
||||
<td>
|
||||
<div class="form-actions">
|
||||
<a class="button secondary compact" href="{% url 'panel:dns_zone_manifest' zone.pk %}" aria-label="Download DNS manifest for {{ zone.domain }}">Download manifest</a>
|
||||
{% if show_dns_replication %}
|
||||
<a class="button secondary compact" href="{% url 'panel:dns_replication' zone.pk %}">Check replication</a>
|
||||
{% endif %}
|
||||
@@ -366,9 +362,6 @@
|
||||
{% if show_mailbox_credentials %}
|
||||
<a class="button secondary compact" href="{% url 'panel:mailbox_credentials' mailbox.pk %}" aria-label="Connection details for {{ mailbox }}">Connection details</a>
|
||||
{% endif %}
|
||||
{% if is_operator %}
|
||||
<a class="button secondary compact" href="{% url 'panel:mailbox_manifest' mailbox.pk %}" aria-label="Download mailbox manifest for {{ mailbox }}">Download manifest</a>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% include "hosting/mailbox_management_links.html" %}
|
||||
</div>
|
||||
@@ -402,19 +395,9 @@
|
||||
{% endif %}
|
||||
</section>
|
||||
{% endif %}
|
||||
{% if show_account_backups or export_backup_manifest %}
|
||||
{% if show_account_backups %}
|
||||
<section class="account-content" id="backups" data-account-section tabindex="-1" aria-label="Backups">
|
||||
{% if export_backup_manifest %}
|
||||
<div class="form-actions account-actions">
|
||||
{% if export_backup_manifest %}
|
||||
<a class="button secondary" href="{% url 'panel:account_backup_manifest' account.pk %}">Download backup manifest</a>
|
||||
{% endif %}
|
||||
{% if export_mail_backup_manifest %}
|
||||
<a class="button secondary" href="{% url 'panel:account_backup_manifest' account.pk %}?include_mail=1">Download backup manifest with mail</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if show_account_backups %}{% include "hosting/account_backups.html" %}{% endif %}
|
||||
{% include "hosting/account_backups.html" %}
|
||||
</section>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
@@ -33,9 +33,6 @@
|
||||
{% if action.value == 'settings' %}<a class="button secondary compact" href="{% url 'panel:independent_php_settings' account.pk website.pk %}">{{ action.label }}</a>
|
||||
{% else %}<a class="button {% if action.value == 'retire' %}danger{% else %}secondary{% endif %} compact" href="{% url 'panel:independent_website_operation' account.pk website.pk action.value %}">{{ action.label }}</a>{% endif %}
|
||||
{% endfor %}
|
||||
{% if is_operator and perms.hosting.view_hostingaccount %}
|
||||
<a class="button secondary compact" href="{% url 'panel:independent_website_manifest' account.pk website.pk %}">Download manifest</a>
|
||||
{% endif %}
|
||||
</div></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
||||
@@ -44,12 +44,8 @@
|
||||
<h2 id="configuration-heading">{% if plan_mode == "current" %}Primary HTTP virtual host{% else %}Virtual host configuration{% endif %}</h2>
|
||||
{% if plan_mode == "current" %}<p class="heading-meta">Coulobre’s recorded configuration. HTTPS and alias configuration are managed separately.</p>{% endif %}
|
||||
</div>
|
||||
{% if plan_mode != "history" and not is_reseller and plan.can_export %}
|
||||
<div class="form-actions">
|
||||
{% if may_manage_https %}<a class="button secondary" href="{% url 'panel:website_https' plan.account_id %}">Manage HTTPS</a>{% endif %}
|
||||
<a class="button secondary" href="{% url 'panel:download_deployment_manifest' plan.pk %}">Download deployment manifest</a>
|
||||
<a class="button secondary" href="{% url 'panel:download_smoke_bundle' plan.pk %}">Download test bundle</a>
|
||||
</div>
|
||||
{% if may_manage_https %}
|
||||
<a class="button secondary" href="{% url 'panel:website_https' plan.account_id %}">Manage HTTPS</a>
|
||||
{% endif %}
|
||||
</header>
|
||||
<pre class="configuration"><code>{{ plan.apache_config }}</code></pre>
|
||||
|
||||
@@ -83,7 +83,7 @@ class AccountBackupManifestTests(TestCase):
|
||||
(self.cms.spec, self.shop.spec),
|
||||
),
|
||||
)
|
||||
self.assertContains(
|
||||
self.assertNotContains(
|
||||
self.client.get(self.account.get_absolute_url()), self.url
|
||||
)
|
||||
|
||||
@@ -141,7 +141,7 @@ class AccountBackupManifestTests(TestCase):
|
||||
AccountBackup.from_manifest(response.json()).mailboxes,
|
||||
(old_mailbox.spec, current_mailbox.spec),
|
||||
)
|
||||
self.assertContains(
|
||||
self.assertNotContains(
|
||||
self.client.get(self.account.get_absolute_url()),
|
||||
"Download backup manifest with mail",
|
||||
)
|
||||
|
||||
@@ -121,7 +121,7 @@ class DatabaseProvisioningTests(TestCase):
|
||||
def test_disabled_provisioning_preserves_existing_manifest_download(self):
|
||||
detail = self.client.get(self.detail)
|
||||
self.assertNotContains(detail, "Provision database")
|
||||
self.assertContains(detail, "Download manifest")
|
||||
self.assertNotContains(detail, "Download manifest")
|
||||
self.assertEqual(self.client.post(self.url).status_code, 404)
|
||||
|
||||
def test_operator_can_retry_a_failed_request(self):
|
||||
|
||||
@@ -391,7 +391,7 @@ class DnsZonePanelTests(TestCase):
|
||||
zone = self.create_zone()
|
||||
self.client.force_login(self.reader)
|
||||
account = self.client.get(self.account.get_absolute_url())
|
||||
self.assertContains(account, "Download DNS manifest")
|
||||
self.assertNotContains(account, "Download DNS manifest")
|
||||
self.assertNotContains(account, "Edit DNS zone")
|
||||
self.assertContains(
|
||||
self.client.get(
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import json
|
||||
import tarfile
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from django.core.management import CommandError, call_command
|
||||
from django.test import TestCase
|
||||
|
||||
from .models import (
|
||||
HostingAccount,
|
||||
HostingDatabase,
|
||||
HostingDnsZone,
|
||||
HostingMailbox,
|
||||
HostingMailDomain,
|
||||
IndependentWebsite,
|
||||
WebProvisioningPlan,
|
||||
WebsiteDomain,
|
||||
)
|
||||
from .tasks import render_web_plan
|
||||
|
||||
|
||||
class ExportManifestTests(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.account = HostingAccount.objects.create(
|
||||
name="Example", primary_domain="example.net"
|
||||
)
|
||||
cls.database = HostingDatabase.objects.create(
|
||||
account=cls.account, suffix="cms"
|
||||
)
|
||||
domain = HostingMailDomain.objects.create(
|
||||
account=cls.account, domain=cls.account.primary_domain
|
||||
)
|
||||
cls.mailbox = HostingMailbox.objects.create(
|
||||
mail_domain=domain, local_part="contact"
|
||||
)
|
||||
cls.zone = HostingDnsZone.objects.create(
|
||||
account=cls.account,
|
||||
domain=cls.account.primary_domain,
|
||||
responsible_name="hostmaster.example.net",
|
||||
nameservers=["ns1.provider.example", "ns2.provider.example"],
|
||||
)
|
||||
cls.website = IndependentWebsite.objects.create(
|
||||
reservation=WebsiteDomain.objects.create(
|
||||
account=cls.account, domain="blog.example.net"
|
||||
)
|
||||
)
|
||||
cls.preview = WebProvisioningPlan.for_account(cls.account)
|
||||
cls.preview.save()
|
||||
|
||||
def export(self, resource, pk, *args):
|
||||
result = StringIO()
|
||||
call_command(
|
||||
"export_manifest", resource, str(pk), *args, stdout=result
|
||||
)
|
||||
return json.loads(result.getvalue())
|
||||
|
||||
def test_provisioning_exports_match_their_manifest_format(self):
|
||||
for resource, model in (
|
||||
("database", self.database),
|
||||
("dns-zone", self.zone),
|
||||
("mailbox", self.mailbox),
|
||||
("website", self.website),
|
||||
):
|
||||
with self.subTest(resource=resource):
|
||||
self.assertEqual(
|
||||
self.export(resource, model.pk), model.spec.manifest()
|
||||
)
|
||||
|
||||
def test_backup_manifest_can_include_requested_mailboxes(self):
|
||||
self.assertNotIn(
|
||||
"mailboxes", self.export("account-backup", self.account.pk)
|
||||
)
|
||||
self.assertEqual(
|
||||
self.export("account-backup", self.account.pk, "--include-mail")[
|
||||
"mailboxes"
|
||||
],
|
||||
[self.mailbox.spec.manifest()],
|
||||
)
|
||||
|
||||
def test_preview_must_be_ready_and_bundle_cannot_replace_a_file(self):
|
||||
with self.assertRaisesMessage(CommandError, "completed preview"):
|
||||
self.export("apache-preview", self.preview.pk)
|
||||
render_web_plan.run(self.preview.pk)
|
||||
self.assertEqual(
|
||||
self.export("apache-preview", self.preview.pk)["domain"],
|
||||
"example.net",
|
||||
)
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory, "test.tar")
|
||||
args = (
|
||||
"apache-preview",
|
||||
str(self.preview.pk),
|
||||
"--smoke-bundle",
|
||||
str(path),
|
||||
)
|
||||
call_command("export_manifest", *args)
|
||||
with tarfile.open(path) as archive:
|
||||
self.assertIn("apache.conf", archive.getnames())
|
||||
with self.assertRaisesMessage(CommandError, "Could not write"):
|
||||
call_command("export_manifest", *args)
|
||||
@@ -82,6 +82,7 @@ class MailboxImportTests(TestCase):
|
||||
email = reverse("panel:domain_workspace", args=[account.pk, "email"])
|
||||
for path in (email, account.get_absolute_url()):
|
||||
response = self.client.get(path, secure=True)
|
||||
self.assertNotContains(response, "View import result")
|
||||
for name, label, variant in (
|
||||
("mailbox_import", "Import mail", "secondary"),
|
||||
(
|
||||
@@ -126,11 +127,19 @@ class MailboxImportTests(TestCase):
|
||||
job.finished_at = timezone.now()
|
||||
job.problem = "Check your source password."
|
||||
job.save(update_fields=["finished_at", "problem"])
|
||||
self.assertContains(self.client.get(email), "Import needs attention")
|
||||
for path in (email, account.get_absolute_url()):
|
||||
response = self.client.get(path)
|
||||
self.assertContains(response, "Import needs attention")
|
||||
self.assertContains(response, ">Review import</a>")
|
||||
self.assertNotContains(response, "View import result")
|
||||
job.problem = ""
|
||||
job.save(update_fields=["problem"])
|
||||
self.assertContains(self.client.get(email), "Import complete")
|
||||
self.assertNotContains(self.client.get(email), "Import in progress")
|
||||
for path in (email, account.get_absolute_url()):
|
||||
response = self.client.get(path)
|
||||
self.assertContains(response, "Import complete")
|
||||
self.assertContains(response, ">View import result</a>", count=1)
|
||||
self.assertContains(response, ">Import mail</a>", count=1)
|
||||
self.assertNotContains(response, "Import in progress")
|
||||
|
||||
def test_only_the_owner_and_authorized_operator_can_start_import(self):
|
||||
self.sign_in(self.stranger)
|
||||
|
||||
@@ -39,7 +39,7 @@ class WebSmokeBundleTests(TestCase):
|
||||
render_web_plan.run(self.plan.pk)
|
||||
|
||||
detail = self.client.get(reverse("panel:plan", args=[self.plan.pk]))
|
||||
self.assertContains(detail, "Download test bundle")
|
||||
self.assertNotContains(detail, "Download test bundle")
|
||||
response = self.client.get(self.download)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response["Content-Type"], "application/x-tar")
|
||||
@@ -96,7 +96,7 @@ class WebSmokeBundleTests(TestCase):
|
||||
render_web_plan.run(self.plan.pk)
|
||||
|
||||
detail = self.client.get(reverse("panel:plan", args=[self.plan.pk]))
|
||||
self.assertContains(detail, "Download deployment manifest")
|
||||
self.assertNotContains(detail, "Download deployment manifest")
|
||||
response = self.client.get(self.manifest)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertIn(".json", response["Content-Disposition"])
|
||||
|
||||
@@ -824,12 +824,6 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView):
|
||||
context["can_enable_alias_https"] = panel_feature_allowed(
|
||||
"LOCAL_WEB_HTTPS"
|
||||
) and user.has_perms(ENABLE_WEB_HTTPS)
|
||||
context["export_backup_manifest"] = not tenant and user.has_perms(
|
||||
EXPORT_BACKUP_MANIFEST
|
||||
)
|
||||
context["export_mail_backup_manifest"] = context[
|
||||
"export_backup_manifest"
|
||||
] and user.has_perm(VIEW_MAILBOXES)
|
||||
if (
|
||||
settings.LOCAL_ACCOUNT_BACKUPS
|
||||
and not tenant
|
||||
|
||||
Reference in new issue
Block a user