Statistics
| Branch: | Tag: | Revision:

root / flowspec / tasks.py @ 6d153302

History | View | Annotate | Download (4.7 kB)

1 9cad4715 Leonidas Poulopoulos
from utils import proxy as PR
2 9cad4715 Leonidas Poulopoulos
from celery.task import task
3 3e99e2d1 Leonidas Poulopoulos
from celery.task.sets import subtask
4 3e99e2d1 Leonidas Poulopoulos
import logging
5 25d08a62 Leonidas Poulopoulos
import json
6 3e99e2d1 Leonidas Poulopoulos
from celery.task.http import *
7 3e99e2d1 Leonidas Poulopoulos
from flowspy.utils import beanstalkc
8 3e99e2d1 Leonidas Poulopoulos
from django.conf import settings
9 3e99e2d1 Leonidas Poulopoulos
10 3e99e2d1 Leonidas Poulopoulos
FORMAT = '%(asctime)s %(levelname)s: %(message)s'
11 3e99e2d1 Leonidas Poulopoulos
logging.basicConfig(format=FORMAT)
12 3e99e2d1 Leonidas Poulopoulos
logger = logging.getLogger(__name__)
13 3e99e2d1 Leonidas Poulopoulos
logger.setLevel(logging.DEBUG)
14 9cad4715 Leonidas Poulopoulos
15 9cad4715 Leonidas Poulopoulos
@task
16 3e99e2d1 Leonidas Poulopoulos
def add(route, callback=None):
17 9cad4715 Leonidas Poulopoulos
    applier = PR.Applier(route_object=route)
18 9cad4715 Leonidas Poulopoulos
    commit, response = applier.apply()
19 9cad4715 Leonidas Poulopoulos
    if commit:
20 97e42c7d Leonidas Poulopoulos
        status = "ACTIVE"
21 9cad4715 Leonidas Poulopoulos
    else:
22 97e42c7d Leonidas Poulopoulos
        status = "ERROR"
23 97e42c7d Leonidas Poulopoulos
    route.status = status
24 9cad4715 Leonidas Poulopoulos
    route.response = response
25 97e42c7d Leonidas Poulopoulos
    subtask(announce).delay("[%s] Route add: %s - Result: %s" %(route.applier, route.name, response), route.applier)
26 3e99e2d1 Leonidas Poulopoulos
    route.save()
27 3e99e2d1 Leonidas Poulopoulos
28 3e99e2d1 Leonidas Poulopoulos
@task
29 3e99e2d1 Leonidas Poulopoulos
def edit(route, callback=None):
30 3e99e2d1 Leonidas Poulopoulos
    applier = PR.Applier(route_object=route)
31 3e99e2d1 Leonidas Poulopoulos
    commit, response = applier.apply(operation="replace")
32 3e99e2d1 Leonidas Poulopoulos
    if commit:
33 97e42c7d Leonidas Poulopoulos
        status = "ACTIVE"
34 3e99e2d1 Leonidas Poulopoulos
    else:
35 97e42c7d Leonidas Poulopoulos
        status = "ERROR"
36 97e42c7d Leonidas Poulopoulos
    route.status = status
37 3e99e2d1 Leonidas Poulopoulos
    route.response = response
38 9cad4715 Leonidas Poulopoulos
    route.save()
39 97e42c7d Leonidas Poulopoulos
    subtask(announce).delay("[%s] Route edit: %s - Result: %s"%(route.applier, route.name, response), route.applier)
40 3e99e2d1 Leonidas Poulopoulos
41 3e99e2d1 Leonidas Poulopoulos
42 971645d6 Leonidas Poulopoulos
43 971645d6 Leonidas Poulopoulos
@task
44 6a946adf Leonidas Poulopoulos
def delete(route, **kwargs):
45 3e99e2d1 Leonidas Poulopoulos
    applier = PR.Applier(route_object=route)
46 3e99e2d1 Leonidas Poulopoulos
    commit, response = applier.apply(operation="delete")
