Skip to content

Commit

Permalink
Tell what script an error originated from (#48)
Browse files Browse the repository at this point in the history
An example case is OSError "[Errno 8] Exec format error".
(subprocess.CalledProcessError carried that information, already.)
  • Loading branch information
hartwork committed Jul 22, 2017
1 parent 326e226 commit e2ed364
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions directory_bootstrap/shared/output_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def run_handle_errors(main_function, messenger, options):
# Manual work to avoid list square brackets in output
command_flat = ' '.join((messenger.escape_shell(e) for e in e.cmd))
text = 'Command "%s" returned non-zero exit status %s' % (command_flat, e.returncode)
elif hasattr(e, '_ib_abs_script_filename'):
text = '%s (script "%s")' % (str(e), e._ib_abs_script_filename)
else:
text = str(e)

Expand Down
26 changes: 23 additions & 3 deletions image_bootstrap/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@
_CONSOLE_CONFIG = 'console=tty0 console=ttyS0,115200'


class _script_filename_telling_exceptions(object):
"""
Extends raised exceptions by filename of the causing script
"""
def __init__(self, abs_script_filename):
self._abs_script_filename = abs_script_filename

def __enter__(self):
pass

def __exit__(self, exc_type, exc_val, exc_tb):
if exc_val is not None:
exc_val._ib_abs_script_filename = self._abs_script_filename


class MachineConfig(object):
def __init__(self,
hostname,
Expand Down Expand Up @@ -488,8 +503,10 @@ def _run_scripts_from(self, abs_scripts_dir, env):
if not self._script_should_be_run(basename):
continue

cmd = [os.path.join(abs_scripts_dir, basename)]
self._executor.check_call(cmd, env=env.copy())
abs_script_filename = os.path.join(abs_scripts_dir, basename)
cmd = [abs_script_filename]
with _script_filename_telling_exceptions(abs_script_filename):
self._executor.check_call(cmd, env=env.copy())

def make_environment(self, tell_mountpoint):
env = os.environ.copy()
Expand Down Expand Up @@ -660,12 +677,15 @@ def _run_chroot_scripts(self):
if not self._script_should_be_run(basename):
continue

abs_script_filename = os.path.join(
self._abs_scripts_dir_chroot, basename)
cmd_run = [
COMMAND_CHROOT,
self._abs_mountpoint,
os.path.join('/', _CHROOT_SCRIPT_TARGET_DIR, basename),
]
self._executor.check_call(cmd_run, env=env.copy())
with _script_filename_telling_exceptions(abs_script_filename):
self._executor.check_call(cmd_run, env=env.copy())

def _remove_chroot_scripts(self):
self._messenger.info('Removing chroot scripts...')
Expand Down

0 comments on commit e2ed364

Please sign in to comment.