]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Better catching errors on executing terraform
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 13 Dec 2019 12:21:28 +0000 (13:21 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 13 Dec 2019 12:21:28 +0000 (13:21 +0100)
lib/cr_tf/handler.py

index ca7bf221193b57fe8ffa4afd533949800bb2c93e..06ea23817d6d91962dbfeae7387aa7f99e3f4f12 100644 (file)
@@ -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))