Commit 2b3c0497 by Belákovics Ádám

Implement create method of Instance

parent cf51acfe
# This is a clouds.yaml file, which can be used by OpenStack tools as a source
# of configuration on how to connect to a cloud. If this is your only cloud,
# just put this file in ~/.config/openstack/clouds.yaml and tools like
# python-openstackclient will just work with no further config. (You will need
# to add your password to the auth section)
# If you have more than one cloud account, add the cloud entry to the clouds
# section of your existing file and you can refer to them by name with
# OS_CLOUD=openstack or --os-cloud=openstack
clouds:
openstack:
auth:
auth_url: http://10.34.0.113/identity/v3
username: "admin"
project_id: 2db5309f541c4466bc80bc534cf579d7
project_name: "admin"
password: "64c7ee341d03844c548c"
user_domain_name: "Default"
region_name: "RegionOne"
interface: "public"
identity_api_version: 3
# Generated by Django 2.2.1 on 2019-07-04 11:54
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('image', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('instance', '0005_auto_20190513_1203'),
]
operations = [
migrations.CreateModel(
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)),
('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)),
],
),
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)),
('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)),
],
),
migrations.AddField(
model_name='instance',
name='disks',
field=models.ManyToManyField(help_text='Disks attached to instance', related_name='instance', to='image.Disk', verbose_name='disks'),
),
migrations.AddField(
model_name='instance',
name='owner',
field=models.ForeignKey(default=None, on_delete='CASCADE', to=settings.AUTH_USER_MODEL),
preserve_default=False,
),
migrations.AlterField(
model_name='instance',
name='lease',
field=models.ForeignKey(on_delete='CASCADE', to='instance.Lease'),
),
migrations.AddField(
model_name='instance',
name='flavor',
field=models.ForeignKey(default=None, help_text='Reasources given to the vm', on_delete='CASCADE', to='instance.Flavor', verbose_name='flavor'),
preserve_default=False,
),
]
# Generated by Django 2.2.1 on 2019-07-04 12:56
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('instance', '0006_auto_20190704_1154'),
]
operations = [
migrations.AlterField(
model_name='instance',
name='owner',
field=models.ForeignKey(null=True, on_delete='CASCADE', to=settings.AUTH_USER_MODEL),
),
]
# Generated by Django 2.2.1 on 2019-07-04 13:10
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('instance', '0007_auto_20190704_1256'),
]
operations = [
migrations.AlterField(
model_name='instance',
name='time_of_delete',
field=models.DateTimeField(blank=True, help_text='After this point in time, the instance will be deleted!'),
),
migrations.AlterField(
model_name='instance',
name='time_of_suspend',
field=models.DateTimeField(blank=True, help_text='After this point in time, the instance will be suspended'),
),
]
from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from storage import Disk
from django.utils import timezone
from image.models import Disk
from implementation.vm.instance import OSVirtualMachineManager
import openstack
ACCESS_METHODS = tuple(
......@@ -26,6 +29,9 @@ class Flavor(models.Model):
"""
name = models.CharField(blank=True, max_length=100)
description = models.CharField(blank=True, max_length=200)
remote_id = models.CharField(
max_length=100, help_text="ID of the instance on the backend"
)
ram = models.IntegerField(blank=True, null=True)
vcpu = models.IntegerField(blank=True, null=True)
initial_disk = models.IntegerField(blank=True, null=True)
......@@ -54,9 +60,11 @@ class Instance(models.Model):
max_length=50, help_text="Original password of the instance"
)
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!"
)
deleted = models.BooleanField(
......@@ -74,7 +82,43 @@ class Instance(models.Model):
help_text="Disks attached to instance",
verbose_name="disks")
owner = models.ForeignKey(User)
owner = models.ForeignKey(User, on_delete="CASCADE", null=True)
flavor = models.ForeignKey(Flavor, help_text="Reasources given to the vm",
verbose_name="flavor")
lease = models.ForeignKey(Lease)
verbose_name="flavor", on_delete="CASCADE")
lease = models.ForeignKey(Lease, on_delete="CASCADE")
@classmethod
def create(cls, lease, owner, flavor, remote_id, params):
inst = cls(lease=lease, flavor=flavor, owner=owner,
remote_id=remote_id, **params)
inst.full_clean()
inst.save()
@classmethod
def create_instance_from_template(cls, params, template, owner, lease,
disks, networks, flavor):
conn = openstack.connect(cloud="openstack")
interface = OSVirtualMachineManager(conn)
remote_inst = interface.create_vm_from_template(params["name"],
template.remote_ID,
flavor.remote_id,
networks,
# disks
)
remote_id = remote_inst.id
new_inst = cls.create(lease, owner, flavor, remote_id, params)
return new_inst
def clean(self, *args, **kwargs):
# self.time_of_suspend, self.time_of_delete = self.get_renew_times()
super(Instance, self).clean(*args, **kwargs)
def get_renew_times(self, lease=None):
"""Returns new suspend and delete times if renew would be called.
"""
if lease is None:
lease = self.lease
return (
timezone.now() + lease.suspend_interval_in_sec,
timezone.now() + lease.delete_interval_in_sec)
......@@ -5,6 +5,11 @@ from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from implementation.vm.instance import OSVirtualMachineManager
from template.models import InstanceTemplate
from instance.models import Instance, Flavor, Lease
from django.contrib.auth.models import User
import openstack
import datetime
......@@ -20,26 +25,28 @@ class InstanceList(APIView):
def post(self, request, format=None):
data = request.data
interface = OSVirtualMachineManager(conn)
imageId = "da51253f-867c-472d-8ce0-81e7b7126d60"
flavorId = "1"
networks = [{"uuid": "c03d0d4b-413e-4cc6-9ebe-c0b5ca0dac3a"}]
newBackendInstance = interface.create_vm_from_template(
"Integration test vm 2", imageId, flavorId, networks
)
newInstance = Instance(
name=data["name"],
remote_id=newBackendInstance.id,
description=data["description"],
access_method="ssh",
system=data["system"],
password="12345678",
lease=data["lease"],
time_of_suspend=datetime.datetime.now(),
time_of_delete=datetime.datetime.now(),
deleted=False,
template = InstanceTemplate(name="test1", description="adsasdasd",
remote_ID="da51253f-867c-472d-8ce0-81e7b7126d60")
flavor = Flavor(name="asd", description="asd", remote_id="1")
lease = Lease(name="test_lease", description="test",
suspend_interval_in_sec=100,
delete_interval_in_sec=100)
newInstance = Instance.create_instance_from_template(
params={"name": data["name"],
"description": data["description"],
"access_method": "ssh",
"system": data["system"],
"deleted": False,
"password": "asd",
},
lease=lease,
networks=[{"uuid": "c03d0d4b-413e-4cc6-9ebe-c0b5ca0dac3a"}],
template=template,
flavor=flavor,
owner=User.objects.get(pk=1),
disks="asdasd"
)
newInstance.save()
return Response(newInstance.pk)
......
......@@ -42,6 +42,7 @@ INSTALLED_APPS = [
"djoser",
"rest_framework_swagger",
"corsheaders",
"template"
]
MIDDLEWARE = [
......
# Generated by Django 2.2.1 on 2019-07-04 12:12
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='InstanceTemplate',
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')),
('remote_ID', models.CharField(help_text='ID, which helps access the template.', max_length=40, unique=True, verbose_name='remote_ID')),
],
),
]
from django.db import models
class InstanceTemplate:
class InstanceTemplate(models.Model):
"""Virtual machine 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