async def cancel_import(request: Request, import_id: int): get_user(request) import_obj = await Import.filter(id=import_id, cancelled=False).get_or_none() if not import_obj: raise HTTPException(404, detail="import does not exist or is already cancelled") import_obj.status = ImportStatus.CANCELLED await import_obj.save() return {}
async def cancel_build(request: Request, build_id: int): get_user(request) build_obj = await Build.filter(id=build_id, cancelled=False).get_or_none() if not build_obj: raise HTTPException( 404, detail="build does not exist or is already cancelled") build_obj.status = BuildStatus.CANCELLED await build_obj.save() return {}
async def cancel_batch_import(request: Request, batch_import_id: int): get_user(request) batch_import_obj = await BatchImport.filter( id=batch_import_id).prefetch_related("imports").get_or_none() if not batch_import_obj: raise HTTPException(404, detail="batch import does not exist") for import_ in batch_import_obj.imports: import_.status = ImportStatus.CANCELLED await import_.save() return {}
async def merge_batch_build(request: Request, batch_build_id: int): get_user(request) batch_build_obj = await BatchBuild.filter( id=batch_build_id).prefetch_related("builds").get_or_none() if not batch_build_obj: raise HTTPException(404, detail="batch build does not exist") for build in batch_build_obj.builds: if build.scratch and not build.scratch_merged: await merge_scratch_task(build.id) return {}
async def cancel_batch_build(request: Request, batch_build_id: int): get_user(request) batch_build_obj = await BatchBuild.filter( id=batch_build_id).prefetch_related("builds").get_or_none() if not batch_build_obj: raise HTTPException(404, detail="batch build does not exist") for build in batch_build_obj.builds: build.status = BuildStatus.CANCELLED await build.save() return {}
async def batch_import_package(request: Request, body: BatchImportRequest): get_user(request) if body.should_precheck: await batch_list_check(body.packages) batch = await BatchImport.create() for build_request in body.packages: await import_package_route( request, dict(**dict(build_request), allow_stream_branches=body.allow_stream_branches), batch.id) return NewBatchResponse(id=batch.id)
async def merge_build(request: Request, build_id: int): get_user(request) build_obj = await Build.filter(id=build_id, scratch_merged=False, scratch=True).get_or_none() if not build_obj: raise HTTPException( 404, detail= "build does not exist, is already merged or not a scratch build") await merge_scratch_task(build_obj.id) return {}
async def retry_failed_batch_imports(request: Request, batch_import_id: int): get_user(request) batch_import_obj = await BatchImport.filter( id=batch_import_id).prefetch_related("imports", "imports__package").get_or_none() if not batch_import_obj: raise HTTPException(404, detail="batch import does not exist") packages = [{ "package_id": b.package.id } for b in batch_import_obj.imports if b.status == ImportStatus.CANCELLED or b.status == ImportStatus.FAILED] return await batch_import_package(request, BatchImportRequest(packages=packages))
async def retry_failed_batch_builds(request: Request, batch_build_id: int): get_user(request) batch_build_obj = await BatchBuild.filter( id=batch_build_id).prefetch_related("builds", "builds__package").get_or_none() if not batch_build_obj: raise HTTPException(404, detail="batch build does not exist") packages = [ { "package_id": b.package.id } for b in batch_build_obj.builds if b.status == BuildStatus.CANCELLED or b.status == BuildStatus.FAILED ] return await batch_queue_build(request, BatchBuildRequest(packages=packages))
async def batch_queue_build(request: Request, body: BatchBuildRequest): get_user(request) if body.should_precheck: await batch_list_check(body.packages, True) batch = await BatchBuild.create() for build_request in body.packages: await queue_build( request, dict(**dict(build_request), ignore_modules=body.ignore_modules, scratch=body.scratch, arch_override=body.arch_override, force_tag=body.force_tag), batch.id) return NewBatchResponse(id=batch.id)
async def put_file_in_lookaside(request: Request, file: UploadFile = File(...)): user = get_user(request) file_content = await file.read() await file.seek(0) sha256sum = hashlib.sha256(file_content).hexdigest() lookaside_session.upload(file.file, sha256sum) await LookasideBlob.create(sum=sha256sum, executor_username=user["preferred_username"]) return LookasideUploadResponse(sha256sum=sha256sum)
async def import_package_route(request: Request, body: Dict[str, ImportRequest], batch_import_id: Optional[int] = None): user = get_user(request) filters = gen_body_filters(body) package = await Package.filter(**filters).get_or_none() if not package: raise HTTPException(404, detail="package does not exist") if package.repo == Repo.MODULAR_CANDIDATE: raise HTTPException(401, detail="modular subpackages cannot be imported") build_order = await create_import_order(package, user["preferred_username"], batch_import_id) await import_package_task(build_order[0][0], build_order[0][1], build_order[1:], body.get("allow_stream_branches") or False) return {}
async def import_from_koji(request: Request): user = get_user(request) all_koji_builds = koji_session.listBuilds() packages_without_builds = await Package.filter(last_build__isnull=True ).all() for package in packages_without_builds: for koji_build in all_koji_builds: if package.name == koji_build[ "name"] and not package.is_module and not package.part_of_module and \ package.repo != Repo.MODULAR_CANDIDATE and koji_build["state"] == 1: await import_build_from_koji(user["preferred_username"], package, koji_build) return {}
async def bootstrap_repo(request: Request, repo: Repo): user = get_user(request) await process_repo_dump(repo, user["preferred_username"]) return JSONResponse(content={})
async def bootstrap_modules(request: Request): user = get_user(request) await process_module_dump(user["preferred_username"]) return JSONResponse(content={})
async def queue_build(request: Request, body: Dict[str, BuildRequest], batch_build_id: Optional[int] = None): user = get_user(request) filters = gen_body_filters(body) package = await Package.filter(**filters).get_or_none() if not package: raise HTTPException(404, detail="package does not exist") if package.repo == Repo.MODULAR_CANDIDATE: raise HTTPException( 400, detail="modular subpackages cannot be built, build the main module" ) extras = {} token = None if body.get("force_tag"): extras["force_tag"] = body.get("force_tag") if body.get("scratch"): extras["scratch"] = True latest_build = await Build.filter( package_id=package.id, status=BuildStatus.SUCCEEDED ).prefetch_related("import_commit").order_by("-created_at").first() if not latest_build: return {} import_commits = [latest_build.import_commit] else: filters = {"package_id": package.id} if body.get("ignore_modules"): filters["module"] = False latest_import = await Import.filter(**filters ).order_by("-created_at").first() import_commits = await ImportCommit.filter(import__id=latest_import.id ).all() if body.get("arch_override"): extras["arch_override"] = body.get("arch_override") only_branch = body.get("only_branch") for import_commit in import_commits: if "-beta" not in import_commit.branch: if only_branch and import_commit.branch != only_branch: continue stream_branch_prefix = f"{settings.original_import_branch_prefix}{settings.version}-stream" if import_commit.branch.startswith(stream_branch_prefix): if body.get("ignore_modules"): continue if package.part_of_module and not package.is_module: continue extras["mbs"] = True if not token: token = request.session.get("token") # temporarily skip containeronly streams containeronly_stream_prefix = f"{settings.original_import_branch_prefix}{settings.version}-containeronly-stream" if import_commit.branch.startswith(containeronly_stream_prefix): continue build = await Build.create( package_id=package.id, status=BuildStatus.QUEUED, executor_username=user["preferred_username"], point_release= f"{settings.version}_{settings.default_point_release}", import_commit_id=import_commit.id, **extras) if batch_build_id: await BatchBuildPackage.create(build_id=build.id, batch_build_id=batch_build_id) await build_package_task(package.id, build.id, token) if only_branch: break return {}