Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
RECIRCLE
/
portal
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
11
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2b3c0497
authored
Jul 05, 2019
by
Belákovics Ádám
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement create method of Instance
parent
cf51acfe
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
222 additions
and
24 deletions
+222
-24
recircle/instance/clouds.yaml
+20
-0
recircle/instance/migrations/0006_auto_20190704_1154.py
+61
-0
recircle/instance/migrations/0007_auto_20190704_1256.py
+19
-0
recircle/instance/migrations/0008_auto_20190704_1310.py
+23
-0
recircle/instance/models.py
+48
-4
recircle/instance/views.py
+26
-19
recircle/recircle/settings.py
+1
-0
recircle/template/migrations/0001_initial.py
+23
-0
recircle/template/models.py
+1
-1
No files found.
recircle/instance/clouds.yaml
0 → 100644
View file @
2b3c0497
# 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
recircle/instance/migrations/0006_auto_20190704_1154.py
0 → 100644
View file @
2b3c0497
# 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
,
),
]
recircle/instance/migrations/0007_auto_20190704_1256.py
0 → 100644
View file @
2b3c0497
# 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
),
),
]
recircle/instance/migrations/0008_auto_20190704_1310.py
0 → 100644
View file @
2b3c0497
# 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'
),
),
]
recircle/instance/models.py
View file @
2b3c0497
from
django.db
import
models
from
django.db
import
models
from
django.conf
import
settings
from
django.conf
import
settings
from
django.contrib.auth.models
import
User
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
(
ACCESS_METHODS
=
tuple
(
...
@@ -26,6 +29,9 @@ class Flavor(models.Model):
...
@@ -26,6 +29,9 @@ class Flavor(models.Model):
"""
"""
name
=
models
.
CharField
(
blank
=
True
,
max_length
=
100
)
name
=
models
.
CharField
(
blank
=
True
,
max_length
=
100
)
description
=
models
.
CharField
(
blank
=
True
,
max_length
=
200
)
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
)
ram
=
models
.
IntegerField
(
blank
=
True
,
null
=
True
)
vcpu
=
models
.
IntegerField
(
blank
=
True
,
null
=
True
)
vcpu
=
models
.
IntegerField
(
blank
=
True
,
null
=
True
)
initial_disk
=
models
.
IntegerField
(
blank
=
True
,
null
=
True
)
initial_disk
=
models
.
IntegerField
(
blank
=
True
,
null
=
True
)
...
@@ -54,9 +60,11 @@ class Instance(models.Model):
...
@@ -54,9 +60,11 @@ class Instance(models.Model):
max_length
=
50
,
help_text
=
"Original password of the instance"
max_length
=
50
,
help_text
=
"Original password of the instance"
)
)
time_of_suspend
=
models
.
DateTimeField
(
time_of_suspend
=
models
.
DateTimeField
(
blank
=
True
,
help_text
=
"After this point in time, the instance will be suspended"
help_text
=
"After this point in time, the instance will be suspended"
)
)
time_of_delete
=
models
.
DateTimeField
(
time_of_delete
=
models
.
DateTimeField
(
blank
=
True
,
help_text
=
"After this point in time, the instance will be deleted!"
help_text
=
"After this point in time, the instance will be deleted!"
)
)
deleted
=
models
.
BooleanField
(
deleted
=
models
.
BooleanField
(
...
@@ -74,7 +82,43 @@ class Instance(models.Model):
...
@@ -74,7 +82,43 @@ class Instance(models.Model):
help_text
=
"Disks attached to instance"
,
help_text
=
"Disks attached to instance"
,
verbose_name
=
"disks"
)
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"
,
flavor
=
models
.
ForeignKey
(
Flavor
,
help_text
=
"Reasources given to the vm"
,
verbose_name
=
"flavor"
)
verbose_name
=
"flavor"
,
on_delete
=
"CASCADE"
)
lease
=
models
.
ForeignKey
(
Lease
)
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
)
recircle/instance/views.py
View file @
2b3c0497
...
@@ -5,6 +5,11 @@ from rest_framework.views import APIView
...
@@ -5,6 +5,11 @@ from rest_framework.views import APIView
from
rest_framework.response
import
Response
from
rest_framework.response
import
Response
from
rest_framework
import
status
from
rest_framework
import
status
from
implementation.vm.instance
import
OSVirtualMachineManager
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
openstack
import
datetime
import
datetime
...
@@ -20,26 +25,28 @@ class InstanceList(APIView):
...
@@ -20,26 +25,28 @@ class InstanceList(APIView):
def
post
(
self
,
request
,
format
=
None
):
def
post
(
self
,
request
,
format
=
None
):
data
=
request
.
data
data
=
request
.
data
interface
=
OSVirtualMachineManager
(
conn
)
template
=
InstanceTemplate
(
name
=
"test1"
,
description
=
"adsasdasd"
,
imageId
=
"da51253f-867c-472d-8ce0-81e7b7126d60"
remote_ID
=
"da51253f-867c-472d-8ce0-81e7b7126d60"
)
flavorId
=
"1"
flavor
=
Flavor
(
name
=
"asd"
,
description
=
"asd"
,
remote_id
=
"1"
)
networks
=
[{
"uuid"
:
"c03d0d4b-413e-4cc6-9ebe-c0b5ca0dac3a"
}]
lease
=
Lease
(
name
=
"test_lease"
,
description
=
"test"
,
newBackendInstance
=
interface
.
create_vm_from_template
(
suspend_interval_in_sec
=
100
,
"Integration test vm 2"
,
imageId
,
flavorId
,
networks
delete_interval_in_sec
=
100
)
)
newInstance
=
Instance
(
newInstance
=
Instance
.
create_instance_from_template
(
name
=
data
[
"name"
],
params
=
{
"name"
:
data
[
"name"
],
remote_id
=
newBackendInstance
.
id
,
"description"
:
data
[
"description"
],
description
=
data
[
"description"
],
"access_method"
:
"ssh"
,
access_method
=
"ssh"
,
"system"
:
data
[
"system"
],
system
=
data
[
"system"
],
"deleted"
:
False
,
password
=
"12345678"
,
"password"
:
"asd"
,
lease
=
data
[
"lease"
],
},
time_of_suspend
=
datetime
.
datetime
.
now
(),
lease
=
lease
,
time_of_delete
=
datetime
.
datetime
.
now
(),
networks
=
[{
"uuid"
:
"c03d0d4b-413e-4cc6-9ebe-c0b5ca0dac3a"
}],
deleted
=
False
,
template
=
template
,
flavor
=
flavor
,
owner
=
User
.
objects
.
get
(
pk
=
1
),
disks
=
"asdasd"
)
)
newInstance
.
save
()
return
Response
(
newInstance
.
pk
)
return
Response
(
newInstance
.
pk
)
...
...
recircle/recircle/settings.py
View file @
2b3c0497
...
@@ -42,6 +42,7 @@ INSTALLED_APPS = [
...
@@ -42,6 +42,7 @@ INSTALLED_APPS = [
"djoser"
,
"djoser"
,
"rest_framework_swagger"
,
"rest_framework_swagger"
,
"corsheaders"
,
"corsheaders"
,
"template"
]
]
MIDDLEWARE
=
[
MIDDLEWARE
=
[
...
...
recircle/template/migrations/0001_initial.py
0 → 100644
View file @
2b3c0497
# 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'
)),
],
),
]
recircle/template/models.py
View file @
2b3c0497
from
django.db
import
models
from
django.db
import
models
class
InstanceTemplate
:
class
InstanceTemplate
(
models
.
Model
)
:
"""Virtual machine template.
"""Virtual machine template.
"""
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment