From 76423ee780921e6cf612be5f5de12a6c425c4935 Mon Sep 17 00:00:00 2001 From: Ina Panova Date: Tue, 3 Mar 2020 14:20:55 +0100 Subject: [PATCH] Do not pre-migrate schema1 docker tags when there are 2 tags with same name witin a repo. closes #6234 https://pulp.plan.io/issues/6234 --- CHANGES/6234.bugfix | 1 + .../app/plugin/docker/migrator.py | 5 +++++ .../app/plugin/docker/utils.py | 20 +++++++++++++++++++ .../app/plugin/iso/migrator.py | 2 ++ pulp_2to3_migration/app/pre_migration.py | 17 ++++++++++++---- 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 CHANGES/6234.bugfix create mode 100644 pulp_2to3_migration/app/plugin/docker/utils.py diff --git a/CHANGES/6234.bugfix b/CHANGES/6234.bugfix new file mode 100644 index 00000000..bcbd4240 --- /dev/null +++ b/CHANGES/6234.bugfix @@ -0,0 +1 @@ +Do not pre-migrate schema1 docker tags when there are 2 tags with same name witin a repo. diff --git a/pulp_2to3_migration/app/plugin/docker/migrator.py b/pulp_2to3_migration/app/plugin/docker/migrator.py index 4494d2c0..cdfceee4 100644 --- a/pulp_2to3_migration/app/plugin/docker/migrator.py +++ b/pulp_2to3_migration/app/plugin/docker/migrator.py @@ -5,6 +5,7 @@ from django.db import IntegrityError from . import pulp2_models +from . import utils from .pulp_2to3_models import ( Pulp2Blob, @@ -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): """ diff --git a/pulp_2to3_migration/app/plugin/docker/utils.py b/pulp_2to3_migration/app/plugin/docker/utils.py new file mode 100644 index 00000000..61a09b53 --- /dev/null +++ b/pulp_2to3_migration/app/plugin/docker/utils.py @@ -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 [] diff --git a/pulp_2to3_migration/app/plugin/iso/migrator.py b/pulp_2to3_migration/app/plugin/iso/migrator.py index 76ea0bc8..200ae70a 100644 --- a/pulp_2to3_migration/app/plugin/iso/migrator.py +++ b/pulp_2to3_migration/app/plugin/iso/migrator.py @@ -44,6 +44,8 @@ class IsoMigrator(Pulp2to3PluginMigrator): 'iso_distributor': IsoDistributor, } + premigrate_hook = {} + @classmethod async def migrate_content_to_pulp3(cls): """ diff --git a/pulp_2to3_migration/app/pre_migration.py b/pulp_2to3_migration/app/pre_migration.py index 839d6399..7da50f85 100644 --- a/pulp_2to3_migration/app/pre_migration.py +++ b/pulp_2to3_migration/app/pre_migration.py @@ -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. @@ -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,