From: Frank Brehm Date: Fri, 13 Dec 2019 12:21:28 +0000 (+0100) Subject: Better catching errors on executing terraform X-Git-Tag: 1.3.14~1^2 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=df92d5634bb0de1e88c10ab4755f226623cfdc06;p=pixelpark%2Fcreate-terraform.git Better catching errors on executing terraform --- diff --git a/lib/cr_tf/handler.py b/lib/cr_tf/handler.py index ca7bf22..06ea238 100644 --- a/lib/cr_tf/handler.py +++ b/lib/cr_tf/handler.py @@ -38,7 +38,7 @@ from fb_tools.common import pp, to_bool, RE_DOT_AT_END from fb_tools.errors import HandlerError, ExpectedHandlerError, CommandNotFoundError -from fb_tools.handling_obj import HandlingObject +from fb_tools.handling_obj import HandlingObject, CalledProcessError from fb_tools.handler import BaseHandler @@ -60,7 +60,7 @@ from .terraform.disk import TerraformDisk from .xlate import XLATOR -__version__ = '3.2.3' +__version__ = '3.3.1' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -2802,8 +2802,17 @@ class CreateTerraformHandler(BaseHandler): print() LOG.info(_("Executing {!r} ...").format('terraform init')) cmd = [str(self.terraform_cmd), 'init'] - result = self.run( - cmd, may_simulate=True, timeout=tf_timeout, stdout=PIPE, stderr=PIPE, check=True) + try: + result = self.run( + cmd, may_simulate=True, timeout=tf_timeout, stdout=PIPE, stderr=PIPE, check=True) + except CalledProcessError as e: + msg = _("Error on executing {cmd!r}: return value {v}.").format( + cmd=e.cmd, v=e.returncode) + if e.stdout: + msg += '\nOutput: {}'.format(e.stdout) + if e.stderr: + msg += '\nError message: {}'.format(e.stderr) + raise ExpectedHandlerError(msg) LOG.debug(_("Completed process:") + "\n" + str(result)) if self.existing_vms: @@ -2820,16 +2829,35 @@ class CreateTerraformHandler(BaseHandler): dc=self.vsphere[vs_name].dc, f=self.vsphere[vs_name].dc_obj.vm_folder, p=vm.path, n=vm.name) cmd = [str(self.terraform_cmd), 'import', vm_obj, path] - result = self.run( - cmd, may_simulate=True, timeout=tf_timeout, - stdout=PIPE, stderr=PIPE, check=True) + try: + result = self.run( + cmd, may_simulate=True, timeout=tf_timeout, + stdout=PIPE, stderr=PIPE, check=True) + except CalledProcessError as e: + msg = _("Error on executing {cmd!r}: return value {v}.").format( + cmd=e.cmd, v=e.returncode) + if e.stdout: + msg += '\nOutput: {}'.format(e.stdout) + if e.stderr: + msg += '\nError message: {}'.format(e.stderr) + raise ExpectedHandlerError(msg) + LOG.debug(_("Completed process:") + "\n" + str(result)) print() LOG.info(_("Executing {!r} ...").format('terraform plan')) cmd = [str(self.terraform_cmd), 'plan'] - result = self.run( - cmd, may_simulate=True, timeout=tf_timeout, stdout=PIPE, stderr=PIPE, check=True) + try: + result = self.run( + cmd, may_simulate=True, timeout=tf_timeout, stdout=PIPE, stderr=PIPE, check=True) + except CalledProcessError as e: + msg = _("Error on executing {cmd!r}: return value {v}.").format( + cmd=e.cmd, v=e.returncode) + if e.stdout: + msg += '\nOutput: {}'.format(e.stdout) + if e.stderr: + msg += '\nError message: {}'.format(e.stderr) + raise ExpectedHandlerError(msg) LOG.debug(_("Completed process:") + "\n" + str(result)) goto = Path(os.path.relpath(self.project_dir, self.start_dir))