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