1
Fork 0

Extract data into config files

This commit is contained in:
Jake Howard 2023-10-31 18:22:31 +00:00
parent 6cc1575565
commit e05d88b259
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 27 additions and 2 deletions

4
config.toml Normal file
View file

@ -0,0 +1,4 @@
[[source]]
type = "traefik"
url = "http://10.23.1.103:8080"
destination = "http://10.23.1.103"

View file

@ -3,15 +3,30 @@ from typing import NamedTuple
import re import re
import sys import sys
import operator import operator
import tomllib
from pathlib import Path
from typing import TypedDict
TRAEFIK_HOST_RE = re.compile(r"Host\(`([a-z0-9\.-]+)`\)") TRAEFIK_HOST_RE = re.compile(r"Host\(`([a-z0-9\.-]+)`\)")
CONFIG_FILE = Path.cwd() / "config.toml"
class Route(NamedTuple): class Route(NamedTuple):
name: str name: str
destination: str destination: str
hostname: str hostname: str
class Source(TypedDict):
type: str
url: str
destination: str
class Config(TypedDict):
source: list[Source]
def parse_traefik_rule(rule: str) -> list[str]: def parse_traefik_rule(rule: str) -> list[str]:
if "Host(" not in rule: if "Host(" not in rule:
return [] return []
@ -58,9 +73,15 @@ def get_dokku_routes(dokku_exporter_url: str, dokku_route: str):
def main(): def main():
config: Config = tomllib.loads(CONFIG_FILE.read_text())
routes = [] routes = []
routes.extend(get_traefik_routes("http://10.23.1.103:8080", "http://10.23.1.103")) for source in config["source"]:
routes.extend(get_dokku_routes("http://10.23.2.3:8000", "http://10.23.2.3")) match source["type"]:
case "traefik":
routes.extend(get_traefik_routes(source["url"], source["destination"]))
case "dokku":
routes.extend(get_dokku_routes(source["url"], source["destination"]))
for route in sorted(routes, key=operator.attrgetter("hostname")): for route in sorted(routes, key=operator.attrgetter("hostname")):
print(f"{route.hostname}\t{route.destination}; # {route.name}") print(f"{route.hostname}\t{route.destination}; # {route.name}")