diff --git a/CHANGES/6639.bugfix b/CHANGES/6639.bugfix new file mode 100644 index 00000000..1b254c00 --- /dev/null +++ b/CHANGES/6639.bugfix @@ -0,0 +1 @@ +Ensure that only one migration plan can be run at a time. diff --git a/pulp_2to3_migration/app/viewsets.py b/pulp_2to3_migration/app/viewsets.py index 5ca41211..a800f28e 100644 --- a/pulp_2to3_migration/app/viewsets.py +++ b/pulp_2to3_migration/app/viewsets.py @@ -1,7 +1,10 @@ from django_filters.rest_framework import filters +from gettext import gettext as _ + from drf_yasg.utils import swagger_auto_schema from rest_framework import mixins from rest_framework.decorators import action +from rest_framework.serializers import ValidationError from pulpcore.app.viewsets.base import DATETIME_FILTER_OPTIONS from pulpcore.app.viewsets.custom_filters import ( @@ -9,6 +12,7 @@ IsoDateTimeFilter ) +from pulpcore.plugin.models import Task from pulpcore.plugin.serializers import AsyncOperationResponseSerializer from pulpcore.plugin.tasking import enqueue_with_reservation from pulpcore.plugin.viewsets import ( @@ -57,6 +61,21 @@ def run(self, request, pk): validate = serializer.validated_data.get('validate', False) dry_run = serializer.validated_data.get('dry_run', False) + # find running/waiting migration plugin tasks + qs = Task.objects.filter(state__in=['waiting', 'running'], + reserved_resources_record__resource='pulp_2to3_migration') + if qs: + raise ValidationError(_("Only one migration plan can run at a time")) + groups_with_running_tasks = Task.objects.filter( + state__in=['waiting', 'running'], + task_group__isnull=False).values_list('task_group_id', flat=True) + groups_with_migration_tasks = Task.objects.filter( + task_group__isnull=False, + reserved_resources_record__resource='pulp_2to3_migration').values_list( + 'task_group_id', flat=True) + if groups_with_running_tasks.intersection(groups_with_migration_tasks): + raise ValidationError(_("Only one migration plan can run at a time")) + result = enqueue_with_reservation( migrate_from_pulp2, [PULP_2TO3_MIGRATION_RESOURCE],