This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add model and dispatch a basic task for the migration tool
closes #5003 https://pulp.plan.io/issues/5003
- Loading branch information
1 parent
79bebc0
commit c1829f9
Showing
9 changed files
with
125 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# for tasking system to ensure only one migration is run at a time | ||
PULP_2TO3_MIGRATION_RESOURCE = "pulp_2to3_migration" |
63 changes: 0 additions & 63 deletions
63
pulp_2to3_migrate/app/management/commands/pulp-2to3-migrate.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from django.db import models | ||
|
||
class Pulp2To3Map(models.Model): | ||
pulp2_id = models.UUIDField(primary_key=True) | ||
pulp3_id = models.UUIDField() | ||
type = models.CharField(max_length=255) | ||
from pulpcore.plugin.models import Model | ||
|
||
|
||
class MigrationPlan(Model): | ||
plan = models.TextField() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from gettext import gettext as _ | ||
|
||
from rest_framework import serializers | ||
|
||
from pulpcore.plugin.serializers import ( | ||
ModelSerializer, | ||
IdentityField | ||
) | ||
|
||
from .models import MigrationPlan | ||
|
||
class MigrationPlanSerializer(ModelSerializer): | ||
_href = IdentityField( | ||
view_name='migration-plans-detail' | ||
) | ||
|
||
plan = serializers.CharField( | ||
help_text= _('Migration Plan in JSON format'), | ||
required=True, | ||
) | ||
|
||
class Meta: | ||
fields = ModelSerializer.Meta.fields + ('plan', ) | ||
model = MigrationPlan | ||
|
||
|
||
def validate(self, data): | ||
""" | ||
Validate that the Serializer contains valid data. | ||
TODO: | ||
Validate JSON structure of migration_plan. | ||
Check validity of the JSON content: | ||
- migration for requested plugins is supported | ||
""" | ||
return data | ||
|
||
|
||
class MigrationPlanRunSerializer(serializers.Serializer): | ||
""" | ||
A serializer for running a migration plan. | ||
""" | ||
dry_run = serializers.BooleanField( | ||
help_text=_('If ``True``, performs validation of a Migration Plan only, no migration is ' | ||
'run. If ``False``, both validation and migration are run.'), | ||
required=False, | ||
default=False, | ||
write_only=True | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import time | ||
|
||
def migrate_from_pulp2(migration_plan_pk, dry_run=False): | ||
""" | ||
Main task to migrate from Pulp 2 to Pulp 3. | ||
Schedule other tasks based on the specified Migration Plan. | ||
Args: | ||
migration_plan_pk (str): The migration plan PK. | ||
dry_run (bool): If True, nothing is migrated, only validation happens. | ||
""" | ||
time.sleep(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from drf_yasg.utils import swagger_auto_schema | ||
from rest_framework import mixins | ||
from rest_framework.decorators import detail_route | ||
|
||
from pulpcore.plugin.serializers import AsyncOperationResponseSerializer | ||
from pulpcore.plugin.tasking import enqueue_with_reservation | ||
from pulpcore.plugin.viewsets import ( | ||
NamedModelViewSet, | ||
OperationPostponedResponse, | ||
) | ||
|
||
from .constants import PULP_2TO3_MIGRATION_RESOURCE | ||
from .models import MigrationPlan | ||
from .serializers import ( | ||
MigrationPlanSerializer, | ||
MigrationPlanRunSerializer, | ||
) | ||
from .tasks.migrate import migrate_from_pulp2 | ||
|
||
|
||
class MigrationPlanViewSet(NamedModelViewSet, | ||
mixins.CreateModelMixin, | ||
mixins.RetrieveModelMixin, | ||
mixins.DestroyModelMixin, | ||
mixins.ListModelMixin): | ||
endpoint_name = 'migration-plans' | ||
queryset = MigrationPlan.objects.all() | ||
serializer_class = MigrationPlanSerializer | ||
|
||
@swagger_auto_schema( | ||
operation_summary="Run migration plan", | ||
operation_description="Trigger an asynchronous task to run a migration from Pulp 2.", | ||
responses={202: AsyncOperationResponseSerializer} | ||
) | ||
@detail_route(methods=('post',), serializer_class=MigrationPlanRunSerializer) | ||
def run(self, request, pk): | ||
migration_plan = self.get_object() | ||
serializer = MigrationPlanRunSerializer( | ||
data=request.data, | ||
context={'request': request} | ||
) | ||
serializer.is_valid(raise_exception=True) | ||
dry_run = serializer.validated_data.get('dry_run', False) | ||
result = enqueue_with_reservation( | ||
migrate_from_pulp2, | ||
[PULP_2TO3_MIGRATION_RESOURCE], | ||
kwargs={ | ||
'migration_plan_pk': migration_plan.pk, | ||
'dry_run': dry_run | ||
} | ||
) | ||
return OperationPostponedResponse(result, request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters