mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-11-22 04:43:18 +01:00
Add distributed cluster mode and sync peer network routes on reload
This commit is contained in:
+20
-1
@@ -8,6 +8,7 @@ from django.conf import settings
|
||||
from django.urls import reverse_lazy
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from cluster.models import ClusterSettings, Worker
|
||||
from wgwadmlibrary.tools import is_valid_ip_or_hostname
|
||||
from .models import NETMASK_CHOICES, WireGuardInstance
|
||||
|
||||
@@ -34,7 +35,8 @@ class WireGuardInstanceForm(forms.ModelForm):
|
||||
model = WireGuardInstance
|
||||
fields = [
|
||||
'name', 'instance_id', 'private_key', 'public_key','hostname', 'listen_port', 'address',
|
||||
'netmask', 'peer_list_refresh_interval', 'dns_primary', 'dns_secondary', 'dns_search_domains', 'enforce_route_policy'
|
||||
'netmask', 'peer_list_refresh_interval', 'dns_primary', 'dns_secondary', 'dns_search_domains', 'enforce_route_policy',
|
||||
'worker'
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
@@ -50,6 +52,19 @@ class WireGuardInstanceForm(forms.ModelForm):
|
||||
self.fields['peer_list_refresh_interval'].widget.attrs['readonly'] = True
|
||||
self.fields['private_key'].widget = forms.PasswordInput(attrs={'class': 'form-control'}, render_value=True)
|
||||
|
||||
# The server that runs the instance is only chosen in the distributed cluster mode
|
||||
distributed = ClusterSettings.is_distributed()
|
||||
if distributed:
|
||||
self.fields['worker'].queryset = Worker.objects.order_by('name')
|
||||
self.fields['worker'].label = _('Server')
|
||||
self.fields['worker'].empty_label = _('Primary Server')
|
||||
self.fields['worker'].help_text = _(
|
||||
'The instance runs only on this server. Peer configurations of instances on a worker use the '
|
||||
'hostname of the worker as endpoint.'
|
||||
)
|
||||
else:
|
||||
del self.fields['worker']
|
||||
|
||||
self.helper = FormHelper()
|
||||
self.helper.form_method = 'post'
|
||||
|
||||
@@ -100,6 +115,10 @@ class WireGuardInstanceForm(forms.ModelForm):
|
||||
Column('enforce_route_policy', css_class='form-group col-md-12 mb-0'),
|
||||
css_class='form-row'
|
||||
),
|
||||
Row(
|
||||
Column('worker', css_class='form-group col-md-12 mb-0'),
|
||||
css_class='form-row'
|
||||
) if distributed else HTML(''),
|
||||
css_class='col-lg-12'
|
||||
),
|
||||
css_class='row'
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
# Generated by Django 5.2.19 on 2026-11-20 19:13
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('cluster', '0019_alter_clustersettings_cluster_mode'),
|
||||
('wireguard', '0033_wireguardinstance_dns_search_domains'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='wireguardinstance',
|
||||
name='worker',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='wireguard_instances', to='cluster.worker'),
|
||||
),
|
||||
]
|
||||
@@ -4,6 +4,7 @@ from typing import Optional
|
||||
|
||||
from django.db import models
|
||||
|
||||
from cluster.models import ClusterSettings
|
||||
from wireguard_tools.networks import normalize_cidr_list, normalize_cidr_pairs, safe_network_cidr
|
||||
|
||||
NETMASK_CHOICES = (
|
||||
@@ -68,6 +69,10 @@ class WireGuardInstance(models.Model):
|
||||
pending_changes = models.BooleanField(default=True)
|
||||
legacy_firewall = models.BooleanField(default=False)
|
||||
enforce_route_policy = models.BooleanField(default=False)
|
||||
# Server that runs the instance in the distributed cluster mode. Empty means the primary server.
|
||||
worker = models.ForeignKey(
|
||||
'cluster.Worker', on_delete=models.PROTECT, blank=True, null=True, related_name='wireguard_instances'
|
||||
)
|
||||
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
updated = models.DateTimeField(auto_now=True)
|
||||
@@ -79,6 +84,13 @@ class WireGuardInstance(models.Model):
|
||||
else:
|
||||
return 'wg' + str(self.instance_id)
|
||||
|
||||
@property
|
||||
def hosting_worker(self):
|
||||
"""Worker that runs this instance, or None when it runs on the primary server."""
|
||||
if self.worker_id and ClusterSettings.is_distributed():
|
||||
return self.worker
|
||||
return None
|
||||
|
||||
@property
|
||||
def network_cidr(self):
|
||||
return safe_network_cidr(self.address, self.netmask)
|
||||
|
||||
Reference in New Issue
Block a user