common.py 5.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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/>.

18
from __future__ import absolute_import, unicode_literals
19
from datetime import timedelta, datetime
20

21
from django.db.models import Model, CharField, IntegerField, permalink
22
from django.utils.translation import ugettext_lazy as _
23
from django.utils.timesince import timeuntil
24 25 26

from model_utils.models import TimeStampedModel

Őry Máté committed
27 28
from acl.models import AclBase

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

ARCHITECTURES = (('x86_64', 'x86-64 (64 bit)'),
                 ('i686', 'x86 (32 bit)'))


class BaseResourceConfigModel(Model):

    """Abstract base for models with base resource configuration parameters.
    """
    num_cores = IntegerField(verbose_name=_('number of cores'),
                             help_text=_('Number of virtual CPU cores '
                                         'available to the virtual machine.'))
    ram_size = IntegerField(verbose_name=_('RAM size'),
                            help_text=_('Mebibytes of memory.'))
    max_ram_size = IntegerField(verbose_name=_('maximal RAM size'),
                                help_text=_('Upper memory size limit '
                                            'for balloning.'))
    arch = CharField(max_length=10, verbose_name=_('architecture'),
                     choices=ARCHITECTURES)
    priority = IntegerField(verbose_name=_('priority'),
                            help_text=_('CPU priority.'))

    class Meta:
        abstract = True


class NamedBaseResourceConfig(BaseResourceConfigModel, TimeStampedModel):

    """Pre-created, named base resource configurations.
    """
    name = CharField(max_length=50, unique=True,
Bach Dániel committed
60 61
                     verbose_name=_('name'),
                     help_text=_('Name of base resource configuration.'))
62 63 64 65 66 67 68 69 70

    class Meta:
        app_label = 'vm'
        db_table = 'vm_namedbaseresourceconfig'

    def __unicode__(self):
        return self.name


Őry Máté committed
71
class Lease(AclBase):
72 73 74 75 76 77

    """Lease times for VM instances.

    Specifies a time duration until suspension and deletion of a VM
    instance.
    """
Őry Máté committed
78 79 80 81
    ACL_LEVELS = (
        ('user', _('user')),          # use this lease
        ('owner', _('owner')),        # change this lease
    )
82 83
    name = CharField(max_length=100, unique=True,
                     verbose_name=_('name'))
84 85 86 87 88 89 90 91
    suspend_interval_seconds = IntegerField(
        verbose_name=_('suspend interval'), help_text=_(
            'Number of seconds after the an instance is suspended.'),
        null=True, blank=True)
    delete_interval_seconds = IntegerField(
        verbose_name=_('delete interval'), help_text=_(
            'Number of seconds after the an instance is deleted.'),
        null=True, blank=True)
92 93 94 95 96

    class Meta:
        app_label = 'vm'
        db_table = 'vm_lease'
        ordering = ['name', ]
Őry Máté committed
97 98 99 100
        permissions = (
            ('create_leases', _('Can create new leases.')),
            ('bypass_leases', _('Can set arbitrary expiration times.')),
        )
101 102 103

    @property
    def suspend_interval(self):
104 105 106 107 108
        v = self.suspend_interval_seconds
        if v is not None:
            return timedelta(seconds=v)
        else:
            return None
109 110 111

    @suspend_interval.setter
    def suspend_interval(self, value):
112 113 114 115
        if value is not None:
            self.suspend_interval_seconds = value.total_seconds()
        else:
            self.suspend_interval_seconds = None
116 117 118

    @property
    def delete_interval(self):
119 120 121 122 123
        v = self.delete_interval_seconds
        if v is not None:
            return timedelta(seconds=v)
        else:
            return None
124 125 126

    @delete_interval.setter
    def delete_interval(self, value):
127 128 129 130
        if value is not None:
            self.delete_interval_seconds = value.total_seconds()
        else:
            self.delete_interval_seconds = None
131

132
    def get_readable_suspend_time(self):
133 134 135 136 137 138
        v = self.suspend_interval
        if v is not None:
            n = datetime.utcnow()
            return timeuntil(n + v, n)
        else:
            return _("never")
139 140

    def get_readable_delete_time(self):
141 142 143 144 145 146
        v = self.delete_interval
        if v is not None:
            n = datetime.utcnow()
            return timeuntil(n + v, n)
        else:
            return _("never")
147

148
    def __unicode__(self):
149 150 151 152
        return _("%(name)s (suspend: %(s)s, remove: %(r)s)") % {
            'name': self.name,
            's': self.get_readable_suspend_time(),
            'r': self.get_readable_delete_time()}
153

154 155 156 157
    @permalink
    def get_absolute_url(self):
        return ('dashboard.views.lease-detail', None, {'pk': self.pk})

158 159 160 161 162 163 164 165 166 167

class Trait(Model):
    name = CharField(max_length=50, verbose_name=_('name'))

    class Meta:
        app_label = 'vm'
        db_table = 'vm_trait'

    def __unicode__(self):
        return self.name