Add execute_handled

merge-requests/17/head^2
erik 12 months ago
parent 036eb53972
commit a6f6b9715e

@ -1,4 +1,4 @@
from subprocess import Popen, PIPE, run from subprocess import Popen, PIPE, run, CalledProcessError
from pathlib import Path from pathlib import Path
from sys import stdout from sys import stdout
from os import chmod, environ from os import chmod, environ
@ -53,37 +53,37 @@ class ImageApi:
self.execution_api = ExecutionApi() self.execution_api = ExecutionApi()
def image(self, name: str, path: Path): 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" f"docker build -t {name} --file {path}/image/Dockerfile {path}/image"
) )
def drun(self, name: str): def drun(self, name: str):
self.execution_api.execute( self.execution_api.execute_handled(
f'docker run -it --entrypoint="" {name} /bin/bash' f'docker run -it --entrypoint="" {name} /bin/bash'
) )
def dockerhub_login(self, username: str, password: str): def dockerhub_login(self, username: str, password: str):
self.execution_api.execute( self.execution_api.execute_handled(
f"docker login --username {username} --password {password}" f"docker login --username {username} --password {password}"
) )
def dockerhub_publish(self, name: str, username: str, tag=None): def dockerhub_publish(self, name: str, username: str, tag=None):
if tag is not None: if tag is not None:
self.execution_api.execute( self.execution_api.execute_handled(
f"docker tag {name} {username}/{name}:{tag}" f"docker tag {name} {username}/{name}:{tag}"
) )
self.execution_api.execute( self.execution_api.execute_handled(
f"docker push {username}/{name}:{tag}" f"docker push {username}/{name}:{tag}"
) )
self.execution_api.execute( self.execution_api.execute_handled(
f"docker tag {name} {username}/{name}:latest" f"docker tag {name} {username}/{name}:latest"
) )
self.execution_api.execute( self.execution_api.execute_handled(
f"docker push {username}/{name}:latest" f"docker push {username}/{name}:latest"
) )
def test(self, name: str, path: Path): 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" f"docker build -t {name} -test --file {path}/test/Dockerfile {path}/test"
) )
@ -101,7 +101,7 @@ class ExecutionApi:
output = output.rstrip() output = output.rstrip()
return output 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: if dry_run:
print(command) print(command)
else: else:
@ -111,6 +111,21 @@ class ExecutionApi:
process.stdout.close() process.stdout.close()
process.wait() 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: class EnvironmentApi:
def get(self, key): def get(self, key):

Loading…
Cancel
Save