blob: 3eb190f15a3a6bca25ac47076cc25d0833b673fc [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 +010022#import sqlite3
23import psycopg2
Matt Hart1306d822013-08-09 12:08:22 +010024import logging
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010025import socket
Matt Hart1306d822013-08-09 12:08:22 +010026
Matt Hart1499bd42013-08-20 11:35:46 +010027class DBHandler(object):
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010028 def __init__(self, config):
29 logging.debug("Creating new DBHandler: %s" % config["dbhost"])
Matt Hart40d6c312013-08-20 14:47:31 +010030 logging.getLogger().name = "DBHandler"
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010031 self.conn = psycopg2.connect(database=config["dbname"], user=config["dbuser"],
32 password=config["dbpass"], host=config["dbhost"])
Matt Hart1499bd42013-08-20 11:35:46 +010033 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.org6e15b5e2013-08-28 19:48:51 +010040 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 Hart63ed9112013-08-20 13:31:50 +010051 def get_res(self, sql):
52 return self.cursor.execute(sql)
53
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010054 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 Hart63ed9112013-08-20 13:31:50 +010057
Matt Hart1499bd42013-08-20 11:35:46 +010058 def close(self):
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010059 logging.debug("Closing DBHandler")
Matt Hart1499bd42013-08-20 11:35:46 +010060 self.cursor.close()
61 self.conn.close()
62
63
64class ListenerServer(object):
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010065# conn = sqlite3.connect("/var/lib/lavapdu/pdu.db", check_same_thread = False)
Matt Hart1499bd42013-08-20 11:35:46 +010066# cursor = conn.cursor()
Matt Hart1499bd42013-08-20 11:35:46 +010067
68 def __init__(self, config):
69 self.server = TCPServer((config["hostname"], config["port"]), TCPRequestHandler)
Matt Hart40d6c312013-08-20 14:47:31 +010070 logging.getLogger().name = "ListenerServer"
Matt Hart7d670612013-08-20 16:47:52 +010071 logging.getLogger().setLevel(config["logging_level"])
Matt Hart1499bd42013-08-20 11:35:46 +010072 logging.info("listening on %s:%s" % (config["hostname"], config["port"]))
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010073 self.server.config = config
74 self.db = DBHandler(config)
Matt Hart1306d822013-08-09 12:08:22 +010075 self.create_db()
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010076 self.db.close()
77 del(self.db)
78 #self.server.db = self.db
Matt Hart1306d822013-08-09 12:08:22 +010079
80 def create_db(self):
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +010081 sql = "create table if not exists pdu_queue (id serial, hostname text, port int, request text)"
Matt Hart1499bd42013-08-20 11:35:46 +010082 self.db.do_sql(sql)
Matt Hart1306d822013-08-09 12:08:22 +010083
84 def start(self):
Matt Hart1499bd42013-08-20 11:35:46 +010085 logging.info("Starting the ListenerServer")
Matt Hart1306d822013-08-09 12:08:22 +010086 self.server.serve_forever()
87
Matt Hart1499bd42013-08-20 11:35:46 +010088
Matt Hart1306d822013-08-09 12:08:22 +010089class TCPRequestHandler(SocketServer.BaseRequestHandler):
Matt Hart1499bd42013-08-20 11:35:46 +010090 #"One instance per connection. Override handle(self) to customize action."
Matt Hart1306d822013-08-09 12:08:22 +010091 def insert_request(self, data):
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010092 logging.getLogger().name = "TCPRequestHandler"
Matt Hart1306d822013-08-09 12:08:22 +010093 array = data.split(" ")
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +010094 if len(array) != 3:
95 logging.info("Wrong data size")
96 raise Exception("Unexpected data")
Matt Hart1306d822013-08-09 12:08:22 +010097 hostname = array[0]
98 port = int(array[1])
99 request = array[2]
matthew.hart@linaro.org1d632392013-08-27 14:40:11 +0100100 if not (request in ["reboot","on","off","delayed"]):
101 logging.info("Unknown request: %s" % request)
102 raise Exception("Unknown request: %s" % request)
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +0100103 #db = self.server.db
104 db = DBHandler(self.server.config)
105 sql = "insert into pdu_queue (hostname,port,request) values ('%s',%i,'%s')" % (hostname,port,request)
Matt Hart1499bd42013-08-20 11:35:46 +0100106 db.do_sql(sql)
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +0100107 db.close()
108 del(db)
Matt Hart1306d822013-08-09 12:08:22 +0100109
110 def handle(self):
Matt Hart40d6c312013-08-20 14:47:31 +0100111 logging.getLogger().name = "TCPRequestHandler"
112 try:
113 data = self.request.recv(4096).strip()
matthew.hart@linaro.org6e15b5e2013-08-28 19:48:51 +0100114 socket.setdefaulttimeout(2)
115 request_host = socket.gethostbyaddr(self.client_address[0])[0]
116 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()