def post(self, request, capture_node): try: capture_node = CaptureNode.objects.get(id=capture_node).hostname except CaptureNode.DoesNotExist: return Response(status=404) ser = self.PostSerializer(data=request.data) if not ser.is_valid(): return Response(data={'warning': format_errors(ser.errors)}) result = iface_tasks.interface_toggle.apply_async([ser.validated_data['iface']], queue=capture_node) _classname = type(self).__name__ # This should be quick, wait for the result to finish. try: result_data = result.get(timeout=self.timeout) ephemeral_task_cleanup(result) except TimeoutError: return Response(data={'warning': "{} timed out.".format(_classname)}) except: log.error("{} ({}). {}".format(_classname, capture_node, format_exc())) return Response(data={'warning': "{} encountered an error.".format(_classname)}) return Response(data={'info': result_data.get('info'), 'data': result_data.get('data')})
def get(self, request, capture_node): try: capture_node = CaptureNode.objects.get(id=capture_node).hostname except CaptureNode.DoesNotExist: return Response(status=404) result = self.task.apply_async(queue=capture_node) _classname = type(self).__name__ log.info("data view {}: {}".format(_classname, repr(self.task))) # This should be quick, wait for the result to finish. try: result_data = result.get(timeout=self.timeout) ephemeral_task_cleanup(result) except TimeoutError: return Response( data={'warning': "{} timed out.".format(_classname)}) except Exception: log.error("{} ({}). {}".format(_classname, capture_node, format_exc())) return Response( data={ 'warning': "{} encountered an error.".format(_classname) }) return Response(data=result_data)
def post(self, request, capture_node): log.error('data? {}'.format(request.data, )) try: capture_node = CaptureNode.objects.get(id=capture_node).hostname except CaptureNode.DoesNotExist: return Response(status=404) ser = self.PostSerializer(data=request.data) if not ser.is_valid(): return Response(data={'warning': format_errors(ser.errors)}) log.error('val_data? {}'.format(list(ser.validated_data.keys()))) devices = ser.validated_data['devices'] if self.device_limit is not None and len(devices) > self.device_limit: return Response( data={ 'warning': 'The maximum number of devices for this command is {}.'. format(self.device_limit) }) result = self.task.apply_async([devices] + self.extra_args, queue=capture_node) _class_name = type(self).__name__ if self.track: # Track the task in our augmented task tracking system TaskTrack.track(result, self.track_desc, request) return Response(data=self.track_start_context) else: # This should be quick, wait for the result to finish. try: result_data = result.get(timeout=self.timeout) ephemeral_task_cleanup(result) except TimeoutError: return Response( data={'warning': "{} timed out.".format(_class_name)}) except: log.error("{} ({}). {}".format(_class_name, capture_node, format_exc())) return Response(data={ 'warning': "{} encountered an error.".format(_class_name) }) return Response(data=result_data)
def post(self, request, capture_node): try: capture_node = CaptureNode.objects.get(id=capture_node).hostname except CaptureNode.DoesNotExist: return Response(status=404) ifaces = request.data.getlist('iface') if len(ifaces) != 1: return Response( {'warning': "One interface name must be specified."}) iface = ifaces[0] queues = request.data.getlist('queues') bad_queues = False if len(queues) != 1: bad_queues = True queues = queues[0] try: queues = int(queues) except ValueError: bad_queues = True if bad_queues: log.error("Invalid Queue Count: {}".format(queues)) return Response({'warning': "Invalid queue count."}) result = iface_tasks.interface_set_queues.apply_async( [iface, queues], queue=capture_node) _classname = type(self).__name__ # This should be quick, wait for the result to finish. try: result_data = result.get(timeout=self.timeout) ephemeral_task_cleanup(result) except TimeoutError: return Response( data={'warning': "{} timed out.".format(_classname)}) except: log.error("{} ({}). {}".format(_classname, capture_node, format_exc())) return Response( data={ 'warning': "{} encountered an error.".format(_classname) }) return Response(data={'info': '{} queues updated.'.format(iface)})
def post(self, request, capture_node=None): """Change the current running state of the capture_node with the given capture_node. URL Args: - capture_node: The id of the effected capture_node. If None, change the state of all capture_nodes. """ if capture_node is None: capture_nodes = CaptureNode.objects.all() else: try: capture_node = int(capture_node) except ValueError: return Response( data={"Invalid capture_node id: {}".format(capture_node)}) try: capture_nodes = [CaptureNode.objects.get(id=capture_node)] except CaptureNode.DoesNotExist: return Response(status=404) state = request.data.getlist('state') if len(state) == 0 or state[0] not in status_tasks.CAPTURE_STATES: return Response(data={'warning': 'Invalid capture state given.'}) state = state[0] log.info(state) tasks = {} for idx in capture_nodes: tasks[idx.hostname] = status_tasks.set_capture_state\ .apply_async([state], queue=idx.hostname) context = {} for idx in capture_nodes: result = tasks[idx.hostname].get(timeout=self.timeout) for level in 'success', 'info', 'warning', 'danger': if level in result: msgs = context.get(level, []) new_msgs = result.get(level) if type(new_msgs) not in (list, tuple): new_msgs = [new_msgs] msgs.extend(new_msgs) context[level] = msgs ephemeral_task_cleanup(tasks[idx.hostname]) return Response(data=context)
def get(self, request, capture_node=None): """Return the list of capture_nodes. :param Request request: URL Args: capture_node: The id of the status for a specific capture_node. Default (None) is all. """ if capture_node is None: capture_nodes = CaptureNode.objects.all() else: try: capture_node = int(capture_node) except ValueError: return Response( data={"Invalid capture_node id: {}".format(capture_node)}) try: capture_nodes = [CaptureNode.objects.get(id=capture_node)] except CaptureNode.DoesNotExist: return Response(status=404) capture_node_info = [] tasks = {} for capnode in capture_nodes: tasks[capnode.hostname] = status_tasks.get_status.apply_async( queue=capnode.hostname) for capnode in capture_nodes: ser_data = CaptureNodeSerializer(capnode).data data = tasks[capnode.hostname].get( timeout=self.timeout).get('data') if hasattr(data, 'get'): ser_data['state'] = data.get('state') else: ser_data['state'] = None ser_data['is_admin'] = request.user.extra.is_admin_for(capnode) ephemeral_task_cleanup(tasks[capnode.hostname]) capture_node_info.append(ser_data) return Response(status=200, data={'data': capture_node_info})