#! /usr/bin/python

#  Copyright 2013 Linaro Limited
#  Author Matt Hart <matthew.hart@linaro.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.

import logging
import json
import os
import sys
import optparse
from logging.handlers import WatchedFileHandler

import daemon
import daemon.pidlockfile

from lavapdu.pdurunner import PDURunner


def getDaemonLogger(filePath, log_format=None, loglevel=logging.INFO):
    logger = logging.getLogger()
    logger.setLevel(loglevel)
    try:
        watchedHandler = WatchedFileHandler(filePath)
    except Exception as e:
        return e

    watchedHandler.setFormatter(logging.Formatter(log_format or '%(asctime)s %(msg)s'))
    logger.addHandler(watchedHandler)
    return logger, watchedHandler


def readSettings(filename):
    """
    Read settings from config file, to listen to all hosts, hostname should be 0.0.0.0
    """
    settings = {"dbuser": "pdudaemon", "dbpass": "pdudaemon", "dbname": "pdu_queue", "dbhost": "127.0.0.1"}
    with open(filename) as stream:
        jobdata = stream.read()
        json_default = json.loads(jobdata)
    if "dbuser" in json_default:
        settings['dbuser'] = json_default['dbuser']
    if "dbpass" in json_default:
        settings['dbpass'] = json_default['dbpass']
    if "dbname" in json_default:
        settings['dbname'] = json_default['dbname']
    if "dbhost" in json_default:
        settings['dbhost'] = json_default['dbhost']
    return settings

if __name__ == '__main__':
    # instance settings come from django - the coordinator doesn't use django and is
    # not necessarily per-instance, so use the command line and a default conf file.
    pidfile = "/var/run/lavapdu-runner.pid"
    logfile = "/var/log/lavapdu-runner.log"
    conffile = "/etc/lavapdu.conf"
    settings = readSettings(conffile)
    usage = "Usage: %prog [--logfile] --[loglevel]"
    description = "LAVA PDU request listener server, host and port are handled in %s" % conffile
    parser = optparse.OptionParser(usage=usage, description=description)
    parser.add_option("--logfile", dest="logfile", action="store",
                      type="string", help="log file [%s]" % logfile)
    parser.add_option("--loglevel", dest="loglevel", action="store",
                      type="string", help="logging level [INFO]")
    (options, args) = parser.parse_args()
    if options.logfile:
        if os.path.exists(os.path.dirname(options.logfile)):
            logfile = options.logfile
        else:
            print "No such directory for specified logfile '%s'" % logfile
    open(logfile, 'w').close()
    level = logging.DEBUG
    if options.loglevel == "DEBUG":
        level = logging.DEBUG
    if options.loglevel == "WARNING":
        level = logging.WARNING
    if options.loglevel == "ERROR":
        level = logging.ERROR
    if options.loglevel == "INFO":
        level = logging.INFO
    client_logger, watched_file_handler = getDaemonLogger(logfile, loglevel=level,
                                                          log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s')
    if isinstance(client_logger, Exception):
        print("Fatal error creating client_logger: " + str(client_logger))
        sys.exit(os.EX_OSERR)
    # noinspection PyArgumentList
    lockfile = daemon.pidlockfile.PIDLockFile(pidfile)
    if lockfile.is_locked():
        logging.error("PIDFile %s already locked" % pidfile)
        sys.exit(os.EX_OSERR)
    context = daemon.DaemonContext(
        working_directory=os.getcwd(),
        pidfile=lockfile,
        files_preserve=[watched_file_handler.stream],
        stderr=watched_file_handler.stream,
        stdout=watched_file_handler.stream)
    starter = {"logging_level": level,
               "dbhost": settings["dbhost"],
               "dbuser": settings["dbuser"],
               "dbpass": settings["dbpass"],
               "dbname": settings["dbname"]}
    with context:
        logging.info("Running LAVA PDU Runner %s dbhost: %s"
                     % (logfile, settings["dbhost"]))
        p = PDURunner(starter)
        p.run_me()
