storage.py 3.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals, absolute_import

19
from django.contrib import messages
20
from django.core.urlresolvers import reverse
21
from django.db.models import Q
22
from django.utils.translation import ugettext_lazy as _
23
from django.views.generic import UpdateView
24

25
from braces.views import SuperuserRequiredMixin
26
from sizefield.utils import filesizeformat
27

28
from common.models import WorkerNotFound
29 30
from storage.models import DataStore, Disk
from ..tables import DiskListTable
31
from ..forms import DataStoreForm, DiskForm
32 33


34
class StorageDetail(SuperuserRequiredMixin, UpdateView):
35 36
    model = DataStore
    form_class = DataStoreForm
37
    template_name = "dashboard/storage/detail.html"
38 39 40 41 42

    def get_object(self):
        return DataStore.objects.get()

    def get_context_data(self, **kwargs):
43
        context = super(StorageDetail, self).get_context_data(**kwargs)
44 45

        ds = self.get_object()
46 47 48 49 50 51 52
        try:
            context['stats'] = self._get_stats()
            context['missing_disks'] = ds.get_missing_disks()
            context['orphan_disks'] = ds.get_orphan_disks()
        except WorkerNotFound:
            messages.error(self.request, _("The DataStore is offline."))

53
        context['disk_table'] = DiskListTable(
54 55 56 57 58 59 60
            self.get_table_data(), request=self.request,
            template="django_tables2/with_pagination.html")
        context['filter_names'] = (
            ('vm', _("virtual machine")),
            ('template', _("template")),
            ('none', _("none")),
        )
61 62
        return context

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    def get_table_data(self):
        ds = self.get_object()
        qs = Disk.objects.filter(datastore=ds, destroyed=None)

        filter_name = self.request.GET.get("filter")
        search = self.request.GET.get("s")

        filter_queries = {
            'vm': {
                'instance_set__isnull': False,
            },
            'template': {
                'template_set__isnull': False,
            },
            'none': {
                'template_set__isnull': True,
                'instance_set__isnull': True,
            }
        }

        if filter_name:
            qs = qs.filter(**filter_queries.get(filter_name, {}))

        if search:
            search = search.strip()
            qs = qs.filter(Q(name__icontains=search) |
                           Q(filename__icontains=search))

        return qs

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    def _get_stats(self):
        stats = self.object.get_statistics()
        free_space = int(stats['free_space'])
        free_percent = float(stats['free_percent'])

        total_space = free_space / (free_percent/100.0)
        used_space = total_space - free_space

        return {
            'used_percent': int(100 - free_percent),
            'free_space': filesizeformat(free_space),
            'used_space': filesizeformat(used_space),
            'total_space': filesizeformat(total_space),
        }

    def get_success_url(self):
109
        return reverse("dashboard.views.storage")
110 111 112 113 114 115


class DiskDetail(SuperuserRequiredMixin, UpdateView):
    model = Disk
    form_class = DiskForm
    template_name = "dashboard/storage/disk.html"
116 117 118

    def form_valid(self, form):
        pass