matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame^] | 1 | #! /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 Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 21 | import logging |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 22 | import time |
Matt Hart | d73bba2 | 2013-08-21 13:46:03 +0100 | [diff] [blame] | 23 | from engine import PDUEngine |
| 24 | from socketserver import DBHandler |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 25 | |
| 26 | class PDURunner(): |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 27 | |
| 28 | def __init__(self, config): |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 29 | logging.basicConfig(level=config["logging_level"]) |
| 30 | logging.getLogger().setLevel(config["logging_level"]) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 31 | logging.getLogger().name = "PDURunner" |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 32 | self.db = DBHandler(config["dbfile"]) |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 33 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 34 | def get_one(self): |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 35 | res = self.db.get_one("SELECT * FROM pdu_queue ORDER BY id asc limit 1") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 36 | if res: |
| 37 | id,hostname,port,request = res |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 38 | logging.debug("Processing queue item: (%s %s) on hostname: %s" % (request, port, hostname)) |
| 39 | #logging.debug(id, hostname, request, port) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 40 | res = self.do_job(hostname,port,request) |
| 41 | self.delete_row(id) |
| 42 | |
| 43 | def delete_row(self, id): |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 44 | logging.debug("deleting row %i" % id) |
| 45 | self.db.do_sql("delete from pdu_queue where id=%i" % id) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 46 | |
| 47 | def do_job(self, hostname, port, request): |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 48 | retries = 5 |
| 49 | while retries > 0: |
| 50 | try: |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 51 | logging.debug("creating a new PDUEngine") |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 52 | pe = PDUEngine(hostname, 23) |
| 53 | if request == "reboot": |
| 54 | pe.driver.port_reboot(port) |
| 55 | elif request == "on": |
| 56 | pe.driver.port_on(port) |
| 57 | elif request == "off": |
| 58 | pe.driver.port_off(port) |
| 59 | elif request == "delayed": |
| 60 | pe.driver.port_delayed(port) |
| 61 | else: |
| 62 | logging.debug("Unknown request type: %s" % request) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 63 | pe.pduclose() |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 64 | retries = 0 |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 65 | except: |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 66 | logging.warn("Failed to execute job: %s %s %s (attempts left %i)" % (hostname,port,request,retries)) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 67 | #logging.warn(e) |
| 68 | time.sleep(5) |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 69 | retries -= 1 |
| 70 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 71 | |
| 72 | def run_me(self): |
| 73 | print("Starting up the PDURunner") |
| 74 | while 1: |
| 75 | self.get_one() |
| 76 | time.sleep(1) |
| 77 | |
| 78 | if __name__ == "__main__": |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 79 | starter = {"logging_level": logging.DEBUG, |
matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame^] | 80 | "dbfile": "/var/lib/lavapdu/pdu.db"} |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 81 | p = PDURunner(starter) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 82 | p.run_me() |