Commit 6814e12d by Szabolcs Gelencser

Implement lease task operations

parent b4fee3ab
# -*- 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),
),
]
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.conf import settings
from django.utils import timezone from django.utils import timezone
from vm.models import Lease from vm.models import Lease
...@@ -18,6 +18,7 @@ class VmLease(Model): ...@@ -18,6 +18,7 @@ class VmLease(Model):
verbose_name=_('time of delete'), verbose_name=_('time of delete'),
help_text=_("Proposed time of automatic " help_text=_("Proposed time of automatic "
"deletion.")) "deletion."))
suspend_done = BooleanField(blank=False, default=False)
def get_renew_times(self, lease=None): def get_renew_times(self, lease=None):
"""Returns new suspend and delete times if renew would be called. """Returns new suspend and delete times if renew would be called.
...@@ -30,7 +31,9 @@ class VmLease(Model): ...@@ -30,7 +31,9 @@ class VmLease(Model):
def clean(self, *args, **kwargs): def clean(self, *args, **kwargs):
self.time_of_suspend, self.time_of_delete = self.get_renew_times() self.time_of_suspend, self.time_of_delete = self.get_renew_times()
self.suspend_done = False
super(VmLease, self).clean(*args, **kwargs) super(VmLease, self).clean(*args, **kwargs)
return self
def is_suspend_expiring(self, threshold=0.1): def is_suspend_expiring(self, threshold=0.1):
limit = timezone.now() + timedelta(seconds=( limit = timezone.now() + timedelta(seconds=(
......
...@@ -309,6 +309,7 @@ class DeployOperation(InstanceOperation): ...@@ -309,6 +309,7 @@ class DeployOperation(InstanceOperation):
def _operation(self, request, node=None): def _operation(self, request, node=None):
openstack_api.nova.server_start(request, self.instance.id) openstack_api.nova.server_start(request, self.instance.id)
VmLease.get_or_create_lease(self.instance.id).clean().save()
@register_operation @register_operation
...@@ -629,14 +630,9 @@ class WakeUpOperation(InstanceOperation): ...@@ -629,14 +630,9 @@ class WakeUpOperation(InstanceOperation):
def is_preferred(self): def is_preferred(self):
return self.instance.status == self.instance.STATUS.SUSPENDED 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): def _operation(self, request):
openstack_api.nova.server_resume(request, self.instance.id) openstack_api.nova.server_resume(request, self.instance.id)
VmLease.get_or_create_lease(self.instance.id).clean().save()
@register_operation @register_operation
...@@ -679,6 +675,9 @@ class RenewOperation(InstanceOperation): ...@@ -679,6 +675,9 @@ class RenewOperation(InstanceOperation):
if save: if save:
vm_lease.lease = lease vm_lease.lease = lease
vm_lease.suspend_done = False
vm_lease.delete_done = False
vm_lease.save() vm_lease.save()
return create_readable(ugettext_noop( return create_readable(ugettext_noop(
......
from celery.decorators import periodic_task from celery.decorators import periodic_task
from celery.task.schedules import crontab from celery.task.schedules import crontab
from django.conf import settings from django.conf import settings
from django.utils import timezone
from openstack_auth.utils import fix_auth_url_version from openstack_auth.utils import fix_auth_url_version
from keystoneauth1.identity import v3 from keystoneauth1.identity import v3
from keystoneauth1 import session from keystoneauth1 import session
...@@ -38,6 +39,23 @@ def get_project_client(project): ...@@ -38,6 +39,23 @@ def get_project_client(project):
return client.Client("2.0", session=sess) 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="*")) @periodic_task(run_every=crontab(hour="*", minute="*", day_of_week="*"))
def check_lease_expiration(): def check_lease_expiration():
projects = get_projects() projects = get_projects()
...@@ -46,3 +64,5 @@ def check_lease_expiration(): ...@@ -46,3 +64,5 @@ def check_lease_expiration():
servers = client.servers.list() servers = client.servers.list()
for server in servers: for server in servers:
lease = VmLease.get_or_create_lease(server.id) lease = VmLease.get_or_create_lease(server.id)
handle_suspend(client, server, lease)
handle_destroy(client, server, lease)
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