forked from harlo/UnveillanceAnnex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unveillance_annex.py
199 lines (154 loc) · 5.26 KB
/
unveillance_annex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import signal, os, logging, re, json
from sys import exit, argv
from multiprocessing import Process
from time import sleep
from fabric.api import local, settings
import tornado.ioloop
import tornado.web
import tornado.httpserver
from api import UnveillanceAPI
from lib.Core.vars import Result
from lib.Core.Utils.funcs import startDaemon, stopDaemon
from lib.Worker.Utils.funcs import getFileType
from lib.Worker.Models.uv_task import UnveillanceTask
from conf import ANNEX_DIR, API_PORT, NUM_PROCESSES, HOST, MONITOR_ROOT, DEBUG
def terminationHandler(signal, frame): exit(0)
signal.signal(signal.SIGINT, terminationHandler)
class UnveillanceAnnex(tornado.web.Application, UnveillanceAPI):
def __init__(self):
self.api_pid_file = os.path.join(MONITOR_ROOT, "api.pid.txt")
self.api_log_file = os.path.join(MONITOR_ROOT, "api.log.txt")
self.reserved_routes = ["files", "sync", "task"]
self.routes = [
(r"/files/(\S+)", self.FileHandler),
(r"/sync/(.+)", self.SyncHandler),
(r"/task/", self.TaskHandler)]
UnveillanceAPI.__init__(self)
class RouteHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self, route): self.routeRequest(route)
def routeRequest(self, route):
res = Result()
if route is not None:
route = [r_ for r_ in route.split("/") if r_ != '']
func_name = "do_%s" % route[0]
if hasattr(self.application, func_name):
if DEBUG : print "doing %s" % func_name
func = getattr(self.application, str(func_name))
res.result = 200
res.data = func(self.request)
else:
if DEBUG : print "could not find function %s" % func_name
try:
if res.data is None:
del res.data
res.result = 412
except AttributeError as e: pass
self.set_status(res.result)
self.finish(res.emit())
class SyncHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self, file_name):
self.application.syncAnnex(file_name)
res = Result()
res.result = 200
self.finish(res.emit())
@tornado.web.asynchronous
def post(self, file_name):
print self.request.body
self.application.syncAnnex(file_name, with_metadata=self.request.body)
res = Result()
res.result = 200
self.finish(res.emit())
class TaskHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
res = Result()
res.result = 412
res.data = self.application.do_tasks(self)
if res.data is None:
del res.data
elif res.data: res.result = 200
self.set_status(res.result)
self.finish(res.emit())
@tornado.web.asynchronous
def post(self):
res = Result()
res.status = 412
res.data = self.application.runTask(self)
if res.data is None:
del res.data
elif res.data:
res.result = 200
self.set_status(res.result)
self.finish(res.emit())
class FileHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self, file_path):
# if file exists in git-annex, return the file
# else, return 404 (file not found)
if self.application.fileExistsInAnnex(file_path):
try:
mime_type = getFileType(os.path.join(ANNEX_DIR, file_path))
except Exception as e:
print e
mime_type = None
with open(os.path.join(ANNEX_DIR, file_path), 'rb') as file:
if mime_type is not None:
self.set_header("Content-Type", "%s; charset=\"binary\"" % mime_type)
self.finish(file.read())
return
# TODO: log this: we want to know who/why is requesting non-entities
else:
self.set_status(404)
res = Result()
self.finish(res.emit())
def startRESTAPI(self):
if DEBUG: print "Starting REST API on port %d" % API_PORT
rr_group = r"/(?:(?!%s))([a-zA-Z0-9_/]*/$)?" % "|".join(self.reserved_routes)
self.routes.append((re.compile(rr_group).pattern, self.RouteHandler))
tornado.web.Application.__init__(self, self.routes)
server = tornado.httpserver.HTTPServer(self)
try:
server.bind(API_PORT)
except Exception as e:
if DEBUG: print "** FAILED TO START UP ON PORT %d\n%s" % (API_PORT, e)
from fabric.api import settings, local
from fabric.context_managers import hide
with settings(warn_only=True):
local("kill $(lsof -t -i:%d)" % API_PORT)
server.bind(API_PORT)
startDaemon(self.api_log_file, self.api_pid_file)
server.start(NUM_PROCESSES)
tornado.ioloop.IOLoop.instance().start()
def stopRESTAPI(self):
print "shutting down REST API"
stopDaemon(self.api_pid_file, extra_pids_port=API_PORT)
def shutdown(self):
self.stopWorker()
self.stopElasticsearch()
self.stopRESTAPI()
def startup(self):
argv.pop()
p = Process(target=self.startWorker)
p.start()
p = Process(target=self.startRESTAPI)
p.start()
p = Process(target=self.startElasticsearch)
p.start()
if __name__ == "__main__":
if len(argv) != 2: exit("Usage: unveillance_annex.py [-start, -stop, -restart]")
if argv[1] == "-config":
from Utils.funcs import exportFrontendConfig
exportFrontendConfig()
exit(0)
os.chdir(ANNEX_DIR)
unveillance_annex = UnveillanceAnnex()
if argv[1] == "-start" or argv[1] == "-firstuse":
unveillance_annex.startup()
elif argv[1] == "-stop":
unveillance_annex.shutdown()
elif argv[1] == "-restart":
unveillance_annex.shutdown()
sleep(5)
unveillance_annex.startup()