Commit bc7d2846 by Chif Gergő

Add status to instance model

parent 1b3e04f2
# Generated by Django 3.0.4 on 2020-03-30 10:18
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Disk',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, help_text='Name of the disk', max_length=100, verbose_name='name')),
('remote_id', models.CharField(help_text='ID, which helps access the disk', max_length=40, unique=True, verbose_name='remote_ID')),
],
),
migrations.CreateModel(
name='Image',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='Human readable name of image.', max_length=100, verbose_name='name')),
('description', models.TextField(blank=True, help_text='Description of the image.', verbose_name='description')),
('remote_id', models.CharField(help_text='ID, which helps access the image.', max_length=40, unique=True, verbose_name='remote_ID')),
('created_at', models.DateTimeField(auto_now_add=True, help_text='Date, when the image created.')),
('uploaded_by_user', models.BooleanField(default=True, editable=False, help_text='The field is false if the image created from instance')),
('created_by', models.ForeignKey(help_text='The user, who create the image', on_delete=django.db.models.deletion.DO_NOTHING, related_name='created_images', to=settings.AUTH_USER_MODEL)),
],
),
]
# Generated by Django 3.0.4 on 2020-03-26 11:58
# Generated by Django 3.0.4 on 2020-03-30 10:18
from django.conf import settings
from django.db import migrations, models
......@@ -10,8 +10,8 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
('image', '__first__'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('image', '0001_initial'),
]
operations = [
......@@ -19,23 +19,23 @@ class Migration(migrations.Migration):
name='Flavor',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, max_length=100)),
('name', models.CharField(max_length=100)),
('description', models.CharField(blank=True, max_length=200)),
('remote_id', models.CharField(help_text='ID of the instance on the backend', max_length=100)),
('ram', models.IntegerField(blank=True, null=True)),
('vcpu', models.IntegerField(blank=True, null=True)),
('initial_disk', models.IntegerField(blank=True, null=True)),
('priority', models.IntegerField(blank=True, null=True)),
('ram', models.PositiveIntegerField(default=0)),
('vcpu', models.PositiveIntegerField(default=0)),
('initial_disk', models.PositiveIntegerField(default=0)),
('priority', models.PositiveIntegerField(blank=True, null=True)),
],
),
migrations.CreateModel(
name='Lease',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, max_length=100)),
('name', models.CharField(max_length=100)),
('description', models.CharField(blank=True, max_length=100)),
('suspend_interval_in_sec', models.IntegerField(blank=True, null=True)),
('delete_interval_in_sec', models.IntegerField(blank=True, null=True)),
('suspend_interval_in_sec', models.IntegerField(default=3600)),
('delete_interval_in_sec', models.IntegerField(default=7200)),
],
),
migrations.CreateModel(
......@@ -50,6 +50,7 @@ class Migration(migrations.Migration):
('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')),
......
# Generated by Django 3.0.4 on 2020-03-26 11:58
# Generated by Django 3.0.4 on 2020-03-30 10:18
from django.db import migrations, models
import django.db.models.deletion
......
# Generated by Django 3.0.4 on 2020-03-26 12:42
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('instance', '0002_instance_template'),
]
operations = [
migrations.AlterField(
model_name='flavor',
name='initial_disk',
field=models.PositiveIntegerField(default=0),
),
migrations.AlterField(
model_name='flavor',
name='name',
field=models.CharField(max_length=100),
),
migrations.AlterField(
model_name='flavor',
name='priority',
field=models.PositiveIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='flavor',
name='ram',
field=models.PositiveIntegerField(default=0),
),
migrations.AlterField(
model_name='flavor',
name='vcpu',
field=models.PositiveIntegerField(default=0),
),
migrations.AlterField(
model_name='lease',
name='delete_interval_in_sec',
field=models.IntegerField(default=7200),
),
migrations.AlterField(
model_name='lease',
name='name',
field=models.CharField(max_length=100),
),
migrations.AlterField(
model_name='lease',
name='suspend_interval_in_sec',
field=models.IntegerField(default=3600),
),
]
......@@ -120,6 +120,7 @@ class Instance(models.Model):
blank=True,
help_text="After this point in time, the instance will be deleted!"
)
status = models.CharField(max_length=50, verbose_name="instance_status", default="NO_STATE")
deleted = models.BooleanField(
help_text="Indicates if the instance is ready for garbage collection",
default=False,
......@@ -144,7 +145,8 @@ class Instance(models.Model):
def create(cls, lease, owner, flavor, template, remote_id, params):
params["password"] = cls.generate_password()
inst = cls(lease=lease, flavor=flavor, owner=owner,
remote_id=remote_id, template=template, **params)
remote_id=remote_id, template=template, status="CREATING",
**params)
inst.full_clean()
inst.save()
......@@ -154,8 +156,7 @@ class Instance(models.Model):
@classmethod
def create_instance_from_template(cls, params, template, owner, lease,
disks, networks, flavor):
# TODO: attach disks when the remote instance created
networks, flavor):
try:
remote_id = interface.create_vm_from_template(params["name"],
template.image.remote_id,
......@@ -213,6 +214,11 @@ class Instance(models.Model):
def get_remote_instance(self):
return interface.get_vm(self.remote_id)
def update_status(self):
remote = self.get_remote_instance()
self.status = remote.status
self.save()
@classmethod
def generate_password(self):
return User.objects.make_random_password(
......
......@@ -8,18 +8,9 @@ class InstanceSerializer(serializers.ModelSerializer):
class Meta:
model = Instance
fields = (
"id",
"name",
"description",
"system",
"lease",
"flavor",
"password",
"template",
"time_of_suspend",
"time_of_delete")
read_only_fields = ("id", "password", "template", "time_of_suspend", "time_of_delete")
fields = "__all__"
read_only_fields = ("id", "status" "password", "template",
"time_of_suspend", "time_of_delete")
class FlavorSerializer(serializers.ModelSerializer):
......@@ -31,4 +22,4 @@ class FlavorSerializer(serializers.ModelSerializer):
class LeaseSerializer(serializers.ModelSerializer):
class Meta:
model = Lease
fields = '__all__'
fields = "__all__"
......@@ -59,6 +59,7 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
def get_merged_object(self, pk):
instance = self.get_object(pk)
instanceDict = InstanceSerializer(instance).data
instance.update_status()
remoteInstance = instance.get_remote_instance()
remoteInstanceDict = {
"remote_id": remoteInstance.id,
......@@ -91,7 +92,6 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
template=template,
flavor=flavor,
owner=request.user,
disks=None
)
return Response(InstanceSerializer(newInstance).data)
......
# Generated by Django 3.0.4 on 2020-03-26 11:58
# Generated by Django 3.0.4 on 2020-03-30 10:18
from django.conf import settings
from django.db import migrations, models
......@@ -10,9 +10,9 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
('image', '__first__'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('instance', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('image', '0001_initial'),
]
operations = [
......
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