Statistics
| Branch: | Tag: | Revision:

root / flowspec / tasks.py @ 052c14aa

History | View | Annotate | Download (4.8 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 e173e7c2 Leonidas Poulopoulos
        for route in routes:
65 e173e7c2 Leonidas Poulopoulos
            route.status='PENDING';route.save()
66 6a946adf Leonidas Poulopoulos
        applier = PR.Applier(route_objects=routes)
67 6a946adf Leonidas Poulopoulos
        conf = applier.delete_routes()
68 6a946adf Leonidas Poulopoulos
        commit, response = applier.apply(configuration = conf)
69 6a946adf Leonidas Poulopoulos
        reason_text = ''
70 6a946adf Leonidas Poulopoulos
        if commit:
71 6a946adf Leonidas Poulopoulos
            status = "INACTIVE"
72 6a946adf Leonidas Poulopoulos
            if "reason" in kwargs and kwargs['reason']=='EXPIRED':
73 6a946adf Leonidas Poulopoulos
                status = 'EXPIRED'
74 6a946adf Leonidas Poulopoulos
                reason_text = " Reason: %s " %status
75 d50fd7b6 Leonidas Poulopoulos
            elif "reason" in kwargs and kwargs['reason']!='EXPIRED':
76 d50fd7b6 Leonidas Poulopoulos
                status = kwargs['reason']
77 d50fd7b6 Leonidas Poulopoulos
                reason_text = " Reason: %s " %status
78 6a946adf Leonidas Poulopoulos
        else:
79 6a946adf Leonidas Poulopoulos
            status = "ERROR"
80 6a946adf Leonidas Poulopoulos
        for route in routes:
81 6a946adf Leonidas Poulopoulos
            route.status = status
82 6a946adf Leonidas Poulopoulos
            route.response = response
83 6a946adf Leonidas Poulopoulos
            route.save()
84 6a946adf Leonidas Poulopoulos
            subtask(announce).delay("[%s] Route removal: %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
85 6a946adf Leonidas Poulopoulos
    else:
86 6a946adf Leonidas Poulopoulos
        return False
87 3e99e2d1 Leonidas Poulopoulos
88 3e99e2d1 Leonidas Poulopoulos
@task
89 25d08a62 Leonidas Poulopoulos
def announce(messg, user):
90 3e99e2d1 Leonidas Poulopoulos
    messg = str(messg)
91 97e42c7d Leonidas Poulopoulos
#    username = user.username
92 97e42c7d Leonidas Poulopoulos
    username = user.get_profile().peer.domain_name
93 3e99e2d1 Leonidas Poulopoulos
    b = beanstalkc.Connection()
94 3e99e2d1 Leonidas Poulopoulos
    b.use(settings.POLLS_TUBE)
95 25d08a62 Leonidas Poulopoulos
    tube_message = json.dumps({'message': messg, 'username':username})
96 25d08a62 Leonidas Poulopoulos
    b.put(tube_message)
97 3e99e2d1 Leonidas Poulopoulos
    b.close()
98 3e99e2d1 Leonidas Poulopoulos
99 97e42c7d Leonidas Poulopoulos
@task
100 97e42c7d Leonidas Poulopoulos
def check_sync(route_name=None, selected_routes = []):
101 6a946adf Leonidas Poulopoulos
    from flowspy.flowspec.models import Route, MatchPort, MatchDscp, ThenAction
102 97e42c7d Leonidas Poulopoulos
    if not selected_routes:
103 97e42c7d Leonidas Poulopoulos
        routes = Route.objects.all()
104 97e42c7d Leonidas Poulopoulos
    else:
105 97e42c7d Leonidas Poulopoulos
        routes = selected_routes
106 97e42c7d Leonidas Poulopoulos
    if route_name:
107 97e42c7d Leonidas Poulopoulos
        routes = routes.filter(name=route_name)
108 6a946adf Leonidas Poulopoulos
    for route in routes:
109 fd3870ee Leonidas Poulopoulos
        if route.has_expired() and (route.status != 'EXPIRED' and route.status != 'ADMININACTIVE' and route.status != 'INACTIVE'):
110 6a946adf Leonidas Poulopoulos
            logger.info('Expiring route %s' %route.name)
111 6a946adf Leonidas Poulopoulos
            subtask(delete).delay(route, reason="EXPIRED")
112 d50fd7b6 Leonidas Poulopoulos
        elif route.has_expired() and (route.status == 'ADMININACTIVE' or route.status == 'INACTIVE'):
113 d50fd7b6 Leonidas Poulopoulos
            route.status = 'EXPIRED'
114 d50fd7b6 Leonidas Poulopoulos
            route.response = 'Route Expired'
115 d50fd7b6 Leonidas Poulopoulos
            route.save()
116 fd3870ee Leonidas Poulopoulos
        else:
117 fd3870ee Leonidas Poulopoulos
            if route.status != 'EXPIRED':
118 fd3870ee Leonidas Poulopoulos
                route.check_sync()
119 6a946adf Leonidas Poulopoulos
120 6a946adf Leonidas Poulopoulos
121 9cad4715 Leonidas Poulopoulos
#def delete(route):
122 9cad4715 Leonidas Poulopoulos
#    
123 9cad4715 Leonidas Poulopoulos
#    applier = PR.Applier(route_object=route)
124 9cad4715 Leonidas Poulopoulos
#    commit, response = applier.apply(configuration=applier.delete_routes())
125 9cad4715 Leonidas Poulopoulos
#    if commit:
126 9cad4715 Leonidas Poulopoulos
#            rows = queryset.update(is_online=False, is_active=False)
127 9cad4715 Leonidas Poulopoulos
#            queryset.update(response="Successfully removed route from network")
128 9cad4715 Leonidas Poulopoulos
#            self.message_user(request, "Successfully removed %s routes from network" % rows)
129 9cad4715 Leonidas Poulopoulos
#        else:
130 9cad4715 Leonidas Poulopoulos
#            self.message_user(request, "Could not remove routes from network")
131 9cad4715 Leonidas Poulopoulos
#    if commit:
132 9cad4715 Leonidas Poulopoulos
#        is_online = False
133 9cad4715 Leonidas Poulopoulos
#        is_active = False
134 9cad4715 Leonidas Poulopoulos
#        response = "Successfully removed route from network"
135 9cad4715 Leonidas Poulopoulos
#    else:
136 9cad4715 Leonidas Poulopoulos
#        is_online = False
137 9cad4715 Leonidas Poulopoulos
#        is_active = True
138 9cad4715 Leonidas Poulopoulos
#    route.is_online = is_online
139 9cad4715 Leonidas Poulopoulos
#    route.is_active = is_active
140 9cad4715 Leonidas Poulopoulos
#    route.response = response
141 9cad4715 Leonidas Poulopoulos
#    route.save()