blob: 7b0364ba3e6642b9c0885c3f269d004ab1e61663 [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
24
Matt Hartfe042212015-07-09 14:13:29 +010025
Matt Hart76fb2542014-06-01 14:24:56 +010026class DBHandler(object):
27 def __init__(self, config):
Matt Hartfe042212015-07-09 14:13:29 +010028 logging.debug("Creating new DBHandler: %s", config["dbhost"])
Matt Hart76fb2542014-06-01 14:24:56 +010029 logging.getLogger().name = "DBHandler"
Matt Hartfe042212015-07-09 14:13:29 +010030 self.conn = psycopg2.connect(database=config["dbname"],
31 user=config["dbuser"],
32 password=config["dbpass"],
33 host=config["dbhost"])
Matt Hart76fb2542014-06-01 14:24:56 +010034 self.cursor = self.conn.cursor()
35
36 def do_sql(self, sql):
Matt Hartfe042212015-07-09 14:13:29 +010037 logging.debug("executing sql: %s", sql)
Matt Hart76fb2542014-06-01 14:24:56 +010038 self.cursor.execute(sql)
39 self.conn.commit()
40
41 def do_sql_with_fetch(self, sql):
Matt Hartfe042212015-07-09 14:13:29 +010042 logging.debug("executing sql: %s", sql)
Matt Hart76fb2542014-06-01 14:24:56 +010043 self.cursor.execute(sql)
44 row = self.cursor.fetchone()
45 self.conn.commit()
46 return row
47
Matt Hartfc646822015-02-27 14:51:16 +000048 def create_db(self):
49 logging.info("Creating db table if it doesn't exist")
50 sql = "create table if not exists pdu_queue (id serial, hostname " \
51 "text, port int, request text, exectime int)"
52 self.cursor.execute(sql)
53 self.conn.commit()
54 sql = "select column_name from information_schema.columns where " \
55 "table_name='pdu_queue' and column_name='exectime'"
56 self.cursor.execute(sql)
57 res = self.cursor.fetchone()
58 if not res:
59 logging.info("Old db schema discovered, upgrading")
60 sql = "alter table pdu_queue add column exectime int default 1"
61 self.cursor.execute(sql)
62 self.conn.commit()
63
Matt Hart76fb2542014-06-01 14:24:56 +010064 def delete_row(self, row_id):
Matt Hartfe042212015-07-09 14:13:29 +010065 logging.debug("deleting row %i", row_id)
Matt Hart76fb2542014-06-01 14:24:56 +010066 self.do_sql("delete from pdu_queue where id=%i" % row_id)
67
68 def get_res(self, sql):
69 return self.cursor.execute(sql)
70
71 def get_next_job(self):
72 now = int(time.time())
Matt Hartfe042212015-07-09 14:13:29 +010073 row = self.do_sql_with_fetch("select id, hostname, port, "
74 "request from pdu_queue where "
75 "(exectime < %i or exectime is null)"
76 "order by id asc limit 1" % now)
Matt Hart76fb2542014-06-01 14:24:56 +010077 return row
78
79 def close(self):
80 logging.debug("Closing DBHandler")
81 self.cursor.close()
Matt Hartfe042212015-07-09 14:13:29 +010082 self.conn.close()