forked from sidnarayanan/flask_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
232 lines (213 loc) · 7.54 KB
/
server.py
File metadata and controls
232 lines (213 loc) · 7.54 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from werkzeug.exceptions import HTTPException
from flask import request, Flask, abort, g
from os import system, path, getenv
from subprocess import check_output
import json
import MySQLdb as sql
from time import time, localtime, strftime
basedir = getenv('FLASK_BASE_DIR')
app = Flask('')
class BadInput(HTTPException):
code = 400
description = '<p>Malformed input.</p>'
class DBError(HTTPException):
code = 500
description = '<p>DB Error.</p>'
def timed(fn):
def _f():
start = time()
print fn.func_name,'starting:',strftime('%Y%m%d:%H:%M:%S', localtime(time()))
print 'data =', request.get_json()
x = fn()
print fn.func_name,'took:',time()-start,'s'
return x
_f.func_name = fn.func_name
return _f
# actions to be taken on push to Panda* repositories
# called through GitHub webhook
@app.route('/push', methods=['POST'])
@timed
def push():
doxy_basedir = basedir + '/doxygen/repos/'
data = request.get_json()
repo = data['repository']['git_url'].split('/')[-1].replace('.git','')
giturl = data['repository']['clone_url']
# actual process is off-loaded to bash script to run asynchronously
cmd = 'bash %s/doxygen/run.sh %s %s %s'%(basedir, basedir, repo, giturl)
print cmd
system(cmd + ' &')
return 'Success!\n'
# manage condor tasks through MySQLdb
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sql.connect(db='bird_watcher', user='snarayan')
return db
def get_cursor():
return get_db().cursor()
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
def query(cmd, args=()):
try:
cur = get_cursor()
cur.execute(cmd, args)
rv = cur.fetchall()
cur.close()
return rv
except Exception as e:
print str(e)
raise DBError
def geo(hostname):
try:
if hostname.endswith('mit.edu'):
if hostname.startswith('t3'):
return (42.364601,-71.102798)
elif 'bat' in hostname:
return (42.364601,-71.102798)
cmd = ['geoiplookup', hostname]
lines = check_output(cmd, shell=False).strip().split('\n')
if len(lines) == 0 or "can't resolve" in lines[0] or 'not found' in lines[0]:
return None, None
ll = lines[1].replace(',','').split()
return float(ll[-4]), float(ll[-3])
except:
print hostname
print lines
return 0,0
def get_host_id(hostname):
r = query('SELECT `id` FROM nodes WHERE host = %s', (hostname,))
if len(r) == 0:
lat,lon = geo(hostname)
get_cursor().execute('INSERT INTO nodes (host,lat,lon) VALUES (%s,%s,%s)', [hostname, lat, lon])
get_db().commit()
r = query('SELECT `id` FROM nodes WHERE host = %s', (hostname,))
return r[0][0]
def insert_missing_hosts():
hosts = list(set(query('SELECT `host` FROM jobs')))
for h in hosts:
if h is not None:
get_host_id(h)
@app.route('/condor/requestdata', methods=['POST'])
@timed
def condor_requestdata():
data = request.get_json()
try:
p = data['path']
now = time()
b = data['bytes'] / 1e6
if len(query('SELECT path FROM files WHERE path = %s', (p,))) > 0:
# two options here...either set just the file, or the entire dataset
# entire dataset
# get_cursor().execute('UPDATE files SET last_access = %s WHERE path LIKE %s',
# (now, '/'.join(p.split('/')[:-1])+'/%'))
# just the file
get_cursor().execute('UPDATE files SET last_access = %s WHERE path = %s', (now, p))
else:
get_cursor().execute('INSERT INTO files (path,last_access,mbytes) VALUES (%s,%s,%s)', (p, now, b))
get_db().commit()
return '0'
except KeyError:
raise BadInput
except sql.Error as e:
print str(e)
raise DBError
@app.route('/condor/query', methods=['GET'])
@timed
def condor_query():
# query db and return
task = request.args.get('task')
if not task:
raise BadInput
where = 'task = %s'
args=[task]
jid = request.args.get('job_id')
if jid:
where += ' AND job_id = %s'
args.append(jid)
payload = query('SELECT `arg`, `job_id`, `timestamp`, `starttime`, `host`, `exit_code` FROM jobs WHERE %s;'%where, args)
return json.dumps(payload)
@app.route('/condor/done', methods=['POST'])
@timed
def condor_done():
# use JSON payload to fill table
data = request.get_json()
try:
task = data['task']
timestamp = data['timestamp']
job_id = data['job_id']
exit_code = data.get('exit_code', 0)
if 'Kraken' in task:
return '0'
# see if we know the job
ids = query('SELECT `id` FROM jobs WHERE task = %s AND job_id = %s', (task, job_id))
if len(ids):
for i in ids:
get_cursor().execute('UPDATE jobs SET timestamp = %s, exit_code = %s WHERE id = %s',
(timestamp, exit_code, i))
get_db().commit()
return str(len(ids)) + '\n'
else:
records = [(task, arg, job_id, timestamp, None, None, exit_code) for arg in data['args']]
get_cursor().executemany('INSERT INTO jobs (task,arg,job_id,timestamp,starttime,host_id,exit_code) VALUES (%s,%s,%s,%s,%s,%s,%s)', records)
get_db().commit()
return str(len(records))+'\n'
except KeyError:
raise BadInput
except sql.Error as e:
print str(e)
raise DBError
@app.route('/condor/start', methods=['POST'])
@timed
def condor_start():
# use JSON payload to fill table
data = request.get_json()
try:
task = data['task']
starttime = data['starttime']
host = data['host']
host_id = get_host_id(host)
job_id = data['job_id']
# see if we know the job, i.e. something that got pre-empted and restarted
if False: # len(query('SELECT `job_id` FROM jobs WHERE task = %s AND job_id = %s', (task, job_id))):
for arg in data['args']:
get_cursor().execute(
'UPDATE jobs SET starttime = %s, host_id = %s WHERE task = %s AND job_id = %s AND arg = %s',
(starttime, host_id, task, job_id, arg)
)
get_db().commit()
return str(len(data['args'])) + '\n'
else:
records = [(task, arg, job_id, None, starttime, host_id) for arg in data['args']]
get_cursor().executemany(
'INSERT INTO jobs (task,arg,job_id,timestamp,starttime,host_id) VALUES (%s,%s,%s,%s,%s,%s)',
records)
get_db().commit()
return str(len(records))+'\n'
get_latlon(host)
except KeyError:
raise BadInput
except sql.Error as e:
print str(e)
raise DBError
@app.route('/condor/clean', methods=['POST'])
@timed
def condor_clean():
# use JSON payload to fill table
data = request.get_json()
try:
task = data['task']
if 'job_id' in data:
job_id = data['job_id']
get_cursor().execute('DELETE FROM jobs WHERE task=%s AND job_id=%s', (task,job_id))
else:
get_cursor().execute('DELETE FROM jobs WHERE task=%s', (task,))
get_db().commit()
return 'Cleaned\n'
except KeyError:
raise BadInput
except sql.Error:
print str(e)
raise DBError