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"})
def upload_volume(request, *args, **kwargs): """ User upload volume data, delete the original data first. """ if not (request.user and request.user.is_authenticated()): raise PermissionDenied() user = request.user assert 'pid' in kwargs pid = kwargs['pid'] assert 'pk' in kwargs id = kwargs['pk'] volume = Volume.objects.get(project__id=pid, id=id) # Check whether the user is the member of this project if not check_member_in_project(volume.project, user): raise PermissionDenied(detail="User {} is not in project {}.".format( user.username, volume.project.name)) if not request.FILES.get('file'): raise ParseError(detail="There is no upload file.") logger.info("User {} upload files to volume {}-{}.".format( user.username, volume.project.name, volume.name)) filename = get_upload_volume_filename(volume, user) save_upload_file_to_disk(request.FILES['file'], filename) client = NFSLocalClient() volume_dir = get_volume_direction_on_nfs(volume) # Clear the dir first client.removedir(volume_dir) client.makedir(volume_dir) client.copy_file_to_remote_and_untar(filename, volume_dir) remove_file_from_disk(filename) return JsonResponse({"detail": "success"})
def upload_volume(request, *args, **kwargs): """ User upload volume data, delete the original data first. """ if not (request.user and request.user.is_authenticated()): raise PermissionDenied() user = request.user assert 'pid' in kwargs pid = kwargs['pid'] assert 'pk' in kwargs id = kwargs['pk'] volume = Volume.objects.get(project__id=pid, id=id) # Check whether the user is the member of this project if not check_member_in_project(volume.project, user): raise PermissionDenied(detail="User {} is not in project {}." .format(user.username, volume.project.name)) if not request.FILES.get('file'): raise ParseError(detail="There is no upload file.") logger.info("User {} upload files to volume {}-{}.".format( user.username, volume.project.name, volume.name)) filename = get_upload_volume_filename(volume, user) save_upload_file_to_disk(request.FILES['file'], filename) client = NFSLocalClient() volume_dir = get_volume_direction_on_nfs(volume) # Clear the dir first client.removedir(volume_dir) client.makedir(volume_dir) client.copy_file_to_remote_and_untar(filename, volume_dir) remove_file_from_disk(filename) return JsonResponse({"detail": "success"})
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
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