Example #1
0
    def __init__(self, request, name, uuid, data):
        super(ImageStoreImageView, self).__init__(request)
        self.data = data
        self.name = name
        self.uuid = uuid
        repositories = ImageStore.get_repositories()

        try:
            self.repo = ImageStore(name, url=repositories[name])
        except KeyError:  # The name is not in VMS_IMAGE_REPOSITORIES
            raise ObjectNotFound(model=ImageStore)
Example #2
0
def imagestore_update(request, repo):
    """
    Ajax page for refreshing imagestore repositories.
    """
    if repo not in ImageStore.get_repositories(
            include_image_vm=request.user.is_staff):
        raise Http404

    res = call_api_view(request,
                        'PUT',
                        imagestore_manage,
                        repo,
                        log_response=True)

    if res.status_code == 200:
        imagestore = res.data['result']
        msg = _(
            'Downloaded metadata for %(image_count)d images from image repository %(name)s'
        )
        messages.success(request, msg % imagestore)

        return redirect('imagestore_list_repo',
                        repo=repo,
                        query_string=request.GET)
    else:
        if res.data.get('result', {}).get('error', None):
            status = 200  # The error will be displayed by ImageStoreList JS
        else:
            status = res.status_code

        return JSONResponse(res.data, status=status)
Example #3
0
class ImageStoreImageView(APIView):
    """
    List images or import image from remote disk image repositories (a.k.a. imagestores).
    """
    dc_bound = False

    def __init__(self, request, name, uuid, data):
        super(ImageStoreImageView, self).__init__(request)
        self.data = data
        self.name = name
        self.uuid = uuid
        repositories = ImageStore.get_repositories()

        try:
            self.repo = ImageStore(name, url=repositories[name])
        except KeyError:  # The name is not in VMS_IMAGE_REPOSITORIES
            raise ObjectNotFound(model=ImageStore)

    def get_image(self):
        uuid = self.uuid

        for img in self.repo.images:
            if img['uuid'] == uuid:
                return img
        else:
            raise ObjectNotFound(model=Image)

    def get(self, many=False):
        if many:
            assert not self.uuid

            if self.full or self.extended:
                res = self.repo.images
            else:
                res = [img['uuid'] for img in self.repo.images]
        else:
            assert self.uuid

            res = self.get_image()

        return SuccessTaskResponse(self.request, res, dc_bound=False)

    def post(self):
        img = self.get_image()
        data = self.data
        data['manifest_url'] = self.repo.get_image_manifest_url(img['uuid'])
        data.pop('file_url', None)
        name = data.get('name', None)

        if not name:
            name = data['name'] = img['name']

        return call_api_view(self.request,
                             'POST',
                             image_manage,
                             name,
                             data=data)
Example #4
0
    def __init__(self, request, name, data, many=False):
        super(ImageStoreView, self).__init__(request)
        self.data = data
        self.name = name
        self.many = many
        repositories = ImageStore.get_repositories()

        if name:
            assert not many
            try:
                self.repo = ImageStore(name, url=repositories[name])
            except KeyError:  # The name is not in VMS_IMAGE_REPOSITORIES
                raise ObjectNotFound(model=ImageStore)
        else:
            assert many
            if request.method == 'PUT' or (self.full or self.extended):
                self.repo = ImageStore.all(repositories)
            else:
                self.repo = repositories.keys()
Example #5
0
def imagestore_settings_changed_handler(task_id, dc, old_settings,
                                        new_settings):
    """
    DC settings have changed (triggered by dc_settings_changed signal).
    Look for changes of VMS_IMAGE_REPOSITORIES in the default DC
    """
    # noinspection PyUnresolvedReferences
    if dc.id == DefaultDc.id:
        old_img_repositories = old_settings.get('VMS_IMAGE_REPOSITORIES', {})
        new_img_repositories = new_settings.get('VMS_IMAGE_REPOSITORIES', {})

        # We just need to delete cached data of removed image repositories
        if old_img_repositories != new_img_repositories:
            for old_name, old_url in old_img_repositories.items():
                if new_img_repositories.get(old_name, None) != old_url:
                    ImageStore(old_name).delete()
