ltmpl: improve exception handling/reporting
- Log the error message (and the line causing the error) to console - Log the whole exception (with some excess junk removed) to debuglog - don't immediately SystemExit if the template won't parse - clean up some comments
This commit is contained in:
parent
473f01c696
commit
318c843e37
@ -33,6 +33,7 @@ from base import DataHolder
|
|||||||
|
|
||||||
from mako.lookup import TemplateLookup
|
from mako.lookup import TemplateLookup
|
||||||
from mako.exceptions import text_error_template
|
from mako.exceptions import text_error_template
|
||||||
|
import sys, traceback
|
||||||
|
|
||||||
class LoraxTemplate(object):
|
class LoraxTemplate(object):
|
||||||
def __init__(self, directories=["/usr/share/lorax"]):
|
def __init__(self, directories=["/usr/share/lorax"]):
|
||||||
@ -47,8 +48,8 @@ class LoraxTemplate(object):
|
|||||||
try:
|
try:
|
||||||
textbuf = template.render(**variables)
|
textbuf = template.render(**variables)
|
||||||
except:
|
except:
|
||||||
print text_error_template().render()
|
logger.error(text_error_template().render())
|
||||||
raise SystemExit(2)
|
raise
|
||||||
|
|
||||||
# split, strip and remove empty lines
|
# split, strip and remove empty lines
|
||||||
lines = textbuf.splitlines()
|
lines = textbuf.splitlines()
|
||||||
@ -94,12 +95,6 @@ def rglob(pathname, root="/", fatal=False):
|
|||||||
def rexists(pathname, root=""):
|
def rexists(pathname, root=""):
|
||||||
return True if rglob(pathname, root) else False
|
return True if rglob(pathname, root) else False
|
||||||
|
|
||||||
# command notes:
|
|
||||||
# "install" and "exist" assume their first argument is in inroot
|
|
||||||
# everything else operates on outroot
|
|
||||||
# multiple args allowed: mkdir, treeinfo, runcmd, remove, replace
|
|
||||||
# globs accepted: chmod, install*, remove*, replace
|
|
||||||
|
|
||||||
class LoraxTemplateRunner(object):
|
class LoraxTemplateRunner(object):
|
||||||
def __init__(self, inroot, outroot, yum=None, fatalerrors=False,
|
def __init__(self, inroot, outroot, yum=None, fatalerrors=False,
|
||||||
templatedir=None, defaults={}):
|
templatedir=None, defaults={}):
|
||||||
@ -130,13 +125,15 @@ class LoraxTemplateRunner(object):
|
|||||||
def run(self, templatefile, **variables):
|
def run(self, templatefile, **variables):
|
||||||
for k,v in self.defaults.items() + self.builtins.items():
|
for k,v in self.defaults.items() + self.builtins.items():
|
||||||
variables.setdefault(k,v)
|
variables.setdefault(k,v)
|
||||||
logger.info("parsing %s", templatefile)
|
logger.debug("parsing %s", templatefile)
|
||||||
|
self.templatefile = templatefile
|
||||||
t = LoraxTemplate(directories=[self.templatedir])
|
t = LoraxTemplate(directories=[self.templatedir])
|
||||||
commands = t.parse(templatefile, variables)
|
commands = t.parse(templatefile, variables)
|
||||||
self._run(commands)
|
self._run(commands)
|
||||||
|
|
||||||
|
|
||||||
def _run(self, parsed_template):
|
def _run(self, parsed_template):
|
||||||
logger.info("running template commands")
|
logger.info("running %s", self.templatefile)
|
||||||
for (num, line) in enumerate(parsed_template,1):
|
for (num, line) in enumerate(parsed_template,1):
|
||||||
logger.debug("template line %i: %s", num, " ".join(line))
|
logger.debug("template line %i: %s", num, " ".join(line))
|
||||||
(cmd, args) = (line[0], line[1:])
|
(cmd, args) = (line[0], line[1:])
|
||||||
@ -146,11 +143,20 @@ class LoraxTemplateRunner(object):
|
|||||||
if f is None or cmd is 'run':
|
if f is None or cmd is 'run':
|
||||||
raise ValueError, "unknown command %s" % cmd
|
raise ValueError, "unknown command %s" % cmd
|
||||||
f(*args)
|
f(*args)
|
||||||
except Exception as e:
|
except Exception:
|
||||||
logger.error("template command error: %s", str(line))
|
logger.error("template command error in %s:", self.templatefile)
|
||||||
|
logger.error(" %s", " ".join(line))
|
||||||
|
# format the exception traceback
|
||||||
|
exclines = traceback.format_exception(*sys.exc_info())
|
||||||
|
# skip the bit about "ltmpl.py, in _run()" - we know that
|
||||||
|
exclines.pop(1)
|
||||||
|
# log the "ErrorType: this is what happened" line
|
||||||
|
logger.error(" " + exclines[-1].strip())
|
||||||
|
# and log the entire traceback to the debug log
|
||||||
|
for line in ''.join(exclines).splitlines():
|
||||||
|
logger.debug(" " + line)
|
||||||
if self.fatalerrors:
|
if self.fatalerrors:
|
||||||
raise
|
raise
|
||||||
logger.error(str(e))
|
|
||||||
|
|
||||||
def install(self, srcglob, dest):
|
def install(self, srcglob, dest):
|
||||||
'''
|
'''
|
||||||
@ -337,6 +343,7 @@ class LoraxTemplateRunner(object):
|
|||||||
'''
|
'''
|
||||||
logger.info(msg)
|
logger.info(msg)
|
||||||
|
|
||||||
|
# TODO: add ssh-keygen, mkisofs(?), find, and other useful commands
|
||||||
def runcmd(self, *cmdlist):
|
def runcmd(self, *cmdlist):
|
||||||
'''
|
'''
|
||||||
runcmd CMD [--chdir=DIR] [ARG ...]
|
runcmd CMD [--chdir=DIR] [ARG ...]
|
||||||
|
Loading…
Reference in New Issue
Block a user