2012-06-06 23:39:37 +00:00
|
|
|
import json, threading, Queue
|
2012-06-06 23:23:23 +00:00
|
|
|
import requests
|
2012-06-07 05:02:17 +00:00
|
|
|
import pathod, utils
|
2012-06-06 23:23:23 +00:00
|
|
|
|
2012-06-06 23:39:37 +00:00
|
|
|
IFACE = "127.0.0.1"
|
|
|
|
|
2012-06-06 23:23:23 +00:00
|
|
|
class Daemon:
|
2012-07-23 03:03:56 +00:00
|
|
|
def __init__(self, staticdir=None, anchors=(), ssl=None, sizelimit=None):
|
2012-06-06 23:23:23 +00:00
|
|
|
self.q = Queue.Queue()
|
2012-07-23 03:03:56 +00:00
|
|
|
self.thread = PaThread(self.q, staticdir, anchors, ssl, sizelimit)
|
2012-06-06 23:23:23 +00:00
|
|
|
self.thread.start()
|
|
|
|
self.port = self.q.get(True, 5)
|
2012-06-07 05:02:17 +00:00
|
|
|
self.urlbase = "%s://%s:%s"%("https" if ssl else "http", IFACE, self.port)
|
2012-06-06 23:23:23 +00:00
|
|
|
|
2012-06-06 23:39:37 +00:00
|
|
|
def info(self):
|
2012-06-21 03:39:40 +00:00
|
|
|
"""
|
|
|
|
Return some basic info about the remote daemon.
|
|
|
|
"""
|
2012-06-07 05:02:17 +00:00
|
|
|
resp = requests.get("%s/api/info"%self.urlbase, verify=False)
|
|
|
|
return resp.json
|
2012-06-06 23:23:23 +00:00
|
|
|
|
2012-06-21 03:39:40 +00:00
|
|
|
def log(self):
|
|
|
|
"""
|
|
|
|
Return the log buffer as a list of dictionaries.
|
|
|
|
"""
|
|
|
|
resp = requests.get("%s/api/log"%self.urlbase, verify=False)
|
|
|
|
return resp.json["log"]
|
|
|
|
|
|
|
|
def clear_log(self):
|
|
|
|
"""
|
|
|
|
Clear the log.
|
|
|
|
"""
|
|
|
|
resp = requests.get("%s/api/clear_log"%self.urlbase, verify=False)
|
|
|
|
return resp.ok
|
|
|
|
|
2012-06-06 23:23:23 +00:00
|
|
|
def shutdown(self):
|
2012-06-21 03:39:40 +00:00
|
|
|
"""
|
|
|
|
Shut the daemon down, return after the thread has exited.
|
|
|
|
"""
|
2012-06-19 01:23:07 +00:00
|
|
|
self.thread.server.shutdown()
|
2012-06-09 08:42:43 +00:00
|
|
|
self.thread.join()
|
2012-06-07 04:35:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PaThread(threading.Thread):
|
2012-07-23 03:03:56 +00:00
|
|
|
def __init__(self, q, staticdir, anchors, ssl, sizelimit):
|
2012-06-07 04:35:54 +00:00
|
|
|
threading.Thread.__init__(self)
|
2012-07-23 03:03:56 +00:00
|
|
|
self.q, self.staticdir, self.anchors, self.ssl, self.sizelimit = q, staticdir, anchors, ssl, sizelimit
|
2012-06-07 04:35:54 +00:00
|
|
|
|
|
|
|
def run(self):
|
2012-06-07 05:02:17 +00:00
|
|
|
if self.ssl is True:
|
|
|
|
ssloptions = dict(
|
|
|
|
keyfile = utils.data.path("resources/server.key"),
|
|
|
|
certfile = utils.data.path("resources/server.crt"),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
ssloptions = self.ssl
|
2012-06-24 03:12:31 +00:00
|
|
|
self.server = pathod.Pathod(
|
|
|
|
(IFACE, 0),
|
|
|
|
ssloptions = ssloptions,
|
2012-06-24 04:20:50 +00:00
|
|
|
anchors = self.anchors,
|
2012-07-23 03:03:56 +00:00
|
|
|
staticdir = self.staticdir,
|
|
|
|
sizelimit = self.sizelimit
|
2012-06-24 03:12:31 +00:00
|
|
|
)
|
2012-06-19 01:23:07 +00:00
|
|
|
self.q.put(self.server.port)
|
|
|
|
self.server.serve_forever()
|