diff options
author | asteroide <thomas.duval@orange.com> | 2017-06-28 13:55:21 +0200 |
---|---|---|
committer | asteroide <thomas.duval@orange.com> | 2017-06-28 13:55:21 +0200 |
commit | 4f120c725dcf091ab5d9a7a3839f110e52c46699 (patch) | |
tree | 31713fb41fe014554d66d577f240c0e7233e4387 | |
parent | 4bcec5c4296280c7831a88a7fcf655a477777cc7 (diff) |
Fork Moon process to let it run in the background and create a pid file
in current directory if the user is not root
Change-Id: I21eff1bcc02e6e8b6332533a2b5ca552a8d839cf
-rw-r--r-- | moonv4/moon_orchestrator/moon_orchestrator/server.py | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/moonv4/moon_orchestrator/moon_orchestrator/server.py b/moonv4/moon_orchestrator/moon_orchestrator/server.py index 959375be..ef69af70 100644 --- a/moonv4/moon_orchestrator/moon_orchestrator/server.py +++ b/moonv4/moon_orchestrator/moon_orchestrator/server.py @@ -5,6 +5,7 @@ import sys import os +import signal import hashlib from oslo_config import cfg from oslo_log import log as logging @@ -34,6 +35,9 @@ docker = Client(base_url=CONF.docker_url) # env = Environment(loader=simple_loader) # return env.get_template(filename) +def kill_handler(signum, frame): + _exit(0) + def create_docker_network(name="moon"): @@ -84,7 +88,7 @@ class DockerManager: docker.remove_container(container=component_id) -def _exit(exit_number=0, docker=None, error=None): +def _exit(exit_number=0, error=None): for _container in CONTAINERS: LOG.warning("Deleting containers named {}...".format(_container)) # print(40 * "-" + _container) @@ -109,9 +113,10 @@ def __save_pid(): open("/var/run/moon_orchestrator.pid", "w").write(str(os.getpid())) except PermissionError: LOG.warning("You don't have the right to write PID file in /var/run... Continuing anyway.") + open("./moon_orchestrator.pid", "w").write(str(os.getpid())) -def main(): +def server(): # conf_file = options.configure(DOMAIN) __save_pid() LOG.info("Starting server with IP {}".format(CONF.orchestrator.host)) @@ -136,12 +141,20 @@ def main(): router.get_status() except oslo_messaging.rpc.client.RemoteError as e: LOG.error("Cannot check status of remote container!") - _exit(1, docker, e) + _exit(1, e) serv = messenger.Server(containers=CONTAINERS, docker_manager=docker_manager, slaves=SLAVES) try: serv.run() finally: - _exit(0, docker) + _exit(0) + + +def main(): + signal.signal(signal.SIGTERM, kill_handler) + signal.signal(signal.SIGHUP, kill_handler) + newpid = os.fork() + if newpid == 0: + server() if __name__ == '__main__': |