Config in CWD and resolve ABRA_DIR #4

Merged
moritz merged 1 commits from eCommons/alakazam:config-location into main 2026-05-18 16:43:29 +00:00

View File

@ -54,7 +54,8 @@ ALL_CONFIGS = {}
# ...
#}
SETTINGS = {}
SETTINGS_PATH = "~/.config/alakazam.yml"
SETTINGS_PATH = "" # path to the alakazam settings file
dannygroenewegen marked this conversation as resolved Outdated

To be able to use the project specific alakazam.yml in nested directories, it would be good to use a function similar to get_abra_dir() to find alakazam.yml inside the parent dirs.

To be able to use the project specific `alakazam.yml` in nested directories, it would be good to use a function similar to `get_abra_dir() ` to find `alakazam.yml` inside the parent dirs.
ABRA_DIR = None # path to the abra data directory
class MySafeConstructor(SafeConstructor):
@ -467,8 +468,61 @@ def generate_all_secrets(domain: str) -> None:
print(f"\t {gen_sec['name']}: {gen_sec['value']}")
dannygroenewegen marked this conversation as resolved Outdated

A little docstring what this function is doing would be nice.

A little docstring what this function is doing would be nice.
def get_abra_dir() -> Path:
"""
Resolve the abra directory path.
Follows the same logic abra uses internally: checks the ABRA_DIR environment
variable first, then walks up the directory tree from cwd looking for an
abra.yaml/abra.yml config with an abraDir key. Stops at the home directory.
Falls back to ~/.abra if nothing is found.
Returns:
Path: The resolved abra directory path.
"""
if abra_dir := os.environ.get("ABRA_DIR"):
return Path(abra_dir)
home = Path.home()
current = Path.cwd()
while current != current.parent:
for name in ("abra.yaml", "abra.yml"):
config_file = current / name
if config_file.exists():
abra_config = read_config(str(config_file))
if abra_dir := abra_config.get("abraDir"):
p = Path(abra_dir)
return (current / p).resolve() if not p.is_absolute() else p
if current == home:
break
current = current.parent
return home / ".abra"
def get_settings_path() -> str:
"""
Resolve the alakazam settings file path.
Walks up the directory tree from cwd looking for an alakazam.yml file.
Stops at the home directory. Falls back to ~/.config/alakazam.yml if
nothing is found.
Returns:
str: The path to the alakazam settings file.
"""
home = Path.home()
current = Path.cwd()
while current != current.parent:
config_file = current / "alakazam.yml"
if config_file.exists():
return str(config_file)
if current == home:
break
current = current.parent
return "~/.config/alakazam.yml"
def get_env_path(server: str, domain: str) -> Path:
return Path(f"~/.abra/servers/{server}/{domain}.env").expanduser()
return Path(ABRA_DIR / "servers" / server / f"{domain}.env")
def exchange_secrets(app1: str, instance_config: Dict[str, Any], apps: Tuple[str]) -> None:
@ -806,20 +860,29 @@ def cli(loglevel: str, group_path: str, exclude:Tuple[str]) -> None:
global INSTANCE_CONFIGS
global ALL_CONFIGS
global SETTINGS
global SETTINGS_PATH
global ABRA_DIR
SETTINGS_PATH = get_settings_path()
SETTINGS = read_config(SETTINGS_PATH)
ABRA_DIR = get_abra_dir()
settings_dir = Path(SETTINGS_PATH).expanduser().parent
if not (root_path:= SETTINGS.get('root')):
root_path = os.getcwd()
logging.warning(f"There is no 'root' path defined in '{SETTINGS_PATH}', use current path '{root_path}'instead")
_group_path = Path(group_path).expanduser().absolute()
_root_path = Path(root_path).expanduser().absolute()
_root_path = Path(root_path).expanduser()
if not _root_path.is_absolute():
_root_path = (settings_dir / _root_path).absolute()
if not str(_group_path).startswith(str(_root_path)):
logging.error(f"{_root_path} does not contain {_group_path}?")
exit(1)
all_group_configs = merge_all_group_configs(_root_path)
exclude_paths = list(map(lambda p: str(Path(p).absolute()), exclude))
if ABRA_DIR.is_relative_to(_root_path) and str(ABRA_DIR) not in exclude_paths:
exclude_paths.append(str(ABRA_DIR))
instance_configs = get_merged_instance_configs(_group_path, all_group_configs, exclude_paths)
INSTANCE_CONFIGS = merge_connection_configs(instance_configs)
all_configs = get_merged_instance_configs(_root_path, all_group_configs, [])
all_configs = get_merged_instance_configs(_root_path, all_group_configs, exclude_paths)
ALL_CONFIGS = merge_connection_configs(all_configs)
if loglevel:
numeric_level = getattr(logging, loglevel.upper(), None)
@ -1357,11 +1420,11 @@ def install() -> None:
@click.option('-p', '--push', is_flag=True, help='Push all changes to the remote')
def git(recipes: Tuple[str], message: str, push: bool = False) -> None:
"""
Run multiple git commands on each ~/.abra/servers. Without any specified options each repository will only be pulled.
Run multiple git commands on each {ABRA_DIR}/servers. Without any specified options each repository will only be pulled.
"""
servers = get_server(recipes)
for server in servers:
server_path = Path(f"~/.abra/servers/{server}").expanduser()
server_path = Path(ABRA_DIR / "servers" / server)
try:
repo = Repo(server_path)
except InvalidGitRepositoryError:
@ -1391,12 +1454,12 @@ def git(recipes: Tuple[str], message: str, push: bool = False) -> None:
@click.option('recipes', '-r', '--recipe', multiple=True, metavar='<RecipeName>', help='Filter for selcted recipes, this option can be specified multiple times.')
def diff(recipes: Tuple[str]) -> None:
"""
Show the changes in the .env repositories inside ~/.abra/servers.
Show the changes in the .env repositories inside {ABRA_DIR}/servers.
"""
init(autoreset=True)
servers = get_server(recipes)
for server in servers:
server_path = Path(f"~/.abra/servers/{server}").expanduser()
server_path = Path(ABRA_DIR / "servers" / server)
try:
repo = Repo(server_path)
except InvalidGitRepositoryError: