import json,yaml from http.client import HTTPSConnection from base64 import b64encode from config import * #----------------------------------------------------------------------FUNCTIONS def basic_auth(username, password): token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ascii") return f'Basic {token}' #----------------------------------------------------------------------GET ALL ROUTERS FROM LOCAL TRAEFIK INSTANCE # Connect to HTTPS Traefik and get all routers c = HTTPSConnection(TRAEFIK_HOST) headers = { 'Authorization' : basic_auth(TRAEFIK_USER, TRAEFIK_PASS) } c.request('GET', '/api/http/routers', headers=headers) res = c.getresponse() # Get the data and convert to dict jsondata = res.read().decode() dictdata = json.loads(jsondata) #----------------------------------------------------------------------CREATE DICT FOR CONFIG AND POPULATE IT dictconf = {"http": {"routers": {}}} # create blank config # Loop through all routers and filter them by exclude list for i in dictdata: if i["service"] in TRAEFIK_EXCLUDE: continue # Add router to dict dictconf["http"]["routers"].update({i["service"]: {"rule":i["rule"],"entryPoints":"https","service":TRAEFIK_PROXY_SERVICE_NAME}}) print(i["service"],i["rule"]) # Add the targeted service and write to out file dictconf["http"].update({"services": {TRAEFIK_PROXY_SERVICE_NAME: {"loadBalancer": {"servers": [{"url":TRAEFIK_PROXY_TARGET}]}}}}) with open(TRAEFIK_CONF_OUT,"w") as f: yaml.dump(dictconf, f)