Commit 07d4ec62 by Bach Dániel

SendMailTask added

parent ff4144e6
......@@ -188,6 +188,7 @@ CELERY_ROUTES = {
'firewall.tasks.reload_dhcp_task': {'queue': 'dhcp'},
'firewall.tasks.reload_blacklist_task': {'queue': 'firewall'},
'firewall.tasks.Periodic': {'queue': 'local'},
'one.tasks.SendMailTask': {'queue': 'local'},
}
store_settings = {
......
......@@ -20,7 +20,7 @@ def reload_blacklist_task(data):
pass
class Periodic(PeriodicTask):
run_every = timedelta(seconds=60)
run_every = timedelta(seconds=10)
def run(self, **kwargs):
......
......@@ -53,11 +53,8 @@ def firewall_api(request):
s = render_to_string('mails/notification-ban-now.txt', { 'user': user, 'bl': obj } )
print s
# send_mail(settings.EMAIL_SUBJECT_PREFIX + (_('New project: %s') % p.identifier), s, settings.SERVER_EMAIL, [])
except Host.DoesNotExist, ValidationError, IntegrityError, AttributeError as e:
except (Host.DoesNotExist, ValidationError, IntegrityError, AttributeError):
pass
except:
raise
print "ok"
print obj.modified_at + datetime.timedelta(minutes=5)
print datetime.datetime.utcnow().replace(tzinfo=utc)
if obj.type == 'tempwhite' and obj.modified_at + datetime.timedelta(minutes=1) < datetime.datetime.utcnow().replace(tzinfo=utc):
......
# -*- coding: utf-8 -*-
from django_extensions.management.jobs import HourlyJob
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 _
class Job(HourlyJob):
help = "Suspend/delete expired Instances."
def calc(self, orig, days=0, hours=0):
return (orig + datetime.timedelta(days=days, hours=hours)).replace(minute=0, second=0, microsecond=0)
def execute(self):
# executing empty sample job TODO
pass
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),
'1h': self.calc(orig=now, hours=2),
}
# for i in d:
# print i+':'+unicode(d[i])
# delete
for i in Instance.objects.filter(state__in=['ACTIVE', 'STOPPED'], time_of_delete__isnull=False):
print "%s delete: %s" % (i.name, i.time_of_delete)
delete = i.time_of_delete.replace(minute=0, second=0, microsecond=0)
if delete < now:
msg = render_to_string('mails/notification-delete-now.txt', { 'user': i.owner, 'instance': i } )
SendMailTask.delay(to=i.owner.email, subject=_('Delete notification'), msg=msg)
else:
for t in d:
if delete == d[t]:
msg = render_to_string('mails/notification-delete.txt', { 'user': i.owner, 'instance': i } )
SendMailTask.delay(to=i.owner.email, subject=_('Delete notification'), msg=msg)
# suspend
for i in Instance.objects.filter(state='ACTIVE', time_of_suspend__isnull=False):
print "%s suspend: %s" % (i.name, i.time_of_suspend)
suspend = i.time_of_suspend.replace(minute=0, second=0, microsecond=0)
if suspend < now:
msg = render_to_string('mails/notification-suspend-now.txt', { 'user': i.owner, 'instance': i } )
SendMailTask.delay(to=i.owner.email, subject=_('Stop notification'), msg=msg)
i.stop()
else:
for t in d:
if suspend == d[t]:
msg = render_to_string('mails/notification-suspend.txt', { 'user': i.owner, 'instance': i } )
SendMailTask.delay(to=i.owner.email, subject=_('Stop notification'), msg=msg)
......@@ -2,9 +2,11 @@ from one.models import *
from django_extensions.management.jobs import HourlyJob
class Job(HourlyJob):
help = "Update Disks and Networks from OpenNebula."
help = "Update Disks, Networks and Instances from OpenNebula."
def execute(self):
Disk.update()
Network.update()
for i in Instance.objects.filter(state__in=['ACTIVE', 'STOPPED'], time_of_delete__isnull=False):
i.update_state()
pass
from celery.task import Task, PeriodicTask
import logging
import celery
import os
import sys
import time
logger = logging.getLogger(__name__)
class SendMailTask(Task):
def run(self, to, subject, msg):
sender = u'cloud@ik.bme.hu'
print u'%s->%s [%s]' % (sender, to, subject)
logger.info("[django][one][tasks.py] %s", msg)
{% base base.txt %}
{% extends "mails/base.txt" %}
{% load i18n %}
{% block body %}
{% blocktrans with vm=instance.name state=instance.state date=exp time=exp|timeuntil %}
{% blocktrans with vm=instance.name state=instance.state date=instance.time_of_delete time=exp|timeuntil %}
Your {{state}} virtual machine "{{vm}}" is going to be DELETED
at {{date}} (in {{time}}).
{% endblocktrans %}
......
{% base base.txt %}
{% extends "mails/base.txt" %}
{% load i18n %}
{% block body %}
{% blocktrans with vm=instance.name state=instance.state date=exp deldate=exp2 %}
{% blocktrans with vm=instance.name state=instance.state date=instance.time_of_suspend %}
Your {{state}} virtual machine "{{vm}}" has been STOPPED
at {{date}}.
{% endblocktrans %}
{% blocktrans with deldate=exp2 %}
{% blocktrans with deldate=instance.time_of_delete %}
The disk and memory image is stored, and you can resume it
until the final expiration time ({{deldate}}).
{% endblocktrans %}
......
{% base base.txt %}
{% extends "mails/base.txt" %}
{% load i18n %}
{% block body %}
{% blocktrans with vm=instance.name state=instance.state date=exp time=exp|timeuntil url=url %}
{% blocktrans with vm=instance.name state=instance.state date=instance.time_of_suspend time=exp|timeuntil url=url %}
Your {{state}} virtual machine "{{vm}}" is going to be STOPPED
at {{date}} (in {{time}}).
{% endblocktrans %}
{% blocktrans with deldate=exp2 %}
{% blocktrans with deldate=instance.time_of_delete %}
The disk and memory image will be stored, and you can resume it
until the final expiration time ({{deldate}}).
{% endblocktrans %}
......
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