(state_dir)
Render the traefik.toml config file
Render the traefik.toml config file
Render the traefik . toml config file
def ensure_traefik_config(state_dir):
    """Render the traefik.toml config file"""
    traefik_std_config_file = os.path.join(state_dir, "traefik.toml")
    traefik_extra_config_dir = os.path.join(CONFIG_DIR, "traefik_config.d")
    traefik_dynamic_config_dir = os.path.join(state_dir, "rules")

    config = load_config()
    config["traefik_api"]["basic_auth"] = compute_basic_auth(
        config["traefik_api"]["username"],
        config["traefik_api"]["password"],
    )

    with open(os.path.join(os.path.dirname(__file__), "traefik.toml.tpl")) as f:
        template = Template(f.read())
    std_config = template.render(config)
    https = config["https"]
    letsencrypt = https["letsencrypt"]
    tls = https["tls"]
    # validate https config
    if https["enabled"]:
        if not tls["cert"] and not letsencrypt["email"]:
            raise ValueError(
                "To enable https, you must set tls.cert+key or letsencrypt.email+domains"
            )
        if (letsencrypt["email"] and not letsencrypt["domains"]) or (
            letsencrypt["domains"] and not letsencrypt["email"]
        ):
            raise ValueError("Both email and domains must be set for letsencrypt")

    # Ensure traefik extra static config dir exists and is private
    os.makedirs(traefik_extra_config_dir, mode=0o700, exist_ok=True)

    # Ensure traefik dynamic config dir exists and is private
    os.makedirs(traefik_dynamic_config_dir, mode=0o700, exist_ok=True)

    try:
        # Load standard config file merge it with the extra config files into a dict
        extra_config = load_extra_config(traefik_extra_config_dir)
        new_toml = _merge_dictionaries(toml.loads(std_config), extra_config)
    except FileNotFoundError:
        new_toml = toml.loads(std_config)

    # Dump the dict into a toml-formatted string and write it to file
    with open(traefik_std_config_file, "w") as f:
        os.fchmod(f.fileno(), 0o600)
        toml.dump(new_toml, f)

    with open(os.path.join(traefik_dynamic_config_dir, "rules.toml"), "w") as f:
        os.fchmod(f.fileno(), 0o600)

    # ensure acme.json exists and is private
    with open(os.path.join(state_dir, "acme.json"), "a") as f:
        os.fchmod(f.fileno(), 0o600)
def ensure_traefik_config ( state_dir ) : traefik_std_config_file = os . path . join ( state_dir , "traefik.toml" ) traefik_extra_config_dir = os . path . join ( CONFIG_DIR , "traefik_config.d" ) traefik_dynamic_config_dir = os . path . join ( state_dir , "rules" ) config = load_config ( ) config [ "traefik_api" ] [ "basic_auth" ] = compute_basic_auth ( config [ "traefik_api" ] [ "username" ] , config [ "traefik_api" ] [ "password" ] , ) with open ( os . path . join ( os . path . dirname ( __file__ ) , "traefik.toml.tpl" ) ) as f : template = Template ( f . read ( ) ) std_config = template . render ( config ) https = config [ "https" ] letsencrypt = https [ "letsencrypt" ] tls = https [ "tls" ] # validate https config if https [ "enabled" ] : if not tls [ "cert" ] and not letsencrypt [ "email" ] : raise ValueError ( "To enable https, you must set tls.cert+key or letsencrypt.email+domains" ) if ( letsencrypt [ "email" ] and not letsencrypt [ "domains" ] ) or ( letsencrypt [ "domains" ] and not letsencrypt [ "email" ] ) : raise ValueError ( "Both email and domains must be set for letsencrypt" ) # Ensure traefik extra static config dir exists and is private os . makedirs ( traefik_extra_config_dir , mode = 0o700 , exist_ok = True ) # Ensure traefik dynamic config dir exists and is private os . makedirs ( traefik_dynamic_config_dir , mode = 0o700 , exist_ok = True ) try : # Load standard config file merge it with the extra config files into a dict extra_config = load_extra_config ( traefik_extra_config_dir ) new_toml = _merge_dictionaries ( toml . loads ( std_config ) , extra_config ) except FileNotFoundError : new_toml = toml . loads ( std_config ) # Dump the dict into a toml-formatted string and write it to file with open ( traefik_std_config_file , "w" ) as f : os . fchmod ( f . fileno ( ) , 0o600 ) toml . dump ( new_toml , f ) with open ( os . path . join ( traefik_dynamic_config_dir , "rules.toml" ) , "w" ) as f : os . fchmod ( f . fileno ( ) , 0o600 ) # ensure acme.json exists and is private with open ( os . path . join ( state_dir , "acme.json" ) , "a" ) as f : os . fchmod ( f . fileno ( ) , 0o600 )