blob: de53eefad5c9ca60331c29e05d423fdc67916416 [file] [log] [blame]
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +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
Matt Hart1499bd42013-08-20 11:35:46 +010021import logging
Matt Hart1306d822013-08-09 12:08:22 +010022import time
Matt Hart132d43c2015-02-25 18:19:53 +000023import json
Matt Hart2af71142014-07-22 10:40:41 +010024import traceback
Matt Hart132d43c2015-02-25 18:19:53 +000025from lavapdu.dbhandler import DBHandler
26from lavapdu.drivers.driver import PDUDriver
27import lavapdu.drivers.strategies
Neil Williams16579dc2014-02-13 11:20:04 +000028
Matt Hart132d43c2015-02-25 18:19:53 +000029class PDURunner(object):
Matt Hart1499bd42013-08-20 11:35:46 +010030
31 def __init__(self, config):
Matt Hart2af71142014-07-22 10:40:41 +010032 self.pdus = config["pdus"]
Matt Hart132d43c2015-02-25 18:19:53 +000033 self.settings = config["daemon"]
34 logging.basicConfig(level=self.settings["logging_level"])
35 logging.getLogger().setLevel(self.settings["logging_level"])
36 logging.getLogger().name = "PDURunner"
Matt Hart1499bd42013-08-20 11:35:46 +010037
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010038 def get_one(self, db):
39 job = db.get_next_job()
40 if job:
Neil Williams16579dc2014-02-13 11:20:04 +000041 job_id, hostname, port, request = job
Matt Hart76fb2542014-06-01 14:24:56 +010042 logging.debug(job)
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010043 logging.info("Processing queue item: (%s %s) on hostname: %s" % (request, port, hostname))
Neil Williams16579dc2014-02-13 11:20:04 +000044 res = self.do_job(hostname, port, request)
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010045 db.delete_row(job_id)
46 else:
47 logging.debug("Found nothing to do in database")
Matt Hart1306d822013-08-09 12:08:22 +010048
Matt Hart2af71142014-07-22 10:40:41 +010049 def driver_from_hostname(self, hostname):
Matt Hart132d43c2015-02-25 18:19:53 +000050 logging.debug("Trying to find a driver for hostname %s" % hostname)
Matt Hart2af71142014-07-22 10:40:41 +010051 logging.debug(self.pdus)
Matt Hart132d43c2015-02-25 18:19:53 +000052 if hostname in self.pdus:
53 drivername = (self.pdus[hostname]["driver"])
54 else:
55 raise NotImplementedError("No configuration available for hostname %s\n"
56 "Is there a section in the lavapdu.conf?" % hostname)
57 logging.debug("Config file wants driver: %s" % drivername)
58 driver = PDUDriver.select(drivername)(hostname, self.pdus[hostname])
59 return driver
Matt Hart2af71142014-07-22 10:40:41 +010060
61 def do_job(self, hostname, port, request, delay=0):
Matt Hart132d43c2015-02-25 18:19:53 +000062 retries = self.settings["retries"]
63 driver = False
Matt Hart63ed9112013-08-20 13:31:50 +010064 while retries > 0:
65 try:
Matt Hart2af71142014-07-22 10:40:41 +010066 driver = self.driver_from_hostname(hostname)
67 return driver.handle(request, port, delay)
Matt Hart76fb2542014-06-01 14:24:56 +010068 except Exception as e:
Matt Hart2af71142014-07-22 10:40:41 +010069 logging.warn(traceback.format_exc())
Matt Hart76fb2542014-06-01 14:24:56 +010070 logging.warn("Failed to execute job: %s %s %s (attempts left %i) error was %s" %
71 (hostname, port, request, retries, e.message))
Matt Hart132d43c2015-02-25 18:19:53 +000072 if driver:
73 #driver._cleanup()
74 driver._bombout()
Matt Hart40d6c312013-08-20 14:47:31 +010075 time.sleep(5)
Matt Hart63ed9112013-08-20 13:31:50 +010076 retries -= 1
Matt Hart2af71142014-07-22 10:40:41 +010077 return False
Matt Hart63ed9112013-08-20 13:31:50 +010078
Matt Hart1306d822013-08-09 12:08:22 +010079 def run_me(self):
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010080 logging.info("Starting up the PDURunner")
Matt Hart1306d822013-08-09 12:08:22 +010081 while 1:
Matt Hart132d43c2015-02-25 18:19:53 +000082 db = DBHandler(self.settings)
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010083 self.get_one(db)
84 db.close()
85 del(db)
86 time.sleep(2)
Matt Hart1306d822013-08-09 12:08:22 +010087
88if __name__ == "__main__":
Matt Hart132d43c2015-02-25 18:19:53 +000089 settings = {}
90 filename = "/etc/lavapdu/lavapdu.conf"
91 print("Reading settings from %s" % filename)
92 with open(filename) as stream:
93 jobdata = stream.read()
94 json_data = json.loads(jobdata)
95
96 p = PDURunner(json_data)
97 #p.do_job("192.168.10.5",18,"reboot",2)
98 p.run_me()