Log output of failed command

If we run a command from template with runcmd,
log it's output if it exits with non-zero return code.
This commit is contained in:
Martin Gracik 2012-01-19 13:50:02 +01:00
parent 4758a2aa80
commit 681f67f954
1 changed files with 8 additions and 2 deletions

View File

@ -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
from subprocess import check_call, check_output
from sysutils import joinpaths, cpfile, mvfile, replace, remove
from yumhelper import * # Lorax*Callback classes
@ -366,11 +366,17 @@ class LoraxTemplateRunner(object):
'''
chdir = lambda: None
cmd = cmdlist
logger.debug('running command: %s', cmd)
if cmd[0].startswith("--chdir="):
dirname = cmd[0].split('=',1)[1]
chdir = lambda: os.chdir(dirname)
cmd = cmd[1:]
check_call(cmd, preexec_fn=chdir)
try:
check_output(cmd, preexec_fn=chdir)
except CalledProcessError as e:
logger.debug('command exited with %d: %s', e.returncode, e.output)
sys.exit(e.returncode)
def installpkg(self, *pkgs):
'''