Commit 878edb70 by Chif Gergő

Extract common fields to a new class for tempaltes and instances

parent bc7d2846
# Generated by Django 3.0.4 on 2020-03-30 10:18
# Generated by Django 3.0.4 on 2020-04-06 12:09
from django.conf import settings
from django.db import migrations, models
......
# Generated by Django 3.0.4 on 2020-03-30 10:18
# Generated by Django 3.0.4 on 2020-04-06 12:09
from django.conf import settings
from django.db import migrations, models
......@@ -10,8 +10,8 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('image', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
......@@ -39,27 +39,38 @@ class Migration(migrations.Migration):
],
),
migrations.CreateModel(
name='Instance',
name='BaseMachineDescriptor',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='Human readable name of instance', max_length=100)),
('name', models.CharField(help_text='Human readable name of template.', max_length=100, verbose_name='name')),
('description', models.TextField(blank=True, help_text='Description of the template.', verbose_name='description')),
('system_type', models.CharField(choices=[('LINUX', 'The system based on Linux'), ('WINDOWS', 'Windows based system')], help_text='The base name of the operating system', max_length=50, verbose_name='operating_system')),
('distro', models.CharField(blank=True, help_text='The specific name and version of the installed OSe. g. Win 7, Ubuntu 18.04', max_length=100, verbose_name='system distribution')),
('access_protocol', models.CharField(choices=[('rdp', 'Remote Desktop Protocol'), ('ssh', 'Secure Shell')], help_text='The protocol which used to connect to the machinethat created from this template', max_length=50, verbose_name='remote_access_protocol')),
('created_at', models.DateTimeField(auto_now_add=True, help_text='Date, when the template created.')),
('network_id', models.CharField(blank=True, help_text='The new instance will be in this network.', max_length=100, null=True, verbose_name='network_id')),
('created_by', models.ForeignKey(help_text='The user, who create the template', on_delete=django.db.models.deletion.DO_NOTHING, related_name='created_templates', to=settings.AUTH_USER_MODEL)),
('flavor', models.ForeignKey(help_text='Resources given to the vm', on_delete=django.db.models.deletion.CASCADE, related_name='templates', to='instance.Flavor', verbose_name='flavor')),
('lease', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='templates', to='instance.Lease')),
],
),
migrations.CreateModel(
name='Instance',
fields=[
('basemachinedescriptor_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='instance.BaseMachineDescriptor')),
('remote_id', models.CharField(help_text='ID of the instance on the backend', max_length=100)),
('description', models.TextField(blank=True, help_text='The description of the instance')),
('access_method', models.CharField(choices=[('rdp', 'Remote Desktop Protocol'), ('ssh', 'Secure Shell')], help_text='Primary remote access method', max_length=10)),
('system', models.CharField(help_text='Operating system type', max_length=50)),
('password', models.CharField(help_text='Original password of the instance', max_length=50)),
('time_of_suspend', models.DateTimeField(blank=True, help_text='After this point in time, the instance will be suspended')),
('time_of_delete', models.DateTimeField(blank=True, help_text='After this point in time, the instance will be deleted!')),
('status', models.CharField(default='NO_STATE', max_length=50, verbose_name='instance_status')),
('deleted', models.BooleanField(default=False, help_text='Indicates if the instance is ready for garbage collection')),
('disks', models.ManyToManyField(help_text='Disks attached to instance', related_name='instance', to='image.Disk', verbose_name='disks')),
('flavor', models.ForeignKey(help_text='Reasources given to the vm', on_delete=django.db.models.deletion.CASCADE, related_name='instances', to='instance.Flavor', verbose_name='flavor')),
('lease', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='instances', to='instance.Lease')),
('owner', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'permissions': (('create_instance', 'Can create a new VM.'), ('create_template_from_instance', 'Can create template from instance.'), ('use_instance', 'Can access the VM connection info.'), ('operate_instance', 'Can use basic lifecycle methods of the VM.'), ('administer_instance', 'Can delete VM.'), ('access_console', 'Can access the graphical console of a VM.'), ('change_resources', 'Can change resources of a VM.'), ('manage_access', 'Can manage access rights for the VM.'), ('config_ports', 'Can configure port forwards.')),
'default_permissions': (),
},
bases=('instance.basemachinedescriptor',),
),
]
# Generated by Django 3.0.4 on 2020-03-30 10:18
# Generated by Django 3.0.4 on 2020-04-06 12:09
from django.db import migrations, models
import django.db.models.deletion
......@@ -9,8 +9,8 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
('instance', '0001_initial'),
('template', '0001_initial'),
('instance', '0001_initial'),
]
operations = [
......
......@@ -11,10 +11,6 @@ import logging
logger = logging.getLogger(__name__)
ACCESS_METHODS = tuple(
[(key, val[0]) for key, val in settings.VM_ACCESS_PROTOCOLS.items()]
)
interface = OSVirtualMachineManager(settings.CONNECTION)
ACTIONS = {
......@@ -76,7 +72,63 @@ class Flavor(models.Model):
logger.error("Can not delete the flavor in remote cloud")
class Instance(models.Model):
class BaseMachineDescriptor(models.Model):
"""Virtual machine template.
"""
OSTypes = (
('LINUX', 'The system based on Linux'),
('WINDOWS', 'Windows based system'),
)
CONN_PROTOCOL = tuple(
[(key, val[0]) for key, val in settings.VM_ACCESS_PROTOCOLS.items()]
)
name = models.CharField(max_length=100,
verbose_name="name",
help_text="Human readable name of template.")
description = models.TextField(verbose_name="description",
blank=True,
help_text="Description of the template.")
system_type = models.CharField(max_length=50,
choices=OSTypes,
verbose_name="operating_system",
help_text="The base name of the operating system"
)
distro = models.CharField(max_length=100,
blank=True,
verbose_name='system distribution',
help_text="The specific name and version of the installed OS"
"e. g. Win 7, Ubuntu 18.04"
)
access_protocol = models.CharField(max_length=50,
choices=CONN_PROTOCOL,
verbose_name="remote_access_protocol",
help_text="The protocol which used to connect to the machine"
"that created from this template"
)
created_at = models.DateTimeField(auto_now_add=True,
editable=False,
help_text="Date, when the template created.")
created_by = models.ForeignKey(User,
on_delete=models.DO_NOTHING,
related_name="created_templates",
help_text="The user, who create the template")
flavor = models.ForeignKey(Flavor,
help_text="Resources given to the vm",
verbose_name="flavor",
on_delete=models.CASCADE,
related_name='templates')
lease = models.ForeignKey(Lease,
on_delete=models.CASCADE,
related_name='templates')
network_id = models.CharField(max_length=100,
verbose_name="network_id",
help_text="The new instance will be in this network.",
null=True,
blank=True)
class Instance(BaseMachineDescriptor):
"""Virtual machine instance.
"""
from template.models import ImageTemplate
......@@ -94,21 +146,9 @@ class Instance(models.Model):
('manage_access', 'Can manage access rights for the VM.'),
('config_ports', 'Can configure port forwards.'),
)
name = models.CharField(max_length=100,
help_text="Human readable name of instance")
remote_id = models.CharField(
max_length=100, help_text="ID of the instance on the backend"
)
description = models.TextField(
blank=True, help_text="The description of the instance"
)
access_method = models.CharField(
max_length=10, choices=ACCESS_METHODS,
help_text="Primary remote access method"
)
system = models.CharField(max_length=50, help_text="Operating system type")
password = models.CharField(
max_length=50, help_text="Original password of the instance"
)
......@@ -135,16 +175,12 @@ class Instance(models.Model):
verbose_name="disks")
owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
flavor = models.ForeignKey(Flavor, help_text="Reasources given to the vm",
verbose_name="flavor", on_delete=models.CASCADE,
related_name='instances')
lease = models.ForeignKey(Lease, on_delete=models.CASCADE,
related_name='instances')
@classmethod
def create(cls, lease, owner, flavor, template, remote_id, params):
params["password"] = cls.generate_password()
inst = cls(lease=lease, flavor=flavor, owner=owner,
inst = cls(lease=lease, flavor=flavor, owner=owner, system_type=template.system_type,
distro=template.distro, access_protocol=template.access_protocol,
remote_id=remote_id, template=template, status="CREATING",
**params)
inst.full_clean()
......
# Generated by Django 3.0.4 on 2020-03-30 10:18
# Generated by Django 3.0.4 on 2020-04-06 12:09
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
......@@ -11,39 +10,17 @@ class Migration(migrations.Migration):
dependencies = [
('instance', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('image', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='BaseTemplate',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='Human readable name of template.', max_length=100, verbose_name='name')),
('description', models.TextField(blank=True, help_text='Description of the template.', verbose_name='description')),
('created_at', models.DateTimeField(auto_now_add=True, help_text='Date, when the template created.')),
('network_id', models.CharField(blank=True, help_text='The new instance will be in this network.', max_length=100, null=True, verbose_name='network_id')),
('created_by', models.ForeignKey(help_text='The user, who create the template', on_delete=django.db.models.deletion.DO_NOTHING, related_name='created_templates', to=settings.AUTH_USER_MODEL)),
('flavor', models.ForeignKey(help_text='Resources given to the vm', on_delete=django.db.models.deletion.CASCADE, related_name='templates', to='instance.Flavor', verbose_name='flavor')),
('lease', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='templates', to='instance.Lease')),
],
),
migrations.CreateModel(
name='ImageTemplate',
fields=[
('basetemplate_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='template.BaseTemplate')),
('type', models.CharField(choices=[('U', 'User create the template from image'), ('I', 'Template created from instance'), ('P', '"Pure" template')], default='U', max_length=10)),
('basemachinedescriptor_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='instance.BaseMachineDescriptor')),
('type', models.CharField(choices=[('SYSTEM', 'Common templates provided by the system.'), ('IMAGE', 'Template created from image by a user'), ('INSTANCE', 'Template created from instance by a user')], default='SYSTEM', max_length=10)),
('image', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='templates', to='image.Image')),
],
bases=('template.basetemplate',),
),
migrations.CreateModel(
name='DiskTemplate',
fields=[
('basetemplate_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='template.BaseTemplate')),
('disk', models.ForeignKey(help_text='The disk where the template is located.', on_delete=django.db.models.deletion.CASCADE, related_name='templates', to='image.Disk')),
],
bases=('template.basetemplate',),
bases=('instance.basemachinedescriptor',),
),
]
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from image.models import Image
from instance.models import BaseMachineDescriptor
from image.models import Disk, Image
from instance.models import Lease, Flavor
from interface_openstack.implementation.storage.openstack_snapshot_manager import SnapshotManager
class BaseTemplate(models.Model):
"""Virtual machine template.
"""
name = models.CharField(max_length=100,
verbose_name="name",
help_text="Human readable name of template.")
description = models.TextField(verbose_name="description",
blank=True,
help_text="Description of the template.")
created_at = models.DateTimeField(auto_now_add=True,
editable=False,
help_text="Date, when the template created.")
created_by = models.ForeignKey(User,
on_delete=models.DO_NOTHING,
related_name="created_templates",
help_text="The user, who create the template")
flavor = models.ForeignKey(Flavor,
help_text="Resources given to the vm",
verbose_name="flavor",
on_delete=models.CASCADE,
related_name='templates')
lease = models.ForeignKey(Lease,
on_delete=models.CASCADE,
related_name='templates')
network_id = models.CharField(max_length=100,
verbose_name="network_id",
help_text="The new instance will be in this network.",
null=True,
blank=True)
class DiskTemplate(BaseTemplate):
disk = models.ForeignKey(Disk,
related_name="templates",
on_delete=models.CASCADE,
help_text="The disk where the template is located.")
@classmethod
def create_from_volume(cls, name, description, disk, user):
interface = SnapshotManager(settings.CONNECTION)
remote_template = interface.create_from_volume(disk.remote_id)
remote_id = remote_template.id
new_template = DiskTemplate.objects.create(name=name,
description=description,
disk=disk,
remote_id=remote_id,
created_by=user)
return new_template
class ImageTemplate(BaseTemplate):
class ImageTemplate(BaseMachineDescriptor):
TYPES = (
('U', 'User create the template from image'),
('I', 'Template created from instance'),
('P', '"Pure" template'),
('SYSTEM', 'Common templates provided by the system.'),
('IMAGE', 'Template created from image by a user'),
('INSTANCE', 'Template created from instance by a user'),
)
image = models.ForeignKey(Image,
related_name="templates",
......@@ -70,7 +15,7 @@ class ImageTemplate(BaseTemplate):
help_text="")
type = models.CharField(max_length=10,
choices=TYPES,
default="U")
default="SYSTEM")
@classmethod
def create_from_instance(cls, name, description, instance, user):
......@@ -79,7 +24,10 @@ class ImageTemplate(BaseTemplate):
description=description,
created_by=user,
image=image,
system_type=instance.system_type,
distro=instance.distro,
access_protocol=instance.access_protocol,
lease=instance.lease,
flavor=instance.flavor,
type="I")
type="INSTANCE")
return new_template
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