83 lines
2.7 KiB
Python
Executable file
83 lines
2.7 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
INTERNAL_IP = "[::1]"
|
|
INTERNAL_PORT = "8080"
|
|
|
|
INCLUDE_MATCH = re.compile(r"\s*[iI]nclude\s+(.*)\s*")
|
|
SERVERNAME_MATCH = re.compile(r"\s*ServerName\s+(.*)\s*")
|
|
SERVERALIAS_MATCH = re.compile(r"\s*ServerAlias\s+(.*)\s*")
|
|
SERVERADMIN_MATCH = re.compile(r"\s*ServerAdmin\s+(.*)\s*")
|
|
|
|
all_server_names = []
|
|
|
|
TARGET_DIR = Path(sys.argv[2])
|
|
|
|
|
|
def parse_server_names(lines):
|
|
server_names = set(x[1] for x in map(SERVERNAME_MATCH.match, lines) if x)
|
|
alias_matched_lines = [x[1] for x in map(SERVERALIAS_MATCH.match, lines) if x]
|
|
server_alias = set(a for z in alias_matched_lines for a in z.split(" "))
|
|
return (server_names, server_alias)
|
|
|
|
|
|
for config_file in Path(sys.argv[1]).glob("*.conf"):
|
|
target_config = TARGET_DIR / config_file.parts[-1]
|
|
print(f"processing {config_file}, target: {target_config}")
|
|
with config_file.open("r") as original:
|
|
lines = original.readlines()
|
|
includes = set(x[1] for x in map(INCLUDE_MATCH.match, lines) if x)
|
|
|
|
server_names, server_alias = parse_server_names(lines)
|
|
server_admin = [x[1] for x in map(SERVERADMIN_MATCH.match, lines) if x]
|
|
|
|
print(f"server_names: {server_names}")
|
|
print(f"server_alias: {server_alias}")
|
|
print(f"includes: {includes}")
|
|
|
|
if server_names or server_alias:
|
|
all_server_names.append([*server_names, *server_alias])
|
|
|
|
with target_config.open("w+") as out:
|
|
print(f"<VirtualHost {INTERNAL_IP}:{INTERNAL_PORT}>", file=out)
|
|
for name in server_names:
|
|
print(f" ServerName {name}", file=out)
|
|
|
|
for alias in server_alias:
|
|
print(f" ServerAlias {alias}", file=out)
|
|
|
|
if server_admin:
|
|
print(f" ServerAdmin {server_admin[-1]}", file=out)
|
|
|
|
for include in includes:
|
|
print(f" Include {include}", file=out)
|
|
|
|
print(f"</VirtualHost>", file=out)
|
|
|
|
for include_file in Path(sys.argv[1]).glob("*.include"):
|
|
print(f"processing {include_file}")
|
|
with include_file.open("r") as original:
|
|
lines = original.readlines()
|
|
server_names, server_alias = parse_server_names(lines)
|
|
print(f"server_names: {server_names}")
|
|
print(f"server_alias: {server_alias}")
|
|
|
|
if server_names or server_alias:
|
|
all_server_names.append([*server_names, *server_alias])
|
|
|
|
|
|
print(f"all_server_names: {all_server_names}")
|
|
|
|
with (TARGET_DIR / "Caddyfile").open("w+") as caddyfile:
|
|
for entry in all_server_names:
|
|
print(
|
|
f"""
|
|
{', '.join(entry)} {{
|
|
reverse_proxy {INTERNAL_IP}:{INTERNAL_PORT}
|
|
}}
|
|
""",
|
|
file=caddyfile,
|
|
)
|