Example #6
0
def imagestore_list(request, repo=None):
    user = request.user
    context = collect_view_data(request, 'dc_image_list')
    context['image_vm'] = ImageVm.get_uuid()
    context['is_staff'] = is_staff = user.is_staff
    context['all'] = _all = is_staff and request.GET.get('all', False)
    context['qs'] = qs = get_query_string(request, all=_all).urlencode()
    context['url_form_admin'] = reverse('admin_image_form', query_string=qs)
    context['form_admin'] = AdminImageForm(request,
                                           None,
                                           prefix='adm',
                                           initial={
                                               'owner': user.username,
                                               'access': Image.PRIVATE,
                                               'dc_bound': True
                                           })
    qs_image_filter = request.GET.copy()
    qs_image_filter.pop('created_since', None)
    qs_image_filter.pop('last', None)
    context['qs_image_filter'] = qs_image_filter.urlencode()
    context['default_limit'] = default_limit = 30
    context['image_uuids'] = set(Image.objects.all().values_list('uuid',
                                                                 flat=True))

    try:
        created_since_days = int(request.GET.get('created_since', 0))
    except (ValueError, TypeError):
        created_since_days = None

    if created_since_days:
        limit = None
    else:
        created_since_days = None

        try:
            limit = int(request.GET.get('last', default_limit))
        except (ValueError, TypeError):
            limit = default_limit

    repositories = ImageStore.get_repositories(
        include_image_vm=request.user.is_staff)
    context['imagestores'] = imagestores = ImageStore.all(repositories)
    context['created_since'] = created_since_days
    context['limit'] = limit

    if repositories:
        if repo and repo in repositories:
            context['imagestore'] = imagestore = ImageStore(
                repo, url=repositories[repo])
        else:  # Choose the first one
            context['imagestore'] = imagestore = imagestores[0]

        if created_since_days:
            created_since = make_aware(datetime.now() -
                                       timedelta(days=created_since_days))
        else:
            created_since = None

        context['images'] = imagestore.images_filter(
            created_since=created_since, limit=limit)
    else:
        context['imagestore'] = None
        context['images'] = []

    return render(request, 'gui/dc/imagestore_list.html', context)
Example #7
0
class ImageStoreImageView(APIView):
    """
    List images or import image from remote disk image repositories (a.k.a. imagestores).
    """
    dc_bound = False

    def __init__(self, request, name, uuid, data):
        super(ImageStoreImageView, self).__init__(request)
        self.data = data
        self.name = name
        self.uuid = uuid
        repositories = ImageStore.get_repositories(
            include_image_vm=request.user.is_staff)

        try:
            self.repo = ImageStore(name, url=repositories[name])
        except KeyError:  # The name is not in VMS_IMAGE_REPOSITORIES
            raise ObjectNotFound(model=ImageStore)

    def get_image(self):
        uuid = self.uuid

        for img in self.repo.images:
            if img['uuid'] == uuid:
                return img
        else:
            raise ObjectNotFound(model=Image)

    def get(self, many=False):
        if many:
            assert not self.uuid

            if self.full or self.extended:
                res = self.repo.images
            else:
                res = [img['uuid'] for img in self.repo.images]
        else:
            assert self.uuid

            res = self.get_image()

        return SuccessTaskResponse(self.request, res, dc_bound=False)

    def post(self):
        img = self.get_image()
        uuid = img['uuid']
        data = self.data
        data['manifest_url'] = self.repo.get_image_manifest_url(uuid)
        data.pop('file_url', None)
        name = data.get('name', None)

        if not name:
            name = data['name'] = img['name']

        # Although this is also checked inside the image_manage, doing it here is better because:
        #   - checking the uniqueness of the UUID is done differently in image_manage and the result is not a 406 error
        #   - it is faster - in case the name/uuid is not unique we don't have to call another view
        if Image.objects.filter(Q(uuid=uuid) | Q(name=name)).exists():
            raise ObjectAlreadyExists(model=Image)

        return call_api_view(self.request,
                             'POST',
                             image_manage,
                             name,
                             data=data)