Commit 94d2ea62 by Szabolcs Gelencser

Add initial template creation

parent bad51a79
No preview for this file type
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-03-13 13:53
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('network', '0004_remove_vxlan_owner'),
]
operations = [
migrations.AlterModelOptions(
name='vxlan',
options={},
),
migrations.RemoveField(
model_name='vxlan',
name='comment',
),
migrations.RemoveField(
model_name='vxlan',
name='created_at',
),
migrations.RemoveField(
model_name='vxlan',
name='description',
),
migrations.RemoveField(
model_name='vxlan',
name='modified_at',
),
migrations.RemoveField(
model_name='vxlan',
name='name',
),
migrations.RemoveField(
model_name='vxlan',
name='vlan',
),
migrations.RemoveField(
model_name='vxlan',
name='vni',
),
migrations.AddField(
model_name='vxlan',
name='os_network_id',
field=models.CharField(default=1, max_length=100, unique=True),
preserve_default=False,
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-03-13 13:53
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('vm', '0008_merge_20180226_1259'),
]
operations = [
migrations.AlterModelOptions(
name='instancetemplate',
options={'ordering': ('name',), 'verbose_name': 'template', 'verbose_name_plural': 'templates'},
),
migrations.RemoveField(
model_name='instancetemplate',
name='access_method',
),
migrations.RemoveField(
model_name='instancetemplate',
name='boot_menu',
),
migrations.RemoveField(
model_name='instancetemplate',
name='disks',
),
migrations.RemoveField(
model_name='instancetemplate',
name='has_agent',
),
migrations.RemoveField(
model_name='instancetemplate',
name='lease',
),
migrations.RemoveField(
model_name='instancetemplate',
name='max_ram_size',
),
migrations.RemoveField(
model_name='instancetemplate',
name='num_cores',
),
migrations.RemoveField(
model_name='instancetemplate',
name='owner',
),
migrations.RemoveField(
model_name='instancetemplate',
name='parent',
),
migrations.RemoveField(
model_name='instancetemplate',
name='priority',
),
migrations.RemoveField(
model_name='instancetemplate',
name='ram_size',
),
migrations.RemoveField(
model_name='instancetemplate',
name='raw_data',
),
migrations.RemoveField(
model_name='instancetemplate',
name='req_traits',
),
migrations.RemoveField(
model_name='instancetemplate',
name='system',
),
migrations.RemoveField(
model_name='instancetemplate',
name='tags',
),
migrations.AddField(
model_name='instancetemplate',
name='flavor_id',
field=models.CharField(default=1, max_length=100, unique=True),
preserve_default=False,
),
migrations.AddField(
model_name='instancetemplate',
name='image_id',
field=models.CharField(default=1, max_length=100, unique=True),
preserve_default=False,
),
migrations.AddField(
model_name='instancetemplate',
name='owner_id',
field=models.CharField(default=1, max_length=100, unique=True),
preserve_default=False,
),
]
......@@ -127,33 +127,29 @@ class VirtualMachineDescModel(BaseResourceConfigModel):
abstract = True
class InstanceTemplate(VirtualMachineDescModel, TimeStampedModel):
class InstanceTemplate(TimeStampedModel):
"""Virtual machine template.
"""
name = CharField(max_length=100, verbose_name=_('name'),
help_text=_('Human readable name of template.'))
description = TextField(verbose_name=_('description'), blank=True)
parent = ForeignKey('self', null=True, blank=True,
verbose_name=_('parent template'),
on_delete=SET_NULL,
help_text=_('Template which this one is derived of.'))
disks = ManyToManyField('storage.Disk', verbose_name=_('disks'),
related_name='template_set',
help_text=_('Disks which are to be mounted.'))
owner = ForeignKey(User)
# parent = ForeignKey('self', null=True, blank=True,
# verbose_name=_('parent template'),
# on_delete=SET_NULL,
# help_text=_('Template which this one is derived of.'))
# disks = ManyToManyField('storage.Disk', verbose_name=_('disks'),
# related_name='template_set',
# help_text=_('Disks which are to be mounted.'))
# lease = ForeignKey(Lease, help_text=_("Preferred expiration periods."),
# verbose_name=_("Lease"))
image_id = CharField(blank=False, max_length=100, unique=True)
flavor_id = CharField(blank=False, max_length=100, unique=True)
owner_id = CharField(blank=False, max_length=100, unique=True)
class Meta:
app_label = 'vm'
db_table = 'vm_instancetemplate'
ordering = ('name', )
permissions = (
('create_template', _('Can create an instance template.')),
('create_base_template',
_('Can create an instance template (base).')),
('change_template_resources',
_('Can change resources of a template.')),
)
verbose_name = _('template')
verbose_name_plural = _('templates')
......@@ -162,9 +158,7 @@ class InstanceTemplate(VirtualMachineDescModel, TimeStampedModel):
@property
def running_instances(self):
"""The number of running instances of the template.
"""
return sum(1 for i in self.instance_set.all() if i.is_running)
return 0 #TODO: see get_running_instances
@property
def os_type(self):
......@@ -174,35 +168,27 @@ class InstanceTemplate(VirtualMachineDescModel, TimeStampedModel):
return 'windows'
else:
return 'linux'
@property
def is_ready(self):
return all(disk.is_ready for disk in self.disks)
def save(self, *args, **kwargs):
is_new = getattr(self, "pk", None) is None
super(InstanceTemplate, self).save(*args, **kwargs)
if is_new:
self.set_level(self.owner, 'owner')
#TODO
@permalink
def get_absolute_url(self):
return ('dashboard.views.template-detail', None, {'pk': self.pk})
def remove_disk(self, disk, **kwargs):
#TODO: why?
self.disks.remove(disk)
def destroy_disks(self):
"""Destroy all associated disks.
"""
#TODO: why?
for disk in self.disks.all():
disk.destroy()
def get_running_instances(self):
return Instance.active.filter(template=self, status="RUNNING")
return () #TODO: do we need this? if so, store created vm ids in template
@property
def metric_prefix(self):
#TODO: what is this?
return 'template.%d' % self.pk
......
......@@ -521,7 +521,16 @@ class SaveAsTemplateOperation(InstanceOperation):
def _operation(self, request, user, system, name=None,
with_shutdown=True, clone=False, task=None, **kwargs):
print "TODO: create template"
template_image_id = openstack_api.nova.snapshot_create(request, self.instance.id, name)
template = InstanceTemplate(
name=name,
image_id=template_image_id,
flavor_id=self.instance.flavor["id"],
owner_id=user.id,
)
template.save()
# return create_readable(
# ugettext_noop("New template: %(template)s"),
# template=reverse('dashboard.views.template-detail',
......
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