Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if not source_repo and not source_repo_version:
raise serializers.ValidationError(
_("Either the 'source_repo' or 'source_repo_version' need to be specified"))
if source_repo and source_repo_version:
raise serializers.ValidationError(
_("Either the 'source_repo' or 'source_repo_version' need to be specified "
"but not both.")
)
if not source_repo and source_repo_version:
repo = {'source_repo': source_repo_version.repository}
new_data.update(repo)
if source_repo and not source_repo_version:
version = RepositoryVersion.latest(source_repo)
if version:
repo_version = {'source_repo_version': version}
new_data.update(repo_version)
else:
raise serializers.ValidationError(
detail=_('Repository has no version available to copy'))
types = data.get('types')
final_types = []
if types:
for t in types:
substitution = RPM_PLUGIN_TYPE_CHOICE_MAP.get(t)
if not substitution:
raise serializers.ValidationError(_(
"'{type}' is an invalid type, please use one of {choices}".format(
def untag_image(tag, repository_pk):
"""
Create a new repository version without a specified manifest's tag name.
"""
repository = Repository.objects.get(pk=repository_pk)
latest_version = RepositoryVersion.latest(repository)
tags_in_latest_repository = latest_version.content.filter(
pulp_type="docker.tag"
)
tags_to_remove = Tag.objects.filter(
pk__in=tags_in_latest_repository,
name=tag
)
with RepositoryVersion.create(repository) as repository_version:
repository_version.remove_content(tags_to_remove)
1. must_remain: These content units are referenced by content units that will not be removed
2. to_remove: These content units are either explicity given by the user,
or they are referenced by the content explicity given, and they are not in must_remain.
3. to_remain: Content in the repo that is not in to_remove. This category
is used to determine must_remain of lower heirarchy content.
Args:
repository_pk (int): The primary key for a Repository for which a new Repository Version
should be created.
content_units (list): List of PKs for :class:`~pulpcore.app.models.Content` that
should be removed from the Repository.
"""
repository = Repository.objects.get(pk=repository_pk)
latest_version = RepositoryVersion.latest(repository)
latest_content = latest_version.content.all() if latest_version else Content.objects.none()
tags_in_repo = Q(pk__in=latest_content.filter(pulp_type='docker.tag'))
manifests_in_repo = Q(pk__in=latest_content.filter(pulp_type='docker.manifest'))
user_provided_content = Q(pk__in=content_units)
type_manifest_list = Q(media_type=MEDIA_TYPE.MANIFEST_LIST)
type_manifest = Q(media_type__in=[MEDIA_TYPE.MANIFEST_V1, MEDIA_TYPE.MANIFEST_V2])
blobs_in_repo = Q(pk__in=latest_content.filter(pulp_type='docker.blob'))
# Tags do not have must_remain because they are the highest level content.
tags_to_remove = Tag.objects.filter(user_provided_content & tags_in_repo)
tags_to_remain = Tag.objects.filter(tags_in_repo).exclude(pk__in=tags_to_remove)
tagged_manifests_must_remain = Q(
pk__in=tags_to_remain.values_list("tagged_manifest", flat=True)
)
tagged_manifests_to_remove = Q(pk__in=tags_to_remove.values_list("tagged_manifest", flat=True))
def validate(self, data):
"""
Validate data passed through a request call.
Check if a repository has got a reference to a latest repository version. A
new dictionary object is initialized by the passed data and altered by a latest
repository version.
"""
new_data = {}
new_data.update(data)
latest_version = RepositoryVersion.latest(data['repository'])
if not latest_version:
raise serializers.ValidationError(
_("The latest repository version of '{}' was not found"
.format(data['repository']))
)
new_data['latest_version'] = latest_version
return new_data
def get_repository_version(self):
"""
Returns the repository version that is supposed to be served by this DockerDistribution.
"""
if self.repository:
return RepositoryVersion.latest(self.repository)
elif self.repository_version:
return self.repository_version
else:
return None
def copy_content(source_repo_version_pk, dest_repo_pk, types):
"""
Copy content from one repo to another.
Args:
source_repo_version_pk: repository version primary key to copy units from
dest_repo_pk: repository primary key to copy units into
types: a tuple of strings representing the '_type' values of types to include in the copy
"""
source_repo_version = RepositoryVersion.objects.get(pk=source_repo_version_pk)
dest_repo = Repository.objects.get(pk=dest_repo_pk)
query = None
for ptype in types:
if query:
query = query | Q(_type=ptype)
else:
query = Q(_type=ptype)
content_to_copy = source_repo_version.content.filter(query)
with RepositoryVersion.create(dest_repo) as new_version:
new_version.add_content(content_to_copy)
resource = CreatedResource(content_object=manifest_tag)
resource.save()
ContentArtifact.objects.get_or_create(
artifact=artifact,
content=manifest_tag,
relative_path=tag
)
tags_to_add = Tag.objects.filter(
pk=manifest_tag.pk
).exclude(
pk__in=latest_version.content.all()
)
with RepositoryVersion.create(repository) as repository_version:
repository_version.remove_content(tags_to_remove)
repository_version.add_content(tags_to_add)
def create(self):
"""
Perform the work. This is the long-blocking call where all syncing occurs.
"""
with WorkingDirectory():
with RepositoryVersion.create(self.repository) as new_version:
loop = asyncio.get_event_loop()
stages = [
self.first_stage.gen_declarative_content,
query_existing_artifacts, artifact_downloader_factory(), artifact_saver,
query_existing_content_units, content_unit_saver,
content_unit_association_factory(new_version)
]
if self.sync_mode is 'additive':
stages.append(end_stage)
elif self.sync_mode is 'mirror':
stages.extend([content_unit_unassociation_factory(new_version), end_stage])
pipeline = create_pipeline(stages)
loop.run_until_complete(pipeline)
def create(self):
"""
Perform the work. This is the long-blocking call where all syncing occurs.
"""
with WorkingDirectory():
with RepositoryVersion.create(self.repository) as new_version:
loop = asyncio.get_event_loop()
stages = self.pipeline_stages(new_version)
stages.append(ContentUnitAssociation(new_version))
if self.mirror:
stages.append(ContentUnitUnassociation(new_version))
stages.append(EndStage())
pipeline = create_pipeline(stages)
loop.run_until_complete(pipeline)
class CopySerializer(serializers.Serializer):
"""
A serializer for Content Copy API.
"""
source_repo = serializers.HyperlinkedRelatedField(
help_text=_('A URI of the repository.'),
required=False,
queryset=RpmRepository.objects.all(),
view_name='repositories-rpm/rpm-detail',
)
source_repo_version = NestedRelatedField(
help_text=_('A URI of the repository version'),
required=False,
queryset=RepositoryVersion.objects.all(),
parent_lookup_kwargs={'repository_pk': 'repository__pk'},
lookup_field='number',
view_name='versions-detail',
)
dest_repo = serializers.HyperlinkedRelatedField(
help_text=_('A URI of the repository.'),
required=True,
queryset=RpmRepository.objects.all(),
view_name='repositories-rpm/rpm-detail',
)
types = serializers.ListField(
help_text=_('A list of types to copy ["package", "advisory"]'),
write_only=True,
default=['package', 'advisory']
)