Provide much better error messages

- When processes fail, they actually print a failure message
  on the user's terminal
- Regardless of success or failure, we print all output to
  /opt/tljh/installer.log

This should make debugging people's issues *much* easier, since
we can actually see the output of failing commands rather than
having to guess.
This commit is contained in:
yuvipanda
2019-05-19 13:45:57 -07:00
parent 190b61d953
commit 7071332445
5 changed files with 99 additions and 29 deletions

37
tljh/utils.py Normal file
View File

@@ -0,0 +1,37 @@
"""
Miscelaneous functions useful in at least two places unrelated to each other
"""
import subprocess
import logging
# Copied into bootstrap/bootstrap.py. Make sure these two copies are exactly the same!
def run_subprocess(cmd, *args, **kwargs):
"""
Run given cmd with smart output behavior.
If command succeeds, print output to debug logging.
If it fails, print output to info logging.
In TLJH, this sends successful output to the installer log,
and failed output directly to the user's screen
"""
logger = logging.getLogger('tljh')
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, *args, **kwargs)
printable_command = ' '.join(cmd)
if proc.returncode != 0:
# Our process failed! Show output to the user
logger.error(proc.stdout.decode())
e = Exception( 'command {command} failed with return code {code}'.format(
printable_command, proc.returncode
))
logger.exception(e)
raise e
else:
# This goes into installer.log
logger.debug('Ran {command} with exit code {code}'.format(
command=printable_command, code=proc.returncode
))
# This produces multi line log output, unfortunately. Not sure how to fix.
# For now, prioritizing human readability over machine readability.
logger.debug(proc.stdout.decode())