blob: b00bbf1a8fd5cf11e9456394460df2d6f48f7f58 [file] [log] [blame]
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +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
Matt Hart1306d822013-08-09 12:08:22 +010021import SocketServer
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010022import psycopg2
Matt Hart1306d822013-08-09 12:08:22 +010023import logging
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010024import socket
Matt Hart1306d822013-08-09 12:08:22 +010025
Matt Hart1499bd42013-08-20 11:35:46 +010026class DBHandler(object):
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010027 def __init__(self, config):
28 logging.debug("Creating new DBHandler: %s" % config["dbhost"])
Matt Hart40d6c312013-08-20 14:47:31 +010029 logging.getLogger().name = "DBHandler"
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010030 self.conn = psycopg2.connect(database=config["dbname"], user=config["dbuser"],
31 password=config["dbpass"], host=config["dbhost"])
Matt Hart1499bd42013-08-20 11:35:46 +010032 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.org6e15b5e2013-08-28 19:48:51 +010039 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 Hart63ed9112013-08-20 13:31:50 +010050 def get_res(self, sql):
51 return self.cursor.execute(sql)
52
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010053 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 Hart63ed9112013-08-20 13:31:50 +010056
Matt Hart1499bd42013-08-20 11:35:46 +010057 def close(self):
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010058 logging.debug("Closing DBHandler")
Matt Hart1499bd42013-08-20 11:35:46 +010059 self.cursor.close()
60 self.conn.close()
61
62
63class ListenerServer(object):
Matt Hart1499bd42013-08-20 11:35:46 +010064
65 def __init__(self, config):
66 self.server = TCPServer((config["hostname"], config["port"]), TCPRequestHandler)
Matt Hart40d6c312013-08-20 14:47:31 +010067 logging.getLogger().name = "ListenerServer"
Matt Hart7d670612013-08-20 16:47:52 +010068 logging.getLogger().setLevel(config["logging_level"])
Matt Hart1499bd42013-08-20 11:35:46 +010069 logging.info("listening on %s:%s" % (config["hostname"], config["port"]))
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010070 self.server.config = config
71 self.db = DBHandler(config)
Matt Hart1306d822013-08-09 12:08:22 +010072 self.create_db()
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010073 self.db.close()
74 del(self.db)
Matt Hart1306d822013-08-09 12:08:22 +010075
76 def create_db(self):
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010077 sql = "create table if not exists pdu_queue (id serial, hostname text, port int, request text)"
Matt Hart1499bd42013-08-20 11:35:46 +010078 self.db.do_sql(sql)
Matt Hart1306d822013-08-09 12:08:22 +010079
80 def start(self):
Matt Hart1499bd42013-08-20 11:35:46 +010081 logging.info("Starting the ListenerServer")
Matt Hart1306d822013-08-09 12:08:22 +010082 self.server.serve_forever()
83
Matt Hart1499bd42013-08-20 11:35:46 +010084
Matt Hart1306d822013-08-09 12:08:22 +010085class TCPRequestHandler(SocketServer.BaseRequestHandler):
Matt Hart1499bd42013-08-20 11:35:46 +010086 #"One instance per connection. Override handle(self) to customize action."
Matt Hart1306d822013-08-09 12:08:22 +010087 def insert_request(self, data):
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010088 logging.getLogger().name = "TCPRequestHandler"
Matt Hart1306d822013-08-09 12:08:22 +010089 array = data.split(" ")
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010090 if len(array) != 3:
91 logging.info("Wrong data size")
92 raise Exception("Unexpected data")
Matt Hart1306d822013-08-09 12:08:22 +010093 hostname = array[0]
94 port = int(array[1])
95 request = array[2]
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010096 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.org6e15b5e2013-08-28 19:48:51 +010099 db = DBHandler(self.server.config)
100 sql = "insert into pdu_queue (hostname,port,request) values ('%s',%i,'%s')" % (hostname,port,request)
Matt Hart1499bd42013-08-20 11:35:46 +0100101 db.do_sql(sql)
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +0100102 db.close()
103 del(db)
Matt Hart1306d822013-08-09 12:08:22 +0100104
105 def handle(self):
Matt Hart40d6c312013-08-20 14:47:31 +0100106 logging.getLogger().name = "TCPRequestHandler"
matthew.hart@linaro.org00612962014-02-13 15:49:27 +0000107 ip = self.client_address[0]
Matt Hart40d6c312013-08-20 14:47:31 +0100108 try:
109 data = self.request.recv(4096).strip()
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +0100110 socket.setdefaulttimeout(2)
matthew.hart@linaro.org00612962014-02-13 15:49:27 +0000111 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.org6e15b5e2013-08-28 19:48:51 +0100116 logging.info("Received a request from %s: '%s'" % (request_host, data))
Matt Hart40d6c312013-08-20 14:47:31 +0100117 self.insert_request(data)
118 self.request.sendall("ack\n")
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +0100119 except Exception as e:
120 logging.debug(e)
Matt Hart40d6c312013-08-20 14:47:31 +0100121 self.request.sendall("nack\n")
Matt Hart1306d822013-08-09 12:08:22 +0100122 self.request.close()
123
124class TCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
125 allow_reuse_address = True
126 daemon_threads = True
127 pass
128
129if __name__ == "__main__":
Matt Hart1499bd42013-08-20 11:35:46 +0100130 logging.basicConfig(level=logging.DEBUG)
Matt Hart63ed9112013-08-20 13:31:50 +0100131 logging.getLogger().setLevel(logging.DEBUG)
Matt Hart1499bd42013-08-20 11:35:46 +0100132 logging.debug("Executing from __main__")
133 starter = {"hostname": "0.0.0.0",
Matt Hart63ed9112013-08-20 13:31:50 +0100134 "port":16421,
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +0100135 "dbhost":"127.0.0.1",
136 "dbuser":"pdudaemon",
137 "dbpass":"pdudaemon",
138 "dbname":"lavapdu",
Matt Hart96ca8662013-08-21 13:37:35 +0100139 "logging_level": logging.DEBUG}
Matt Hart1499bd42013-08-20 11:35:46 +0100140 ss = ListenerServer(starter)
Matt Hart1306d822013-08-09 12:08:22 +0100141 ss.start()