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 |
| 26 | import daemon |
| 27 | import daemon.pidlockfile |
| 28 | from logging.handlers import WatchedFileHandler |
| 29 | from socketserver import ListenerServer |
| 30 | |
| 31 | def getDaemonLogger(filePath, log_format=None, loglevel=logging.INFO): |
| 32 | logger = logging.getLogger() |
| 33 | logger.setLevel(loglevel) |
| 34 | try: |
| 35 | watchedHandler = WatchedFileHandler(filePath) |
| 36 | except Exception as e: |
| 37 | return e |
| 38 | |
| 39 | watchedHandler.setFormatter(logging.Formatter(log_format or '%(asctime)s %(msg)s')) |
| 40 | logger.addHandler(watchedHandler) |
| 41 | return logger, watchedHandler |
| 42 | |
| 43 | |
| 44 | def readSettings(filename): |
| 45 | """ |
| 46 | Read settings from config file, to listen to all hosts, hostname should be 0.0.0.0 |
| 47 | """ |
| 48 | settings = {"port": 16421, "hostname": "0.0.0.0"} |
| 49 | with open(filename) as stream: |
| 50 | jobdata = stream.read() |
| 51 | json_default = json.loads(jobdata) |
| 52 | if "port" in json_default: |
| 53 | settings['port'] = json_default['port'] |
| 54 | if "hostname" in json_default: |
| 55 | settings['hostname'] = json_default['hostname'] |
| 56 | return settings |
| 57 | |
| 58 | if __name__ == '__main__': |
| 59 | # instance settings come from django - the coordinator doesn't use django and is |
| 60 | # not necessarily per-instance, so use the command line and a default conf file. |
| 61 | pidfile = "/var/run/lava-pdu-listener.pid" |
| 62 | logfile = "/var/log/lava-pdu-listener.log" |
| 63 | conffile = "/etc/lava-pdu/lava-pdu-listener.conf" |
| 64 | settings = readSettings(conffile) |
| 65 | usage = "Usage: %prog [--logfile] --[loglevel]" |
| 66 | description = "LAVA PDU request listener server, host and port are handled in %s" % conffile |
| 67 | parser = optparse.OptionParser(usage=usage, description=description) |
| 68 | parser.add_option("--logfile", dest="logfile", action="store", |
| 69 | type="string", help="log file [%s]" % logfile) |
| 70 | parser.add_option("--loglevel", dest="loglevel", action="store", |
| 71 | type="string", help="logging level [INFO]") |
| 72 | (options, args) = parser.parse_args() |
| 73 | if options.logfile: |
| 74 | if os.path.exists(os.path.dirname(options.logfile)): |
| 75 | logfile = options.logfile |
| 76 | else: |
| 77 | print "No such directory for specified logfile '%s'" % logfile |
| 78 | open(logfile, 'w').close() |
| 79 | level = logging.INFO |
| 80 | if options.loglevel == "DEBUG": |
| 81 | level = logging.DEBUG |
| 82 | if options.loglevel == "WARNING": |
| 83 | level = logging.WARNING |
| 84 | if options.loglevel == "ERROR": |
| 85 | level = logging.ERROR |
| 86 | client_logger, watched_file_handler = getDaemonLogger(logfile, loglevel=level) |
| 87 | if isinstance(client_logger, Exception): |
| 88 | print("Fatal error creating client_logger: " + str(client_logger)) |
| 89 | sys.exit(os.EX_OSERR) |
| 90 | # noinspection PyArgumentList |
| 91 | lockfile = daemon.pidlockfile.PIDLockFile(pidfile) |
| 92 | if lockfile.is_locked(): |
| 93 | logging.error("PIDFile %s already locked" % pidfile) |
| 94 | sys.exit(os.EX_OSERR) |
| 95 | context = daemon.DaemonContext( |
| 96 | working_directory=os.getcwd(), |
| 97 | pidfile=lockfile, |
| 98 | files_preserve=[watched_file_handler.stream], |
| 99 | stderr=watched_file_handler.stream, |
| 100 | stdout=watched_file_handler.stream) |
| 101 | starter = {"logging_level": options.loglevel, |
| 102 | "hostname": settings['hostname'], |
| 103 | "port": settings['port']} |
| 104 | with context: |
| 105 | logging.info("Running LAVA PDU Listener %s %s %d." |
| 106 | % (logfile, settings['hostname'], settings['port'])) |
| 107 | ss = ListenerServer(starter) |
| 108 | ss.start() |