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