예제 #1
0
def create_image(request, *args, **kwargs):
    if not (request.user and request.user.is_authenticated()):
        raise PermissionDenied()

    if not request.FILES.get('file'):
        raise ParseError(detail="There is no image build file.")

    assert 'pid' in kwargs
    pid = kwargs['pid']

    data = request.POST
    data['project'] = pid
    data['user'] = request.user.id

    # create image metadata
    serializer = ImageSerializer(data=data)
    serializer.is_valid(raise_exception=True)

    images = Image.objects.filter(project__id=pid, name=data['name'],
        version=data['version'])
    if images:
        raise ValidationError(detail="Already has an image called {}."
            .format(data['name']))
    serializer.save()

    image = serializer.data

    is_image = is_image_or_dockerfile(data.get('is_image', None))
    dockerfile = None
    old_image_name = None
    old_image_version = None
    if is_image == 0:
        dockerfile = data.get('dockerfile', 'Dockerfile')
    if is_image == 1:
        old_image_name = data.get('old_image_name', image['name'])
        old_image_version = data.get('old_image_version', image['version'])

    filename = get_upload_image_filename(image, request.user)

    save_upload_file_to_disk(request.FILES['file'], filename)

    # create a true image instance, and upload into private registry
    builder = ImageBuilder(
        build_file=filename,
        is_image=is_image,
        dockerfile=dockerfile,
        image_id=image['id'],
        old_image_name=old_image_name,
        old_image_version=old_image_version
        )
    builder.create_image()

    return JsonResponse({"detail": "success"})
예제 #2
0
파일: views.py 프로젝트: Huangu0/hummer
def create_image(request, *args, **kwargs):
    if not (request.user and request.user.is_authenticated()):
        raise PermissionDenied()

    if not request.FILES.get('file'):
        raise ParseError(detail="There is no image build file.")

    assert 'pid' in kwargs
    pid = kwargs['pid']

    data = request.POST
    data['project'] = pid
    data['user'] = request.user.id

    # create image metadata
    serializer = ImageSerializer(data=data)
    serializer.is_valid(raise_exception=True)

    images = Image.objects.filter(project__id=pid, name=data['name'],
        version=data['version'])
    if images:
        raise ValidationError(detail="Already has an image called {}."
            .format(data['name']))
    serializer.save()

    image = serializer.data

    is_image = is_image_or_dockerfile(data.get('is_image', None))
    dockerfile = None
    old_image_name = None
    old_image_version = None
    if is_image == 0:
        dockerfile = data.get('dockerfile', 'Dockerfile')
    if is_image == 1:
        old_image_name = data.get('old_image_name', image['name'])
        old_image_version = data.get('old_image_version', image['version'])

    filename = get_upload_image_filename(image, request.user)

    save_upload_file_to_disk(request.FILES['file'], filename)

    # create a true image instance, and upload into private registry
    builder = ImageBuilder(
        build_file=filename,
        is_image=is_image,
        dockerfile=dockerfile,
        image_id=image['id'],
        old_image_name=old_image_name,
        old_image_version=old_image_version
        )
    builder.create_image()

    return JsonResponse({"detail": "success"})
예제 #3
0
    def create(self, request, *args, **kwargs):
        """
        Create an image instance.
        """
        logger.info("user %s will create a new image." % request.user.username)

        if not request.FILES.get('file'):
            raise ParseError(detail="There is no image build file.")

        assert 'pid' in self.kwargs
        pid = self.kwargs['pid']

        data = request.data
        data['project'] = pid
        data['user'] = request.user.id

        logger.debug(data)
        # create image metadata
        serializer = self.get_serializer(data=data)
        serializer.is_valid(raise_exception=True)

        images = Image.objects.filter(project__id=pid,
                                      name=data['name'],
                                      version=data['version'])
        if images:
            raise ValidationError(
                detail="Already has an image called {}:{}.".format(
                    name, version))

        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        response = Response(serializer.data,
                            status=status.HTTP_201_CREATED,
                            headers=headers)

        image = serializer.data

        is_image = is_image_or_dockerfile(request.data.get('is_image', None))
        dockerfile = None
        old_image_name = None
        old_image_version = None
        if is_image == 0:
            dockerfile = request.data.get('dockerfile', 'Dockerfile')
        if is_image == 1:
            old_image_name = request.data.get('old_image_name', image['name'])
            old_image_version = request.data.get('old_image_version',
                                                 image['version'])

        filename = get_upload_image_filename(image, request.user)

        save_upload_file_to_disk(request.FILES['file'], filename)

        # create a true image instance, and upload into private registry
        builder = ImageBuilder(build_file=filename,
                               is_image=is_image,
                               dockerfile=dockerfile,
                               image_id=image['id'],
                               old_image_name=old_image_name,
                               old_image_version=old_image_version)
        builder.create_image()

        return response
예제 #4
0
    def create(self, request, *args, **kwargs):
        """
        Create an image instance.
        """
        logger.info("user %s will create a new image." % request.user.username)

        if not request.FILES.get('file'):
            raise ParseError(detail="There is no image build file.")

        assert 'pid' in self.kwargs
        pid = self.kwargs['pid']

        data = request.data
        data['project'] = pid
        data['user'] = request.user.id

        logger.debug(data)
        # create image metadata
        serializer = self.get_serializer(data=data)
        serializer.is_valid(raise_exception=True)

        images = Image.objects.filter(project__id=pid, name=data['name'],
            version=data['version'])
        if images:
            raise ValidationError(detail="Already has an image called {}:{}."
                .format(name, version))

        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        response = Response(serializer.data, status=status.HTTP_201_CREATED,
            headers=headers)

        image = serializer.data

        is_image = is_image_or_dockerfile(request.data.get('is_image', None))
        dockerfile = None
        old_image_name = None
        old_image_version = None
        if is_image == 0:
            dockerfile = request.data.get('dockerfile', 'Dockerfile')
        if is_image == 1:
            old_image_name = request.data.get('old_image_name',
                image['name'])
            old_image_version = request.data.get('old_image_version',
                image['version'])

        filename = get_upload_image_filename(image, request.user)

        save_upload_file_to_disk(request.FILES['file'], filename)

        # create a true image instance, and upload into private registry
        builder = ImageBuilder(
            build_file=filename,
            is_image=is_image,
            dockerfile=dockerfile,
            image_id=image['id'],
            old_image_name=old_image_name,
            old_image_version=old_image_version
            )
        builder.create_image()

        return response