Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
circlestack
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
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
6814e12d
authored
May 16, 2018
by
Szabolcs Gelencser
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement lease task operations
parent
b4fee3ab
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
49 additions
and
7 deletions
+49
-7
.idea/workspace.xml
+0
-0
circle/vm/migrations/0013_vmlease_suspend_done.py
+20
-0
circle/vm/models/vm_lease.py
+4
-1
circle/vm/operations.py
+5
-6
circle/vm/tasks/lease_tasks.py
+20
-0
No files found.
.idea/workspace.xml
View file @
6814e12d
This diff is collapsed.
Click to expand it.
circle/vm/migrations/0013_vmlease_suspend_done.py
0 → 100644
View file @
6814e12d
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-05-16 12:49
from
__future__
import
unicode_literals
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'vm'
,
'0012_auto_20180515_1151'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'vmlease'
,
name
=
'suspend_done'
,
field
=
models
.
BooleanField
(
default
=
False
),
),
]
circle/vm/models/vm_lease.py
View file @
6814e12d
from
django.db.models
import
Model
,
ForeignKey
,
CharField
,
DateTimeField
from
django.db.models
import
Model
,
ForeignKey
,
CharField
,
DateTimeField
,
BooleanField
from
django.conf
import
settings
from
django.utils
import
timezone
from
vm.models
import
Lease
...
...
@@ -18,6 +18,7 @@ class VmLease(Model):
verbose_name
=
_
(
'time of delete'
),
help_text
=
_
(
"Proposed time of automatic "
"deletion."
))
suspend_done
=
BooleanField
(
blank
=
False
,
default
=
False
)
def
get_renew_times
(
self
,
lease
=
None
):
"""Returns new suspend and delete times if renew would be called.
...
...
@@ -30,7 +31,9 @@ class VmLease(Model):
def
clean
(
self
,
*
args
,
**
kwargs
):
self
.
time_of_suspend
,
self
.
time_of_delete
=
self
.
get_renew_times
()
self
.
suspend_done
=
False
super
(
VmLease
,
self
)
.
clean
(
*
args
,
**
kwargs
)
return
self
def
is_suspend_expiring
(
self
,
threshold
=
0.1
):
limit
=
timezone
.
now
()
+
timedelta
(
seconds
=
(
...
...
circle/vm/operations.py
View file @
6814e12d
...
...
@@ -309,6 +309,7 @@ class DeployOperation(InstanceOperation):
def
_operation
(
self
,
request
,
node
=
None
):
openstack_api
.
nova
.
server_start
(
request
,
self
.
instance
.
id
)
VmLease
.
get_or_create_lease
(
self
.
instance
.
id
)
.
clean
()
.
save
()
@register_operation
...
...
@@ -629,14 +630,9 @@ class WakeUpOperation(InstanceOperation):
def
is_preferred
(
self
):
return
self
.
instance
.
status
==
self
.
instance
.
STATUS
.
SUSPENDED
def
on_abort
(
self
,
activity
,
error
):
if
isinstance
(
error
,
SchedulerError
):
activity
.
resultant_state
=
None
else
:
activity
.
resultant_state
=
'ERROR'
def
_operation
(
self
,
request
):
openstack_api
.
nova
.
server_resume
(
request
,
self
.
instance
.
id
)
VmLease
.
get_or_create_lease
(
self
.
instance
.
id
)
.
clean
()
.
save
()
@register_operation
...
...
@@ -679,6 +675,9 @@ class RenewOperation(InstanceOperation):
if
save
:
vm_lease
.
lease
=
lease
vm_lease
.
suspend_done
=
False
vm_lease
.
delete_done
=
False
vm_lease
.
save
()
return
create_readable
(
ugettext_noop
(
...
...
circle/vm/tasks/lease_tasks.py
View file @
6814e12d
from
celery.decorators
import
periodic_task
from
celery.task.schedules
import
crontab
from
django.conf
import
settings
from
django.utils
import
timezone
from
openstack_auth.utils
import
fix_auth_url_version
from
keystoneauth1.identity
import
v3
from
keystoneauth1
import
session
...
...
@@ -38,6 +39,23 @@ def get_project_client(project):
return
client
.
Client
(
"2.0"
,
session
=
sess
)
def
handle_suspend
(
client
,
server
,
lease
):
now
=
timezone
.
now
()
if
now
>
lease
.
time_of_suspend
and
server
.
status
==
'ACTIVE'
:
if
lease
.
suspend_done
:
lease
.
clean
()
.
save
()
else
:
client
.
servers
.
suspend
(
server
)
lease
.
suspend_done
=
True
lease
.
save
()
def
handle_destroy
(
client
,
server
,
lease
):
now
=
timezone
.
now
()
if
now
>
lease
.
time_of_delete
:
client
.
servers
.
delete
(
server
)
@periodic_task
(
run_every
=
crontab
(
hour
=
"*"
,
minute
=
"*"
,
day_of_week
=
"*"
))
def
check_lease_expiration
():
projects
=
get_projects
()
...
...
@@ -46,3 +64,5 @@ def check_lease_expiration():
servers
=
client
.
servers
.
list
()
for
server
in
servers
:
lease
=
VmLease
.
get_or_create_lease
(
server
.
id
)
handle_suspend
(
client
,
server
,
lease
)
handle_destroy
(
client
,
server
,
lease
)
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