blob: c1e7852d0263a55e02037da3f5f5f4b1f0a9fc92 [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 """
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010051 settings = {"port": 16421, "hostname": "0.0.0.0", "dbuser": "pdudaemon",
52 "dbpass": "pdudaemon", "dbname": "pdu_queue", "dbhost": "127.0.0.1"}
Matt Hart1499bd42013-08-20 11:35:46 +010053 with open(filename) as stream:
54 jobdata = stream.read()
55 json_default = json.loads(jobdata)
56 if "port" in json_default:
57 settings['port'] = json_default['port']
58 if "hostname" in json_default:
59 settings['hostname'] = json_default['hostname']
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010060 if "dbuser" in json_default:
61 settings['dbuser'] = json_default['dbuser']
62 if "dbpass" in json_default:
63 settings['dbpass'] = json_default['dbpass']
64 if "dbname" in json_default:
65 settings['dbname'] = json_default['dbname']
66 if "dbhost" in json_default:
67 settings['dbhost'] = json_default['dbhost']
Matt Hart1499bd42013-08-20 11:35:46 +010068 return settings
69
70if __name__ == '__main__':
71 # instance settings come from django - the coordinator doesn't use django and is
72 # not necessarily per-instance, so use the command line and a default conf file.
matthew.hart@linaro.org3e6b91b2013-08-27 15:19:20 +010073 pidfile = "/var/run/lavapdu-listen.pid"
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010074 logfile = "/var/log/lavapdu-listener.log"
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010075 conffile = "/etc/lavapdu.conf"
Matt Hart1499bd42013-08-20 11:35:46 +010076 settings = readSettings(conffile)
77 usage = "Usage: %prog [--logfile] --[loglevel]"
78 description = "LAVA PDU request listener server, host and port are handled in %s" % conffile
79 parser = optparse.OptionParser(usage=usage, description=description)
80 parser.add_option("--logfile", dest="logfile", action="store",
81 type="string", help="log file [%s]" % logfile)
82 parser.add_option("--loglevel", dest="loglevel", action="store",
83 type="string", help="logging level [INFO]")
84 (options, args) = parser.parse_args()
85 if options.logfile:
86 if os.path.exists(os.path.dirname(options.logfile)):
87 logfile = options.logfile
88 else:
89 print "No such directory for specified logfile '%s'" % logfile
90 open(logfile, 'w').close()
Matt Hart7d670612013-08-20 16:47:52 +010091 level = logging.DEBUG
Matt Hart1499bd42013-08-20 11:35:46 +010092 if options.loglevel == "DEBUG":
93 level = logging.DEBUG
94 if options.loglevel == "WARNING":
95 level = logging.WARNING
96 if options.loglevel == "ERROR":
97 level = logging.ERROR
matthew.hart@linaro.org426d0852013-10-30 15:23:15 -070098 if options.loglevel == "INFO":
99 level = logging.INFO
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +0100100 client_logger, watched_file_handler = getDaemonLogger(logfile, loglevel=level,
101 log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s')
Matt Hart1499bd42013-08-20 11:35:46 +0100102 if isinstance(client_logger, Exception):
103 print("Fatal error creating client_logger: " + str(client_logger))
104 sys.exit(os.EX_OSERR)
105 # noinspection PyArgumentList
106 lockfile = daemon.pidlockfile.PIDLockFile(pidfile)
107 if lockfile.is_locked():
108 logging.error("PIDFile %s already locked" % pidfile)
109 sys.exit(os.EX_OSERR)
110 context = daemon.DaemonContext(
111 working_directory=os.getcwd(),
112 pidfile=lockfile,
113 files_preserve=[watched_file_handler.stream],
114 stderr=watched_file_handler.stream,
115 stdout=watched_file_handler.stream)
Matt Hart7d670612013-08-20 16:47:52 +0100116 starter = {"logging_level": level,
Matt Hart1499bd42013-08-20 11:35:46 +0100117 "hostname": settings['hostname'],
Matt Hart7d670612013-08-20 16:47:52 +0100118 "port": settings['port'],
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +0100119 "dbhost": settings["dbhost"],
120 "dbuser": settings["dbuser"],
121 "dbpass": settings["dbpass"],
122 "dbname": settings["dbname"]}
Matt Hart1499bd42013-08-20 11:35:46 +0100123 with context:
124 logging.info("Running LAVA PDU Listener %s %s %d."
125 % (logfile, settings['hostname'], settings['port']))
Matt Hart7d670612013-08-20 16:47:52 +0100126 #logging.getLogger().setLevel(options.loglevel)
Matt Hart1499bd42013-08-20 11:35:46 +0100127 ListenerServer(starter).start()