termination handling

- moved to base cli class to handle centrally and duplicate less code
- now avoids duplication and reiteration of signal handler by reassigning it
- left note on how to do non-graceful in case we add in future
  as I won't remember everything i did here and don't want to 'relearn' it.
This commit is contained in:
Brian Coca 2016-02-10 09:48:05 -05:00
commit 38120c1075
3 changed files with 19 additions and 22 deletions

View file

@ -27,6 +27,7 @@ import time
import yaml
import re
import getpass
import signal
import subprocess
from ansible import __version__
@ -44,7 +45,7 @@ except ImportError:
class SortedOptParser(optparse.OptionParser):
'''Optparser which sorts the options by opt before outputting --help'''
# TODO: epilog parsing: OptionParser.format_epilog = lambda self, formatter: self.epilog
#FIXME: epilog parsing: OptionParser.format_epilog = lambda self, formatter: self.epilog
def format_help(self, formatter=None, epilog=None):
self.option_list.sort(key=operator.methodcaller('get_opt_string'))
@ -77,6 +78,20 @@ class CLI(object):
self.action = None
self.callback = callback
def _terminate(self, signum=None, framenum=None):
if signum == signal.SIGTERM:
if hasattr(os, 'getppid'):
display.debug("Termination requested in parent, shutting down gracefully")
signal.signal(signal.SIGTERM, signal.SIG_DFL)
else:
display.debug("Term signal in child, harakiri!")
signal.signal(signal.SIGTERM, signal.SIG_IGN)
raise SystemExit
#NOTE: if ever want to make this immediately kill children use on parent:
#os.killpg(os.getpgid(0), signal.SIGTERM)
def set_action(self):
"""
Get the action the user wants to execute from the sys argv list.
@ -109,6 +124,9 @@ class CLI(object):
else:
display.display(u"No config file found; using defaults")
# Manage user interruptions
signal.signal(signal.SIGTERM, self._terminate)
@staticmethod
def ask_vault_passwords(ask_new_vault_pass=False, rekey=False):
''' prompt for vault password and/or password change '''