Navigation Menu

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

Commit

Permalink
Add model and dispatch a basic task for the migration tool
Browse files Browse the repository at this point in the history
  • Loading branch information
goosemania committed Jul 2, 2019
1 parent 79bebc0 commit c1829f9
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 73 deletions.
7 changes: 2 additions & 5 deletions README.md
Expand Up @@ -37,11 +37,8 @@ $ sudo mount <IP address of machine which exports /var/lib/pulp>:/var/lib/pulp /

### Installation

Clone repositories for the base tool and extensions for the plugins of interest, and install them.
Clone the repository and install it.
```
$ git clone https://github.com/pulp/pulp-2to3-migrate.git
$ pip install -e pulp-2to3-migrate
$
$ git clone https://github.com/pulp/pulp-2to3-migrate-iso.git
$ pip install -e pulp-2to3-migrate-iso
```
```
2 changes: 2 additions & 0 deletions pulp_2to3_migrate/app/constants.py
@@ -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 pulp_2to3_migrate/app/management/commands/pulp-2to3-migrate.py

This file was deleted.

9 changes: 5 additions & 4 deletions pulp_2to3_migrate/app/models.py
@@ -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()
50 changes: 50 additions & 0 deletions pulp_2to3_migrate/app/serializers.py
@@ -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.
13 changes: 13 additions & 0 deletions pulp_2to3_migrate/app/tasks/migrate.py
@@ -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)
52 changes: 52 additions & 0 deletions pulp_2to3_migrate/app/viewsets.py
@@ -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)
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -9,7 +9,7 @@

setup(
name='pulp-2to3-migrate',
version='0.0.1a1.dev1',
version='0.0.1a1.dev',
description='Pulp 2 to Pulp 3 migration tool',
license='GPLv2+',
author='Pulp Team',
Expand Down

0 comments on commit c1829f9

Please sign in to comment.