activity.py 8.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

18
from __future__ import absolute_import, unicode_literals
Őry Máté committed
19 20 21
from contextlib import contextmanager
from logging import getLogger

22
from celery.signals import worker_ready
Kálmán Viktor committed
23 24
from celery.contrib.abortable import AbortableAsyncResult

25
from django.core.urlresolvers import reverse
26
from django.db.models import CharField, ForeignKey
Őry Máté committed
27 28 29
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

30
from common.models import (
31
    ActivityModel, activitycontextimpl, join_activity_code, split_activity_code
32 33
)

Kálmán Viktor committed
34 35
from manager.mancelery import celery

36

Őry Máté committed
37 38 39
logger = getLogger(__name__)


40 41 42 43 44 45 46 47 48 49 50 51
class ActivityInProgressError(Exception):

        def __init__(self, activity, message=None):
            if message is None:
                message = ("Another activity is currently in progress: '%s'."
                           % activity.activity_code)

            Exception.__init__(self, message)

            self.activity = activity


Őry Máté committed
52
class InstanceActivity(ActivityModel):
53
    ACTIVITY_CODE_BASE = join_activity_code('vm', 'Instance')
Őry Máté committed
54 55 56
    instance = ForeignKey('Instance', related_name='activity_log',
                          help_text=_('Instance this activity works on.'),
                          verbose_name=_('instance'))
57
    resultant_state = CharField(blank=True, max_length=20, null=True)
Őry Máté committed
58 59 60 61

    class Meta:
        app_label = 'vm'
        db_table = 'vm_instanceactivity'
62
        ordering = ['-finished', '-started', 'instance', '-id']
Őry Máté committed
63 64 65 66 67 68 69 70 71 72

    def __unicode__(self):
        if self.parent:
            return '{}({})->{}'.format(self.parent.activity_code,
                                       self.instance,
                                       self.activity_code)
        else:
            return '{}({})'.format(self.activity_code,
                                   self.instance)

Dudás Ádám committed
73 74
    def abort(self):
        AbortableAsyncResult(self.task_uuid, backend=celery.backend).abort()
75

Őry Máté committed
76
    @classmethod
77 78
    def create(cls, code_suffix, instance, task_uuid=None, user=None,
               concurrency_check=True):
79 80
        # Check for concurrent activities
        active_activities = instance.activity_log.filter(finished__isnull=True)
81
        if concurrency_check and active_activities.exists():
82 83
            raise ActivityInProgressError(active_activities[0])

84 85 86 87
        activity_code = join_activity_code(cls.ACTIVITY_CODE_BASE, code_suffix)
        act = cls(activity_code=activity_code, instance=instance, parent=None,
                  resultant_state=None, started=timezone.now(),
                  task_uuid=task_uuid, user=user)
Őry Máté committed
88 89 90
        act.save()
        return act

91
    def create_sub(self, code_suffix, task_uuid=None, concurrency_check=True):
92 93
        # Check for concurrent activities
        active_children = self.children.filter(finished__isnull=True)
94
        if concurrency_check and active_children.exists():
95 96
            raise ActivityInProgressError(active_children[0])

Őry Máté committed
97
        act = InstanceActivity(
98
            activity_code=join_activity_code(self.activity_code, code_suffix),
99 100
            instance=self.instance, parent=self, resultant_state=None,
            started=timezone.now(), task_uuid=task_uuid, user=self.user)
Őry Máté committed
101 102 103
        act.save()
        return act

Dudás Ádám committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    def get_absolute_url(self):
        return reverse('dashboard.views.vm-activity', args=[self.pk])

    def get_readable_name(self):
        activity_code_last_suffix = split_activity_code(self.activity_code)[-1]
        return activity_code_last_suffix.replace('_', ' ').capitalize()

    def get_status_id(self):
        if self.succeeded is None:
            return 'wait'
        elif self.succeeded:
            return 'success'
        else:
            return 'failed'

119 120 121 122 123 124 125 126
    def has_percentage(self):
        op = self.instance.get_operation_from_activity_code(self.activity_code)
        return (self.task_uuid and op and op.has_percentage
                and not self.finished)

    def get_percentage(self):
        """Returns the percentage of the running operation if available.
        """
127 128 129 130 131
        result = celery.AsyncResult(id=self.task_uuid)
        if self.has_percentage() and result.info is not None:
            return result.info.get("percent")
        else:
            return 0
132

