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