blob: be887a066f400e6cca4da43832923410c115e74e [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]")
50 (options, args) = parser.parse_args()
51 if options.logfile:
52 if os.path.exists(os.path.dirname(options.logfile)):
53 logfile = options.logfile
54 else:
55 print "No such directory for specified logfile '%s'" % logfile
56 open(logfile, 'w').close()
Matt Hart7d670612013-08-20 16:47:52 +010057 level = logging.DEBUG
Matt Harte7bddc02015-07-08 18:05:08 +010058 daemon_settings = settings["daemon"]
59 if daemon_settings["logging_level"] == "DEBUG":
Matt Hart1499bd42013-08-20 11:35:46 +010060 level = logging.DEBUG
Matt Harte7bddc02015-07-08 18:05:08 +010061 if daemon_settings["logging_level"] == "WARNING":
Matt Hart1499bd42013-08-20 11:35:46 +010062 level = logging.WARNING
Matt Harte7bddc02015-07-08 18:05:08 +010063 if daemon_settings["logging_level"] == "ERROR":
Matt Hart1499bd42013-08-20 11:35:46 +010064 level = logging.ERROR
Matt Harte7bddc02015-07-08 18:05:08 +010065 if daemon_settings["logging_level"] == "INFO":
matthew.hart@linaro.org426d0852013-10-30 15:23:15 -070066 level = logging.INFO
Matt Hartfe042212015-07-09 14:13:29 +010067 client_logger, watched_file_handler = get_daemon_logger(
68 logfile,
69 loglevel=level,
70 log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s')
Matt Hart1499bd42013-08-20 11:35:46 +010071 if isinstance(client_logger, Exception):
72 print("Fatal error creating client_logger: " + str(client_logger))
73 sys.exit(os.EX_OSERR)
74 # noinspection PyArgumentList
Neil Williams51f0f442015-07-01 19:31:39 +010075 lockfile = pidlockfile.PIDLockFile(pidfile)
Matt Hart1499bd42013-08-20 11:35:46 +010076 if lockfile.is_locked():
77 logging.error("PIDFile %s already locked" % pidfile)
78 sys.exit(os.EX_OSERR)
79 context = daemon.DaemonContext(
Neil Williams51f0f442015-07-01 19:31:39 +010080 detach_process=True,
Matt Hart1499bd42013-08-20 11:35:46 +010081 working_directory=os.getcwd(),
82 pidfile=lockfile,
83 files_preserve=[watched_file_handler.stream],
84 stderr=watched_file_handler.stream,
85 stdout=watched_file_handler.stream)
Matt Hart1499bd42013-08-20 11:35:46 +010086 with context:
87 logging.info("Running LAVA PDU Listener %s %s %d."
Matt Hartfe042212015-07-09 14:13:29 +010088 % (logfile,
89 daemon_settings['hostname'],
90 daemon_settings['port']))
Neil Williams51f0f442015-07-01 19:31:39 +010091 ListenerServer(settings).start()