1 from utils import proxy as PR
2 from celery.task import task
3 from celery.task.sets import subtask
6 from celery.task.http import *
7 from flowspy.utils import beanstalkc
8 from django.conf import settings
14 LOG_FILENAME = os.path.join(settings.LOG_FILE_LOCATION, 'celery_jobs.log')
16 #FORMAT = '%(asctime)s %(levelname)s: %(message)s'
17 #logging.basicConfig(format=FORMAT)
18 formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
20 logger = logging.getLogger(__name__)
21 logger.setLevel(logging.DEBUG)
22 handler = logging.FileHandler(LOG_FILENAME)
23 handler.setFormatter(formatter)
24 logger.addHandler(handler)
28 def add(route, callback=None):
29 applier = PR.Applier(route_object=route)
30 commit, response = applier.apply()
36 route.response = response
37 subtask(announce).delay("[%s] Rule add: %s - Result: %s" %(route.applier, route.name, response), route.applier)
41 def edit(route, callback=None):
42 applier = PR.Applier(route_object=route)
43 commit, response = applier.apply(operation="replace")
49 route.response = response
51 subtask(announce).delay("[%s] Rule edit: %s - Result: %s"%(route.applier, route.name, response), route.applier)
56 def delete(route, **kwargs):
57 applier = PR.Applier(route_object=route)
58 commit, response = applier.apply(operation="delete")
62 if "reason" in kwargs and kwargs['reason']=='EXPIRED':
64 reason_text = " Reason: %s " %status
68 route.response = response
70 subtask(announce).delay("[%s] Rule removal: %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
72 # May not work in the first place... proxy is not aware of Route models
74 def batch_delete(routes, **kwargs):
77 route.status='PENDING';route.save()
78 applier = PR.Applier(route_objects=routes)
79 conf = applier.delete_routes()
80 commit, response = applier.apply(configuration = conf)
84 if "reason" in kwargs and kwargs['reason']=='EXPIRED':
86 reason_text = " Reason: %s " %status
87 elif "reason" in kwargs and kwargs['reason']!='EXPIRED':
88 status = kwargs['reason']
89 reason_text = " Reason: %s " %status
94 route.response = response
96 subtask(announce).delay("[%s] Rule removal: %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
101 def announce(messg, user):
103 # username = user.username
104 username = user.get_profile().peer.domain_name
105 b = beanstalkc.Connection()
106 b.use(settings.POLLS_TUBE)
107 tube_message = json.dumps({'message': messg, 'username':username})
112 def check_sync(route_name=None, selected_routes = []):
113 from flowspy.flowspec.models import Route, MatchPort, MatchDscp, ThenAction
114 if not selected_routes:
115 routes = Route.objects.all()
117 routes = selected_routes
119 routes = routes.filter(name=route_name)
121 if route.has_expired() and (route.status != 'EXPIRED' and route.status != 'ADMININACTIVE' and route.status != 'INACTIVE'):
122 logger.info('Expiring route %s' %route.name)
123 subtask(delete).delay(route, reason="EXPIRED")
124 elif route.has_expired() and (route.status == 'ADMININACTIVE' or route.status == 'INACTIVE'):
125 route.status = 'EXPIRED'
126 route.response = 'Rule Expired'
127 logger.info('Expiring route %s' %route.name)
130 if route.status != 'EXPIRED':
136 # applier = PR.Applier(route_object=route)
137 # commit, response = applier.apply(configuration=applier.delete_routes())
139 # rows = queryset.update(is_online=False, is_active=False)
140 # queryset.update(response="Successfully removed route from network")
141 # self.message_user(request, "Successfully removed %s routes from network" % rows)
143 # self.message_user(request, "Could not remove routes from network")
147 # response = "Successfully removed route from network"
151 # route.is_online = is_online
152 # route.is_active = is_active
153 # route.response = response