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
49
dc-ops
49
dc-ops
|
@ -21,6 +21,25 @@ with configfile.open("r") as f:
|
||||||
loglevel = cfg.get("loglevel", "INFO").upper()
|
loglevel = cfg.get("loglevel", "INFO").upper()
|
||||||
log.basicConfig(format="%(message)s", level=loglevel)
|
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
|
# iterate all stacks
|
||||||
for stack in cfg["stacks"]:
|
for stack in cfg["stacks"]:
|
||||||
# skip disabled stacks
|
# skip disabled stacks
|
||||||
|
@ -59,40 +78,20 @@ for stack in cfg["stacks"]:
|
||||||
|
|
||||||
# run pre-command
|
# run pre-command
|
||||||
if "pre" in stack:
|
if "pre" in stack:
|
||||||
try:
|
if not run_subprocess(stack["pre"], stackdir):
|
||||||
subprocess.run(
|
|
||||||
stack["pre"],
|
|
||||||
cwd=stackdir,
|
|
||||||
shell=True,
|
|
||||||
text=True,
|
|
||||||
)
|
|
||||||
except subprocess.CalledProcessError:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# run `docker compose` for all compose-files
|
# run `docker compose` for all compose-files
|
||||||
# (or just for the directory if no compose-file defined)
|
# (or just for the directory if no compose-file defined)
|
||||||
composefiles = stack.get("compose-files", ["docker-compose.yml"])
|
composefiles = stack.get("compose-files", ["docker-compose.yml"])
|
||||||
for composefile in composefiles:
|
for composefile in composefiles:
|
||||||
try:
|
if not run_subprocess(
|
||||||
subprocess.run(
|
|
||||||
f"docker compose --file {composefile} up --build --detach --remove-orphans", # noqa
|
f"docker compose --file {composefile} up --build --detach --remove-orphans", # noqa
|
||||||
cwd=stackdir,
|
stackdir,
|
||||||
shell=True,
|
):
|
||||||
text=True,
|
|
||||||
)
|
|
||||||
except subprocess.CalledProcessError:
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# run post-command
|
# run post-command
|
||||||
if "post" in stack:
|
if "post" in stack:
|
||||||
try:
|
if not run_subprocess(stack["post"], stackdir):
|
||||||
print(
|
|
||||||
subprocess.run(
|
|
||||||
stack["post"],
|
|
||||||
cwd=stackdir,
|
|
||||||
shell=True,
|
|
||||||
text=True,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
except subprocess.CalledProcessError:
|
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in a new issue