1 from utils import proxy as PR
2 from celery.task import task
3 from celery.task.sets import subtask
7 from celery.task.http import *
8 from flowspy.utils import beanstalkc
9 from django.conf import settings
11 FORMAT = '%(asctime)s %(levelname)s: %(message)s'
12 logging.basicConfig(format=FORMAT)
13 logger = logging.getLogger(__name__)
14 logger.setLevel(logging.DEBUG)
17 def add(route, callback=None):
18 applier = PR.Applier(route_object=route)
19 commit, response = applier.apply()
25 route.response = response
26 subtask(announce).delay("[%s] Route add: %s - Result: %s" %(route.applier, route.name, response), route.applier)
30 def edit(route, callback=None):
31 applier = PR.Applier(route_object=route)
32 commit, response = applier.apply(operation="replace")
38 route.response = response
40 subtask(announce).delay("[%s] Route edit: %s - Result: %s"%(route.applier, route.name, response), route.applier)
45 def delete(route, **kwargs):
46 applier = PR.Applier(route_object=route)
47 commit, response = applier.apply(operation="delete")
51 if "reason" in kwargs and kwargs['reason']=='EXPIRED':
53 reason_text = " Reason: %s " %status
57 route.response = response
59 subtask(announce).delay("[%s] Route removal: %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
61 # May not work in the first place... proxy is not aware of Route models
63 def batch_delete(routes, **kwargs):
65 applier = PR.Applier(route_objects=routes)
66 conf = applier.delete_routes()
67 commit, response = applier.apply(configuration = conf)
71 if "reason" in kwargs and kwargs['reason']=='EXPIRED':
73 reason_text = " Reason: %s " %status
74 elif "reason" in kwargs and kwargs['reason']!='EXPIRED':
75 status = kwargs['reason']
76 reason_text = " Reason: %s " %status
81 route.response = response
83 subtask(announce).delay("[%s] Route removal: %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
88 def announce(messg, user):
90 # username = user.username
91 username = user.get_profile().peer.domain_name
92 b = beanstalkc.Connection()
93 b.use(settings.POLLS_TUBE)
94 tube_message = json.dumps({'message': messg, 'username':username})
99 def check_sync(route_name=None, selected_routes = []):
100 from flowspy.flowspec.models import Route, MatchPort, MatchDscp, ThenAction
101 if not selected_routes:
102 routes = Route.objects.all()
104 routes = selected_routes
106 routes = routes.filter(name=route_name)
108 if route.has_expired() and (route.status != 'EXPIRED' or route.status != 'ADMININACTIVE' or route.status != 'INACTIVE'):
109 logger.info('Expiring route %s' %route.name)
110 subtask(delete).delay(route, reason="EXPIRED")
111 elif route.has_expired() and (route.status == 'ADMININACTIVE' or route.status == 'INACTIVE'):
112 route.status = 'EXPIRED'
113 route.response = 'Route Expired'
115 elif route.status != 'EXPIRED':
121 # applier = PR.Applier(route_object=route)
122 # commit, response = applier.apply(configuration=applier.delete_routes())
124 # rows = queryset.update(is_online=False, is_active=False)
125 # queryset.update(response="Successfully removed route from network")
126 # self.message_user(request, "Successfully removed %s routes from network" % rows)
128 # self.message_user(request, "Could not remove routes from network")
132 # response = "Successfully removed route from network"
136 # route.is_online = is_online
137 # route.is_active = is_active
138 # route.response = response