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
15 LOG_FILENAME = os.path.join(settings.LOG_FILE_LOCATION, 'celery_jobs.log')
17 #FORMAT = '%(asctime)s %(levelname)s: %(message)s'
18 #logging.basicConfig(format=FORMAT)
19 formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
21 logger = logging.getLogger(__name__)
22 logger.setLevel(logging.DEBUG)
23 handler = logging.FileHandler(LOG_FILENAME)
24 handler.setFormatter(formatter)
25 logger.addHandler(handler)
29 def add(route, callback=None):
30 applier = PR.Applier(route_object=route)
31 commit, response = applier.apply()
37 route.response = response
38 subtask(announce).delay("[%s] Rule add: %s - Result: %s" %(route.applier, route.name, response), route.applier)
42 def edit(route, callback=None):
43 applier = PR.Applier(route_object=route)
44 commit, response = applier.apply(operation="replace")
50 route.response = response
52 subtask(announce).delay("[%s] Rule edit: %s - Result: %s"%(route.applier, route.name, response), route.applier)
57 def delete(route, **kwargs):
58 applier = PR.Applier(route_object=route)
59 commit, response = applier.apply(operation="delete")
63 if "reason" in kwargs and kwargs['reason']=='EXPIRED':
65 reason_text = " Reason: %s " %status
69 route.response = response
71 subtask(announce).delay("[%s] Suspending rule : %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
73 # May not work in the first place... proxy is not aware of Route models
75 def batch_delete(routes, **kwargs):
78 route.status='PENDING';route.save()
79 applier = PR.Applier(route_objects=routes)
80 conf = applier.delete_routes()
81 commit, response = applier.apply(configuration = conf)
85 if "reason" in kwargs and kwargs['reason']=='EXPIRED':
87 reason_text = " Reason: %s " %status
88 elif "reason" in kwargs and kwargs['reason']!='EXPIRED':
89 status = kwargs['reason']
90 reason_text = " Reason: %s " %status
95 route.response = response
96 route.expires = datetime.date.today()
98 subtask(announce).delay("[%s] Rule removal: %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
103 def announce(messg, user):
105 # username = user.username
106 username = user.get_profile().peer.domain_name
107 b = beanstalkc.Connection()
108 b.use(settings.POLLS_TUBE)
109 tube_message = json.dumps({'message': messg, 'username':username})
114 def check_sync(route_name=None, selected_routes = []):
115 from flowspy.flowspec.models import Route, MatchPort, MatchDscp, ThenAction
116 if not selected_routes:
117 routes = Route.objects.all()
119 routes = selected_routes
121 routes = routes.filter(name=route_name)
123 if route.has_expired() and (route.status != 'EXPIRED' and route.status != 'ADMININACTIVE' and route.status != 'INACTIVE'):
124 logger.info('Expiring route %s' %route.name)
125 subtask(delete).delay(route, reason="EXPIRED")
126 # elif route.has_expired() and (route.status == 'ADMININACTIVE' or route.status == 'INACTIVE'):
127 # route.status = 'EXPIRED'
128 # route.response = 'Rule Expired'
129 # logger.info('Expiring route %s' %route.name)
132 if route.status != 'EXPIRED':
138 # applier = PR.Applier(route_object=route)
139 # commit, response = applier.apply(configuration=applier.delete_routes())
141 # rows = queryset.update(is_online=False, is_active=False)
142 # queryset.update(response="Successfully removed route from network")
143 # self.message_user(request, "Successfully removed %s routes from network" % rows)
145 # self.message_user(request, "Could not remove routes from network")
149 # response = "Successfully removed route from network"
153 # route.is_online = is_online
154 # route.is_active = is_active
155 # route.response = response