scheduled_vm_cleanup.py 3.41 KB
Newer Older
Bach Dániel committed
1
# -*- coding: utf-8 -*-
Őry Máté committed
2
from django_extensions.management.jobs import HourlyJob
Bach Dániel committed
3 4 5 6 7 8
import datetime
from django.utils.timezone import utc
from one.models import Instance
from django.template.loader import render_to_string
from one.tasks import SendMailTask
from django.utils.translation import ugettext_lazy as _
Bach Dániel committed
9
from django.conf import settings
Őry Máté committed
10
from django.utils import translation
11

Őry Máté committed
12 13 14
class Job(HourlyJob):
    help = "Suspend/delete expired Instances."

Bach Dániel committed
15 16 17
    def calc(self, orig, days=0, hours=0):
        return (orig + datetime.timedelta(days=days, hours=hours)).replace(minute=0, second=0, microsecond=0)

Őry Máté committed
18
    def execute(self):
Bach Dániel committed
19 20
        url = settings.CLOUD_URL
        site = settings.SITE_NAME
Bach Dániel committed
21 22 23 24 25 26
        now = datetime.datetime.utcnow().replace(tzinfo=utc)
        d = {
            '1m': self.calc(orig=now, days=30),
            '2w': self.calc(orig=now, days=14),
            '1w': self.calc(orig=now, days=7),
            '1d': self.calc(orig=now, days=1),
27
            '1h': self.calc(orig=now, hours=1),
Bach Dániel committed
28 29 30 31 32
        }
        # for i in d:
        #    print i+':'+unicode(d[i])

        # delete
Bach Dániel committed
33 34
        for i in Instance.objects.filter(state__in=['ACTIVE', 'STOPPED'],
                time_of_delete__isnull=False, waiting=False):
Őry Máté committed
35 36 37 38
            try:
                translation.activate(i.owner.person_set.get().language)
            except:
                pass
x committed
39
#            print u'%s delete: %s' % (i.name, i.time_of_delete)
Bach Dániel committed
40
            delete = i.time_of_delete.replace(minute=0, second=0, microsecond=0)
41 42
            if not settings.DELETE_VM:
                continue
43
            if i.time_of_delete < now:
Bach Dániel committed
44 45 46 47 48
                msg = render_to_string('mails/notification-delete-now.txt',
                        { 'user': i.owner, 'instance': i, 'url': url, 'site': site } )
                SendMailTask.delay(to=i.owner.email, subject='[%s] %s' %
                        (site, i.name), msg=msg)
                i.one_delete()
Bach Dániel committed
49 50 51
            else:
                for t in d:
                    if delete == d[t]:
Bach Dániel committed
52 53 54 55
                        msg = render_to_string('mails/notification-delete.txt',
                                { 'user': i.owner, 'instance': i, 'url': url, 'site': site } )
                        SendMailTask.delay(to=i.owner.email, subject='[%s] %s' %
                                (site, i.name), msg=msg)
Bach Dániel committed
56 57

        # suspend
Bach Dániel committed
58 59
        for i in Instance.objects.filter(state='ACTIVE',
                time_of_suspend__isnull=False, waiting=False):
Őry Máté committed
60 61 62 63
            try:
                translation.activate(i.owner.person_set.get().language)
            except:
                pass
x committed
64
#            print u'%s suspend: %s' % (i.name, i.time_of_suspend)
Bach Dániel committed
65 66
            suspend = i.time_of_suspend.replace(minute=0, second=0, microsecond=0)

67
            if i.time_of_suspend < now:
Bach Dániel committed
68 69 70 71
                msg = render_to_string('mails/notification-suspend-now.txt',
                        { 'user': i.owner, 'instance': i, 'url': url, 'site': site } )
                SendMailTask.delay(to=i.owner.email, subject='[%s] %s' %
                        (site, i.name), msg=msg)
Bach Dániel committed
72 73 74 75
                i.stop()
            else:
                for t in d:
                    if suspend == d[t]:
Bach Dániel committed
76 77 78 79
                        msg = render_to_string('mails/notification-suspend.txt',
                                { 'user': i.owner, 'instance': i, 'url': url, 'site': site } )
                        SendMailTask.delay(to=i.owner.email, subject='[%s] %s' %
                                (site, i.name), msg=msg)