|
| 1 | +from django.db import transaction |
| 2 | + |
| 3 | +from pulpcore.plugin.models import CreatedResource |
| 4 | + |
| 5 | +from pulp_container.app.models import ( |
| 6 | + ContainerDistribution, |
| 7 | + ContainerPushRepository, |
| 8 | + ContainerRepository, |
| 9 | +) |
| 10 | +from pulp_container.app.serializers import ContainerRepositorySerializer |
| 11 | + |
| 12 | + |
| 13 | +def migrate_push_repository(push_repository_pk, copy_versions=False): |
| 14 | + """ |
| 15 | + Convert a ContainerPushRepository into a ContainerRepository. |
| 16 | +
|
| 17 | + Creates a new container repository with the same name and metadata, copies |
| 18 | + content from the push repository, reassociates any distributions, and deletes |
| 19 | + the original push repository. |
| 20 | +
|
| 21 | + Args: |
| 22 | + push_repository_pk (str): The primary key for the push repository to migrate. |
| 23 | + copy_versions (bool): If True, copy the full repository version history. If False, |
| 24 | + only copy content from the latest repository version. |
| 25 | + """ |
| 26 | + push_repository = ContainerPushRepository.objects.get(pk=push_repository_pk) |
| 27 | + |
| 28 | + original_name = push_repository.name |
| 29 | + temp_name = f"{original_name}__migrating__{push_repository.pk}" |
| 30 | + |
| 31 | + with transaction.atomic(): |
| 32 | + push_repository.name = temp_name |
| 33 | + push_repository.save(update_fields=["name"]) |
| 34 | + |
| 35 | + container_repository = ContainerRepository( |
| 36 | + name=original_name, |
| 37 | + pulp_domain=push_repository.pulp_domain, |
| 38 | + pulp_labels=push_repository.pulp_labels, |
| 39 | + description=push_repository.description, |
| 40 | + retain_repo_versions=push_repository.retain_repo_versions, |
| 41 | + retain_checkpoints=push_repository.retain_checkpoints, |
| 42 | + user_hidden=push_repository.user_hidden, |
| 43 | + manifest_signing_service=push_repository.manifest_signing_service, |
| 44 | + ) |
| 45 | + container_repository.save() |
| 46 | + |
| 47 | + container_repository.pending_blobs.set(push_repository.pending_blobs.all()) |
| 48 | + container_repository.pending_manifests.set(push_repository.pending_manifests.all()) |
| 49 | + |
| 50 | + if copy_versions: |
| 51 | + for push_version in push_repository.versions.complete().order_by("number"): |
| 52 | + if push_version.number == 0 and not push_version.content.exists(): |
| 53 | + continue |
| 54 | + with container_repository.new_version() as new_version: |
| 55 | + new_version.set_content(push_version.content.all()) |
| 56 | + else: |
| 57 | + latest_version = push_repository.latest_version() |
| 58 | + if latest_version: |
| 59 | + with container_repository.new_version() as new_version: |
| 60 | + new_version.set_content(latest_version.content.all()) |
| 61 | + |
| 62 | + ContainerDistribution.objects.filter(repository=push_repository).update( |
| 63 | + repository=container_repository |
| 64 | + ) |
| 65 | + |
| 66 | + push_repository.delete() |
| 67 | + |
| 68 | + CreatedResource(content_object=container_repository).save() |
| 69 | + |
| 70 | + return ContainerRepositorySerializer( |
| 71 | + instance=container_repository, context={"request": None} |
| 72 | + ).data |
0 commit comments