diff --git a/bareon/cmd/agent.py b/bareon/cmd/agent.py index 97145e3..071c43a 100644 --- a/bareon/cmd/agent.py +++ b/bareon/cmd/agent.py @@ -12,12 +12,15 @@ # License for the specific language governing permissions and limitations # under the License. +import os +import signal import sys from oslo_config import cfg import six import yaml +from bareon import errors from bareon.openstack.common import log as logging from bareon.utils import utils from bareon import version @@ -112,6 +115,16 @@ def print_err(line): sys.stderr.write('\n') +def handle_sigterm(signum, frame): + # NOTE(agordeev): Since group pid of spawned subprocess is unknown, + # fuel-agent needs to ignore SIGTERM sent by itself. + signal.signal(signal.SIGTERM, signal.SIG_IGN) + print_err('SIGTERM RECEIVED. Propagating it to subprocesses') + os.killpg(os.getpid(), signal.SIGTERM) + raise errors.UnexpectedProcessError( + 'Application was shut down gracefully') + + def handle_exception(exc): LOG = logging.getLogger(__name__) LOG.exception(exc) @@ -121,6 +134,10 @@ def handle_exception(exc): def main(actions=None): + # NOTE(agordeev): get its own process group by calling setpgrp. + # Process group is used to distribute signals to subprocesses. + os.setpgrp() + signal.signal(signal.SIGTERM, handle_sigterm) CONF(sys.argv[1:], project='bareon', version=version.version_info.release_string())