diff --git a/src/main/python/ddadevops/infrastructure/infrastructure.py b/src/main/python/ddadevops/infrastructure/infrastructure.py index 65533cd..3747417 100644 --- a/src/main/python/ddadevops/infrastructure/infrastructure.py +++ b/src/main/python/ddadevops/infrastructure/infrastructure.py @@ -1,4 +1,4 @@ -from subprocess import Popen, PIPE, run +from subprocess import Popen, PIPE, run, CalledProcessError from pathlib import Path from sys import stdout from os import chmod, environ @@ -49,55 +49,42 @@ class FileApi: class ImageApi: + def __init__(self): + self.execution_api = ExecutionApi() + def image(self, name: str, path: Path): - run( - f"docker build -t {name} --file {path}/image/Dockerfile {path}/image", - shell=True, - check=True, + self.execution_api.run_handled( + f"docker build -t {name} --file {path}/image/Dockerfile {path}/image" ) def drun(self, name: str): - run( - f'docker run -it --entrypoint="" {name} /bin/bash', - shell=True, - check=True, + self.execution_api.run_handled( + f'docker run -it --entrypoint="" {name} /bin/bash' ) def dockerhub_login(self, username: str, password: str): - run( - f"docker login --username {username} --password {password}", - shell=True, - check=True, + self.execution_api.run_handled( + f"docker login --username {username} --password {password}" ) def dockerhub_publish(self, name: str, username: str, tag=None): if tag is not None: - run( - f"docker tag {name} {username}/{name}:{tag}", - shell=True, - check=True, + self.execution_api.run_handled( + f"docker tag {name} {username}/{name}:{tag}" ) - run( - f"docker push {username}/{name}:{tag}", - shell=True, - check=True, + self.execution_api.run_handled( + f"docker push {username}/{name}:{tag}" ) - run( - f"docker tag {name} {username}/{name}:latest", - shell=True, - check=True, + self.execution_api.run_handled( + f"docker tag {name} {username}/{name}:latest" ) - run( - f"docker push {username}/{name}:latest", - shell=True, - check=True, + self.execution_api.run_handled( + f"docker push {username}/{name}:latest" ) def test(self, name: str, path: Path): - run( - f"docker build -t {name} -test --file {path}/test/Dockerfile {path}/test", - shell=True, - check=True, + self.execution_api.run_handled( + f"docker build -t {name} -test --file {path}/test/Dockerfile {path}/test" ) @@ -123,6 +110,18 @@ class ExecutionApi: print(line.decode("utf-8"), end="") process.stdout.close() process.wait() + + def run_handled(self, command: str, shell=True, check=True): + try: + run( + command, + shell=shell, + check=check, + capture_output=True, + text=True) + except CalledProcessError as exc: + print("Command failed with code: ", exc.returncode, " and message:", exc.stderr) + class EnvironmentApi: