Skip to content

Commit 2bf0e88

Browse files
committed
Added SWAG config patch script
1 parent 3102667 commit 2bf0e88

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

swag_config_patcher.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/python3
2+
3+
import os
4+
import re
5+
6+
from pathlib import Path
7+
8+
use_authelia = True
9+
use_full_vpn = True
10+
11+
# Check paths
12+
FOLDER_FOR_DATA = os.getenv('FOLDER_FOR_DATA')
13+
if not FOLDER_FOR_DATA:
14+
print('Data folder not in ENV.')
15+
exit(1)
16+
data_dir = Path(FOLDER_FOR_DATA)
17+
proxy_conf_dir = data_dir / 'swag/nginx/proxy-confs'
18+
if not proxy_conf_dir.is_dir():
19+
print('SWAG proxy config folder does not exist.')
20+
exit(2)
21+
auth_conf_path = data_dir / 'swag/nginx/site-confs/default.conf'
22+
if not auth_conf_path.is_file():
23+
print('SWAG default site config does not exist.')
24+
exit(3)
25+
26+
# Container / config names
27+
stack_network = {'authelia', 'heimdall', 'homarr', 'homepage', 'ddns-updater'}
28+
stack_vpn = {'bazarr', 'filebot', 'flaresolverr', 'jellyfin', 'jellyseerr', 'lidarr', 'mylar', 'plex', 'portainer', 'prowlarr', 'qbittorrent', 'radarr', 'readarr', 'sabnzbd', 'sonarr', 'tdarr', 'whisparr'}
29+
stack_full = stack_network | stack_vpn
30+
31+
for container in stack_full:
32+
print(f'Processing "{container}".')
33+
34+
# Get port from env
35+
caps = container.upper().replace('-', '_')
36+
port = os.getenv(f'WEBUI_PORT_{caps}') or os.getenv(f'{caps}_PORT')
37+
38+
# Read sample config
39+
file = proxy_conf_dir / f'{container}.subdomain.conf.sample'
40+
if not file.is_file():
41+
print(f'Can not find config file "{file}".')
42+
continue
43+
text = file.read_text()
44+
45+
# Patch ports
46+
if port:
47+
text = re.sub(r'set \$upstream_port \d{2,5};', f'set $upstream_port {port};', text)
48+
# Activate authelia
49+
if use_authelia:
50+
text = text.replace('#include /config/nginx/authelia', 'include /config/nginx/authelia')
51+
# Rewire to gluetun
52+
if use_full_vpn and container in stack_vpn:
53+
text = text.replace(f'set $upstream_app {container};', 'set $upstream_app gluetun;')
54+
# Rename subdomain for authelia to auth
55+
if container == 'authelia':
56+
text = text.replace('server_name authelia.*;', 'server_name auth.*;')
57+
58+
conf = file.with_suffix('').write_text(text)
59+
60+
# Activate authelia in default config
61+
# FIXME: Do we need this?
62+
if use_authelia:
63+
text = auth_conf_path.read_text()
64+
text = text.replace('#include /config/nginx/authelia', 'include /config/nginx/authelia')
65+
auth_conf_path.write_text(text)

0 commit comments

Comments
 (0)