Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Do not pre-migrate schema1 docker tags when there are 2 tags with sam…
Browse files Browse the repository at this point in the history
…e name witin a repo.

closes #6234
https://pulp.plan.io/issues/6234
  • Loading branch information
ipanova committed Mar 4, 2020
1 parent 35712d7 commit 76423ee
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/6234.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not pre-migrate schema1 docker tags when there are 2 tags with same name witin a repo.
5 changes: 5 additions & 0 deletions pulp_2to3_migration/app/plugin/docker/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.db import IntegrityError

from . import pulp2_models
from . import utils

from .pulp_2to3_models import (
Pulp2Blob,
Expand Down Expand Up @@ -75,6 +76,10 @@ class DockerMigrator(Pulp2to3PluginMigrator):
'docker_distributor_web': DockerDistributor,
}

premigrate_hook = {
'docker_tag': utils.find_tags
}

@classmethod
async def migrate_content_to_pulp3(cls):
"""
Expand Down
20 changes: 20 additions & 0 deletions pulp_2to3_migration/app/plugin/docker/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from . import pulp2_models


def find_tags():
"""
Find tags that have same name within the repo.
Return only one tag out of 2 tags with the same name.
Prefer schema2 over schema1.
"""

# sort the schema version in desc mode.
sort_stage = {'$sort': {'schema_version': -1}}
# group tags by name and repo_id; take just first result out of the 2 tags with the same name
group_stage1 = {'$group': {'_id': {'name': '$name', 'repo_id': '$repo_id'},
'tags_id': {'$first': '$_id'}}}
group_stage2 = {'$group': {'_id': None, 'tags_ids': {'$addToSet': '$tags_id'}}}
result = pulp2_models.Tag.objects.aggregate([sort_stage, group_stage1, group_stage2])
if result._has_next():
return result.next()['tags_ids']
return []
2 changes: 2 additions & 0 deletions pulp_2to3_migration/app/plugin/iso/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class IsoMigrator(Pulp2to3PluginMigrator):
'iso_distributor': IsoDistributor,
}

premigrate_hook = {}

@classmethod
async def migrate_content_to_pulp3(cls):
"""
Expand Down
17 changes: 13 additions & 4 deletions pulp_2to3_migration/app/pre_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ async def pre_migrate_all_content(plan):
pulp_2to3_detail=pulp_2to3_detail_model)
# identify wether the content is mutable
mutable_type = content_model.pulp2.TYPE_ID in plugin.migrator.mutable_content_models
pre_migrators.append(pre_migrate_content(content_model, mutable_type))
# check if the content type has a premigration hook
premigrate_hook = None
if content_model.pulp2.TYPE_ID in plugin.migrator.premigrate_hook:
premigrate_hook = plugin.migrator.premigrate_hook[content_model.pulp2.TYPE_ID]
pre_migrators.append(pre_migrate_content(content_model, mutable_type, premigrate_hook))

_logger.debug('Pre-migrating Pulp 2 content')
await asyncio.wait(pre_migrators)


async def pre_migrate_content(content_model, mutable_type):
async def pre_migrate_content(content_model, mutable_type, premigrate_hook):
"""
A coroutine to pre-migrate Pulp 2 content, including all details for on_demand content.
Expand All @@ -81,8 +85,13 @@ async def pre_migrate_content(content_model, mutable_type):
type=content_type,
timestamp=last_updated))

# query only newly created/updated items
mongo_content_qs = content_model.pulp2.objects(_last_updated__gte=last_updated)
if premigrate_hook:
pulp2_content_ids = premigrate_hook()
mongo_content_qs = content_model.pulp2.objects(
_last_updated__gte=last_updated, id__in=pulp2_content_ids)
else:
# query only newly created/updated items
mongo_content_qs = content_model.pulp2.objects(_last_updated__gte=last_updated)
total_content = mongo_content_qs.count()
_logger.debug('Total count for {type} content to migrate: {total}'.format(
type=content_type,
Expand Down

0 comments on commit 76423ee

Please sign in to comment.