models.py 6.73 KB
Newer Older
1 2 3
# coding=utf-8

import logging
4 5
import uuid

6 7 8
from django.contrib.auth.models import User
from django.db.models import (Model, BooleanField, CharField, DateTimeField,
                              ForeignKey, TextField)
9
from django.utils import timezone
10
from django.utils.translation import ugettext_lazy as _
11
from model_utils.models import TimeStampedModel
12
from sizefield.models import FileSizeField
13

14
from .tasks import local_tasks, remote_tasks
15 16 17 18

logger = logging.getLogger(__name__)


19
class DataStore(Model):
Guba Sándor committed
20

21 22
    """Collection of virtual disks.
    """
23 24 25 26
    name = CharField(max_length=100, unique=True, verbose_name=_('name'))
    path = CharField(max_length=200, unique=True, verbose_name=_('path'))
    hostname = CharField(max_length=40, unique=True,
                         verbose_name=_('hostname'))
Guba Sándor committed
27

28 29 30 31 32 33 34 35 36 37
    class Meta:
        ordering = ['name']
        verbose_name = _('datastore')
        verbose_name_plural = _('datastores')

    def __unicode__(self):
        return u'%s (%s)' % (self.name, self.path)


class Disk(TimeStampedModel):
Guba Sándor committed
38

39 40 41 42
    """A virtual disk.
    """
    TYPES = [('qcow2-norm', 'qcow2 normal'), ('qcow2-snap', 'qcow2 snapshot'),
             ('iso', 'iso'), ('raw-ro', 'raw read-only'), ('raw-rw', 'raw')]
43 44 45 46 47
    name = CharField(blank=True, max_length=100, verbose_name=_("name"))
    filename = CharField(max_length=256, verbose_name=_("filename"))
    datastore = ForeignKey(DataStore, verbose_name=_("datastore"),
                           help_text=_("The datastore that holds the disk."))
    type = CharField(max_length=10, choices=TYPES)
48
    size = FileSizeField()
49 50 51 52
    base = ForeignKey('self', blank=True, null=True,
                      related_name='derivatives')
    ready = BooleanField(default=False)
    dev_num = CharField(default='a', max_length=1,
53
                        verbose_name=_("device number"))
54 55 56 57 58 59

    class Meta:
        ordering = ['name']
        verbose_name = _('disk')
        verbose_name_plural = _('disks')

60 61 62 63 64 65 66 67
    class WrongDiskTypeError(Exception):
        def __init__(self, type):
            self.type = type

        def __str__(self):
            return ("Operation can't be invoked on a disk of type '%s'." %
                    self.type)

68 69 70 71 72 73 74 75 76 77 78 79 80 81
    @property
    def path(self):
        return self.datastore.path + '/' + self.filename

    @property
    def format(self):
        return {
            'qcow2-norm': 'qcow2',
            'qcow2-snap': 'qcow2',
            'iso': 'iso',
            'raw-ro': 'raw',
            'raw-rw': 'raw',
        }[self.type]

82 83 84 85 86 87 88
    @property
    def device_type(self):
        return {
            'qcow2': 'vd',
            'raw': 'vd',
            'iso': 'hd',
        }[self.format]
89

90 91
    def get_exclusive(self):
        """Get an instance of the disk for exclusive usage.
92

93 94 95
        This method manipulates the database only.
        """
        type_mapping = {
96 97 98
            'qcow2-norm': 'qcow2-snap',
            'iso': 'iso',
            'raw-ro': 'raw-rw',
99 100 101 102 103 104 105
        }

        if self.type not in type_mapping.keys():
            raise self.WrongDiskTypeError(self.type)

        filename = self.filename if self.type == 'iso' else str(uuid.uuid4())
        new_type = type_mapping[self.type]
106

107 108 109
        return Disk.objects.create(base=self, datastore=self.datastore,
                                   filename=filename, name=self.name,
                                   size=self.size, type=new_type)
110 111 112

    def get_vmdisk_desc(self):
        return {
113
            'source': self.path,
114 115 116 117 118
            'driver_type': self.format,
            'driver_cache': 'default',
            'target_device': self.device_type + self.dev_num
        }

119 120 121 122 123 124 125 126 127 128
    def get_disk_desc(self):
        return {
            'name': self.filename,
            'dir': self.datastore.path,
            'format': self.format,
            'size': self.size,
            'base_name': self.base.filename if self.base else None,
            'type': 'snapshot' if self.type == 'qcow2-snap' else 'normal'
        }

129 130 131
    def __unicode__(self):
        return u"%s (#%d)" % (self.name, self.id)

132 133 134 135 136
    def clean(self, *args, **kwargs):
        if self.size == "" and self.base:
            self.size = self.base.size
        super(Disk, self).clean(*args, **kwargs)

137
    def deploy(self, user=None, task_uuid=None):
138 139 140 141 142 143 144 145 146
        """Reify the disk model on the associated data store.

        :param self: the disk model to reify
        :type self: storage.models.Disk

        :return: True if a new reification of the disk has been created;
                 otherwise, False.
        :rtype: bool
        """
147
        if self.ready:
148
            return False
149

150 151 152 153 154 155 156 157
        act = DiskActivity(activity_code='storage.Disk.deploy')
        act.disk = self
        act.started = timezone.now()
        act.state = 'PENDING'
        act.task_uuid = task_uuid
        act.user = user
        act.save()

158
        # Delegate create / snapshot jobs
159
        queue_name = self.datastore.hostname + ".storage"
160
        disk_desc = self.get_disk_desc()
161
        if self.type == 'qcow2-snap':
162
            act.update_state('CREATING SNAPSHOT')
163 164
            remote_tasks.snapshot.apply_async(args=[disk_desc],
                                              queue=queue_name).get()
165
        else:
166
            act.update_state('CREATING DISK')
167 168
            remote_tasks.create.apply_async(args=[disk_desc],
                                            queue=queue_name).get()
169

170 171
        self.ready = True
        self.save()
172 173

        act.finish('SUCCESS')
174
        return True
175

176
    def deploy_async(self, user=None):
177 178
        """Execute deploy asynchronously.
        """
179 180
        local_tasks.deploy.apply_async(args=[self, user],
                                       queue="localhost.man")
181

182
    def delete(self):
183 184 185
        # TODO
        # StorageDriver.delete_disk.delay(instance.to_json()).get()
        pass
186 187 188 189 190 191 192 193 194 195 196 197 198 199


class DiskActivity(TimeStampedModel):
    activity_code = CharField(verbose_name=_('activity_code'), max_length=100)
    task_uuid = CharField(verbose_name=_('task_uuid'), blank=True,
                          max_length=50, null=True, unique=True)
    disk = ForeignKey(Disk, verbose_name=_('disk'),
                      related_name='activity_log')
    user = ForeignKey(User, verbose_name=_('user'), blank=True, null=True)
    started = DateTimeField(verbose_name=_('started'), blank=True, null=True)
    finished = DateTimeField(verbose_name=_('finished'), blank=True, null=True)
    result = TextField(verbose_name=_('result'), blank=True, null=True)
    state = CharField(verbose_name=_('state'), default='PENDING',
                      max_length=50)
200 201 202 203 204 205 206 207 208 209 210

    def update_state(self, new_state):
        self.state = new_state
        self.save()

    def finish(self, result=None):
        if not self.finished:
            self.finished = timezone.now()
            self.result = result
            self.state = 'COMPLETED'
            self.save()