Gracefully handle log following output

If the Zookeeper node goes away (e.g. VM is deleted), return a
reasonable error message instead of a stack trace.
This commit is contained in:
Joshua Boniface 2019-09-07 12:30:31 -04:00
parent 983daceaed
commit 0c9d6db14f
1 changed files with 29 additions and 24 deletions

View File

@ -529,30 +529,35 @@ def follow_console_log(zk_conn, domain, lines=10):
# Print the initial data and begin following # Print the initial data and begin following
print(loglines, end='') print(loglines, end='')
while True: try:
# Grab the next line set while True:
new_console_log = zkhandler.readdata(zk_conn, '/domains/{}/consolelog'.format(dom_uuid)) # Grab the next line set
# Split the new and old log strings into constitutent lines new_console_log = zkhandler.readdata(zk_conn, '/domains/{}/consolelog'.format(dom_uuid))
old_console_loglines = console_log.split('\n') # Split the new and old log strings into constitutent lines
new_console_loglines = new_console_log.split('\n') old_console_loglines = console_log.split('\n')
# Set the console log to the new log value for the next iteration new_console_loglines = new_console_log.split('\n')
console_log = new_console_log # Set the console log to the new log value for the next iteration
# Remove the lines from the old log until we hit the first line of the new log; this console_log = new_console_log
# ensures that the old log is a string that we can remove from the new log entirely # Remove the lines from the old log until we hit the first line of the new log; this
for index, line in enumerate(old_console_loglines, start=0): # ensures that the old log is a string that we can remove from the new log entirely
if line == new_console_loglines[0]: for index, line in enumerate(old_console_loglines, start=0):
del old_console_loglines[0:index] if line == new_console_loglines[0]:
break del old_console_loglines[0:index]
# Rejoin the log lines into strings break
old_console_log = '\n'.join(old_console_loglines) # Rejoin the log lines into strings
new_console_log = '\n'.join(new_console_loglines) old_console_log = '\n'.join(old_console_loglines)
# Remove the old lines from the new log new_console_log = '\n'.join(new_console_loglines)
diff_console_log = new_console_log.replace(old_console_log, "") # Remove the old lines from the new log
# If there's a difference, print it out diff_console_log = new_console_log.replace(old_console_log, "")
if diff_console_log: # If there's a difference, print it out
print(diff_console_log, end='') if diff_console_log:
# Wait a second print(diff_console_log, end='')
time.sleep(1) # Wait a second
time.sleep(1)
except kazoo.exceptions.NoNodeError:
return False, 'ERROR: VM has gone away.'
except:
return False, 'ERROR: Lost connection to Zookeeper node.'
return True, '' return True, ''