blob: 3c1ed299b61267297696ecf014842a89d0e29ed2 [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
26class 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 Hart1499bd42013-08-20 11:35:46 +010033
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010034 def get_one(self, db):
35 job = db.get_next_job()
36 if job:
37 job_id,hostname,port,request = job
38 logging.info("Processing queue item: (%s %s) on hostname: %s" % (request, port, hostname))
Matt Hart63ed9112013-08-20 13:31:50 +010039 #logging.debug(id, hostname, request, port)
Matt Hart1306d822013-08-09 12:08:22 +010040 res = self.do_job(hostname,port,request)
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010041 db.delete_row(job_id)
42 else:
43 logging.debug("Found nothing to do in database")
Matt Hart1306d822013-08-09 12:08:22 +010044
45 def do_job(self, hostname, port, request):
Matt Hart63ed9112013-08-20 13:31:50 +010046 retries = 5
47 while retries > 0:
48 try:
49 pe = PDUEngine(hostname, 23)
50 if request == "reboot":
51 pe.driver.port_reboot(port)
52 elif request == "on":
53 pe.driver.port_on(port)
54 elif request == "off":
55 pe.driver.port_off(port)
56 elif request == "delayed":
57 pe.driver.port_delayed(port)
58 else:
59 logging.debug("Unknown request type: %s" % request)
Matt Hart40d6c312013-08-20 14:47:31 +010060 pe.pduclose()
Matt Hart63ed9112013-08-20 13:31:50 +010061 retries = 0
Matt Hart40d6c312013-08-20 14:47:31 +010062 except:
Matt Hart63ed9112013-08-20 13:31:50 +010063 logging.warn("Failed to execute job: %s %s %s (attempts left %i)" % (hostname,port,request,retries))
Matt Hart40d6c312013-08-20 14:47:31 +010064 #logging.warn(e)
65 time.sleep(5)
Matt Hart63ed9112013-08-20 13:31:50 +010066 retries -= 1
67
Matt Hart1306d822013-08-09 12:08:22 +010068
69 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__":
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010079 starter = {"dbhost":"127.0.0.1",
80 "dbuser":"pdudaemon",
81 "dbpass":"pdudaemon",
82 "dbname":"lavapdu",
83 "logging_level": logging.DEBUG}
Matt Hart1499bd42013-08-20 11:35:46 +010084 p = PDURunner(starter)
Matt Hart1306d822013-08-09 12:08:22 +010085 p.run_me()