-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.py
executable file
·255 lines (213 loc) · 10.5 KB
/
application.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
import json
import os
import tempfile
import uuid
import boto3
from flask import abort, Flask, request, send_file
from globus_sdk import ConfidentialAppAuthClient
from container_handler import convert_definition_file, pull_container
from pg_utils import build_schema, create_table_entry, prep_database, select_by_column, table_exists, update_table_entry
from sqs_queue_utils import put_message
from task_manager import TaskManager
application = Flask(__name__)
manager = TaskManager(max_threads=11, kill_time=10)
manager.start_prune_thread(10)
@application.route("/change_thread", methods=["POST"])
def change_thread():
global manager
manager = TaskManager(max_threads=request.json["threads"])
return "k"
@application.before_first_request
def config():
if not(table_exists("definition") and table_exists('build')):
prep_database()
@application.route('/thread')
def thread():
return json.dumps(manager.thread_status)
@application.route('/')
def index():
global manager
return str(manager.max_threads)
@application.route('/upload_def_file', methods=["POST"])
def upload_file():
if 'Authorization' not in request.headers:
abort(401, 'You must be logged in to perform this function.')
token = request.headers.get('Authorization')
token = str.replace(str(token), 'Bearer ', '')
conf_app = ConfidentialAppAuthClient(os.environ["GL_CLIENT"], os.environ["GL_CLIENT_SECRET"])
intro_obj = conf_app.oauth2_token_introspect(token)
if "client_id" in intro_obj:
client_id = str(intro_obj["client_id"])
if 'file' not in request.files:
abort(400, "No file")
file = request.files['file']
if file.filename == '':
abort(400, "No file selected")
if file:
filename = file.filename
definition_id = str(uuid.uuid4())
create_table_entry("definition",
definition_id=definition_id,
definition_type="docker" if filename == "Dockerfile" else "singularity",
definition_name=filename,
location="s3",
definition_owner=client_id)
s3 = boto3.client('s3')
s3.upload_fileobj(file, "xtract-container-service",
f'{definition_id}/{filename}')
return definition_id
else:
return abort(400, "Failed to upload file")
else:
abort(400, "Failed to authenticate user")
@application.route('/build', methods=["POST", "GET"])
def build():
if 'Authorization' not in request.headers:
abort(401, "You must be logged in to perform this function.")
token = request.headers.get('Authorization')
token = str.replace(str(token), 'Bearer ', '')
conf_app = ConfidentialAppAuthClient(os.environ["GL_CLIENT"], os.environ["GL_CLIENT_SECRET"])
intro_obj = conf_app.oauth2_token_introspect(token)
if "client_id" in intro_obj:
client_id = str(intro_obj["client_id"])
if request.method == "POST":
params = request.json
required_params = {"definition_id", "to_format", "container_name"}
if set(params.keys()) >= required_params and params["to_format"] in ["docker", "singularity"]:
definition_entry = select_by_column("definition", definition_id=params["definition_id"])
if definition_entry is not None and len(definition_entry) == 1:
definition_entry = definition_entry[0]
if definition_entry["definition_owner"] != client_id:
abort(400, "You don't have permission to use this definition file")
else:
build_entry = select_by_column("build", definition_id=params["definition_id"],
container_type=params["to_format"])
if build_entry is not None and len(build_entry) == 1:
build_entry = build_entry[0]
build_id = build_entry["build_id"]
build_entry["build_status"] = "pending"
update_table_entry("build", build_id, **build_entry)
else:
build_id = str(uuid.uuid4())
build_entry = build_schema
build_entry["build_id"] = build_id if build_id is not None else str(uuid.uuid4())
build_entry["container_name"] = params["container_name"]
build_entry["definition_id"] = params["definition_id"]
build_entry["container_type"] = params["to_format"]
build_entry["container_owner"] = client_id
build_entry["build_status"] = "pending"
create_table_entry("build", **build_entry)
put_message({"function_name": "build_container",
"build_entry": build_entry,
"to_format": params["to_format"],
"container_name": params["container_name"]})
manager.start_thread()
return build_id
else:
abort(400, f"""No definition DB entry for {params["definition_id"]}""")
else:
abort(400, f"Missing {set(params.keys())} parameters")
elif request.method == "GET":
build_entry = select_by_column("build", container_owner=client_id,
build_id=request.json["build_id"])
if build_entry is not None and len(build_entry) == 1:
return build_entry[0]
else:
abort(400, "Build ID not valid")
else:
abort(400, "Failed to authenticate user")
@application.route('/pull', methods=["GET"])
def pull():
if 'Authorization' not in request.headers:
abort(401, 'You must be logged in to perform this function.')
token = request.headers.get('Authorization')
token = str.replace(str(token), 'Bearer ', '')
conf_app = ConfidentialAppAuthClient(os.environ["GL_CLIENT"], os.environ["GL_CLIENT_SECRET"])
intro_obj = conf_app.oauth2_token_introspect(token)
if "client_id" in intro_obj:
client_id = intro_obj["client_id"]
params = request.json
if "build_id" in params:
build_id = params["build_id"]
build_entry = select_by_column("build", build_id=build_id)
if build_entry is not None and len(build_entry) == 1:
build_entry = build_entry[0]
if build_entry["container_owner"] != client_id:
abort(400, "You do not have access to this definition file")
else:
abort(400, "Invalid build ID")
try:
file_name = pull_container(build_entry)
response = send_file(os.path.basename(file_name))
if os.path.exists(file_name):
os.remove(file_name)
return response
except Exception as e:
file_name = build_id + (".tar" if build_entry["container_type"] == "docker" else ".sif")
if os.path.exists(file_name):
os.remove(file_name)
print(f"Exception {e}")
abort(400, f"Failed to pull {build_id}")
else:
abort(400, "No build ID")
else:
abort(400, "Failed to authenticate user")
@application.route('/repo2docker', methods=["POST"])
def repo2docker():
if 'Authorization' not in request.headers:
abort(401, 'You must be logged in to perform this function.')
token = request.headers.get('Authorization')
token = str.replace(str(token), 'Bearer ', '')
conf_app = ConfidentialAppAuthClient(os.environ["GL_CLIENT"], os.environ["GL_CLIENT_SECRET"])
intro_obj = conf_app.oauth2_token_introspect(token)
if "client_id" in intro_obj:
client_id = str(intro_obj["client_id"])
build_id = str(uuid.uuid4())
if request.json is not None and "git_repo" in request.json and "container_name" in request.json:
put_message({"function_name": "repo2docker_container",
"client_id": client_id, "build_id": build_id, "target": request.json["git_repo"],
"container_name": request.json["container_name"]})
manager.start_thread()
return build_id
elif 'file' in request.files:
file = request.files['file']
if file.filename == '':
abort(400, "No file selected")
if file:
file_path = tempfile.mkstemp()[1]
with open(file_path, "wb") as f:
f.write(file.read())
put_message({"function_name": "repo2docker_container",
"client_id": client_id, "build_id": build_id, "target": file_path,
"container_name": file.filename})
manager.start_thread()
return build_id
else:
return abort(400, "Failed to upload file")
else:
abort(400, "No git repo or file")
else:
abort(400, "Failed to authenticate user")
@application.route('/convert', methods=["POST"])
def convert():
if 'Authorization' not in request.headers:
abort(401, 'You must be logged in to perform this function.')
token = request.headers.get('Authorization')
token = str.replace(str(token), 'Bearer ', '')
conf_app = ConfidentialAppAuthClient(os.environ["GL_CLIENT"], os.environ["GL_CLIENT_SECRET"])
intro_obj = conf_app.oauth2_token_introspect(token)
if "client_id" in intro_obj:
client_id = str(intro_obj["client_id"])
definition_entry = select_by_column("definition", definition_id=request.json["definition_id"])
if definition_entry is not None and len(definition_entry) == 1:
definition_entry = definition_entry[0]
if definition_entry["definition_owner"] != client_id:
abort(400, "You don't have permission to use this definition file")
else:
return convert_definition_file(definition_entry)
else:
abort(400, "Definition ID not valid")
else:
abort(400, "Failed to authenticate user")
if __name__ == "__main__":
application.run(debug=False, threaded=True)