From bbbb57ec065ae50a81484614ac2800f7f41c5a40 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Thu, 19 Jan 2023 13:20:53 -0700 Subject: [PATCH] Implement stop on error building containers --- app/build_containers.py | 13 +++++++++++-- cli.py | 8 +++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/build_containers.py b/app/build_containers.py index 8f03ccec..75e78f2d 100644 --- a/app/build_containers.py +++ b/app/build_containers.py @@ -21,6 +21,7 @@ # TODO: display the available list of containers; allow re-build of either all or specific containers import os +import sys from decouple import config import subprocess import click @@ -45,6 +46,7 @@ def command(ctx, include, exclude): dry_run = ctx.obj.dry_run local_stack = ctx.obj.local_stack stack = ctx.obj.stack + continue_on_error = ctx.obj.continue_on_error # See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure container_build_dir = Path(__file__).absolute().parent.joinpath("data", "container-build") @@ -112,8 +114,15 @@ def command(ctx, include, exclude): if verbose: print(f"Executing: {build_command}") build_result = subprocess.run(build_command, shell=True, env=container_build_env) - # TODO: check result in build_result.returncode - print(f"Result is: {build_result}") + if verbose: + print(f"Return code is: {build_result.returncode}") + if build_result.returncode != 0: + print(f"Error running build for {container}") + if not continue_on_error: + print("FATAL Error: container build failed and --continue-on-error not set, exiting") + sys.exit(1) + else: + print("****** Container Build Error, continuing because --continue-on-error is set") else: print("Skipped") diff --git a/cli.py b/cli.py index c0fa3990..04ecd738 100644 --- a/cli.py +++ b/cli.py @@ -27,13 +27,14 @@ CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) # TODO: this seems kind of weird and heavy on boilerplate -- check it is # the best Python can do for us. class Options(object): - def __init__(self, stack, quiet, verbose, dry_run, local_stack, debug): + def __init__(self, stack, quiet, verbose, dry_run, local_stack, debug, continue_on_error): self.stack = stack self.quiet = quiet self.verbose = verbose self.dry_run = dry_run self.local_stack = local_stack self.debug = debug + self.continue_on_error = continue_on_error @click.group(context_settings=CONTEXT_SETTINGS) @@ -43,11 +44,12 @@ class Options(object): @click.option('--dry-run', is_flag=True, default=False) @click.option('--local-stack', is_flag=True, default=False) @click.option('--debug', is_flag=True, default=False) +@click.option('--continue-on-error', is_flag=True, default=False) # See: https://click.palletsprojects.com/en/8.1.x/complex/#building-a-git-clone @click.pass_context -def cli(ctx, stack, quiet, verbose, dry_run, local_stack, debug): +def cli(ctx, stack, quiet, verbose, dry_run, local_stack, debug, continue_on_error): """Laconic Stack Orchestrator""" - ctx.obj = Options(stack, quiet, verbose, dry_run, local_stack, debug) + ctx.obj = Options(stack, quiet, verbose, dry_run, local_stack, debug, continue_on_error) cli.add_command(setup_repositories.command, "setup-repositories")