Add execute_handled

This commit is contained in:
erik 2023-07-13 11:07:59 +02:00
parent 036eb53972
commit a6f6b9715e

View file

@ -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
@ -53,37 +53,37 @@ class ImageApi:
self.execution_api = ExecutionApi()
def image(self, name: str, path: Path):
self.execution_api.execute(
self.execution_api.execute_handled(
f"docker build -t {name} --file {path}/image/Dockerfile {path}/image"
)
def drun(self, name: str):
self.execution_api.execute(
self.execution_api.execute_handled(
f'docker run -it --entrypoint="" {name} /bin/bash'
)
def dockerhub_login(self, username: str, password: str):
self.execution_api.execute(
self.execution_api.execute_handled(
f"docker login --username {username} --password {password}"
)
def dockerhub_publish(self, name: str, username: str, tag=None):
if tag is not None:
self.execution_api.execute(
self.execution_api.execute_handled(
f"docker tag {name} {username}/{name}:{tag}"
)
self.execution_api.execute(
self.execution_api.execute_handled(
f"docker push {username}/{name}:{tag}"
)
self.execution_api.execute(
self.execution_api.execute_handled(
f"docker tag {name} {username}/{name}:latest"
)
self.execution_api.execute(
self.execution_api.execute_handled(
f"docker push {username}/{name}:latest"
)
def test(self, name: str, path: Path):
self.execution_api.execute(
self.execution_api.execute_handled(
f"docker build -t {name} -test --file {path}/test/Dockerfile {path}/test"
)
@ -101,7 +101,7 @@ class ExecutionApi:
output = output.rstrip()
return output
def execute_live(self, command, dry_run=False, shell=True):
def execute_live(self, command: str, dry_run=False, shell=True):
if dry_run:
print(command)
else:
@ -111,6 +111,21 @@ class ExecutionApi:
process.stdout.close()
process.wait()
def execute_handled(self, command: str, dry_run=False, shell=True, check=True):
if dry_run:
print(command)
else:
try:
run(
command,
shell=shell,
check=check,
stderr=PIPE,
text=True)
except CalledProcessError as exc:
print("Command failed with code: ", exc.returncode, " and message:", exc.stderr)
raise exc
class EnvironmentApi:
def get(self, key):