From daacb4465d52e3ff77340ef36a1b13b59b53234e Mon Sep 17 00:00:00 2001 From: Will Woods Date: Mon, 18 Jun 2012 18:18:37 -0400 Subject: [PATCH] fix '-runcmd' and improve logging The '-cmd' functionality depends on the individual lorax template commands raising errors, so they shouldn't do sys.exit(). Also, capture stderr along with stdout, and put both in the log. --- src/pylorax/ltmpl.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/pylorax/ltmpl.py b/src/pylorax/ltmpl.py index 994b6012..7b5b29e6 100644 --- a/src/pylorax/ltmpl.py +++ b/src/pylorax/ltmpl.py @@ -25,7 +25,7 @@ logger = logging.getLogger("pylorax.ltmpl") import os, re, glob, shlex, fnmatch from os.path import basename, isdir -from subprocess import check_call, check_output, CalledProcessError +from subprocess import check_call, check_output, CalledProcessError, STDOUT from sysutils import joinpaths, cpfile, mvfile, replace, remove from yumhelper import * # Lorax*Callback classes @@ -155,8 +155,7 @@ class LoraxTemplateRunner(object): f(*args) except Exception: if skiperror: - logger.error("template command error in %s (ignored):", self.templatefile) - logger.error(" %s", " ".join(line)) + logger.debug("ignoring error") continue logger.error("template command error in %s:", self.templatefile) logger.error(" %s", " ".join(line)) @@ -375,10 +374,15 @@ class LoraxTemplateRunner(object): cmd = cmd[1:] try: - check_output(cmd, preexec_fn=chdir) + output = check_output(cmd, preexec_fn=chdir, stderr=STDOUT) + if output: + logger.debug('command output:\n%s', output) + logger.debug("command finished successfully") except CalledProcessError as e: - logger.debug('command exited with %d: %s', e.returncode, e.output) - sys.exit(e.returncode) + if e.output: + logger.debug('command output:\n%s', e.output) + logger.debug('command returned failure (%d)', e.returncode) + raise def installpkg(self, *pkgs): ''' @@ -399,7 +403,7 @@ class LoraxTemplateRunner(object): # FIXME: save exception and re-raise after the loop finishes logger.error("installpkg %s failed: %s",p,str(e)) if required: - sys.exit(1) + raise def removepkg(self, *pkgs): '''