implemented recursive file search for identifying states in the current environment checked out in the state repository

This commit is contained in:
Linus Vogel 2026-04-23 20:08:58 +02:00
parent 9e3e83c8c4
commit 84770287e4
3 changed files with 43 additions and 1 deletions

View File

@ -50,7 +50,6 @@ class COCallbacks(CheckoutCallbacks):
print(f"why: {why}")
def checkout_remote_branch(config: Config, branch_name: str) -> None:
"""Checkout a remote branch from the state repository.
@ -119,3 +118,4 @@ def checkout_remote_branch(config: Config, branch_name: str) -> None:
print(f"Failed to checkout branch: {exc}")
raise ValueError(f"Failed to checkout branch: {exc}")

View File

@ -12,6 +12,7 @@ from starlette.responses import JSONResponse
from pillar_tool.db import Host
from pillar_tool.db.models.top_data import Environment, EnvironmentAssignment
from pillar_tool.util.files import recursive_list_dir
from pillar_tool.util.validation import validate_environment_name
from pillar_tool.util import config, Config
from pillar_tool.git.repository import checkout_remote_branch
@ -189,5 +190,24 @@ def environment_patch(req: Request, name: str) -> JSONResponse:
# Attempt to check the requested branch out
try:
checkout_remote_branch(cfg, name)
# TODO: read the file tree and find all sls files and init.sls files to enumerate the available states
# Branch has been checked out
print(f"Reading states that are available in '{name}':")
print(f"{cfg.git.state_repo_path}:")
all_files = recursive_list_dir(cfg.git.state_repo_path)
sls_files = filter(lambda f: f.endswith(".sls"), all_files)
state_file_paths = map(lambda x: x.replace("/init.sls", "").replace(".sls", ""), sls_files)
state_names = map(lambda x: x.replace(f"{cfg.git.state_repo_path}/", "").replace("/", "."), state_file_paths)
for state_name in state_names:
print(f"Checking state '{state_name}'")
# TODO: the environment should be a field of a state in the database
# TODO: ensure that each named state exists in the current environment
except Exception as exc:
print(f"Failed to import environment: {exc}")
raise HTTPException(status_code=404, detail=str(exc))

22
pillar_tool/util/files.py Normal file
View File

@ -0,0 +1,22 @@
from os import listdir
from os.path import isfile, isdir, realpath
def recursive_list_dir(path) -> list[str]:
try:
# place to accumulate all the state files
file_list = []
# list all the files that are in the directory pointed to by path
current_contents = [ f"{path}/{x}" for x in listdir(path) if not x.endswith("/.git") or not isdir(x) ]
for file in current_contents:
if isdir(file):
file_list.extend(recursive_list_dir(file))
elif isfile(file):
file_list.append(file)
return file_list
except Exception:
raise