Commit fa4cc272 by Czémán Arnold

storage: add DataStoreHost model, make DataStore and Disk model Ceph block store compatible

parent 1698b3d4
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('storage', '0002_disk_bus'),
]
operations = [
migrations.CreateModel(
name='DataStoreHost',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('address', models.CharField(max_length=1024, verbose_name='address')),
('port', models.IntegerField(null=True, verbose_name='port')),
],
),
migrations.AddField(
model_name='datastore',
name='ceph_user',
field=models.CharField(max_length=255, null=True, verbose_name='Ceph username'),
),
migrations.AddField(
model_name='datastore',
name='secret_uuid',
field=models.CharField(max_length=255, null=True, verbose_name='uuid of secret'),
),
migrations.AddField(
model_name='datastore',
name='type',
field=models.CharField(default='file', max_length=10, verbose_name='type', choices=[('file', 'filesystem'), ('ceph_block', 'Ceph block device')]),
),
migrations.AlterField(
model_name='datastore',
name='hostname',
field=models.CharField(max_length=40, verbose_name='hostname'),
),
migrations.AlterField(
model_name='disk',
name='type',
field=models.CharField(max_length=10, choices=[('qcow2-norm', 'qcow2 normal'), ('qcow2-snap', 'qcow2 snapshot'), ('ceph-norm', 'Ceph block normal'), ('ceph-snap', 'Ceph block snapshot'), ('iso', 'iso'), ('raw-ro', 'raw read-only'), ('raw-rw', 'raw')]),
),
migrations.AddField(
model_name='datastore',
name='hosts',
field=models.ManyToManyField(to='storage.DataStoreHost', verbose_name='hosts'),
),
]
...@@ -27,7 +27,7 @@ import re ...@@ -27,7 +27,7 @@ import re
from django.conf import settings from django.conf import settings
from celery.contrib.abortable import AbortableAsyncResult from celery.contrib.abortable import AbortableAsyncResult
from django.db.models import (Model, BooleanField, CharField, DateTimeField, from django.db.models import (Model, BooleanField, CharField, DateTimeField,
ForeignKey) ForeignKey, IntegerField, ManyToManyField)
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _, ugettext_noop from django.utils.translation import ugettext_lazy as _, ugettext_noop
...@@ -43,14 +43,30 @@ from common.models import ( ...@@ -43,14 +43,30 @@ from common.models import (
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class DataStoreHost(Model):
""" Address and port of a data store.
"""
address = CharField(max_length=1024, verbose_name=_('address'))
port = IntegerField(null=True, verbose_name=_('port'))
class DataStore(Model): class DataStore(Model):
"""Collection of virtual disks. """Collection of virtual disks.
""" """
TYPES = [('file', 'filesystem'), ('ceph_block', 'Ceph block device')]
type = CharField(max_length=10, verbose_name=_('type'),
default='file', choices=TYPES)
name = CharField(max_length=100, unique=True, verbose_name=_('name')) name = CharField(max_length=100, unique=True, verbose_name=_('name'))
path = CharField(max_length=200, unique=True, verbose_name=_('path')) path = CharField(max_length=200, unique=True, verbose_name=_('path'))
hostname = CharField(max_length=40, unique=True, hostname = CharField(max_length=40, verbose_name=_('hostname'))
verbose_name=_('hostname')) hosts = ManyToManyField('DataStoreHost', verbose_name=_('hosts'))
ceph_user = CharField(max_length=255, null=True,
verbose_name=_('Ceph username'))
secret_uuid = CharField(max_length=255, null=True,
verbose_name=_('uuid of secret'))
class Meta: class Meta:
ordering = ['name'] ordering = ['name']
...@@ -79,6 +95,24 @@ class DataStore(Model): ...@@ -79,6 +95,24 @@ class DataStore(Model):
self.disk_set.filter( self.disk_set.filter(
destroyed__isnull=False) if disk.is_deletable] destroyed__isnull=False) if disk.is_deletable]
def get_disk_desc(self, disk):
if self.type == "ceph_block":
return disk.get_disk_desc_for_ceph_block_device()
return disk.get_disk_desc_for_filesystem()
def get_vmdisk_desc(self, disk):
if self.type == "ceph_block":
return disk.get_vmdisk_desc_for_ceph_block_device()
return disk.get_vmdisk_desc_for_filesystem()
def get_hosts(self):
return [(host.address, host.port) for host in self.hosts.all()]
@method_cache(30) @method_cache(30)
def get_statistics(self, timeout=15): def get_statistics(self, timeout=15):
q = self.get_remote_queue_name("storage", priority="fast") q = self.get_remote_queue_name("storage", priority="fast")
...@@ -126,8 +160,11 @@ class Disk(TimeStampedModel): ...@@ -126,8 +160,11 @@ class Disk(TimeStampedModel):
"""A virtual disk. """A virtual disk.
""" """
TYPES = [('qcow2-norm', 'qcow2 normal'), ('qcow2-snap', 'qcow2 snapshot'), TYPES = [('qcow2-norm', 'qcow2 normal'), ('qcow2-snap', 'qcow2 snapshot'),
('ceph-norm', 'Ceph block normal'),
('ceph-snap', 'Ceph block snapshot'),
('iso', 'iso'), ('raw-ro', 'raw read-only'), ('raw-rw', 'raw')] ('iso', 'iso'), ('raw-ro', 'raw read-only'), ('raw-rw', 'raw')]
BUS_TYPES = (('virtio', 'virtio'), ('ide', 'ide'), ('scsi', 'scsi')) BUS_TYPES = (('virtio', 'virtio'), ('ide', 'ide'), ('scsi', 'scsi'))
name = CharField(blank=True, max_length=100, verbose_name=_("name")) name = CharField(blank=True, max_length=100, verbose_name=_("name"))
filename = CharField(max_length=256, unique=True, filename = CharField(max_length=256, unique=True,
verbose_name=_("filename")) verbose_name=_("filename"))
...@@ -230,24 +267,28 @@ class Disk(TimeStampedModel): ...@@ -230,24 +267,28 @@ class Disk(TimeStampedModel):
return join(self.datastore.path, self.filename) return join(self.datastore.path, self.filename)
@property @property
def vm_format(self): def format_for_vmdriver(self):
"""Returns the proper file format for different type of images. """Returns the proper file format for different type of images.
""" """
return { return {
'qcow2-norm': 'qcow2', 'qcow2-norm': 'qcow2',
'qcow2-snap': 'qcow2', 'qcow2-snap': 'qcow2',
'ceph-norm': 'raw',
'ceph-snap': 'raw',
'iso': 'raw', 'iso': 'raw',
'raw-ro': 'raw', 'raw-ro': 'raw',
'raw-rw': 'raw', 'raw-rw': 'raw',
}[self.type] }[self.type]
@property @property
def format(self): def format_for_storagedriver(self):
"""Returns the proper file format for different types of images. """Returns the proper file format for different types of images.
""" """
return { return {
'qcow2-norm': 'qcow2', 'qcow2-norm': 'qcow2',
'qcow2-snap': 'qcow2', 'qcow2-snap': 'qcow2',
'ceph-norm': 'rbd',
'ceph-snap': 'rbd',
'iso': 'iso', 'iso': 'iso',
'raw-ro': 'raw', 'raw-ro': 'raw',
'raw-rw': 'raw', 'raw-rw': 'raw',
...@@ -260,6 +301,8 @@ class Disk(TimeStampedModel): ...@@ -260,6 +301,8 @@ class Disk(TimeStampedModel):
return { return {
'qcow2-norm': 'vd', 'qcow2-norm': 'vd',
'qcow2-snap': 'vd', 'qcow2-snap': 'vd',
'ceph-norm': 'vd',
'ceph-snap': 'vd',
'iso': 'sd', 'iso': 'sd',
'raw-ro': 'vd', 'raw-ro': 'vd',
'raw-rw': 'vd', 'raw-rw': 'vd',
...@@ -274,6 +317,8 @@ class Disk(TimeStampedModel): ...@@ -274,6 +317,8 @@ class Disk(TimeStampedModel):
return { return {
'qcow2-norm': 'virtio', 'qcow2-norm': 'virtio',
'qcow2-snap': 'virtio', 'qcow2-snap': 'virtio',
'ceph-norm': 'virtio',
'ceph-snap': 'virtio',
'iso': 'ide', 'iso': 'ide',
'raw-ro': 'virtio', 'raw-ro': 'virtio',
'raw-rw': 'virtio', 'raw-rw': 'virtio',
...@@ -334,27 +379,54 @@ class Disk(TimeStampedModel): ...@@ -334,27 +379,54 @@ class Disk(TimeStampedModel):
def get_vmdisk_desc(self): def get_vmdisk_desc(self):
"""Serialize disk object to the vmdriver. """Serialize disk object to the vmdriver.
""" """
return { return self.datastore.get_vmdisk_desc(self)
'source': self.path,
'driver_type': self.vm_format,
'driver_cache': 'none',
'target_device': self.device_type + self.dev_num,
'target_bus': self.device_bus,
'disk_device': 'cdrom' if self.type == 'iso' else 'disk'
}
def get_disk_desc(self): def get_disk_desc(self):
"""Serialize disk object to the storage driver. """Serialize disk object to the storage driver.
""" """
return self.datastore.get_disk_desc(self)
def get_disk_desc_for_filesystem(self):
return { return {
'name': self.filename, 'name': self.filename,
'dir': self.datastore.path, 'dir': self.datastore.path,
'format': self.format, 'format': self.format_for_storagedriver,
'size': self.size, 'size': self.size,
'base_name': self.base.filename if self.base else None, 'base_name': self.base.filename if self.base else None,
'type': 'snapshot' if self.base else 'normal' 'type': 'snapshot' if self.base else 'normal'
} }
def get_disk_desc_for_ceph_block_device(self):
desc = self.get_disk_desc_for_filesystem()
desc["hosts"] = self.datastore.get_hosts()
desc["ceph_user"] = self.datastore.ceph_user
desc["secret_uuid"] = self.datastore.secret_uuid
return desc
def get_vmdisk_desc_for_filesystem(self):
return {
'data_store_type': self.datastore.type,
'source': self.path,
'driver_type': self.format_for_vmdriver,
'driver_cache': 'none',
'target_device': self.device_type + self.dev_num,
'target_bus': self.device_bus,
'disk_device': 'cdrom' if self.type == 'iso' else 'disk'
}
def get_vmdisk_desc_for_ceph_block_device(self):
desc = self.get_vmdisk_desc_for_filesystem()
desc["hosts"] = self.datastore.get_hosts()
desc["ceph_user"] = self.datastore.ceph_user
desc["secret_uuid"] = self.datastore.secret_uuid
return desc
def get_remote_queue_name(self, queue_id='storage', priority=None, def get_remote_queue_name(self, queue_id='storage', priority=None,
check_worker=True): check_worker=True):
"""Returns the proper queue name based on the datastore. """Returns the proper queue name based on the datastore.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment