Add export_configs management command for WireGuard and Caddy configurations

This commit is contained in:
Eduardo Silva
2026-03-24 15:18:17 -03:00
parent 018cf1380e
commit d14615a73e
4 changed files with 39 additions and 9 deletions

16
init.sh
View File

@@ -4,6 +4,13 @@ set -e
# Lets wait for the DNS container to start # Lets wait for the DNS container to start
sleep 5 sleep 5
# Django startup
python manage.py migrate --noinput
python manage.py collectstatic --noinput
# Export WireGuard, firewall and Caddy configs before bringing up interfaces
python manage.py export_configs
# Starts each WireGuard configuration file found in /etc/wireguard # Starts each WireGuard configuration file found in /etc/wireguard
shopt -s nullglob shopt -s nullglob
config_files=(/etc/wireguard/*.conf) config_files=(/etc/wireguard/*.conf)
@@ -12,15 +19,6 @@ if [ ${#config_files[@]} -gt 0 ]; then
wg-quick up "$(basename "${f}" .conf)" wg-quick up "$(basename "${f}" .conf)"
done done
fi fi
# Django startup
python manage.py migrate --noinput
python manage.py collectstatic --noinput
if [[ "${CADDY_ENABLED,,}" == "true" ]]; then
echo "Exporting Caddy configuration (auth_policies.json, applications.json, routes.json)..."
python manage.py shell -c "from app_gateway.caddy_config_export import export_caddy_config; export_caddy_config('/caddy_json_export')" || echo "Failed to export Caddy configuration."
fi
if [[ "${DEV_MODE,,}" == "true" ]]; then if [[ "${DEV_MODE,,}" == "true" ]]; then
echo "" echo ""
echo "" echo ""

View File

View File

@@ -0,0 +1,32 @@
import os
from django.conf import settings
from django.core.management.base import BaseCommand
from wireguard_tools.views import export_firewall_configuration, export_wireguard_configuration
class Command(BaseCommand):
help = 'Export WireGuard, firewall and Caddy configuration files'
def handle(self, *args, **options):
self.stdout.write('Exporting WireGuard configuration...')
export_wireguard_configuration()
self.stdout.write(self.style.SUCCESS('WireGuard configuration exported.'))
self.stdout.write('Exporting firewall configuration...')
export_firewall_configuration()
self.stdout.write(self.style.SUCCESS('Firewall configuration exported.'))
if settings.CADDY_ENABLED:
self.stdout.write('Exporting Caddy configuration...')
try:
from app_gateway.caddy_config_export import export_caddy_config
export_caddy_config('/caddy_json_export/')
if settings.DEBUG:
export_caddy_config(os.path.join(settings.BASE_DIR, 'containers', 'caddy', 'config_files'))
self.stdout.write(self.style.SUCCESS('Caddy configuration exported.'))
except Exception as e:
self.stdout.write(self.style.WARNING(f'Failed to export Caddy configuration: {e}'))
else:
self.stdout.write('Skipping Caddy configuration export (CADDY_ENABLED is not set).')