blob: ec820bab2889ed1c2f45f9aced412a2e417e0299 [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
Matt Hart1499bd42013-08-20 11:35:46 +010022import os
23import sys
24import optparse
Matt Hartfe042212015-07-09 14:13:29 +010025from lavapdu.shared import get_daemon_logger
26from lavapdu.shared import read_settings
Matt Hart96ca8662013-08-21 13:37:35 +010027
Matt Hart1499bd42013-08-20 11:35:46 +010028import daemon
Neil Williams51f0f442015-07-01 19:31:39 +010029try:
30 import daemon.pidlockfile as pidlockfile
31except ImportError:
32 from lockfile import pidlockfile
Matt Hart96ca8662013-08-21 13:37:35 +010033
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010034from lavapdu.socketserver import ListenerServer
Matt Hart96ca8662013-08-21 13:37:35 +010035
Matt Hart1499bd42013-08-20 11:35:46 +010036
Matt Hart1499bd42013-08-20 11:35:46 +010037if __name__ == '__main__':
matthew.hart@linaro.org3e6b91b2013-08-27 15:19:20 +010038 pidfile = "/var/run/lavapdu-listen.pid"
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010039 logfile = "/var/log/lavapdu-listener.log"
Matt Hart2af71142014-07-22 10:40:41 +010040 conffile = "/etc/lavapdu/lavapdu.conf"
Matt Hartfe042212015-07-09 14:13:29 +010041 settings = read_settings(conffile)
Matt Hart1499bd42013-08-20 11:35:46 +010042 usage = "Usage: %prog [--logfile] --[loglevel]"
Matt Hartfe042212015-07-09 14:13:29 +010043 description = "LAVA PDU request listener server," \
44 "host and port are handled in %s" % conffile
Matt Hart1499bd42013-08-20 11:35:46 +010045 parser = optparse.OptionParser(usage=usage, description=description)
46 parser.add_option("--logfile", dest="logfile", action="store",
47 type="string", help="log file [%s]" % logfile)
48 parser.add_option("--loglevel", dest="loglevel", action="store",
49 type="string", help="logging level [INFO]")
Matt Hart07f56a02015-08-06 14:17:18 +010050 parser.add_option("--purge", dest="purge", action="store_true")
Matt Hart1499bd42013-08-20 11:35:46 +010051 (options, args) = parser.parse_args()
52 if options.logfile:
53 if os.path.exists(os.path.dirname(options.logfile)):
54 logfile = options.logfile
55 else:
56 print "No such directory for specified logfile '%s'" % logfile
57 open(logfile, 'w').close()
Matt Hart7d670612013-08-20 16:47:52 +010058 level = logging.DEBUG
Matt Harte7bddc02015-07-08 18:05:08 +010059 daemon_settings = settings["daemon"]
60 if daemon_settings["logging_level"] == "DEBUG":
Matt Hart1499bd42013-08-20 11:35:46 +010061 level = logging.DEBUG
Matt Harte7bddc02015-07-08 18:05:08 +010062 if daemon_settings["logging_level"] == "WARNING":
Matt Hart1499bd42013-08-20 11:35:46 +010063 level = logging.WARNING
Matt Harte7bddc02015-07-08 18:05:08 +010064 if daemon_settings["logging_level"] == "ERROR":
Matt Hart1499bd42013-08-20 11:35:46 +010065 level = logging.ERROR
Matt Harte7bddc02015-07-08 18:05:08 +010066 if daemon_settings["logging_level"] == "INFO":
matthew.hart@linaro.org426d0852013-10-30 15:23:15 -070067 level = logging.INFO
Matt Hartfe042212015-07-09 14:13:29 +010068 client_logger, watched_file_handler = get_daemon_logger(
69 logfile,
70 loglevel=level,
71 log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s')
Matt Hart1499bd42013-08-20 11:35:46 +010072 if isinstance(client_logger, Exception):
73 print("Fatal error creating client_logger: " + str(client_logger))
74 sys.exit(os.EX_OSERR)
75 # noinspection PyArgumentList
Neil Williams51f0f442015-07-01 19:31:39 +010076 lockfile = pidlockfile.PIDLockFile(pidfile)
Matt Hart1499bd42013-08-20 11:35:46 +010077 if lockfile.is_locked():
78 logging.error("PIDFile %s already locked" % pidfile)
79 sys.exit(os.EX_OSERR)
80 context = daemon.DaemonContext(
Neil Williams51f0f442015-07-01 19:31:39 +010081 detach_process=True,
Matt Hart1499bd42013-08-20 11:35:46 +010082 working_directory=os.getcwd(),
83 pidfile=lockfile,
84 files_preserve=[watched_file_handler.stream],
85 stderr=watched_file_handler.stream,
86 stdout=watched_file_handler.stream)
Matt Hart07f56a02015-08-06 14:17:18 +010087 if options.purge:
88 settings["purge"] = True
Matt Hart1499bd42013-08-20 11:35:46 +010089 with context:
90 logging.info("Running LAVA PDU Listener %s %s %d."
Matt Hartfe042212015-07-09 14:13:29 +010091 % (logfile,
92 daemon_settings['hostname'],
93 daemon_settings['port']))
Neil Williams51f0f442015-07-01 19:31:39 +010094 ListenerServer(settings).start()