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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377 | @router.post(
"/collect/pip/",
response_model=TaskGroupActivityV2Read,
)
async def collect_tasks_pip(
request: Request,
response: Response,
background_tasks: BackgroundTasks,
request_data: CollectionRequestData = Depends(parse_request_data),
private: bool = False,
user_group_id: Optional[int] = None,
user: UserOAuth = Depends(current_active_verified_user),
db: AsyncSession = Depends(get_async_db),
) -> TaskGroupActivityV2Read:
"""
Task-collection endpoint
"""
# Get settings
settings = Inject(get_settings)
# Get some validated request data
task_collect = request_data.task_collect
# Initialize task-group attributes
task_group_attrs = dict(
user_id=user.id,
origin=request_data.origin,
)
# Set/check python version
if task_collect.python_version is None:
task_group_attrs[
"python_version"
] = settings.FRACTAL_TASKS_PYTHON_DEFAULT_VERSION
else:
task_group_attrs["python_version"] = task_collect.python_version
try:
get_python_interpreter_v2(
python_version=task_group_attrs["python_version"]
)
except ValueError:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=(
f"Python version {task_group_attrs['python_version']} is "
"not available for Fractal task collection."
),
)
# Set pip_extras
if task_collect.package_extras is not None:
task_group_attrs["pip_extras"] = task_collect.package_extras
# Set pinned_package_versions
if task_collect.pinned_package_versions is not None:
task_group_attrs[
"pinned_package_versions"
] = task_collect.pinned_package_versions
# Initialize wheel_file_content as None
wheel_file = None
# Set pkg_name, version, origin and wheel_path
if request_data.origin == TaskGroupV2OriginEnum.WHEELFILE:
try:
wheel_filename = request_data.file.filename
wheel_info = _parse_wheel_filename(wheel_filename)
wheel_file_content = await request_data.file.read()
wheel_file = WheelFile(
filename=wheel_filename,
contents=wheel_file_content,
)
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=(
f"Invalid wheel-file name {wheel_filename}. "
f"Original error: {str(e)}",
),
)
task_group_attrs["pkg_name"] = normalize_package_name(
wheel_info["distribution"]
)
task_group_attrs["version"] = wheel_info["version"]
elif request_data.origin == TaskGroupV2OriginEnum.PYPI:
pkg_name = task_collect.package
task_group_attrs["pkg_name"] = normalize_package_name(pkg_name)
latest_version = await get_package_version_from_pypi(
task_collect.package,
task_collect.package_version,
)
task_group_attrs["version"] = latest_version
# Validate query parameters related to user-group ownership
user_group_id = await _get_valid_user_group_id(
user_group_id=user_group_id,
private=private,
user_id=user.id,
db=db,
)
# Set user_group_id
task_group_attrs["user_group_id"] = user_group_id
# Validate user settings (backend-specific)
user_settings = await validate_user_settings(
user=user, backend=settings.FRACTAL_RUNNER_BACKEND, db=db
)
# Set path and venv_path
if settings.FRACTAL_RUNNER_BACKEND == "slurm_ssh":
base_tasks_path = user_settings.ssh_tasks_dir
else:
base_tasks_path = settings.FRACTAL_TASKS_DIR.as_posix()
task_group_path = (
Path(base_tasks_path)
/ str(user.id)
/ task_group_attrs["pkg_name"]
/ task_group_attrs["version"]
).as_posix()
task_group_attrs["path"] = task_group_path
task_group_attrs["venv_path"] = Path(task_group_path, "venv").as_posix()
# Validate TaskGroupV2 attributes
try:
TaskGroupCreateV2Strict(**task_group_attrs)
except ValidationError as e:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=f"Invalid task-group object. Original error: {e}",
)
# Database checks
# Verify non-duplication constraints
await _verify_non_duplication_user_constraint(
user_id=user.id,
pkg_name=task_group_attrs["pkg_name"],
version=task_group_attrs["version"],
db=db,
)
await _verify_non_duplication_group_constraint(
user_group_id=task_group_attrs["user_group_id"],
pkg_name=task_group_attrs["pkg_name"],
version=task_group_attrs["version"],
db=db,
)
# Verify that task-group path is unique
stm = select(TaskGroupV2).where(TaskGroupV2.path == task_group_path)
res = await db.execute(stm)
for conflicting_task_group in res.scalars().all():
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=(
f"Another task-group already has path={task_group_path}.\n"
f"{conflicting_task_group=}"
),
)
# On-disk checks
if settings.FRACTAL_RUNNER_BACKEND != "slurm_ssh":
# Verify that folder does not exist (for local collection)
if Path(task_group_path).exists():
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=f"{task_group_path} already exists.",
)
# Create TaskGroupV2 object
task_group = TaskGroupV2(**task_group_attrs)
db.add(task_group)
await db.commit()
await db.refresh(task_group)
db.expunge(task_group)
# All checks are OK, proceed with task collection
task_group_activity = TaskGroupActivityV2(
user_id=task_group.user_id,
taskgroupv2_id=task_group.id,
status=TaskGroupActivityStatusV2.PENDING,
action=TaskGroupActivityActionV2.COLLECT,
pkg_name=task_group.pkg_name,
version=task_group.version,
)
db.add(task_group_activity)
await db.commit()
await db.refresh(task_group_activity)
logger = set_logger(logger_name="collect_tasks_pip")
# END of SSH/non-SSH common part
if settings.FRACTAL_RUNNER_BACKEND == "slurm_ssh":
# SSH task collection
# Use appropriate FractalSSH object
ssh_credentials = dict(
user=user_settings.ssh_username,
host=user_settings.ssh_host,
key_path=user_settings.ssh_private_key_path,
)
fractal_ssh_list = request.app.state.fractal_ssh_list
fractal_ssh = fractal_ssh_list.get(**ssh_credentials)
background_tasks.add_task(
collect_ssh,
task_group_id=task_group.id,
task_group_activity_id=task_group_activity.id,
fractal_ssh=fractal_ssh,
tasks_base_dir=user_settings.ssh_tasks_dir,
wheel_file=wheel_file,
)
else:
# Local task collection
background_tasks.add_task(
collect_local,
task_group_id=task_group.id,
task_group_activity_id=task_group_activity.id,
wheel_file=wheel_file,
)
logger.debug(
"Task-collection endpoint: start background collection "
"and return task_group_activity"
)
reset_logger_handlers(logger)
response.status_code = status.HTTP_202_ACCEPTED
return task_group_activity
|