Queue managed databases and DNS zones on creation
This commit is contained in:
1 parent
cf9ce8e5b6
commit
a8985c3041
10 files changed
+166
-79
No files matched your search
+11
-8
@@ -1115,10 +1115,12 @@ sudo systemctl restart coulobre-panel coulobre-worker coulobre-beat
|
||||
sudo systemctl enable --now coulobre-database-provisioner.service
|
||||
```
|
||||
|
||||
Open a hosting account and select **Provision database** for a database request.
|
||||
Reload the account page to see the status. Use **Retry provisioning** after a
|
||||
reported failure. Operators need permission to view hosting accounts and
|
||||
database requests and the **Can add database provisioning** permission.
|
||||
Select **Add database** on a hosting account to queue its provisioning.
|
||||
Customers can add databases on their own accounts. Customer managers need
|
||||
permission to manage resources for their assigned accounts. For a pending
|
||||
request, an operator can select **Provision database**. Operators need
|
||||
permission to view hosting accounts and databases and the **Can add database
|
||||
provisioning** permission. Use **Retry provisioning** after a reported failure.
|
||||
|
||||
The MariaDB node stores the application login in the root-only file
|
||||
`/var/lib/coulobre/database-node/<database-name>.json`. Use the credentials
|
||||
@@ -1774,10 +1776,11 @@ sudo systemctl restart coulobre-panel coulobre-worker coulobre-beat
|
||||
sudo systemctl enable --now coulobre-dns-publisher.service
|
||||
```
|
||||
|
||||
Open a hosting account and select **Publish zone**. Reload the account page to
|
||||
see progress and the last SOA serial confirmed by the local BIND9 node.
|
||||
Operators need permission to view hosting accounts and DNS zones and the
|
||||
**Can add DNS publication** permission.
|
||||
Select **Save and publish zone** to queue a new zone from a hosting account.
|
||||
Use **Publish zone** to publish a pending revision of an existing zone.
|
||||
The account page shows progress and the last SOA serial confirmed by the
|
||||
local BIND9 node. Operators need permission to view hosting accounts and DNS
|
||||
zones and the **Can add DNS publication** permission.
|
||||
|
||||
Each request captures the reviewed SOA serial and records. You can edit the
|
||||
zone while it publishes. Publish the newer revision after the current request
|
||||
|
||||
@@ -27,7 +27,9 @@ def lock_hosting_account(account_id, customer_id):
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def create_database_request(account_id, suffix, *, customer_id=None):
|
||||
def create_database_request(
|
||||
account_id, suffix, *, customer_id=None, auto_provision=False
|
||||
):
|
||||
account = lock_hosting_account(account_id, customer_id)
|
||||
require_open_account(account)
|
||||
databases = account.databases
|
||||
@@ -45,7 +47,11 @@ def create_database_request(account_id, suffix, *, customer_id=None):
|
||||
raise ValidationError(
|
||||
"This hosting account has reached its database limit."
|
||||
)
|
||||
return databases.create(suffix=suffix)
|
||||
database = databases.create(suffix=suffix)
|
||||
if auto_provision:
|
||||
require_active_services(account)
|
||||
DatabaseProvisioning.objects.queue(database=database)
|
||||
return database
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
|
||||
@@ -133,7 +133,7 @@
|
||||
<section id="databases" data-account-section tabindex="-1" class="surface" aria-labelledby="databases-heading">
|
||||
<header class="section-heading">
|
||||
<div>
|
||||
<h2 id="databases-heading" tabindex="-1">MariaDB database requests</h2>
|
||||
<h2 id="databases-heading" tabindex="-1">MariaDB databases</h2>
|
||||
{% if is_tenant or perms.hosting.view_hostingplan %}
|
||||
{% if account.hosting_plan and account.hosting_plan.database_limit is not None %}
|
||||
<p class="heading-meta">{{ databases|length }} of {{ account.hosting_plan.database_limit }} databases requested</p>
|
||||
@@ -141,51 +141,50 @@
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if is_customer or reseller_resource_management or is_operator and perms.hosting.add_hostingdatabase %}
|
||||
<a class="button primary" href="{% url 'panel:database_create' account.pk %}">Request database</a>
|
||||
<a class="button primary" href="{% url 'panel:database_create' account.pk %}">{% if automatic_database_provisioning %}Add{% else %}Request{% endif %} database</a>
|
||||
{% endif %}
|
||||
</header>
|
||||
{% if databases %}
|
||||
<div class="table-scroll">
|
||||
<table>
|
||||
<thead><tr><th scope="col">Database</th>{% if is_operator or manage_database_provisioning %}<th scope="col">Deployment</th>{% endif %}</tr></thead>
|
||||
<thead><tr><th scope="col">Database</th><th scope="col">Status and actions</th></tr></thead>
|
||||
<tbody>
|
||||
{% for database in databases %}
|
||||
<tr>
|
||||
<td class="domain">
|
||||
{{ database }}
|
||||
{% if show_database_credentials %}
|
||||
<div><a class="record-link" href="{% url 'panel:database_credentials' database.pk %}" aria-label="Connection details for {{ database }}">Connection details</a></div>
|
||||
{% endif %}
|
||||
{% if show_phpmyadmin %}
|
||||
<form method="post" action="{% url 'panel:phpmyadmin_launch' database.pk %}">
|
||||
{% csrf_token %}
|
||||
<button class="button secondary" type="submit">Open phpMyAdmin</button>
|
||||
</form>
|
||||
<th scope="row" class="domain">{{ database }}</th>
|
||||
<td>
|
||||
<div class="table-actions">
|
||||
{% if manage_database_provisioning %}
|
||||
{% url 'panel:database_provision' database.pk as provision_url %}
|
||||
{% include "hosting/resource_provisioning.html" with provisioning=database.provisioning enabled=local_database_enabled action=provision_url resource=database provision_label="Provision database" %}
|
||||
{% else %}
|
||||
{% include "hosting/resource_provisioning.html" with provisioning=database.provisioning enabled=False %}
|
||||
{% endif %}
|
||||
{% if not database.provisioning %}<span class="status pending">Requested</span>{% endif %}
|
||||
{% if show_database_credentials %}
|
||||
<a class="record-link" href="{% url 'panel:database_credentials' database.pk %}" aria-label="Connection details for {{ database }}">Connection details</a>
|
||||
{% endif %}
|
||||
{% if show_phpmyadmin %}
|
||||
<form method="post" action="{% url 'panel:phpmyadmin_launch' database.pk %}">
|
||||
{% csrf_token %}
|
||||
<button class="button secondary" type="submit">Open phpMyAdmin</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
{% if is_operator %}
|
||||
<a class="record-link" 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>
|
||||
{% endif %}
|
||||
</td>
|
||||
{% if is_operator or manage_database_provisioning %}
|
||||
<td>
|
||||
<div class="form-actions">
|
||||
{% if is_operator %}
|
||||
<a class="record-link" href="{% url 'panel:database_manifest' database.pk %}" aria-label="Download manifest for {{ database }}">Download manifest</a>
|
||||
{% endif %}
|
||||
{% if manage_database_provisioning %}
|
||||
{% url 'panel:database_provision' database.pk as provision_url %}
|
||||
{% include "hosting/resource_provisioning.html" with provisioning=database.provisioning enabled=local_database_enabled action=provision_url resource=database provision_label="Provision database" %}
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if is_operator and manage_database_provisioning and database.provisioning.problem %}
|
||||
<p class="heading-meta">{{ database.provisioning.problem }}</p>
|
||||
{% endif %}
|
||||
</td>
|
||||
{% endif %}
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% else %}
|
||||
<p class="empty">No databases requested yet.</p>
|
||||
<p class="empty">No databases yet.</p>
|
||||
{% endif %}
|
||||
</section>
|
||||
{% endif %}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
{% extends "hosting/base.html" %}
|
||||
{% block title %}Request MariaDB database · Coulobre Panel{% endblock %}
|
||||
{% block title %}{% if auto_provision %}Add{% else %}Request{% endif %} MariaDB database · Coulobre Panel{% endblock %}
|
||||
{% block content %}
|
||||
<a class="back-link" href="{{ account.get_absolute_url }}">← {{ account.name }}</a>
|
||||
<header class="page-heading"><h1>Request MariaDB database</h1></header>
|
||||
<header class="page-heading"><h1>{% if auto_provision %}Add{% else %}Request{% endif %} MariaDB database</h1></header>
|
||||
<section class="surface editor-surface">
|
||||
<form method="post" class="editor">
|
||||
{% include "hosting/customer_action_fields.html" %}
|
||||
{{ form.as_div }}
|
||||
<div class="form-actions">
|
||||
{% if can_request_resource %}
|
||||
<button class="button primary" type="submit">Request database</button>
|
||||
<button class="button primary" type="submit">{% if auto_provision %}Add{% else %}Request{% endif %} database</button>
|
||||
{% endif %}
|
||||
<a class="button secondary" href="{{ account.get_absolute_url }}">Cancel</a>
|
||||
</div>
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
<script src="{% static 'hosting/dns-record-editor.js' %}" defer></script>
|
||||
{% endif %}
|
||||
<div class="form-actions">
|
||||
<button class="button primary" type="submit">Save zone</button>
|
||||
<button class="button primary" type="submit">{% if publish_on_create %}Save and publish zone{% else %}Save zone{% endif %}</button>
|
||||
<a class="button secondary" href="{{ account.get_absolute_url }}">Cancel</a>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
{% extends "hosting/base.html" %}
|
||||
{% block title %}{% if automatic_mailbox_provisioning %}Add{% else %}Request{% endif %} mailbox · Coulobre Panel{% endblock %}
|
||||
{% block title %}{% if auto_provision %}Add{% else %}Request{% endif %} mailbox · Coulobre Panel{% endblock %}
|
||||
{% block content %}
|
||||
<a class="back-link" href="{{ account.get_absolute_url }}">← {{ account.name }}</a>
|
||||
<header class="page-heading">
|
||||
<div>
|
||||
<h1>{% if automatic_mailbox_provisioning %}Add{% else %}Request{% endif %} mailbox</h1>
|
||||
<h1>{% if auto_provision %}Add{% else %}Request{% endif %} mailbox</h1>
|
||||
<p class="heading-meta domain">{{ account.primary_domain }}</p>
|
||||
</div>
|
||||
</header>
|
||||
@@ -14,7 +14,7 @@
|
||||
{{ form.as_div }}
|
||||
<div class="form-actions">
|
||||
{% if can_request_resource %}
|
||||
<button class="button primary" type="submit">{% if automatic_mailbox_provisioning %}Add{% else %}Request{% endif %} mailbox</button>
|
||||
<button class="button primary" type="submit">{% if auto_provision %}Add{% else %}Request{% endif %} mailbox</button>
|
||||
{% endif %}
|
||||
<a class="button secondary" href="{{ account.get_absolute_url }}">Cancel</a>
|
||||
</div>
|
||||
|
||||
@@ -87,6 +87,34 @@ class DatabaseProvisioningTests(TestCase):
|
||||
self.assertEqual(self.client.post(self.url).status_code, 403)
|
||||
self.assertFalse(DatabaseProvisioning.objects.exists())
|
||||
|
||||
def test_customer_creation_queues_a_database_and_shows_its_status(self):
|
||||
self.client.force_login(self.user)
|
||||
url = reverse("panel:database_create", args=[self.account.pk])
|
||||
self.assertContains(self.client.get(url), "Add database")
|
||||
self.assertRedirects(
|
||||
self.client.post(url, {"suffix": "new"}), self.detail
|
||||
)
|
||||
job = DatabaseProvisioning.objects.get(database__suffix="new")
|
||||
self.assertEqual(job.state, "queued")
|
||||
self.assertContains(self.client.get(self.detail), "Queued")
|
||||
|
||||
def test_operator_without_provisioning_permission_creates_a_request(self):
|
||||
requester = get_user_model().objects.create_user("requester")
|
||||
requester.user_permissions.add(
|
||||
*self.permissions.exclude(codename="add_databaseprovisioning"),
|
||||
Permission.objects.get(codename="add_hostingdatabase"),
|
||||
)
|
||||
self.client.force_login(requester)
|
||||
url = reverse("panel:database_create", args=[self.account.pk])
|
||||
self.assertContains(self.client.get(url), "Request database")
|
||||
self.assertRedirects(
|
||||
self.client.post(url, {"suffix": "request"}), self.detail
|
||||
)
|
||||
self.assertTrue(
|
||||
self.account.databases.filter(suffix="request").exists()
|
||||
)
|
||||
self.assertFalse(DatabaseProvisioning.objects.exists())
|
||||
|
||||
@override_settings(LOCAL_DATABASE_PROVISIONING=False)
|
||||
def test_disabled_provisioning_preserves_existing_manifest_download(self):
|
||||
detail = self.client.get(self.detail)
|
||||
|
||||
@@ -42,6 +42,31 @@ class DnsPublicationTests(TestCase):
|
||||
self.url = reverse("panel:dns_zone_publish", args=[self.zone.pk])
|
||||
self.detail = self.account.get_absolute_url()
|
||||
|
||||
def test_creating_a_managed_zone_queues_its_first_snapshot(self):
|
||||
account = HostingAccount.objects.create(
|
||||
name="Example DNS", primary_domain="new.example"
|
||||
)
|
||||
url = reverse("panel:dns_zone_create", args=[account.pk])
|
||||
self.assertContains(self.client.get(url), "Save and publish zone")
|
||||
self.assertRedirects(
|
||||
self.client.post(
|
||||
url,
|
||||
{
|
||||
"nameservers": "ns.provider.example",
|
||||
"responsible_name": "hostmaster.new.example",
|
||||
"addresses": "new.example 192.0.2.20",
|
||||
},
|
||||
),
|
||||
account.get_absolute_url(),
|
||||
)
|
||||
zone = account.dns_zones.get()
|
||||
self.assertEqual(
|
||||
(zone.publication.state, zone.publication.serial), ("queued", 1)
|
||||
)
|
||||
self.assertEqual(
|
||||
DnsZone.from_manifest(zone.publication.manifest), zone.spec
|
||||
)
|
||||
|
||||
def test_queued_snapshot_survives_edits_and_next_revision_can_publish(
|
||||
self,
|
||||
):
|
||||
|
||||
@@ -196,13 +196,11 @@ class ResellerOnboardingTests(TestCase):
|
||||
for model, resource in (("database", database), ("mailbox", mailbox)):
|
||||
url = reverse(f"panel:{model}_provision", args=[resource.pk])
|
||||
manifest = reverse(f"panel:{model}_manifest", args=[resource.pk])
|
||||
if model == "database":
|
||||
self.assertContains(
|
||||
self.client.get(detail), "Provision database"
|
||||
)
|
||||
else:
|
||||
self.assertEqual(mailbox.provisioning.state, "queued")
|
||||
self.assertContains(self.client.get(detail), "Queued")
|
||||
self.assertEqual(resource.provisioning.state, "queued")
|
||||
self.assertContains(self.client.get(detail), "Queued")
|
||||
self.assertNotContains(
|
||||
self.client.get(detail), f"Provision {model}"
|
||||
)
|
||||
self.assertEqual(self.client.get(manifest).status_code, 403)
|
||||
for _ in range(2):
|
||||
self.assertRedirects(self.client.post(url), detail)
|
||||
@@ -214,7 +212,7 @@ class ResellerOnboardingTests(TestCase):
|
||||
"action", flat=True
|
||||
)
|
||||
),
|
||||
[Action.RESOURCE_REQUEST] * 2 + [Action.RESOURCE_PROVISION] * 2,
|
||||
[Action.RESOURCE_REQUEST, Action.RESOURCE_PROVISION] * 2,
|
||||
)
|
||||
DatabaseProvisioning.objects.update(
|
||||
finished_at=timezone.now(), problem="Failed"
|
||||
|
||||
@@ -238,10 +238,8 @@ PROVISION_MAILBOX = (
|
||||
CHANGE_MAILBOX_QUOTA = (*PROVISION_MAILBOX, "hosting.change_hostingmailbox")
|
||||
|
||||
|
||||
def automatic_mailbox_provisioning(user):
|
||||
return settings.LOCAL_MAILBOX_PROVISIONING and (
|
||||
is_tenant(user) or user.has_perms(PROVISION_MAILBOX)
|
||||
)
|
||||
def automatic_resource_provisioning(user, enabled, permissions):
|
||||
return enabled and (is_tenant(user) or user.has_perms(permissions))
|
||||
|
||||
|
||||
def missing_account_setup(account, actor):
|
||||
@@ -830,11 +828,17 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView):
|
||||
"show_mailbox_credentials"
|
||||
] and roundcube_available(self.request)
|
||||
if tenant or user.has_perm(VIEW_DATABASES):
|
||||
databases = self.object.databases.all()
|
||||
context["automatic_database_provisioning"] = (
|
||||
automatic_resource_provisioning(
|
||||
user,
|
||||
settings.LOCAL_DATABASE_PROVISIONING,
|
||||
PROVISION_DATABASE,
|
||||
)
|
||||
)
|
||||
databases = self.object.databases.select_related("provisioning")
|
||||
if manage_resources or (
|
||||
not tenant and user.has_perms(PROVISION_DATABASE)
|
||||
):
|
||||
databases = databases.select_related("provisioning")
|
||||
context["manage_database_provisioning"] = True
|
||||
context["local_database_enabled"] = (
|
||||
settings.LOCAL_DATABASE_PROVISIONING
|
||||
@@ -898,7 +902,11 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView):
|
||||
)
|
||||
if tenant or user.has_perm(VIEW_MAILBOXES):
|
||||
context["automatic_mailbox_provisioning"] = (
|
||||
automatic_mailbox_provisioning(user)
|
||||
automatic_resource_provisioning(
|
||||
user,
|
||||
settings.LOCAL_MAILBOX_PROVISIONING,
|
||||
PROVISION_MAILBOX,
|
||||
)
|
||||
)
|
||||
context["manage_mail_routing"] = mail_policy_enabled(
|
||||
self.request, "routing"
|
||||
@@ -1932,6 +1940,8 @@ def provisioning_conflict(error):
|
||||
|
||||
class ResourceRequestCreate(PermissionRequiredMixin, CreateView):
|
||||
audit_operation = None
|
||||
provisioning_setting = None
|
||||
provisioning_permissions = ()
|
||||
|
||||
@cached_property
|
||||
def customer(self):
|
||||
@@ -1959,9 +1969,23 @@ class ResourceRequestCreate(PermissionRequiredMixin, CreateView):
|
||||
return super().get_context_data(
|
||||
account=self.account,
|
||||
can_request_resource=can_edit_customer_resource(self.request),
|
||||
auto_provision=self.auto_provision,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def auto_provision(self):
|
||||
return automatic_resource_provisioning(
|
||||
self.request.user,
|
||||
getattr(settings, self.provisioning_setting),
|
||||
self.provisioning_permissions,
|
||||
)
|
||||
|
||||
def create_request(self, account_id, **values):
|
||||
return self.create_resource(
|
||||
account_id, auto_provision=self.auto_provision, **values
|
||||
)
|
||||
|
||||
def get_form(self, form_class=None):
|
||||
form = super().get_form(form_class)
|
||||
if not can_edit_customer_resource(self.request):
|
||||
@@ -2008,7 +2032,9 @@ class DatabaseCreate(ResourceRequestCreate):
|
||||
template_name = "hosting/database_form.html"
|
||||
permission_required = CUSTOMER_WRITE_PERMISSIONS["panel:database_create"]
|
||||
audit_operation = CustomerImpersonationAction.Operation.DATABASE
|
||||
create_request = staticmethod(create_database_request)
|
||||
provisioning_setting = "LOCAL_DATABASE_PROVISIONING"
|
||||
provisioning_permissions = PROVISION_DATABASE
|
||||
create_resource = staticmethod(create_database_request)
|
||||
|
||||
|
||||
class DownloadDatabaseManifest(
|
||||
@@ -2129,6 +2155,17 @@ class DnsZoneCreate(OperatorOnlyMixin, DnsZoneEditor):
|
||||
"record_rows": "[]",
|
||||
}
|
||||
|
||||
@cached_property
|
||||
def publish_on_create(self):
|
||||
return settings.LOCAL_DNS_PUBLISHING and self.request.user.has_perms(
|
||||
PUBLISH_DNS
|
||||
)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
return super().get_context_data(
|
||||
publish_on_create=self.publish_on_create, **kwargs
|
||||
)
|
||||
|
||||
def form_valid(self, form):
|
||||
try:
|
||||
with transaction.atomic():
|
||||
@@ -2136,12 +2173,14 @@ class DnsZoneCreate(OperatorOnlyMixin, DnsZoneEditor):
|
||||
self.account.pk, customer_id=None
|
||||
)
|
||||
require_open_account(account)
|
||||
HostingDnsZone.objects.create(
|
||||
zone = HostingDnsZone.objects.create(
|
||||
account=account,
|
||||
domain=account.primary_domain,
|
||||
**form.cleaned_data,
|
||||
)
|
||||
except ValidationError as error:
|
||||
if self.publish_on_create:
|
||||
queue_dns_publication(zone.pk, str(zone.serial))
|
||||
except (ValidationError, ValueError) as error:
|
||||
form.add_error(None, error)
|
||||
return self.form_invalid(form)
|
||||
except IntegrityError:
|
||||
@@ -2332,20 +2371,9 @@ class MailboxCreate(ResourceRequestCreate):
|
||||
permission_required = CUSTOMER_WRITE_PERMISSIONS["panel:mailbox_create"]
|
||||
audit_operation = CustomerImpersonationAction.Operation.MAILBOX
|
||||
|
||||
def create_request(self, account_id, **values):
|
||||
return create_mailbox_request(
|
||||
account_id,
|
||||
auto_provision=automatic_mailbox_provisioning(self.request.user),
|
||||
**values,
|
||||
)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
return super().get_context_data(
|
||||
automatic_mailbox_provisioning=automatic_mailbox_provisioning(
|
||||
self.request.user
|
||||
),
|
||||
**kwargs,
|
||||
)
|
||||
provisioning_setting = "LOCAL_MAILBOX_PROVISIONING"
|
||||
provisioning_permissions = PROVISION_MAILBOX
|
||||
create_resource = staticmethod(create_mailbox_request)
|
||||
|
||||
def get_form(self, form_class=None):
|
||||
form = super().get_form(form_class)
|
||||
|
||||
Reference in new issue
Block a user