Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2013 Linaro Limited |
| 4 | # Author Matt Hart <matthew.hart@linaro.org> |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation; either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | # MA 02110-1301, USA. |
| 20 | |
| 21 | import lavapdu.pdurunner as pdurunner |
| 22 | from multiprocessing import Process |
| 23 | from setproctitle import setproctitle # pylint: disable=no-name-in-module |
| 24 | from lavapdu.shared import pdus_from_config |
| 25 | import signal |
| 26 | import sys |
| 27 | import os |
| 28 | import logging |
| 29 | processes = [] |
| 30 | log = logging.getLogger(__name__) |
| 31 | |
| 32 | |
| 33 | def start_runner(config, pdu): |
| 34 | setproctitle("pdurunner for %s" % pdu) |
| 35 | p = pdurunner.PDURunner(config, pdu) |
| 36 | p.run_me() |
| 37 | |
| 38 | |
| 39 | def start_em_up(config, pidfile): |
| 40 | pid = os.getpid() |
| 41 | if os.path.isfile(pidfile): |
| 42 | log.error("Pidfile already exists") |
| 43 | sys.exit(1) |
| 44 | f = open(pidfile, 'w') |
| 45 | f.write(str(pid)) |
| 46 | f.close() |
| 47 | pdus = pdus_from_config(config) |
| 48 | for pdu in pdus: |
| 49 | p = Process(target=start_runner, args=(config, pdu)) |
| 50 | p.start() |
| 51 | processes.append(p) |
| 52 | signal.signal(signal.SIGTERM, signal_term_handler) |
| 53 | |
| 54 | |
| 55 | def signal_term_handler(a, b): |
| 56 | del a, b |
| 57 | print 'Sending sigterm to all children' |
| 58 | for proc in processes: |
| 59 | log.debug("Terminate %s", proc.pid) |
| 60 | proc.terminate() |
| 61 | sys.exit(0) |