Commit 07d4ec62 by Bach Dániel

SendMailTask added

parent ff4144e6
...@@ -188,6 +188,7 @@ CELERY_ROUTES = { ...@@ -188,6 +188,7 @@ CELERY_ROUTES = {
'firewall.tasks.reload_dhcp_task': {'queue': 'dhcp'}, 'firewall.tasks.reload_dhcp_task': {'queue': 'dhcp'},
'firewall.tasks.reload_blacklist_task': {'queue': 'firewall'}, 'firewall.tasks.reload_blacklist_task': {'queue': 'firewall'},
'firewall.tasks.Periodic': {'queue': 'local'}, 'firewall.tasks.Periodic': {'queue': 'local'},
'one.tasks.SendMailTask': {'queue': 'local'},
} }
store_settings = { store_settings = {
......
...@@ -20,7 +20,7 @@ def reload_blacklist_task(data): ...@@ -20,7 +20,7 @@ def reload_blacklist_task(data):
pass pass
class Periodic(PeriodicTask): class Periodic(PeriodicTask):
run_every = timedelta(seconds=60) run_every = timedelta(seconds=10)
def run(self, **kwargs): def run(self, **kwargs):
......
...@@ -53,11 +53,8 @@ def firewall_api(request): ...@@ -53,11 +53,8 @@ def firewall_api(request):
s = render_to_string('mails/notification-ban-now.txt', { 'user': user, 'bl': obj } ) s = render_to_string('mails/notification-ban-now.txt', { 'user': user, 'bl': obj } )
print s print s
# send_mail(settings.EMAIL_SUBJECT_PREFIX + (_('New project: %s') % p.identifier), s, settings.SERVER_EMAIL, []) # 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 pass
except:
raise
print "ok"
print obj.modified_at + datetime.timedelta(minutes=5) print obj.modified_at + datetime.timedelta(minutes=5)
print datetime.datetime.utcnow().replace(tzinfo=utc) 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): 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 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): class Job(HourlyJob):
help = "Suspend/delete expired Instances." 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): def execute(self):
# executing empty sample job TODO now = datetime.datetime.utcnow().replace(tzinfo=utc)
pass 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 * ...@@ -2,9 +2,11 @@ from one.models import *
from django_extensions.management.jobs import HourlyJob from django_extensions.management.jobs import HourlyJob
class Job(HourlyJob): class Job(HourlyJob):
help = "Update Disks and Networks from OpenNebula." help = "Update Disks, Networks and Instances from OpenNebula."
def execute(self): def execute(self):
Disk.update() Disk.update()
Network.update() Network.update()
for i in Instance.objects.filter(state__in=['ACTIVE', 'STOPPED'], time_of_delete__isnull=False):
i.update_state()
pass 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 %} {% load i18n %}
{% block body %} {% 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 Your {{state}} virtual machine "{{vm}}" is going to be DELETED
at {{date}} (in {{time}}). at {{date}} (in {{time}}).
{% endblocktrans %} {% endblocktrans %}
......
{% base base.txt %} {% extends "mails/base.txt" %}
{% load i18n %} {% load i18n %}
{% block body %} {% 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 Your {{state}} virtual machine "{{vm}}" has been STOPPED
at {{date}}. at {{date}}.
{% endblocktrans %} {% endblocktrans %}
{% blocktrans with deldate=exp2 %} {% blocktrans with deldate=instance.time_of_delete %}
The disk and memory image is stored, and you can resume it The disk and memory image is stored, and you can resume it
until the final expiration time ({{deldate}}). until the final expiration time ({{deldate}}).
{% endblocktrans %} {% endblocktrans %}
......
{% base base.txt %} {% extends "mails/base.txt" %}
{% load i18n %} {% load i18n %}
{% block body %} {% 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 Your {{state}} virtual machine "{{vm}}" is going to be STOPPED
at {{date}} (in {{time}}). at {{date}} (in {{time}}).
{% endblocktrans %} {% 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 The disk and memory image will be stored, and you can resume it
until the final expiration time ({{deldate}}). until the final expiration time ({{deldate}}).
{% endblocktrans %} {% 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