From d3e2d4b3cced3542f2eecd61ba6f062792758757 Mon Sep 17 00:00:00 2001 From: Nagarajan Manokaran Date: Tue, 15 Feb 2022 17:40:53 +0800 Subject: [PATCH 1/2] Brought packer build under error handling with native sh exeception --- packer.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/packer.py b/packer.py index 33cc535..9daf51d 100644 --- a/packer.py +++ b/packer.py @@ -6,12 +6,11 @@ DEFAULT_PACKER_PATH = 'packer' -class Packer(object): +class Packer(): """A packer client """ - def __init__(self, packerfile, exc=None, only=None, vars=None, - var_file=None, exec_path=DEFAULT_PACKER_PATH, out_iter=None, + var_file=None, exec_path=None, out_iter=None, err_iter=None): """ :param string packerfile: Path to Packer template file @@ -42,9 +41,8 @@ def __init__(self, packerfile, exc=None, only=None, vars=None, self.packer = self.packer.bake(**kwargs) def build(self, parallel=True, debug=False, force=False, - machine_readable=False): + machine_readable=True): """Executes a `packer build` - :param bool parallel: Run builders in parallel :param bool debug: Run in debug mode :param bool force: Force artifact output even if exists @@ -58,12 +56,21 @@ def build(self, parallel=True, debug=False, force=False, self._add_opt('-machine-readable' if machine_readable else None) self._append_base_arguments() self._add_opt(self.packerfile) - - return self.packer_cmd() + try: + validation = self.packer_cmd() + validation.status = validation.exit_code == 0 + validation.error = None + except sh.ErrorReturnCode as e: + validation = ValidationObject() + validation.status = False + validation.error = e.stderr + validation.stdout = e.stdout + finally: + validation.output = ( validation.error.decode() if validation.error is not None else '' )+ validation.stdout.decode() + return validation def fix(self, to_file=None): """Implements the `packer fix` function - :param string to_file: File to output fixed template to """ self.packer_cmd = self.packer.fix @@ -79,7 +86,6 @@ def fix(self, to_file=None): def inspect(self, mrf=True): """Inspects a Packer Templates file (`packer inspect -machine-readable`) - To return the output in a readable form, the `-machine-readable` flag is appended automatically, afterwhich the output is parsed and returned as a dict of the following format: @@ -104,7 +110,6 @@ def inspect(self, mrf=True): "name": "amazon" } ] - :param bool mrf: output in machine-readable form. """ self.packer_cmd = self.packer.inspect @@ -122,7 +127,6 @@ def inspect(self, mrf=True): def push(self, create=True, token=False): """Implmenets the `packer push` function - UNTESTED! Must be used alongside an Atlas account """ self.packer_cmd = self.packer.push @@ -135,7 +139,6 @@ def push(self, create=True, token=False): def validate(self, syntax_only=False): """Validates a Packer Template file (`packer validate`) - If the validation failed, an `sh` exception will be raised. :param bool syntax_only: Whether to validate the syntax only without validating the configuration itself. @@ -162,7 +165,6 @@ def validate(self, syntax_only=False): def version(self): """Returns Packer's version number (`packer version`) - As of v0.7.5, the format shows when running `packer version` is: Packer vX.Y.Z. This method will only returns the number, without the `packer v` prefix so that you don't have to parse the version @@ -182,7 +184,6 @@ def _validate_argtype(self, arg, argtype): def _append_base_arguments(self): """Appends base arguments to packer commands. - -except, -only, -var and -var-file are appeneded to almost all subcommands in packer. As such this can be called to add these flags to the subcommand. @@ -205,7 +206,6 @@ def _join_comma(self, lst): def _parse_inspection_output(self, output): """Parses the machine-readable output `packer inspect` provides. - See the inspect method for more info. This has been tested vs. Packer v0.7.5 """ @@ -227,7 +227,7 @@ def _parse_inspection_output(self, output): return parts -class Installer(object): +class Installer(): def __init__(self, packer_path, installer_path): self.packer_path = packer_path self.installer_path = installer_path @@ -252,6 +252,5 @@ def _verify_packer_installed(self, packer_path): class ValidationObject(): pass - class PackerException(Exception): - pass + pass \ No newline at end of file From dd6765cc29703553d1ef775d6f97bc274c90c101 Mon Sep 17 00:00:00 2001 From: Nagarajan Manokaran Date: Tue, 15 Feb 2022 17:53:00 +0800 Subject: [PATCH 2/2] referenced back packer exec --- packer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packer.py b/packer.py index 9daf51d..4f09693 100644 --- a/packer.py +++ b/packer.py @@ -10,7 +10,7 @@ class Packer(): """A packer client """ def __init__(self, packerfile, exc=None, only=None, vars=None, - var_file=None, exec_path=None, out_iter=None, + var_file=None, exec_path=DEFAULT_PACKER_PATH, out_iter=None, err_iter=None): """ :param string packerfile: Path to Packer template file