Skip to content

Commit

Permalink
Add enable / disable vHost feature (#94)
Browse files Browse the repository at this point in the history
Provide the option to disable virtual hosts which removed the host from
the web server configuration and do not try to create LE certificates.

Closes #59
  • Loading branch information
drscream authored May 31, 2024
1 parent 2db6d2f commit d8e0a83
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion kumquat/management/commands/letsencrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def issue_cert():
# Use or generate new account for ACME API
key, regr = account()

vhosts = [vhost for vhost in VHost.objects.filter(use_letsencrypt=True) if vhost.letsencrypt_state() in ['REQUEST', 'RENEW']]
vhosts = [vhost for vhost in VHost.objects.filter(use_letsencrypt=True, is_enabled=True) if vhost.letsencrypt_state() in ['REQUEST', 'RENEW']]
if not vhosts:
return

Expand Down
2 changes: 1 addition & 1 deletion kumquat/management/commands/update_vhosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def write_certs():

def write_vhost_config():
config = ''
for vhost in VHost.objects.all().annotate(is_defaultvhost=Count('defaultvhost')).order_by('is_defaultvhost'):
for vhost in VHost.objects.filter(is_enabled = True).annotate(is_defaultvhost=Count('defaultvhost')).order_by('is_defaultvhost'):
context = {
'vhost': vhost,
}
Expand Down
18 changes: 18 additions & 0 deletions web/migrations/0014_vhost_is_enabled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.13 on 2024-05-31 10:29

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('web', '0013_vhost_access_logging'),
]

operations = [
migrations.AddField(
model_name='vhost',
name='is_enabled',
field=models.BooleanField(default=True, verbose_name='Enable virtual host'),
),
]
1 change: 1 addition & 0 deletions web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class VHost(models.Model):
name = models.CharField(max_length=default_length, verbose_name=_('Sub Domain'), help_text=_('Child part of your domain that is used to organize your site content.'), validators=[DomainNameValidator()])
domain = models.ForeignKey(Domain, blank=False, on_delete=models.CASCADE)
cert = models.ForeignKey('SSLCert', blank=True, null=True, on_delete=models.SET_NULL, verbose_name='SSL Certificate')
is_enabled = models.BooleanField(verbose_name=_('Enable virtual host'), default=True)
use_letsencrypt = models.BooleanField(verbose_name=_('SSL Certificate managed by Let\'s Encrypt'), default=False)
access_logging = models.BooleanField(verbose_name=_('Enable web server access log'), default=False)

Expand Down
2 changes: 2 additions & 0 deletions web/templates/web/vhost_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ <h1>{% trans "VHosts" %}</h1>
<thead>
<tr>
<th>{% trans "VHost" %}</th>
<th>{% trans "Enabled" %}</th>
<th>{% trans "SSL" %}</th>
<th>{% trans "Catch-All" %}</th>
<th>&nbsp;</th>
Expand All @@ -23,6 +24,7 @@ <h1>{% trans "VHosts" %}</h1>
{% for vhost in object_list %}
<tr>
<td><a href="{% url "web_vhost_update" vhost.pk %}">{{ vhost }}</a></td>
<td>{% if vhost.is_enabled %}{% bootstrap_icon "ok" %}{% else %}{% bootstrap_icon "remove" %}{% endif %}</td>
<td>{% if vhost.cert %}{% bootstrap_icon "ok" %}{% else %}{% bootstrap_icon "remove" %}{% endif %}</td>
<td>
{% if vhost.defaultvhost_set.count %}
Expand Down
4 changes: 2 additions & 2 deletions web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ class VHostList(LoginRequiredMixin, ListView):

class VHostCreate(LoginRequiredMixin, SuccessMessageMixin, SuccessActionFormMixin, CreateView):
model = VHost
fields = ('name', 'domain', 'cert', 'use_letsencrypt', 'access_logging')
fields = ('name', 'domain', 'cert', 'is_enabled', 'use_letsencrypt', 'access_logging')
success_url = reverse_lazy('web_vhost_list')
success_message = _("%(name)s was created successfully")
success_action = update_vhosts

class VHostUpdate(LoginRequiredMixin, SuccessActionFormMixin, UpdateView):
model = VHost
fields = ['cert', 'use_letsencrypt', 'access_logging']
fields = ['cert', 'is_enabled', 'use_letsencrypt', 'access_logging']
success_url = reverse_lazy('web_vhost_list')
success_action = update_vhosts

Expand Down

0 comments on commit d8e0a83

Please # to comment.