blob: 278a9988fcf1f6870d6b503a947fcb1e5391cd01 [file] [log] [blame]
Matt Harta903c622013-08-15 16:00:16 +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 Harta903c622013-08-15 16:00:16 +010028import daemon
29import daemon.pidlockfile
Matt Hart96ca8662013-08-21 13:37:35 +010030
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010031from lavapdu.pdurunner import PDURunner
Matt Hart96ca8662013-08-21 13:37:35 +010032
Matt Harta903c622013-08-15 16:00:16 +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.org5e4fce92013-08-22 11:29:21 +010051 settings = {"dbfile":"/var/lib/lavapdu/pdu.db"}
Matt Harta903c622013-08-15 16:00:16 +010052 with open(filename) as stream:
53 jobdata = stream.read()
54 json_default = json.loads(jobdata)
Matt Hart1499bd42013-08-20 11:35:46 +010055 if "dbfile" in json_default:
56 settings['dbfile'] = json_default['dbfile']
Matt Harta903c622013-08-15 16:00:16 +010057 return settings
58
59if __name__ == '__main__':
60 # instance settings come from django - the coordinator doesn't use django and is
61 # not necessarily per-instance, so use the command line and a default conf file.
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010062 pidfile = "/var/run/lavapdu-runner.pid"
63 logfile = "/var/log/lavapdu-runner.log"
64 conffile = "/etc/lavapdu-runner.conf"
Matt Harta903c622013-08-15 16:00:16 +010065 settings = readSettings(conffile)
66 usage = "Usage: %prog [--logfile] --[loglevel]"
67 description = "LAVA PDU request listener server, host and port are handled in %s" % conffile
68 parser = optparse.OptionParser(usage=usage, description=description)
69 parser.add_option("--logfile", dest="logfile", action="store",
70 type="string", help="log file [%s]" % logfile)
71 parser.add_option("--loglevel", dest="loglevel", action="store",
72 type="string", help="logging level [INFO]")
73 (options, args) = parser.parse_args()
74 if options.logfile:
75 if os.path.exists(os.path.dirname(options.logfile)):
76 logfile = options.logfile
77 else:
78 print "No such directory for specified logfile '%s'" % logfile
79 open(logfile, 'w').close()
Matt Hart7d670612013-08-20 16:47:52 +010080 level = logging.DEBUG
Matt Harta903c622013-08-15 16:00:16 +010081 if options.loglevel == "DEBUG":
82 level = logging.DEBUG
83 if options.loglevel == "WARNING":
84 level = logging.WARNING
85 if options.loglevel == "ERROR":
86 level = logging.ERROR
87 client_logger, watched_file_handler = getDaemonLogger(logfile, loglevel=level)
88 if isinstance(client_logger, Exception):
89 print("Fatal error creating client_logger: " + str(client_logger))
90 sys.exit(os.EX_OSERR)
91 # noinspection PyArgumentList
92 lockfile = daemon.pidlockfile.PIDLockFile(pidfile)
93 if lockfile.is_locked():
94 logging.error("PIDFile %s already locked" % pidfile)
95 sys.exit(os.EX_OSERR)
96 context = daemon.DaemonContext(
97 working_directory=os.getcwd(),
98 pidfile=lockfile,
99 files_preserve=[watched_file_handler.stream],
100 stderr=watched_file_handler.stream,
101 stdout=watched_file_handler.stream)
Matt Hart7d670612013-08-20 16:47:52 +0100102 starter = {"logging_level": level,
Matt Hart1499bd42013-08-20 11:35:46 +0100103 "dbfile": settings["dbfile"]}
Matt Harta903c622013-08-15 16:00:16 +0100104 with context:
Matt Hart1499bd42013-08-20 11:35:46 +0100105 logging.info("Running LAVA PDU Runner %s %s"
106 % (logfile, settings["dbfile"]))
107 p = PDURunner(starter)
108 p.run_me()