47 6a946adf Leonidas Poulopoulos
    reason_text = ''
48 3e99e2d1 Leonidas Poulopoulos
    if commit:
49 97e42c7d Leonidas Poulopoulos
        status = "INACTIVE"
50 6a946adf Leonidas Poulopoulos
        if "reason" in kwargs and kwargs['reason']=='EXPIRED':
51 6a946adf Leonidas Poulopoulos
            status = 'EXPIRED'
52 6a946adf Leonidas Poulopoulos
            reason_text = " Reason: %s " %status
53 3e99e2d1 Leonidas Poulopoulos
    else:
54 97e42c7d Leonidas Poulopoulos
        status = "ERROR"
55 97e42c7d Leonidas Poulopoulos
    route.status = status
56 3e99e2d1 Leonidas Poulopoulos
    route.response = response
57 3e99e2d1 Leonidas Poulopoulos
    route.save()
58 6a946adf Leonidas Poulopoulos
    subtask(announce).delay("[%s] Route removal: %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
59 3e99e2d1 Leonidas Poulopoulos
60 6a946adf Leonidas Poulopoulos
# May not work in the first place... proxy is not aware of Route models
61 6a946adf Leonidas Poulopoulos
@task
62 6a946adf Leonidas Poulopoulos
def batch_delete(routes, **kwargs):
63 6a946adf Leonidas Poulopoulos
    if routes:
64 6a946adf Leonidas Poulopoulos
        applier = PR.Applier(route_objects=routes)
65 6a946adf Leonidas Poulopoulos
        conf = applier.delete_routes()
66 6a946adf Leonidas Poulopoulos
        commit, response = applier.apply(configuration = conf)
67 6a946adf Leonidas Poulopoulos
        reason_text = ''
68 6a946adf Leonidas Poulopoulos
        if commit:
69 6a946adf Leonidas Poulopoulos
            status = "INACTIVE"
70 6a946adf Leonidas Poulopoulos
            if "reason" in kwargs and kwargs['reason']=='EXPIRED':
71 6a946adf Leonidas Poulopoulos
                status = 'EXPIRED'
72 6a946adf Leonidas Poulopoulos
                reason_text = " Reason: %s " %status
73 d50fd7b6 Leonidas Poulopoulos
            elif "reason" in kwargs and kwargs['reason']!='EXPIRED':
74 d50fd7b6 Leonidas Poulopoulos
                status = kwargs['reason']
75 d50fd7b6 Leonidas Poulopoulos
                reason_text = " Reason: %s " %status
76 6a946adf Leonidas Poulopoulos
        else:
77 6a946adf Leonidas Poulopoulos
            status = "ERROR"
78 6a946adf Leonidas Poulopoulos
        for route in routes:
79 6a946adf Leonidas Poulopoulos
            route.status = status
80 6a946adf Leonidas Poulopoulos
            route.response = response
81 6a946adf Leonidas Poulopoulos
            route.save()
82 6a946adf Leonidas Poulopoulos
            subtask(announce).delay("[%s] Route removal: %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
83 6a946adf Leonidas Poulopoulos
    else:
84 6a946adf Leonidas Poulopoulos
        return False
85 3e99e2d1 Leonidas Poulopoulos
86 3e99e2d1 Leonidas Poulopoulos
@task
87 25d08a62 Leonidas Poulopoulos
def announce(messg, user):
88 3e99e2d1 Leonidas Poulopoulos
    messg = str(messg)
89 97e42c7d Leonidas Poulopoulos
#    username = user.username
90 97e42c7d Leonidas Poulopoulos
    username = user.get_profile().peer.domain_name
91 3e99e2d1 Leonidas Poulopoulos
    b = beanstalkc.Connection()
92 3e99e2d1 Leonidas Poulopoulos
    b.use(settings.POLLS_TUBE)
93 25d08a62 Leonidas Poulopoulos
    tube_message = json.dumps({'message': messg, 'username':username})
94 25d08a62 Leonidas Poulopoulos
    b.put(tube_message)
95 3e99e2d1 Leonidas Poulopoulos
    b.close()
96 3e99e2d1 Leonidas Poulopoulos
97 97e42c7d Leonidas Poulopoulos
@task
98 97e42c7d Leonidas Poulopoulos
def check_sync(route_name=None, selected_routes = []):
99 6a946adf Leonidas Poulopoulos
    from flowspy.flowspec.models import Route, MatchPort, MatchDscp, ThenAction
100 97e42c7d Leonidas Poulopoulos
    if not selected_routes:
101 97e42c7d Leonidas Poulopoulos
        routes = Route.objects.all()
102 97e42c7d Leonidas Poulopoulos
    else:
103 97e42c7d Leonidas Poulopoulos
        routes = selected_routes
104 97e42c7d Leonidas Poulopoulos
    if route_name:
105 97e42c7d Leonidas Poulopoulos
        routes = routes.filter(name=route_name)
106 6a946adf Leonidas Poulopoulos
    for route in routes:
107 fd3870ee Leonidas Poulopoulos
        if route.has_expired() and (route.status != 'EXPIRED' and route.status != 'ADMININACTIVE' and route.status != 'INACTIVE'):
108 6a946adf Leonidas Poulopoulos
            logger.info('Expiring route %s' %route.name)
109 6a946adf Leonidas Poulopoulos
            subtask(delete).delay(route, reason="EXPIRED")
110 d50fd7b6 Leonidas Poulopoulos
        elif route.has_expired() and (route.status == 'ADMININACTIVE' or route.status == 'INACTIVE'):
111 d50fd7b6 Leonidas Poulopoulos
            route.status = 'EXPIRED'
112 d50fd7b6 Leonidas Poulopoulos
            route.response = 'Route Expired'
113 d50fd7b6 Leonidas Poulopoulos
            route.save()
114 fd3870ee Leonidas Poulopoulos
        else:
115 fd3870ee Leonidas Poulopoulos
            if route.status != 'EXPIRED':
116 fd3870ee Leonidas Poulopoulos
                route.check_sync()
117 6a946adf Leonidas Poulopoulos
118 6a946adf Leonidas Poulopoulos
119 9cad4715 Leonidas Poulopoulos
#def delete(route):
120 9cad4715 Leonidas Poulopoulos
#    
121 9cad4715 Leonidas Poulopoulos
#    applier = PR.Applier(route_object=route)
122 9cad4715 Leonidas Poulopoulos
#    commit, response = applier.apply(configuration=applier.delete_routes())
123 9cad4715 Leonidas Poulopoulos
#    if commit:
124 9cad4715 Leonidas Poulopoulos
#            rows = queryset.update(is_online=False, is_active=False)
125 9cad4715 Leonidas Poulopoulos
#            queryset.update(response="Successfully removed route from network")
126 9cad4715 Leonidas Poulopoulos
#            self.message_user(request, "Successfully removed %s routes from network" % rows)
127 9cad4715 Leonidas Poulopoulos
#        else:
128 9cad4715 Leonidas Poulopoulos
#            self.message_user(request, "Could not remove routes from network")
129 9cad4715 Leonidas Poulopoulos
#    if commit:
130 9cad4715 Leonidas Poulopoulos
#        is_online = False
131 9cad4715 Leonidas Poulopoulos
#        is_active = False
132 9cad4715 Leonidas Poulopoulos
#        response = "Successfully removed route from network"
133 9cad4715 Leonidas Poulopoulos
#    else:
134 9cad4715 Leonidas Poulopoulos
#        is_online = False
135 9cad4715 Leonidas Poulopoulos
#        is_active = True
136 9cad4715 Leonidas Poulopoulos
#    route.is_online = is_online
137 9cad4715 Leonidas Poulopoulos
#    route.is_active = is_active
138 9cad4715 Leonidas Poulopoulos
#    route.response = response
139 9cad4715 Leonidas Poulopoulos
#    route.save()