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 |
| 22 | import json |
| 23 | import os |
| 24 | import sys |
| 25 | import optparse |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 26 | from logging.handlers import WatchedFileHandler |
| 27 | |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 28 | import daemon |
| 29 | import daemon.pidlockfile |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 30 | |
matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame] | 31 | from lavapdu.pdurunner import PDURunner |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 32 | |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 33 | |
| 34 | def getDaemonLogger(filePath, log_format=None, loglevel=logging.INFO): |
| 35 | logger = logging.getLogger() |
| 36 | logger.setLevel(loglevel) |
| 37 | try: |
| 38 | watchedHandler = WatchedFileHandler(filePath) |
| 39 | except Exception as e: |
| 40 | return e |
| 41 | |
| 42 | watchedHandler.setFormatter(logging.Formatter(log_format or '%(asctime)s %(msg)s')) |
| 43 | logger.addHandler(watchedHandler) |
| 44 | return logger, watchedHandler |
| 45 | |
| 46 | |
| 47 | def readSettings(filename): |
| 48 | """ |
| 49 | Read settings from config file, to listen to all hosts, hostname should be 0.0.0.0 |
| 50 | """ |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 51 | pdus = {} |
| 52 | settings = {} |
| 53 | print("Reading settings from %s" % conffile) |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 54 | with open(filename) as stream: |
| 55 | jobdata = stream.read() |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 56 | json_data = json.loads(jobdata) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 57 | return json_data |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 58 | |
| 59 | if __name__ == '__main__': |
| 60 | # instance settings come from django - the coordinator doesn't use django and is |
| 61 | # not necessarily per-instance, so use the command line and a default conf file. |
matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame] | 62 | pidfile = "/var/run/lavapdu-runner.pid" |
| 63 | logfile = "/var/log/lavapdu-runner.log" |
Matt Hart | 2af7114 | 2014-07-22 10:40:41 +0100 | [diff] [blame] | 64 | conffile = "/etc/lavapdu/lavapdu.conf" |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 65 | settings = readSettings(conffile) |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 66 | usage = "Usage: %prog [--logfile] --[loglevel]" |
| 67 | description = "LAVA PDU request listener server, host and port are handled in %s" % conffile |
| 68 | parser = optparse.OptionParser(usage=usage, description=description) |
| 69 | parser.add_option("--logfile", dest="logfile", action="store", |
| 70 | type="string", help="log file [%s]" % logfile) |
| 71 | parser.add_option("--loglevel", dest="loglevel", action="store", |
| 72 | type="string", help="logging level [INFO]") |
| 73 | (options, args) = parser.parse_args() |
| 74 | if options.logfile: |
| 75 | if os.path.exists(os.path.dirname(options.logfile)): |
| 76 | logfile = options.logfile |
| 77 | else: |
| 78 | print "No such directory for specified logfile '%s'" % logfile |
| 79 | open(logfile, 'w').close() |
Matt Hart | 7d67061 | 2013-08-20 16:47:52 +0100 | [diff] [blame] | 80 | level = logging.DEBUG |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 81 | daemon_settings = settings["daemon"] |
| 82 | if daemon_settings["logging_level"] == "DEBUG": |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 83 | level = logging.DEBUG |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 84 | if daemon_settings["logging_level"] == "WARNING": |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 85 | level = logging.WARNING |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 86 | if daemon_settings["logging_level"] == "ERROR": |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 87 | level = logging.ERROR |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 88 | if daemon_settings["logging_level"] == "INFO": |
matthew.hart@linaro.org | 426d085 | 2013-10-30 15:23:15 -0700 | [diff] [blame] | 89 | level = logging.INFO |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 90 | client_logger, watched_file_handler = getDaemonLogger(logfile, loglevel=level, |
| 91 | log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s') |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 92 | if isinstance(client_logger, Exception): |
| 93 | print("Fatal error creating client_logger: " + str(client_logger)) |
| 94 | sys.exit(os.EX_OSERR) |
| 95 | # noinspection PyArgumentList |
| 96 | lockfile = daemon.pidlockfile.PIDLockFile(pidfile) |
| 97 | if lockfile.is_locked(): |
| 98 | logging.error("PIDFile %s already locked" % pidfile) |
| 99 | sys.exit(os.EX_OSERR) |
| 100 | context = daemon.DaemonContext( |
| 101 | working_directory=os.getcwd(), |
| 102 | pidfile=lockfile, |
| 103 | files_preserve=[watched_file_handler.stream], |
| 104 | stderr=watched_file_handler.stream, |
| 105 | stdout=watched_file_handler.stream) |
Matt Hart | a903c62 | 2013-08-15 16:00:16 +0100 | [diff] [blame] | 106 | with context: |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 107 | logging.info("Running LAVA PDU Runner %s dbhost: %s" |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 108 | % (logfile, settings["daemon"]["dbhost"])) |
| 109 | p = PDURunner(settings) |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 110 | p.run_me() |