1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

centralized TERM signal handling

This commit is contained in:
Brian Coca 2016-02-09 18:21:33 -05:00
parent 1b8dec9c88
commit 5a88478ccc
3 changed files with 9 additions and 15 deletions

View file

@ -27,6 +27,7 @@ import time
import yaml import yaml
import re import re
import getpass import getpass
import signal
import subprocess import subprocess
from ansible import __version__ from ansible import __version__
@ -77,6 +78,11 @@ class CLI(object):
self.action = None self.action = None
self.callback = callback self.callback = callback
def _terminate(self, signum=None, framenum=None):
if signum is not None:
display.debug("Termination signal detected, shutting down gracefully: %d" % os.getpid() )
raise SystemExit
def set_action(self): def set_action(self):
""" """
Get the action the user wants to execute from the sys argv list. Get the action the user wants to execute from the sys argv list.
@ -109,6 +115,9 @@ class CLI(object):
else: else:
display.display(u"No config file found; using defaults") display.display(u"No config file found; using defaults")
# Manage user interruptions
signal.signal(signal.SIGTERM, self._terminate)
@staticmethod @staticmethod
def ask_vault_passwords(ask_new_vault_pass=False, rekey=False): def ask_vault_passwords(ask_new_vault_pass=False, rekey=False):
''' prompt for vault password and/or password change ''' ''' prompt for vault password and/or password change '''

View file

@ -21,7 +21,6 @@ __metaclass__ = type
######################################################## ########################################################
import os import os
import signal
from ansible import constants as C from ansible import constants as C
from ansible.cli import CLI from ansible.cli import CLI
@ -89,10 +88,6 @@ class AdHocCLI(CLI):
tasks = [ dict(action=dict(module=self.options.module_name, args=parse_kv(self.options.module_args)), async=async, poll=poll) ] tasks = [ dict(action=dict(module=self.options.module_name, args=parse_kv(self.options.module_args)), async=async, poll=poll) ]
) )
def _terminate(self, signum=None, framenum=None):
if signum is not None:
display.debug("Termination signal detected, shutting down gracefully")
raise SystemExit
def run(self): def run(self):
''' use Runner lib to do SSH things ''' ''' use Runner lib to do SSH things '''
@ -176,9 +171,6 @@ class AdHocCLI(CLI):
# now create a task queue manager to execute the play # now create a task queue manager to execute the play
self._tqm = None self._tqm = None
try: try:
# Manage user interruptions
signal.signal(signal.SIGTERM, self._terminate)
self._tqm = TaskQueueManager( self._tqm = TaskQueueManager(
inventory=inventory, inventory=inventory,
variable_manager=variable_manager, variable_manager=variable_manager,

View file

@ -22,7 +22,6 @@ __metaclass__ = type
import getpass import getpass
import locale import locale
import os import os
import signal
import sys import sys
from ansible.compat.six import string_types from ansible.compat.six import string_types
@ -69,8 +68,6 @@ class PlaybookExecutor:
may limit the runs to serialized groups, etc. may limit the runs to serialized groups, etc.
''' '''
signal.signal(signal.SIGTERM, self._terminate)
result = 0 result = 0
entrylist = [] entrylist = []
entry = {} entry = {}
@ -207,10 +204,6 @@ class PlaybookExecutor:
return result return result
def _terminate(self, signum=None, framenum=None):
display.debug("Termination signal detected, shutting down gracefully")
raise SystemExit
def _get_serialized_batches(self, play): def _get_serialized_batches(self, play):
''' '''
Returns a list of hosts, subdivided into batches based on Returns a list of hosts, subdivided into batches based on