blob: d31b8fd89d53030d5c39e660c8e864bdf51ff2d9 [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 Hart76fb2542014-06-01 14:24:56 +010023from dbhandler import DBHandler
Matt Hart2af71142014-07-22 10:40:41 +010024import traceback
Neil Williams16579dc2014-02-13 11:20:04 +000025
Matt Hart1306d822013-08-09 12:08:22 +010026class PDURunner():
Matt Hart1499bd42013-08-20 11:35:46 +010027
28 def __init__(self, config):
Matt Hart63ed9112013-08-20 13:31:50 +010029 logging.basicConfig(level=config["logging_level"])
30 logging.getLogger().setLevel(config["logging_level"])
Matt Hart40d6c312013-08-20 14:47:31 +010031 logging.getLogger().name = "PDURunner"
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010032 self.config = config
Matt Hart2af71142014-07-22 10:40:41 +010033 self.pdus = config["pdus"]
Matt Hart1499bd42013-08-20 11:35:46 +010034
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010035 def get_one(self, db):
36 job = db.get_next_job()
37 if job:
Neil Williams16579dc2014-02-13 11:20:04 +000038 job_id, hostname, port, request = job
Matt Hart76fb2542014-06-01 14:24:56 +010039 logging.debug(job)
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010040 logging.info("Processing queue item: (%s %s) on hostname: %s" % (request, port, hostname))
Neil Williams16579dc2014-02-13 11:20:04 +000041 res = self.do_job(hostname, port, request)
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010042 db.delete_row(job_id)
43 else:
44 logging.debug("Found nothing to do in database")
Matt Hart1306d822013-08-09 12:08:22 +010045
Matt Hart2af71142014-07-22 10:40:41 +010046 def driver_from_hostname(self, hostname):
47 logging.debug("Trying to find a driver for %s" % hostname)
48 logging.debug(self.pdus)
49 driver = self.pdus[hostname]["driver"]
50 logging.debug("Driver: %s" % driver)
51 module = __import__("drivers.%s" % driver.lower(), fromlist=[driver])
52 class_ = getattr(module, driver)
53 return class_(hostname)
54
55 def do_job(self, hostname, port, request, delay=0):
Matt Hart76fb2542014-06-01 14:24:56 +010056 retries = 10
Matt Hart63ed9112013-08-20 13:31:50 +010057 while retries > 0:
58 try:
Matt Hart2af71142014-07-22 10:40:41 +010059 driver = self.driver_from_hostname(hostname)
60 return driver.handle(request, port, delay)
Matt Hart76fb2542014-06-01 14:24:56 +010061 except Exception as e:
Matt Hart2af71142014-07-22 10:40:41 +010062 driver.cleanup()
63 logging.warn(traceback.format_exc())
Matt Hart76fb2542014-06-01 14:24:56 +010064 logging.warn("Failed to execute job: %s %s %s (attempts left %i) error was %s" %
65 (hostname, port, request, retries, e.message))
Matt Hart40d6c312013-08-20 14:47:31 +010066 time.sleep(5)
Matt Hart63ed9112013-08-20 13:31:50 +010067 retries -= 1
Matt Hart2af71142014-07-22 10:40:41 +010068 return False
Matt Hart63ed9112013-08-20 13:31:50 +010069
Matt Hart1306d822013-08-09 12:08:22 +010070 def run_me(self):
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010071 logging.info("Starting up the PDURunner")
Matt Hart1306d822013-08-09 12:08:22 +010072 while 1:
Matt Hart2af71142014-07-22 10:40:41 +010073 db = DBHandler(self.config["settings"])
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010074 self.get_one(db)
75 db.close()
76 del(db)
77 time.sleep(2)
Matt Hart1306d822013-08-09 12:08:22 +010078
79if __name__ == "__main__":
Matt Hart2af71142014-07-22 10:40:41 +010080 starter = { "settings": {"dbhost": "127.0.0.1",
Neil Williams16579dc2014-02-13 11:20:04 +000081 "dbuser": "pdudaemon",
82 "dbpass": "pdudaemon",
Matt Hart2af71142014-07-22 10:40:41 +010083 "dbname": "lavapdu"},
84 "pdus": { "pdu14": { "driver": "APC9218" },"pdu15": {"driver": "APC8959"} },
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010085 "logging_level": logging.DEBUG}
Matt Hart1499bd42013-08-20 11:35:46 +010086 p = PDURunner(starter)
Matt Hart2af71142014-07-22 10:40:41 +010087 p.do_job("pdu15",18,"off",10)
88 p.do_job("pdu15",18,"reboot",10)
89 p.do_job("pdu15",18,"on",10)
90 #p.run_me()