blob: f32b2171002060d44533c488190ea7cc915bbd97 [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
Matt Harta903c622013-08-15 16:00:16 +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 Hartd4a09982015-07-17 16:13:11 +010027import lavapdu.runnermaster as runnermaster
Matt Hart96ca8662013-08-21 13:37:35 +010028
Matt Harta903c622013-08-15 16:00:16 +010029import daemon
Neil Williams51f0f442015-07-01 19:31:39 +010030try:
31 import daemon.pidlockfile as pidlockfile
32except ImportError:
33 from lockfile import pidlockfile
Matt Hart96ca8662013-08-21 13:37:35 +010034
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010035from lavapdu.pdurunner import PDURunner
Matt Hart96ca8662013-08-21 13:37:35 +010036
Matt Harta903c622013-08-15 16:00:16 +010037
Matt Harta903c622013-08-15 16:00:16 +010038if __name__ == '__main__':
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010039 pidfile = "/var/run/lavapdu-runner.pid"
40 logfile = "/var/log/lavapdu-runner.log"
Matt Hart2af71142014-07-22 10:40:41 +010041 conffile = "/etc/lavapdu/lavapdu.conf"
Matt Hartfe042212015-07-09 14:13:29 +010042 settings = read_settings(conffile)
Matt Hart7d670612013-08-20 16:47:52 +010043 level = logging.DEBUG
Matt Hart132d43c2015-02-25 18:19:53 +000044 daemon_settings = settings["daemon"]
45 if daemon_settings["logging_level"] == "DEBUG":
Matt Harta903c622013-08-15 16:00:16 +010046 level = logging.DEBUG
Matt Hart132d43c2015-02-25 18:19:53 +000047 if daemon_settings["logging_level"] == "WARNING":
Matt Harta903c622013-08-15 16:00:16 +010048 level = logging.WARNING
Matt Hart132d43c2015-02-25 18:19:53 +000049 if daemon_settings["logging_level"] == "ERROR":
Matt Harta903c622013-08-15 16:00:16 +010050 level = logging.ERROR
Matt Hart132d43c2015-02-25 18:19:53 +000051 if daemon_settings["logging_level"] == "INFO":
matthew.hart@linaro.org426d0852013-10-30 15:23:15 -070052 level = logging.INFO
Matt Hartfe042212015-07-09 14:13:29 +010053 client_logger, watched_file_handler = get_daemon_logger(
54 logfile,
55 loglevel=level,
56 log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s')
Matt Harta903c622013-08-15 16:00:16 +010057 if isinstance(client_logger, Exception):
58 print("Fatal error creating client_logger: " + str(client_logger))
59 sys.exit(os.EX_OSERR)
Matt Harta903c622013-08-15 16:00:16 +010060 context = daemon.DaemonContext(
Neil Williams51f0f442015-07-01 19:31:39 +010061 detach_process=True,
Matt Harta903c622013-08-15 16:00:16 +010062 working_directory=os.getcwd(),
Matt Harta903c622013-08-15 16:00:16 +010063 files_preserve=[watched_file_handler.stream],
64 stderr=watched_file_handler.stream,
65 stdout=watched_file_handler.stream)
Matt Harta903c622013-08-15 16:00:16 +010066 with context:
Matt Hartd4a09982015-07-17 16:13:11 +010067 runnermaster.start_em_up(settings, pidfile)