blob: 95c67674505280e866c0c3748aa65ece02dbeb65 [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 Hart2af71142014-07-22 10:40:41 +010023import traceback
Matt Hart132d43c2015-02-25 18:19:53 +000024from lavapdu.dbhandler import DBHandler
25from lavapdu.drivers.driver import PDUDriver
Matt Hartfe042212015-07-09 14:13:29 +010026import lavapdu.drivers.strategies # pylint: disable=W0611
Matt Harte7bddc02015-07-08 18:05:08 +010027from lavapdu.shared import drivername_from_hostname
Matt Hartd4a09982015-07-17 16:13:11 +010028from lavapdu.shared import pdus_from_config
Matt Hartfe042212015-07-09 14:13:29 +010029assert lavapdu.drivers.strategies
Matt Hartd4a09982015-07-17 16:13:11 +010030log = logging.getLogger(__name__)
Matt Hartfe042212015-07-09 14:13:29 +010031
Neil Williams16579dc2014-02-13 11:20:04 +000032
Matt Hart132d43c2015-02-25 18:19:53 +000033class PDURunner(object):
Matt Hart1499bd42013-08-20 11:35:46 +010034
Matt Hartd4a09982015-07-17 16:13:11 +010035 def __init__(self, config, single_pdu=False):
Matt Hart132d43c2015-02-25 18:19:53 +000036 self.settings = config["daemon"]
Matt Hartd4a09982015-07-17 16:13:11 +010037 self.pdus = config["pdus"]
38 if single_pdu:
39 if single_pdu not in pdus_from_config(config):
40 raise NotImplementedError
41 self.single_pdu = single_pdu
42 self.dbh = DBHandler(self.settings)
Matt Hart1499bd42013-08-20 11:35:46 +010043
Matt Hartd4a09982015-07-17 16:13:11 +010044 def get_one(self):
45 job = self.dbh.get_next_job(self.single_pdu)
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010046 if job:
Neil Williams16579dc2014-02-13 11:20:04 +000047 job_id, hostname, port, request = job
Matt Hartd4a09982015-07-17 16:13:11 +010048 log.debug(job)
49 log.info("Processing queue item: (%s %s) on hostname: %s",
50 request, port, hostname)
Matt Hartfe042212015-07-09 14:13:29 +010051 self.do_job(hostname, port, request)
Matt Hartd4a09982015-07-17 16:13:11 +010052 self.dbh.delete_row(job_id)
Matt Hart1306d822013-08-09 12:08:22 +010053
Matt Hart2af71142014-07-22 10:40:41 +010054 def driver_from_hostname(self, hostname):
Matt Harte7bddc02015-07-08 18:05:08 +010055 drivername = drivername_from_hostname(hostname, self.pdus)
Matt Hart132d43c2015-02-25 18:19:53 +000056 driver = PDUDriver.select(drivername)(hostname, self.pdus[hostname])
57 return driver
Matt Hart2af71142014-07-22 10:40:41 +010058
59 def do_job(self, hostname, port, request, delay=0):
Matt Hart132d43c2015-02-25 18:19:53 +000060 retries = self.settings["retries"]
61 driver = False
Matt Hart63ed9112013-08-20 13:31:50 +010062 while retries > 0:
63 try:
Matt Hart2af71142014-07-22 10:40:41 +010064 driver = self.driver_from_hostname(hostname)
65 return driver.handle(request, port, delay)
Matt Hartfe042212015-07-09 14:13:29 +010066 except Exception as e: # pylint: disable=broad-except
Matt Hartd4a09982015-07-17 16:13:11 +010067 log.warn(traceback.format_exc())
68 log.warn("Failed to execute job: %s %s %s "
69 "(attempts left %i) error was %s",
70 hostname, port, request, retries, e.message)
Matt Hart132d43c2015-02-25 18:19:53 +000071 if driver:
Matt Hartd4a09982015-07-17 16:13:11 +010072 driver._bombout() # pylint: disable=W0212,E1101
Matt Hart40d6c312013-08-20 14:47:31 +010073 time.sleep(5)
Matt Hart63ed9112013-08-20 13:31:50 +010074 retries -= 1
Matt Hart2af71142014-07-22 10:40:41 +010075 return False
Matt Hart63ed9112013-08-20 13:31:50 +010076
Matt Hart1306d822013-08-09 12:08:22 +010077 def run_me(self):
Matt Hartd4a09982015-07-17 16:13:11 +010078 if self.single_pdu:
79 log.info("Starting a PDURunner for PDU: %s", self.single_pdu)
80 else:
81 log.info("Starting a PDURunner for all PDUS")
Matt Hart1306d822013-08-09 12:08:22 +010082 while 1:
Matt Hartd4a09982015-07-17 16:13:11 +010083 self.get_one()
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010084 time.sleep(2)