refactor: move subprocess calls to function
- extract subprocess execution into a separate function
- replace direct subprocess calls with function calls
🤖
This commit is contained in:
parent
55d00d1b08
commit
8388362701
1 changed files with 25 additions and 26 deletions
51
dc-ops
51
dc-ops
|
@ -21,6 +21,25 @@ with configfile.open("r") as f:
|
|||
loglevel = cfg.get("loglevel", "INFO").upper()
|
||||
log.basicConfig(format="%(message)s", level=loglevel)
|
||||
|
||||
|
||||
def run_subprocess(command: str, workdir: str) -> bool:
|
||||
"""execute subprocess and print output
|
||||
|
||||
Args:
|
||||
cmd (str): command
|
||||
cwd (str): working directory
|
||||
|
||||
Returns:
|
||||
bool: False if subprocess fails
|
||||
"""
|
||||
try:
|
||||
subprocess.run(command, cwd=workdir, shell=True, text=True)
|
||||
except subprocess.CalledProcessError:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
# iterate all stacks
|
||||
for stack in cfg["stacks"]:
|
||||
# skip disabled stacks
|
||||
|
@ -59,40 +78,20 @@ for stack in cfg["stacks"]:
|
|||
|
||||
# run pre-command
|
||||
if "pre" in stack:
|
||||
try:
|
||||
subprocess.run(
|
||||
stack["pre"],
|
||||
cwd=stackdir,
|
||||
shell=True,
|
||||
text=True,
|
||||
)
|
||||
except subprocess.CalledProcessError:
|
||||
if not run_subprocess(stack["pre"], stackdir):
|
||||
continue
|
||||
|
||||
# run `docker compose` for all compose-files
|
||||
# (or just for the directory if no compose-file defined)
|
||||
composefiles = stack.get("compose-files", ["docker-compose.yml"])
|
||||
for composefile in composefiles:
|
||||
try:
|
||||
subprocess.run(
|
||||
f"docker compose --file {composefile} up --build --detach --remove-orphans", # noqa
|
||||
cwd=stackdir,
|
||||
shell=True,
|
||||
text=True,
|
||||
)
|
||||
except subprocess.CalledProcessError:
|
||||
if not run_subprocess(
|
||||
f"docker compose --file {composefile} up --build --detach --remove-orphans", # noqa
|
||||
stackdir,
|
||||
):
|
||||
pass
|
||||
|
||||
# run post-command
|
||||
if "post" in stack:
|
||||
try:
|
||||
print(
|
||||
subprocess.run(
|
||||
stack["post"],
|
||||
cwd=stackdir,
|
||||
shell=True,
|
||||
text=True,
|
||||
)
|
||||
)
|
||||
except subprocess.CalledProcessError:
|
||||
if not run_subprocess(stack["post"], stackdir):
|
||||
continue
|
||||
|
|
Loading…
Reference in a new issue