diff --git a/alakazam.py b/alakazam.py index 0fbd873..2d85428 100755 --- a/alakazam.py +++ b/alakazam.py @@ -54,7 +54,7 @@ ALL_CONFIGS = {} # ... #} SETTINGS = {} -SETTINGS_PATH = "~/.config/alakazam.yml" +SETTINGS_PATH = "alakazam.yml" if Path("alakazam.yml").exists() else "~/.config/alakazam.yml" class MySafeConstructor(SafeConstructor): @@ -467,8 +467,27 @@ def generate_all_secrets(domain: str) -> None: print(f"\t {gen_sec['name']}: {gen_sec['value']}") +def get_abra_dir() -> 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_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,7 +825,9 @@ def cli(loglevel: str, group_path: str, exclude:Tuple[str]) -> None: global INSTANCE_CONFIGS global ALL_CONFIGS global SETTINGS + global ABRA_DIR SETTINGS = read_config(SETTINGS_PATH) + ABRA_DIR = get_abra_dir() 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") @@ -817,9 +838,11 @@ def cli(loglevel: str, group_path: str, exclude:Tuple[str]) -> None: 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 +1380,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 +1414,12 @@ def git(recipes: Tuple[str], message: str, push: bool = False) -> None: @click.option('recipes', '-r', '--recipe', multiple=True, metavar='', 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: