blob: 5526e1be2f7e4806f20e7b7ec23ff4b9f3b3a847 [file] [log] [blame]
Matt Hart76fb2542014-06-01 14:24:56 +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
21import logging
22import psycopg2
23import time
Matt Hartd4a09982015-07-17 16:13:11 +010024log = logging.getLogger(__name__)
Matt Hart76fb2542014-06-01 14:24:56 +010025
Matt Hartfe042212015-07-09 14:13:29 +010026
Matt Hart76fb2542014-06-01 14:24:56 +010027class DBHandler(object):
28 def __init__(self, config):
Matt Hartd4a09982015-07-17 16:13:11 +010029 log.debug("Creating new DBHandler: %s %s", config["dbhost"],
30 config["dbname"])
Matt Hartfe042212015-07-09 14:13:29 +010031 self.conn = psycopg2.connect(database=config["dbname"],
32 user=config["dbuser"],
33 password=config["dbpass"],
34 host=config["dbhost"])
Matt Hart76fb2542014-06-01 14:24:56 +010035 self.cursor = self.conn.cursor()
36
37 def do_sql(self, sql):
Matt Hartd4a09982015-07-17 16:13:11 +010038 log.debug("executing sql: %s", sql)
Matt Hart76fb2542014-06-01 14:24:56 +010039 self.cursor.execute(sql)
40 self.conn.commit()
41
42 def do_sql_with_fetch(self, sql):
Matt Hartd4a09982015-07-17 16:13:11 +010043 log.debug("executing sql: %s", sql)
Matt Hart76fb2542014-06-01 14:24:56 +010044 self.cursor.execute(sql)
45 row = self.cursor.fetchone()
46 self.conn.commit()
47 return row
48
Matt Hartfc646822015-02-27 14:51:16 +000049 def create_db(self):
Matt Hartd4a09982015-07-17 16:13:11 +010050 log.info("Creating db table if it doesn't exist")
Matt Hartfc646822015-02-27 14:51:16 +000051 sql = "create table if not exists pdu_queue (id serial, hostname " \
52 "text, port int, request text, exectime int)"
Matt Hartd4a09982015-07-17 16:13:11 +010053 self.do_sql(sql)
Matt Hartfc646822015-02-27 14:51:16 +000054 sql = "select column_name from information_schema.columns where " \
55 "table_name='pdu_queue' and column_name='exectime'"
Matt Hartd4a09982015-07-17 16:13:11 +010056 res = self.do_sql_with_fetch(sql)
Matt Hartfc646822015-02-27 14:51:16 +000057 if not res:
Matt Hartd4a09982015-07-17 16:13:11 +010058 log.info("Old db schema discovered, upgrading")
Matt Hartfc646822015-02-27 14:51:16 +000059 sql = "alter table pdu_queue add column exectime int default 1"
Matt Hartd4a09982015-07-17 16:13:11 +010060 self.do_sql(sql)
61
62 def insert_request(self, hostname, port, request, exectime):
63 sql = "insert into pdu_queue (hostname,port,request,exectime) " \
64 "values ('%s',%i,'%s',%i)" % (hostname, port, request, exectime)
65 self.do_sql(sql)
Matt Hartfc646822015-02-27 14:51:16 +000066
Matt Hart76fb2542014-06-01 14:24:56 +010067 def delete_row(self, row_id):
Matt Hartd4a09982015-07-17 16:13:11 +010068 log.debug("deleting row %i", row_id)
Matt Hart76fb2542014-06-01 14:24:56 +010069 self.do_sql("delete from pdu_queue where id=%i" % row_id)
70
71 def get_res(self, sql):
72 return self.cursor.execute(sql)
73
Matt Hartd4a09982015-07-17 16:13:11 +010074 def get_next_job(self, single_pdu=False):
Matt Hart76fb2542014-06-01 14:24:56 +010075 now = int(time.time())
Matt Hartd4a09982015-07-17 16:13:11 +010076 extra_sql = ""
77 if single_pdu:
78 log.debug("Looking for jobs for PDU: %s", single_pdu)
79 extra_sql = "and hostname='%s'" % single_pdu
80 row = self.do_sql_with_fetch("select id, hostname, port, request "
81 "from pdu_queue where ((exectime < %i "
82 "or exectime is null) %s) order by id asc"
83 " limit 1" % (now, extra_sql))
Matt Hart76fb2542014-06-01 14:24:56 +010084 return row
85
86 def close(self):
Matt Hartd4a09982015-07-17 16:13:11 +010087 log.debug("Closing DBHandler")
Matt Hart76fb2542014-06-01 14:24:56 +010088 self.cursor.close()
Matt Hartfe042212015-07-09 14:13:29 +010089 self.conn.close()