Dudás Ádám committed
133 134 135 136 137
    @property
    def is_abortable(self):
        """Can the activity be aborted?

        :returns: True if the activity can be aborted; otherwise, False.
138
        """
Dudás Ádám committed
139 140
        op = self.instance.get_operation_from_activity_code(self.activity_code)
        return self.task_uuid and op and op.abortable and not self.finished
Őry Máté committed
141

142 143 144 145 146 147 148
    def is_abortable_for(self, user):
        """Can the given user abort the activity?
        """

        return self.is_abortable and (
            user.is_superuser or user in (self.instance.owner, self.user))

149 150 151 152 153 154 155 156 157
    @property
    def is_aborted(self):
        """Has the activity been aborted?

        :returns: True if the activity has been aborted; otherwise, False.
        """
        return self.task_uuid and AbortableAsyncResult(self.task_uuid
                                                       ).is_aborted()

158 159 160 161 162
    def save(self, *args, **kwargs):
        ret = super(InstanceActivity, self).save(*args, **kwargs)
        self.instance._update_status()
        return ret

Őry Máté committed
163
    @contextmanager
164
    def sub_activity(self, code_suffix, on_abort=None, on_commit=None,
165
                     task_uuid=None, concurrency_check=True):
166 167
        """Create a transactional context for a nested instance activity.
        """
168
        act = self.create_sub(code_suffix, task_uuid, concurrency_check)
169
        return activitycontextimpl(act, on_abort=on_abort, on_commit=on_commit)
Őry Máté committed
170 171 172


@contextmanager
173
def instance_activity(code_suffix, instance, on_abort=None, on_commit=None,
174
                      task_uuid=None, user=None, concurrency_check=True):
175 176
    """Create a transactional context for an instance activity.
    """
177 178
    act = InstanceActivity.create(code_suffix, instance, task_uuid, user,
                                  concurrency_check)
179
    return activitycontextimpl(act, on_abort=on_abort, on_commit=on_commit)
180 181 182


class NodeActivity(ActivityModel):
183
    ACTIVITY_CODE_BASE = join_activity_code('vm', 'Node')
184 185 186 187 188 189 190 191
    node = ForeignKey('Node', related_name='activity_log',
                      help_text=_('Node this activity works on.'),
                      verbose_name=_('node'))

    class Meta:
        app_label = 'vm'
        db_table = 'vm_nodeactivity'

192 193 194 195 196 197 198 199 200 201 202 203
    def __unicode__(self):
        if self.parent:
            return '{}({})->{}'.format(self.parent.activity_code,
                                       self.node,
                                       self.activity_code)
        else:
            return '{}({})'.format(self.activity_code,
                                   self.node)

    def get_readable_name(self):
        return self.activity_code.split('.')[-1].replace('_', ' ').capitalize()

204 205
    @classmethod
    def create(cls, code_suffix, node, task_uuid=None, user=None):
206 207 208
        activity_code = join_activity_code(cls.ACTIVITY_CODE_BASE, code_suffix)
        act = cls(activity_code=activity_code, node=node, parent=None,
                  started=timezone.now(), task_uuid=task_uuid, user=user)
209 210 211 212 213
        act.save()
        return act

    def create_sub(self, code_suffix, task_uuid=None):
        act = NodeActivity(
214
            activity_code=join_activity_code(self.activity_code, code_suffix),
215 216 217 218 219 220 221 222 223 224 225 226 227
            node=self.node, parent=self, started=timezone.now(),
            task_uuid=task_uuid, user=self.user)
        act.save()
        return act

    @contextmanager
    def sub_activity(self, code_suffix, task_uuid=None):
        act = self.create_sub(code_suffix, task_uuid)
        return activitycontextimpl(act)


@contextmanager
def node_activity(code_suffix, node, task_uuid=None, user=None):
228
    act = NodeActivity.create(code_suffix, node, task_uuid, user)
229
    return activitycontextimpl(act)
230 231 232 233 234 235 236 237


@worker_ready.connect()
def cleanup(conf=None, **kwargs):
    # TODO check if other manager workers are running
    for i in InstanceActivity.objects.filter(finished__isnull=True):
        i.finish(False, "Manager is restarted, activity is cleaned up. "
                 "You can try again now.")
238
        logger.error('Forced finishing stale activity %s', i)
239 240 241
    for i in NodeActivity.objects.filter(finished__isnull=True):
        i.finish(False, "Manager is restarted, activity is cleaned up. "
                 "You can try again now.")
242
        logger.error('Forced finishing stale activity %s', i)