mirror of
https://github.com/Cian-H/I-Form_Server_Node_Deployer.git
synced 2025-12-23 06:32:08 +00:00
Refactored for better config & cleaner debug mode
This commit is contained in:
60
config.py
60
config.py
@@ -1,3 +1,8 @@
|
||||
# flake8: noqa: F821
|
||||
#* This file sets a number of config constants by modifying its own globals
|
||||
#* As a result, F821 is disabled as the intereter cannot be trusted to know
|
||||
#* when F821 should be raised.
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import tomllib
|
||||
@@ -5,30 +10,57 @@ import tomllib
|
||||
import docker
|
||||
|
||||
|
||||
def get_config(config: str = "default") -> dict:
|
||||
CLIENT = docker.from_env(version="auto")
|
||||
|
||||
type ConfigLabel = str | list[str]
|
||||
|
||||
|
||||
def get_config(config_label: ConfigLabel = ["default"]) -> dict:
|
||||
if isinstance(config_label, str):
|
||||
config_label = [config_label]
|
||||
with open("config.toml", "rb") as f:
|
||||
configs: dict = tomllib.load(f)
|
||||
out_config: dict = configs["default"]
|
||||
out_config.update(configs[config])
|
||||
out_config: dict = {}
|
||||
for c in config_label:
|
||||
out_config.update(configs[c])
|
||||
return out_config
|
||||
|
||||
|
||||
def apply_config(config: dict) -> None:
|
||||
config["CLIENT"] = docker.from_env(version="auto")
|
||||
config["ROOT_DIR"] = Path(config["ROOT_DIR"]).absolute()
|
||||
config["BUILD_DIR"] = Path(config["BUILD_DIR"]).absolute()
|
||||
config["DOCKERFILE_DIR"] = Path(config["DOCKERFILE_DIR"]).absolute()
|
||||
config["FUELIGNITION_BUILD_DIR"] = config["BUILD_DIR"] / config["FUELIGNITION_BUILD_DIR"]
|
||||
config["CWD_MOUNTDIR"] = Path(config["CWD_MOUNTDIR"])
|
||||
def finalise_config(config: dict) -> None:
|
||||
# First, convert base paths to Path objects
|
||||
for k, v in config.items():
|
||||
match k:
|
||||
case "ROOT_DIR" | "BUILD_DIR" | "DOCKERFILE_DIR":
|
||||
config[k] = Path(v).absolute()
|
||||
case "CWD_MOUNTDIR":
|
||||
config[k] = Path(v)
|
||||
# Then, get required paths from config or globals if not present
|
||||
build_dir = config.get("BUILD_DIR", BUILD_DIR)
|
||||
cwd_mountdir = config.get("CWD_MOUNTDIR", CWD_MOUNTDIR)
|
||||
root_dir = config.get("ROOT_DIR", ROOT_DIR)
|
||||
# Finally, construct the secondary parameters
|
||||
config["FUELIGNITION_BUILD_DIR"] = build_dir / config.get(
|
||||
"FUELIGNITION_BUILD_DIR",
|
||||
FUELIGNITION_BUILD_DIR
|
||||
)
|
||||
config["CWD_MOUNT"] = docker.types.Mount(
|
||||
target=str(config["CWD_MOUNTDIR"]),
|
||||
source=str(config["ROOT_DIR"]),
|
||||
target=str(cwd_mountdir),
|
||||
source=str(root_dir),
|
||||
type="bind",
|
||||
)
|
||||
|
||||
|
||||
def apply_config(config: dict) -> None:
|
||||
finalise_config(config)
|
||||
globals().update(config)
|
||||
|
||||
|
||||
def init(config: str = "default") -> None:
|
||||
apply_config(get_config(config))
|
||||
def update_config(config_label: ConfigLabel = "default") -> None:
|
||||
apply_config(get_config(config_label))
|
||||
|
||||
|
||||
def init() -> None:
|
||||
globals().update(get_config())
|
||||
update_config()
|
||||
|
||||
init()
|
||||
Reference in New Issue
Block a user