mirror of
https://github.com/jupyterhub/the-littlest-jupyterhub.git
synced 2025-12-18 21:54:05 +08:00
Refactor
This commit is contained in:
@@ -21,8 +21,6 @@ checksums = {
|
|||||||
"linux-amd64": "3c2d153d80890b6fc8875af9f8ced32c4d684e1eb5a46d9815337cb343dfd92e"
|
"linux-amd64": "3c2d153d80890b6fc8875af9f8ced32c4d684e1eb5a46d9815337cb343dfd92e"
|
||||||
}
|
}
|
||||||
|
|
||||||
traefik_extra_config_dir = "traefik_config.d"
|
|
||||||
|
|
||||||
def checksum_file(path):
|
def checksum_file(path):
|
||||||
"""Compute the sha256 checksum of a path"""
|
"""Compute the sha256 checksum of a path"""
|
||||||
hasher = hashlib.sha256()
|
hasher = hashlib.sha256()
|
||||||
@@ -82,13 +80,17 @@ def compute_basic_auth(username, password):
|
|||||||
hashed_password = str(ht.to_string()).split(":")[1][:-3]
|
hashed_password = str(ht.to_string()).split(":")[1][:-3]
|
||||||
return username + ":" + hashed_password
|
return username + ":" + hashed_password
|
||||||
|
|
||||||
def load_extra_config(std_toml):
|
def load_extra_config(std_toml, extra_config_dir):
|
||||||
extra_configs = sorted(glob(os.path.join(CONFIG_DIR, traefik_extra_config_dir, '*.py')))
|
extra_configs = sorted(glob(os.path.join(extra_config_dir, '*.py')))
|
||||||
config = toml.load(extra_configs + [std_toml])
|
# Load the toml list of files into dicts and merge them
|
||||||
|
config = toml.load([std_toml] + extra_configs)
|
||||||
return config
|
return config
|
||||||
|
|
||||||
def ensure_traefik_config(state_dir):
|
def ensure_traefik_config(state_dir):
|
||||||
"""Render the traefik.toml config file"""
|
"""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")
|
||||||
|
|
||||||
config = load_config()
|
config = load_config()
|
||||||
config['traefik_api']['basic_auth'] = compute_basic_auth(
|
config['traefik_api']['basic_auth'] = compute_basic_auth(
|
||||||
config['traefik_api']['username'],
|
config['traefik_api']['username'],
|
||||||
@@ -113,17 +115,19 @@ def ensure_traefik_config(state_dir):
|
|||||||
raise ValueError("Both email and domains must be set for letsencrypt")
|
raise ValueError("Both email and domains must be set for letsencrypt")
|
||||||
|
|
||||||
# Ensure extra config dir exists and is private
|
# Ensure extra config dir exists and is private
|
||||||
for path in [CONFIG_DIR, os.path.join(CONFIG_DIR, traefik_extra_config_dir)]:
|
os.makedirs(traefik_extra_config_dir, mode=0o700, exist_ok=True)
|
||||||
os.makedirs(path, mode=0o700, exist_ok=True)
|
|
||||||
|
|
||||||
with open(os.path.join(state_dir, "traefik.toml"), "w") as f:
|
# Write standard config to file
|
||||||
|
with open(traefik_std_config_file, "w") as f:
|
||||||
os.fchmod(f.fileno(), 0o600)
|
os.fchmod(f.fileno(), 0o600)
|
||||||
f.write(new_toml)
|
f.write(new_toml)
|
||||||
|
|
||||||
traefik_toml = load_extra_config(os.path.join(state_dir, "traefik.toml"))
|
# Load standard config file and extra config files into a dict
|
||||||
|
traefik_toml = load_extra_config(traefik_std_config_file, traefik_extra_config_dir)
|
||||||
|
|
||||||
|
# Dump the dict into a toml-formatted string and write it to file
|
||||||
print(f"Writing traefik config {traefik_toml}...")
|
print(f"Writing traefik config {traefik_toml}...")
|
||||||
with open(os.path.join(state_dir, "traefik.toml"), "w") as f:
|
with open(traefik_std_config_file, "w") as f:
|
||||||
os.fchmod(f.fileno(), 0o600)
|
os.fchmod(f.fileno(), 0o600)
|
||||||
toml.dump(traefik_toml, f)
|
toml.dump(traefik_toml, f)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user