Extract data into config files
This commit is contained in:
parent
6cc1575565
commit
e05d88b259
2 changed files with 27 additions and 2 deletions
4
config.toml
Normal file
4
config.toml
Normal file
|
@ -0,0 +1,4 @@
|
|||
[[source]]
|
||||
type = "traefik"
|
||||
url = "http://10.23.1.103:8080"
|
||||
destination = "http://10.23.1.103"
|
|
@ -3,15 +3,30 @@ from typing import NamedTuple
|
|||
import re
|
||||
import sys
|
||||
import operator
|
||||
import tomllib
|
||||
from pathlib import Path
|
||||
from typing import TypedDict
|
||||
|
||||
TRAEFIK_HOST_RE = re.compile(r"Host\(`([a-z0-9\.-]+)`\)")
|
||||
|
||||
CONFIG_FILE = Path.cwd() / "config.toml"
|
||||
|
||||
class Route(NamedTuple):
|
||||
name: str
|
||||
destination: 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]:
|
||||
if "Host(" not in rule:
|
||||
return []
|
||||
|
@ -58,9 +73,15 @@ def get_dokku_routes(dokku_exporter_url: str, dokku_route: str):
|
|||
|
||||
|
||||
def main():
|
||||
config: Config = tomllib.loads(CONFIG_FILE.read_text())
|
||||
|
||||
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 source in config["source"]:
|
||||
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")):
|
||||
print(f"{route.hostname}\t{route.destination}; # {route.name}")
|
||||
|
|
Reference in a new issue