blob: ec5b00b1d982a3e7d0156f127bbf79f23bdbe837 [file] [log] [blame]
Matt Hart1499bd42013-08-20 11:35:46 +01001#! /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
21import logging
22import json
23import os
24import sys
25import optparse
Matt Hart96ca8662013-08-21 13:37:35 +010026from logging.handlers import WatchedFileHandler
27
Matt Hart1499bd42013-08-20 11:35:46 +010028import daemon
29import daemon.pidlockfile
Matt Hart96ca8662013-08-21 13:37:35 +010030
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010031from lavapdu.socketserver import ListenerServer
Matt Hart96ca8662013-08-21 13:37:35 +010032
Matt Hart1499bd42013-08-20 11:35:46 +010033
34def 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
47def readSettings(filename):
48 """
49 Read settings from config file, to listen to all hosts, hostname should be 0.0.0.0
50 """
Matt Hart132d43c2015-02-25 18:19:53 +000051 #settings = {}
Matt Hart2af71142014-07-22 10:40:41 +010052 print("Reading settings from %s" % conffile)
Matt Hart1499bd42013-08-20 11:35:46 +010053 with open(filename) as stream:
54 jobdata = stream.read()
Matt Hart2af71142014-07-22 10:40:41 +010055 json_data = json.loads(jobdata)
Matt Hart132d43c2015-02-25 18:19:53 +000056 return json_data
Matt Hart1499bd42013-08-20 11:35:46 +010057
58if __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.
matthew.hart@linaro.org3e6b91b2013-08-27 15:19:20 +010061 pidfile = "/var/run/lavapdu-listen.pid"
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010062 logfile = "/var/log/lavapdu-listener.log"
Matt Hart2af71142014-07-22 10:40:41 +010063 conffile = "/etc/lavapdu/lavapdu.conf"
Matt Hart1499bd42013-08-20 11:35:46 +010064 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()
Matt Hart7d670612013-08-20 16:47:52 +010079 level = logging.DEBUG
Matt Hart132d43c2015-02-25 18:19:53 +000080 settings = settings["daemon"]
81 if settings["logging_level"] == "DEBUG":
Matt Hart1499bd42013-08-20 11:35:46 +010082 level = logging.DEBUG
Matt Hart132d43c2015-02-25 18:19:53 +000083 if settings["logging_level"] == "WARNING":
Matt Hart1499bd42013-08-20 11:35:46 +010084 level = logging.WARNING
Matt Hart132d43c2015-02-25 18:19:53 +000085 if settings["logging_level"] == "ERROR":
Matt Hart1499bd42013-08-20 11:35:46 +010086 level = logging.ERROR
Matt Hart132d43c2015-02-25 18:19:53 +000087 if settings["logging_level"] == "INFO":
matthew.hart@linaro.org426d0852013-10-30 15:23:15 -070088 level = logging.INFO
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010089 client_logger, watched_file_handler = getDaemonLogger(logfile, loglevel=level,
90 log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s')
Matt Hart1499bd42013-08-20 11:35:46 +010091 if isinstance(client_logger, Exception):
92 print("Fatal error creating client_logger: " + str(client_logger))
93 sys.exit(os.EX_OSERR)
94 # noinspection PyArgumentList
95 lockfile = daemon.pidlockfile.PIDLockFile(pidfile)
96 if lockfile.is_locked():
97 logging.error("PIDFile %s already locked" % pidfile)
98 sys.exit(os.EX_OSERR)
99 context = daemon.DaemonContext(
100 working_directory=os.getcwd(),
101 pidfile=lockfile,
102 files_preserve=[watched_file_handler.stream],
103 stderr=watched_file_handler.stream,
104 stdout=watched_file_handler.stream)
Matt Hart1499bd42013-08-20 11:35:46 +0100105 with context:
106 logging.info("Running LAVA PDU Listener %s %s %d."
107 % (logfile, settings['hostname'], settings['port']))
Matt Hart132d43c2015-02-25 18:19:53 +0000108 ListenerServer(settings).start()