blob: cc2b19dea68d83640fd0ce72d52380b88dc13c3d [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 Hartd73bba22013-08-21 13:46:03 +010023from engine import PDUEngine
24from socketserver import DBHandler
Matt Hart1306d822013-08-09 12:08:22 +010025
Neil Williams16579dc2014-02-13 11:20:04 +000026
Matt Hart1306d822013-08-09 12:08:22 +010027class PDURunner():
Matt Hart1499bd42013-08-20 11:35:46 +010028
29 def __init__(self, config):
Matt Hart63ed9112013-08-20 13:31:50 +010030 logging.basicConfig(level=config["logging_level"])
31 logging.getLogger().setLevel(config["logging_level"])
Matt Hart40d6c312013-08-20 14:47:31 +010032 logging.getLogger().name = "PDURunner"
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010033 self.config = config
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
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010039 logging.info("Processing queue item: (%s %s) on hostname: %s" % (request, port, hostname))
Matt Hart63ed9112013-08-20 13:31:50 +010040 #logging.debug(id, hostname, request, port)
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
46 def do_job(self, hostname, port, request):
Matt Hart63ed9112013-08-20 13:31:50 +010047 retries = 5
48 while retries > 0:
49 try:
50 pe = PDUEngine(hostname, 23)
51 if request == "reboot":
52 pe.driver.port_reboot(port)
53 elif request == "on":
54 pe.driver.port_on(port)
55 elif request == "off":
56 pe.driver.port_off(port)
57 elif request == "delayed":
58 pe.driver.port_delayed(port)
59 else:
60 logging.debug("Unknown request type: %s" % request)
Matt Hart40d6c312013-08-20 14:47:31 +010061 pe.pduclose()
Matt Hart63ed9112013-08-20 13:31:50 +010062 retries = 0
Matt Hart40d6c312013-08-20 14:47:31 +010063 except:
Neil Williams16579dc2014-02-13 11:20:04 +000064 logging.warn("Failed to execute job: %s %s %s (attempts left %i)" % (hostname, port, request, retries))
Matt Hart40d6c312013-08-20 14:47:31 +010065 #logging.warn(e)
66 time.sleep(5)
Matt Hart63ed9112013-08-20 13:31:50 +010067 retries -= 1
68
Matt Hart1306d822013-08-09 12:08:22 +010069 def run_me(self):
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010070 logging.info("Starting up the PDURunner")
Matt Hart1306d822013-08-09 12:08:22 +010071 while 1:
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010072 db = DBHandler(self.config)
73 self.get_one(db)
74 db.close()
75 del(db)
76 time.sleep(2)
Matt Hart1306d822013-08-09 12:08:22 +010077
78if __name__ == "__main__":
Neil Williams16579dc2014-02-13 11:20:04 +000079 starter = {"dbhost": "127.0.0.1",
80 "dbuser": "pdudaemon",
81 "dbpass": "pdudaemon",
82 "dbname": "lavapdu",
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010083 "logging_level": logging.DEBUG}
Matt Hart1499bd42013-08-20 11:35:46 +010084 p = PDURunner(starter)
Neil Williams16579dc2014-02-13 11:20:04 +000085 p.run_me()