Revision 15d4014d

/dev/null
1
from django.core.management import setup_environ  
2
import settings
3
setup_environ(settings)
4
from django.core.mail import send_mail
5
from flowspy.flowspec.models import *
6
from django.template.loader import render_to_string
7
from django.contrib.sites.models import Site
8
from django.core.urlresolvers import reverse
9

  
10
import datetime
11

  
12

  
13
def notify_expired():
14
    routes = Route.objects.all()
15
    for route in routes:
16
        if route.status not in ['EXPIRED', 'ADMININACTIVE', 'INACTIVE', 'ERROR']:
17
            expiration_days = (route.expires - datetime.date.today()).days
18
            if expiration_days < settings.EXPIRATION_NOTIFY_DAYS:
19
                try:
20
                    fqdn = Site.objects.get_current().domain
21
                    admin_url = "https://%s%s" % \
22
                    (fqdn,
23
                     "/fod/edit/%s"%route.name)
24
                    mail_body = render_to_string("rule_expiration.txt",
25
                                             {"route": route, 'expiration_days':expiration_days, 'url':admin_url})
26
                    days_num = ' days'
27
                    expiration_days_text = "%s %s" %('in',expiration_days)
28
                    if expiration_days == 0:
29
                        days_num = ' today'
30
                        expiration_days_text = ''
31
                    if expiration_days == 1:
32
                        days_num = ' day'
33
                    send_mail(settings.EMAIL_SUBJECT_PREFIX + "Rule %s expires %s%s" %
34
                              (route.name,expiration_days_text, days_num),
35
                              mail_body, settings.SERVER_EMAIL,
36
                              [route.applier.email])
37
                except Exception as e:
38
                    print e
39
                    pass
40

  
41
if __name__ == "__main__":
42
    notify_expired()
43

  
b/flowspec/tasks.py
7 7
import beanstalkc
8 8
from django.conf import settings
9 9
import datetime
10

  
10
from flowspy.flowspec.models import *
11
from django.core.mail import send_mail
12
from flowspy.flowspec.models import *
13
from django.template.loader import render_to_string
14
from django.contrib.sites.models import Site
15
from django.core.urlresolvers import reverse
11 16
import os
12 17

  
13 18

  
......
132 137
            if route.status != 'EXPIRED':
133 138
                route.check_sync()
134 139

  
140
@task(ignore_result=True)
141
def notify_expired():
142
    logger.info('Initializing expiration notification')
143
    routes = Route.objects.all()
144
    for route in routes:
145
        if route.status not in ['EXPIRED', 'ADMININACTIVE', 'INACTIVE', 'ERROR']:
146
            expiration_days = (route.expires - datetime.date.today()).days
147
            if expiration_days < settings.EXPIRATION_NOTIFY_DAYS:
148
                try:
149
                    fqdn = Site.objects.get_current().domain
150
                    admin_url = "https://%s%s" % \
151
                    (fqdn,
152
                     "/fod/edit/%s"%route.name)
153
                    mail_body = render_to_string("rule_expiration.txt",
154
                                             {"route": route, 'expiration_days':expiration_days, 'url':admin_url})
155
                    days_num = ' days'
156
                    expiration_days_text = "%s %s" %('in',expiration_days)
157
                    if expiration_days == 0:
158
                        days_num = ' today'
159
                        expiration_days_text = ''
160
                    if expiration_days == 1:
161
                        days_num = ' day'
162
                    logger.info('Route %s expires %s%s. Notifying %s (%s)' %(route.name, expiration_days_text, days_num, route.applier.username, route.applier.email))
163
                    send_mail(settings.EMAIL_SUBJECT_PREFIX + "Rule %s expires %s%s" %
164
                              (route.name,expiration_days_text, days_num),
165
                              mail_body, settings.SERVER_EMAIL,
166
                              [route.applier.email])
167
                except Exception as e:
168
                    logger.info("Exception: %s"%e)
169
                    pass
170
    logger.info('Expiration notification process finished')
135 171

  
136 172
#def delete(route):
137 173
#    
b/settings.py.dist
193 193

  
194 194
PROTECTED_SUBNETS = ['10.10.0.0/16']
195 195

  
196
CELERYBEAT_SCHEDULE = {
197
    # Executes every day at 01:35 AM 
196
CELERYBEAT_SCHEDULE = { 
198 197
    "every-day-sync": {
199 198
        "task": "flowspec.tasks.check_sync",
200 199
        "schedule": crontab(minute=01, hour=01),
201 200
        "args": (),
202 201
    },
202
    "notify-expired": {
203
        "task": "flowspec.tasks.notify_expired",
204
        "schedule": crontab(minute=01, hour=02),
205
        "args": (),
206
    },
203 207
}
204 208
# whois
205 209
PRIMARY_WHOIS = 'whois.grnet.gr'

Also available in: Unified diff