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 |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame^] | 24 | from dbhandler import DBHandler |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 25 | |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 26 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 27 | class PDURunner(): |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 28 | |
| 29 | def __init__(self, config): |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 30 | logging.basicConfig(level=config["logging_level"]) |
| 31 | logging.getLogger().setLevel(config["logging_level"]) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 32 | logging.getLogger().name = "PDURunner" |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 33 | self.config = config |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 34 | |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 35 | def get_one(self, db): |
| 36 | job = db.get_next_job() |
| 37 | if job: |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 38 | job_id, hostname, port, request = job |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame^] | 39 | logging.debug(job) |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 40 | logging.info("Processing queue item: (%s %s) on hostname: %s" % (request, port, hostname)) |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 41 | #logging.debug(id, hostname, request, port) |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 42 | res = self.do_job(hostname, port, request) |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 43 | db.delete_row(job_id) |
| 44 | else: |
| 45 | logging.debug("Found nothing to do in database") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 46 | |
| 47 | def do_job(self, hostname, port, request): |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame^] | 48 | retries = 10 |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 49 | while retries > 0: |
| 50 | try: |
| 51 | pe = PDUEngine(hostname, 23) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame^] | 52 | if request == "on": |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 53 | pe.driver.port_on(port) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame^] | 54 | return true |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 55 | elif request == "off": |
| 56 | pe.driver.port_off(port) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame^] | 57 | return true |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 58 | else: |
| 59 | logging.debug("Unknown request type: %s" % request) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame^] | 60 | return false |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 61 | pe.pduclose() |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame^] | 62 | del(pe) |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 63 | retries = 0 |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame^] | 64 | except Exception as e: |
| 65 | logging.warn("Failed to execute job: %s %s %s (attempts left %i) error was %s" % |
| 66 | (hostname, port, request, retries, e.message)) |
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 |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame^] | 70 | return false |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 71 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 72 | def run_me(self): |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 73 | logging.info("Starting up the PDURunner") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 74 | while 1: |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 75 | db = DBHandler(self.config) |
| 76 | self.get_one(db) |
| 77 | db.close() |
| 78 | del(db) |
| 79 | time.sleep(2) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 80 | |
| 81 | if __name__ == "__main__": |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 82 | starter = {"dbhost": "127.0.0.1", |
| 83 | "dbuser": "pdudaemon", |
| 84 | "dbpass": "pdudaemon", |
| 85 | "dbname": "lavapdu", |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 86 | "logging_level": logging.DEBUG} |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 87 | p = PDURunner(starter) |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 88 | p.run_me() |