-
Notifications
You must be signed in to change notification settings - Fork 6
/
mock_connect.py
305 lines (245 loc) · 7.4 KB
/
mock_connect.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# Installation:
# virtualenv flask
# source flask/bin/activate
# pip install flask
# To run:
# FLASK_APP=mock_connect.py flask run --host=0.0.0.0
# Use the API key below (0123456789abcdef0123456789abcdef) in rsconnect-jupyter
import io
import tarfile
from datetime import datetime
from json import dumps, loads
from functools import wraps
from pprint import pprint
from flask import Flask, Blueprint, abort, after_this_request, g, request, url_for, jsonify
def error(code, reason):
def set_code(response):
response.status_code = code
return response
after_this_request(set_code)
return {
'error': reason
}
app = Flask(__name__)
apps = {}
app_id = 0
bundles = {}
bundle_id = 0
tasks = {}
task_id = 0
apikeys = {
'0123456789abcdef0123456789abcdef': 'admin'
}
users = {
'admin': {
"username": "admin",
"active_time": "2018-08-30T23:49:18.421238194Z",
"first_name": "Super",
"last_name": "User",
"locked": False,
"privileges": [
"add_users",
"add_vanities",
"change_app_permissions",
"change_apps",
"change_groups",
"change_usernames",
"change_users",
"change_variant_schedule",
"create_groups",
"edit_run_as",
"edit_runtime",
"lock_users",
"publish_apps",
"remove_apps",
"remove_groups",
"remove_users",
"remove_vanities",
"view_app_settings",
"view_apps"
],
"guid": "29a74070-2c13-4ef9-a898-cfc6bcf0f275",
"user_role": "administrator",
"updated_time": "2018-08-29T19:25:23.68280816Z",
"confirmed": True,
"created_time": "2018-08-29T19:25:23.68280816Z",
"password": "",
"email": "admin@example.com"
}
}
def authenticated(f):
@wraps(f)
def wrapper(*args, **kw):
auth = request.headers.get('Authorization')
if auth is None or not auth.startswith('Key '):
abort(401)
key = auth[4:]
if key not in apikeys:
abort(401)
g.user = users[apikeys[key]]
return f(*args, **kw)
return wrapper
def json(f):
@wraps(f)
def wrapper(*args, **kw):
return jsonify(f(*args, **kw))
return wrapper
def item_by_id(d):
def decorator(f):
@wraps(f)
def wrapper(id, *args, **kw):
item = d.get(id)
if item is None:
return dumps(error(404, 'Not found'))
return f(item, *args, **kw)
return wrapper
return decorator
api = Blueprint('api', __name__)
@app.route('/')
def index():
return '<html><body>Welcome to Mock Connect!</body></html>'
@api.route('me')
@authenticated
@json
def me():
return g.user
def timestamp():
return datetime.utcnow().replace(microsecond=0).isoformat() + 'Z'
@api.route('applications', methods=['GET', 'POST'])
@authenticated
@json
def applications():
if request.method == 'POST':
app = request.get_json(force=True)
name = app.get('name')
if name and [app for app in apps.values() if app.get('name') == name]:
return error(409, 'An object with that name already exists.')
global app_id
app_id = app_id + 1
app['id'] = app_id
app['url'] = '{0}content/{1}'.format(url_for('index', _external=True), app_id)
app['owner_username'] = g.user.get('username')
app['owner_first_name'] = g.user.get('first_name')
app['owner_last_name'] = g.user.get('last_name')
app['owner_email'] = g.user.get('email')
app['owner_locked'] = g.user.get('locked')
app['bundle_id'] = None
app['needs_config'] = True
app['access_type'] = None
app['description'] = ''
app['app_mode'] = None
app['created_time'] = timestamp()
app.setdefault('title', '')
apps[str(app_id)] = app
return app
else:
count = int(request.args.get('count', 10000))
search = request.args.get('search')
def match(app):
return (search is None or (app.get('title') or '').startswith(search))
matches = list(filter(match, apps.values()))[:count]
return {
'count': len(matches),
'total': len(matches),
'applications': matches,
}
@api.route('applications/<id>', methods=['GET', 'POST'])
@authenticated
@json
@item_by_id(apps)
def application(app):
if request.method == 'GET':
return app
else:
app.update(request.get_json(force=True))
return app
@api.route('applications/<id>/config')
@authenticated
@json
@item_by_id(apps)
def config(app):
return {
'config_url': '{0}content/apps/{1}'.format(url_for('index', _external=True), app['id'])
}
@api.route('applications/<id>/upload', methods=['POST'])
@authenticated
@json
@item_by_id(apps)
def upload(app):
global bundle_id
bundle_id = bundle_id + 1
ts = timestamp()
bundle = {
'id': bundle_id,
'app_id': app['id'],
'created_time': ts,
'updated_time': ts,
}
bundles[bundle_id] = (bundle, request.data)
return bundle
def read_bundle_file(tarball, filename):
bio = io.BytesIO(tarball)
with tarfile.open('r:gz', fileobj=bio) as tar:
return tar.extractfile(filename).read()
def read_manifest(tarball):
manifest_data = read_bundle_file(tarball, 'manifest.json').decode('utf-8')
return loads(manifest_data)
def read_html(tarball):
manifest = read_manifest(tarball)
meta = manifest['metadata']
filename = meta.get('primary_html') or meta.get('entrypoint')
return read_bundle_file(tarball, filename).decode('utf-8')
app_modes = {
'static': 4,
'jupyter-static': 7,
}
@api.route('applications/<id>/deploy', methods=['POST'])
@authenticated
@json
@item_by_id(apps)
def deploy(app):
bundle_id = request.get_json(force=True).get('bundle')
if bundle_id is None:
return error(400, 'bundle_id is required') # message and status code probably wrong
if bundle_id not in bundles:
return error(404, 'bundle %s not found' % bundle_id) # message and status code probably wrong
bundle, tarball = bundles[bundle_id]
manifest = read_manifest(tarball)
pprint(manifest)
old_app_mode = app['app_mode']
new_app_mode = app_modes[manifest['metadata']['appmode']]
if old_app_mode is not None and old_app_mode != new_app_mode:
return error(400, 'Cannot change app mode once deployed') # message and status code probably wrong
app['app_mode'] = new_app_mode
app['bundle_id'] = bundle_id
app['last_deployed_time'] = timestamp()
global task_id
task_id = task_id + 1
task = {
'id': task_id,
'user_id': 0,
'finished': True,
'code': 0,
'error': '',
'last_status': 0,
'status': ['Building static content', 'Deploying static content'],
}
tasks[str(task_id)] = task
return task
@api.route('tasks/<id>')
@authenticated
@json
@item_by_id(tasks)
def get_task(task):
return task
@api.route('server_settings')
@json
def server_settings():
# for our purposes, any non-error response will do
return {}
@app.route('/content/apps/<id>')
@item_by_id(apps)
def content(app):
bundle, tarball = bundles[app['bundle_id']]
return read_html(tarball)
app.register_blueprint(api, url_prefix='/__api__')