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
10 FORMAT = '%(asctime)s %(levelname)s: %(message)s'
11 logging.basicConfig(format=FORMAT)
12 logger = logging.getLogger(__name__)
13 logger.setLevel(logging.DEBUG)
16 def add(route, callback=None):
17 applier = PR.Applier(route_object=route)
18 commit, response = applier.apply()
24 route.response = response
25 subtask(announce).delay("[%s] Rule add: %s - Result: %s" %(route.applier, route.name, response), route.applier)
29 def edit(route, callback=None):
30 applier = PR.Applier(route_object=route)
31 commit, response = applier.apply(operation="replace")
37 route.response = response
39 subtask(announce).delay("[%s] Rule edit: %s - Result: %s"%(route.applier, route.name, response), route.applier)
44 def delete(route, **kwargs):
45 applier = PR.Applier(route_object=route)
46 commit, response = applier.apply(operation="delete")
50 if "reason" in kwargs and kwargs['reason']=='EXPIRED':
52 reason_text = " Reason: %s " %status
56 route.response = response
58 subtask(announce).delay("[%s] Rule removal: %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
60 # May not work in the first place... proxy is not aware of Route models
62 def batch_delete(routes, **kwargs):
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)
72 if "reason" in kwargs and kwargs['reason']=='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
82 route.response = response
84 subtask(announce).delay("[%s] Rule removal: %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
89 def announce(messg, user):
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})
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()
105 routes = selected_routes
107 routes = routes.filter(name=route_name)
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'
117 if route.status != 'EXPIRED':
123 # applier = PR.Applier(route_object=route)
124 # commit, response = applier.apply(configuration=applier.delete_routes())
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)
130 # self.message_user(request, "Could not remove routes from network")
134 # response = "Successfully removed route from network"
138 # route.is_online = is_online
139 # route.is_active = is_active
140 # route.response = response