matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +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 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 21 | import SocketServer |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 22 | import psycopg2 |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 23 | import logging |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 24 | import socket |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 25 | |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 26 | |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 27 | class DBHandler(object): |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 28 | def __init__(self, config): |
| 29 | logging.debug("Creating new DBHandler: %s" % config["dbhost"]) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 30 | logging.getLogger().name = "DBHandler" |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 31 | self.conn = psycopg2.connect(database=config["dbname"], user=config["dbuser"], |
| 32 | password=config["dbpass"], host=config["dbhost"]) |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 33 | self.cursor = self.conn.cursor() |
| 34 | |
| 35 | def do_sql(self, sql): |
| 36 | logging.debug("executing sql: %s" % sql) |
| 37 | self.cursor.execute(sql) |
| 38 | self.conn.commit() |
| 39 | |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 40 | def do_sql_with_fetch(self, sql): |
| 41 | logging.debug("executing sql: %s" % sql) |
| 42 | self.cursor.execute(sql) |
| 43 | row = self.cursor.fetchone() |
| 44 | self.conn.commit() |
| 45 | return row |
| 46 | |
| 47 | def delete_row(self, row_id): |
| 48 | logging.debug("deleting row %i" % row_id) |
| 49 | self.do_sql("delete from pdu_queue where id=%i" % row_id) |
| 50 | |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 51 | def get_res(self, sql): |
| 52 | return self.cursor.execute(sql) |
| 53 | |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 54 | def get_next_job(self): |
| 55 | row = self.do_sql_with_fetch("select * from pdu_queue order by id asc limit 1") |
| 56 | return row |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 57 | |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 58 | def close(self): |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 59 | logging.debug("Closing DBHandler") |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 60 | self.cursor.close() |
| 61 | self.conn.close() |
| 62 | |
| 63 | |
| 64 | class ListenerServer(object): |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 65 | |
| 66 | def __init__(self, config): |
| 67 | self.server = TCPServer((config["hostname"], config["port"]), TCPRequestHandler) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 68 | logging.getLogger().name = "ListenerServer" |
Matt Hart | 7d67061 | 2013-08-20 16:47:52 +0100 | [diff] [blame] | 69 | logging.getLogger().setLevel(config["logging_level"]) |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 70 | logging.info("listening on %s:%s" % (config["hostname"], config["port"])) |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 71 | self.server.config = config |
| 72 | self.db = DBHandler(config) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 73 | self.create_db() |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 74 | self.db.close() |
| 75 | del(self.db) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 76 | |
| 77 | def create_db(self): |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 78 | sql = "create table if not exists pdu_queue (id serial, hostname text, port int, request text)" |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 79 | self.db.do_sql(sql) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 80 | |
| 81 | def start(self): |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 82 | logging.info("Starting the ListenerServer") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 83 | self.server.serve_forever() |
| 84 | |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 85 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 86 | class TCPRequestHandler(SocketServer.BaseRequestHandler): |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 87 | #"One instance per connection. Override handle(self) to customize action." |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 88 | def insert_request(self, data): |
matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 89 | logging.getLogger().name = "TCPRequestHandler" |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 90 | array = data.split(" ") |
matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 91 | if len(array) != 3: |
| 92 | logging.info("Wrong data size") |
| 93 | raise Exception("Unexpected data") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 94 | hostname = array[0] |
| 95 | port = int(array[1]) |
| 96 | request = array[2] |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 97 | if not (request in ["reboot", "on", "off", "delayed"]): |
matthew.hart@linaro.org | 1d63239 | 2013-08-27 14:40:11 +0100 | [diff] [blame] | 98 | logging.info("Unknown request: %s" % request) |
| 99 | raise Exception("Unknown request: %s" % request) |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 100 | db = DBHandler(self.server.config) |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 101 | sql = "insert into pdu_queue (hostname,port,request) values ('%s',%i,'%s')" % (hostname, port, request) |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 102 | db.do_sql(sql) |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 103 | db.close() |
| 104 | del(db) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 105 | |
| 106 | def handle(self): |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 107 | logging.getLogger().name = "TCPRequestHandler" |
matthew.hart@linaro.org | 0061296 | 2014-02-13 15:49:27 +0000 | [diff] [blame] | 108 | ip = self.client_address[0] |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 109 | try: |
| 110 | data = self.request.recv(4096).strip() |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 111 | socket.setdefaulttimeout(2) |
matthew.hart@linaro.org | 0061296 | 2014-02-13 15:49:27 +0000 | [diff] [blame] | 112 | try: |
| 113 | request_host = socket.gethostbyaddr(ip)[0] |
| 114 | except socket.herror as e: |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 115 | logging.debug("Unable to resolve: %s error: %s" % (ip, e)) |
matthew.hart@linaro.org | 0061296 | 2014-02-13 15:49:27 +0000 | [diff] [blame] | 116 | request_host = ip |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 117 | logging.info("Received a request from %s: '%s'" % (request_host, data)) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 118 | self.insert_request(data) |
| 119 | self.request.sendall("ack\n") |
matthew.hart@linaro.org | 6e15b5e | 2013-08-28 19:48:51 +0100 | [diff] [blame] | 120 | except Exception as e: |
| 121 | logging.debug(e) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 122 | self.request.sendall("nack\n") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 123 | self.request.close() |
| 124 | |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 125 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 126 | class TCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): |
| 127 | allow_reuse_address = True |
| 128 | daemon_threads = True |
| 129 | pass |
| 130 | |
| 131 | if __name__ == "__main__": |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 132 | logging.basicConfig(level=logging.DEBUG) |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 133 | logging.getLogger().setLevel(logging.DEBUG) |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 134 | logging.debug("Executing from __main__") |
| 135 | starter = {"hostname": "0.0.0.0", |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 136 | "port": 16421, |
| 137 | "dbhost": "127.0.0.1", |
| 138 | "dbuser": "pdudaemon", |
| 139 | "dbpass": "pdudaemon", |
| 140 | "dbname": "lavapdu", |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 141 | "logging_level": logging.DEBUG} |
Matt Hart | 1499bd4 | 2013-08-20 11:35:46 +0100 | [diff] [blame] | 142 | ss = ListenerServer(starter) |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame] | 143 | ss.start() |