1
Fork 0
This repository has been archived on 2023-11-05. You can view files and clone it, but cannot push or open issues or pull requests.
dynamic-nginx-host-map/create-nginx-map.py

60 lines
1.4 KiB
Python

import requests
from typing import NamedTuple
import re
TRAEFIK_HOST_RE = re.compile(r"Host\(`([a-z0-9\.-]+)`\)")
class Route(NamedTuple):
name: str
destination: str
hostname: str
def get_traefik_routes(traefik_host: str, traefik_route: str):
api_response = requests.get(f"{traefik_host}/api/http/routers").json()
routes = set()
for router in api_response:
if "Host(" not in router["rule"]:
continue
hosts = TRAEFIK_HOST_RE.findall(router["rule"])
if not hosts:
raise ValueError(f"Failed to find host for {router['rule']}")
routes.add(Route(
router["service"],
traefik_route,
hosts[0]
))
return routes
def get_dokku_routes(dokku_exporter_url: str, dokku_route: str):
api_response = requests.get(dokku_exporter_url).json()
routes = set()
for app in api_response:
for vhost in app["vhosts"]:
routes.add(Route(
app["app"],
dokku_route,
vhost
))
return routes
def main():
routes = []
routes.extend(get_traefik_routes("http://10.23.1.103:8080", "http://10.23.1.103"))
routes.extend(get_dokku_routes("http://10.23.2.3:8000", "http://10.23.2.3"))
for route in sorted(routes):
print(f"{route.hostname}\t{route.destination}; # {route.name}")
if __name__ == "__main__":
main()