Matt Hart | 76fb254 | 2014-06-01 14:24:56 +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 | |
| 21 | import logging |
| 22 | import psycopg2 |
| 23 | import time |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 24 | log = logging.getLogger(__name__) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 25 | |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 26 | |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 27 | class DBHandler(object): |
| 28 | def __init__(self, config): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 29 | log.debug("Creating new DBHandler: %s %s", config["dbhost"], |
| 30 | config["dbname"]) |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 31 | self.conn = psycopg2.connect(database=config["dbname"], |
| 32 | user=config["dbuser"], |
| 33 | password=config["dbpass"], |
| 34 | host=config["dbhost"]) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 35 | self.cursor = self.conn.cursor() |
| 36 | |
| 37 | def do_sql(self, sql): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 38 | log.debug("executing sql: %s", sql) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 39 | self.cursor.execute(sql) |
| 40 | self.conn.commit() |
| 41 | |
| 42 | def do_sql_with_fetch(self, sql): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 43 | log.debug("executing sql: %s", sql) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 44 | self.cursor.execute(sql) |
| 45 | row = self.cursor.fetchone() |
| 46 | self.conn.commit() |
| 47 | return row |
| 48 | |
Matt Hart | fc64682 | 2015-02-27 14:51:16 +0000 | [diff] [blame] | 49 | def create_db(self): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 50 | log.info("Creating db table if it doesn't exist") |
Matt Hart | fc64682 | 2015-02-27 14:51:16 +0000 | [diff] [blame] | 51 | sql = "create table if not exists pdu_queue (id serial, hostname " \ |
| 52 | "text, port int, request text, exectime int)" |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 53 | self.do_sql(sql) |
Matt Hart | fc64682 | 2015-02-27 14:51:16 +0000 | [diff] [blame] | 54 | sql = "select column_name from information_schema.columns where " \ |
| 55 | "table_name='pdu_queue' and column_name='exectime'" |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 56 | res = self.do_sql_with_fetch(sql) |
Matt Hart | fc64682 | 2015-02-27 14:51:16 +0000 | [diff] [blame] | 57 | if not res: |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 58 | log.info("Old db schema discovered, upgrading") |
Matt Hart | fc64682 | 2015-02-27 14:51:16 +0000 | [diff] [blame] | 59 | sql = "alter table pdu_queue add column exectime int default 1" |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 60 | 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 Hart | fc64682 | 2015-02-27 14:51:16 +0000 | [diff] [blame] | 66 | |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 67 | def delete_row(self, row_id): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 68 | log.debug("deleting row %i", row_id) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 69 | 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 Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 74 | def get_next_job(self, single_pdu=False): |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 75 | now = int(time.time()) |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 76 | 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 Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 84 | return row |
| 85 | |
| 86 | def close(self): |
Matt Hart | d4a0998 | 2015-07-17 16:13:11 +0100 | [diff] [blame^] | 87 | log.debug("Closing DBHandler") |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 88 | self.cursor.close() |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame] | 89 | self.conn.close() |