Matt Hart | a903c62 | 2013-08-15 16:00:16 +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 logging |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 22 | import os |
| 23 | import sys |
| 24 | import optparse |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 25 | from lavapdu.shared import get_daemon_logger |
| 26 | from lavapdu.shared import read_settings |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 27 | import lavapdu.runnermaster as runnermaster |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 28 | |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 29 | import daemon |
Neil Williams | 51f0f44 | 2015-07-01 19:31:39 +0100 | [diff] [blame] | 30 | try: |
| 31 | import daemon.pidlockfile as pidlockfile |
| 32 | except ImportError: |
| 33 | from lockfile import pidlockfile |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 34 | |
matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame] | 35 | from lavapdu.pdurunner import PDURunner |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 36 | |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 37 | |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 38 | if __name__ == '__main__': |
matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame] | 39 | pidfile = "/var/run/lavapdu-runner.pid" |
| 40 | logfile = "/var/log/lavapdu-runner.log" |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 41 | conffile = "/etc/lavapdu/lavapdu.conf" |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 42 | settings = read_settings(conffile) |
Matt Hart | 7d67061 | 2013-08-20 16:47:52 +0100 | [diff] [blame] | 43 | level = logging.DEBUG |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 44 | daemon_settings = settings["daemon"] |
| 45 | if daemon_settings["logging_level"] == "DEBUG": |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 46 | level = logging.DEBUG |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 47 | if daemon_settings["logging_level"] == "WARNING": |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 48 | level = logging.WARNING |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 49 | if daemon_settings["logging_level"] == "ERROR": |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 50 | level = logging.ERROR |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 51 | if daemon_settings["logging_level"] == "INFO": |
matthew.hart@linaro.org | 426d085 | 2013-10-30 15:23:15 -0700 | [diff] [blame] | 52 | level = logging.INFO |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 53 | client_logger, watched_file_handler = get_daemon_logger( |
| 54 | logfile, |
| 55 | loglevel=level, |
| 56 | log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s') |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 57 | if isinstance(client_logger, Exception): |
| 58 | print("Fatal error creating client_logger: " + str(client_logger)) |
| 59 | sys.exit(os.EX_OSERR) |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 60 | context = daemon.DaemonContext( |
Neil Williams | 51f0f44 | 2015-07-01 19:31:39 +0100 | [diff] [blame] | 61 | detach_process=True, |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 62 | working_directory=os.getcwd(), |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 63 | files_preserve=[watched_file_handler.stream], |
| 64 | stderr=watched_file_handler.stream, |
| 65 | stdout=watched_file_handler.stream) |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 66 | with context: |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame] | 67 | runnermaster.start_em_up(settings, pidfile) |