async def _generate_get_response(self, instance_id, start_line, end_line): wait_event = Event() response = await self.client( Operation( operation_type="INSTANCE_LOGS", args=[instance_id], kwargs={ "wait_event": wait_event, "start_line": start_line, "end_line": end_line, }, ), serialize_kwargs={"to_string": False}, ) wait_timeout = float(self.get_argument("timeout", default="15")) if wait_timeout < 0: wait_timeout = None if not await event_wait(wait_event, wait_timeout): raise TimeoutExceededError("Timeout exceeded") response = await self.client( Operation(operation_type="REQUEST_READ", args=[response["id"]]), serialize_kwargs={"to_string": False}, ) if response["status"] == "ERROR": raise RequestProcessingError(response["output"])
def _read_log(self, **kwargs): """Handle read log Request""" log_file = find_log_file() if not log_file: raise RequestProcessingError( "Error attempting to retrieve logs - unable to determine log filename. " "Please verify that the plugin is writing to a log file." ) try: return read_log_file(log_file=log_file, **kwargs) except IOError as e: raise RequestProcessingError( "Error attempting to retrieve logs - unable to read log file at {0}. " "Root cause I/O error {1}: {2}".format(log_file, e.errno, e.strerror) )
def _invoke_command(self, target, request): """Invoke the function named in request.command. :param target: The object to search for the function implementation. Will be self or self.client. :param request: The request to process :raise RequestProcessingError: The specified target does not define a callable implementation of request.command :return: The output of the function invocation """ if not callable(getattr(target, request.command, None)): raise RequestProcessingError("Could not find an implementation of command '%s'" % request.command) # It's kinda weird that we need to add the object arg only if we're trying to call # a function on self In both cases the function object is bound... # think it has something to do with our decorators args = [self] if target is self else [] return getattr(target, request.command)(*args, **request.parameters)
def _pre_process(self, message, verify_system=True): if self.shutdown_event.is_set(): raise RequestProcessingError('Unable to process message - currently shutting down') try: request = self.parser.parse_request(message, from_string=True) except Exception as ex: self.logger.exception("Unable to parse message body: {0}. Exception: {1}" .format(message, ex)) raise DiscardMessageException('Error parsing message body') if (verify_system and request.command_type and request.command_type.upper() != 'EPHEMERAL' and request.system.upper() != self.system.name.upper()): raise DiscardMessageException("Received message for a different system {0}" .format(request.system.upper())) return request
def _invoke_command(self, target, request, headers): """Invoke the function named in request.command Args: target: The object to search for the function implementation. request: The request to process headers: The headers for this request Returns: The output of the function call Raises: RequestProcessingError: The specified target does not define a callable implementation of request.command """ if not callable(getattr(target, request.command, None)): raise RequestProcessingError( "Could not find an implementation of command '%s'" % request.command) # Get the command to use the parameter definitions when resolving command = None if self._system: command = self._system.get_command_by_name(request.command) # Now resolve parameters, if necessary if request.is_ephemeral or not command: parameters = request.parameters or {} else: parameters = self._resolver.resolve( request.parameters, definitions=command.parameters, upload=False, ) return getattr(target, request.command)(**parameters)
def _is_running(self, _): """Validate that this plugin is still running""" if self._shutdown_event.is_set(): raise RequestProcessingError( "Unable to process message - currently shutting down" )
async def get(self, instance_id): """ --- summary: Retrieve a specific Instance parameters: - name: instance_id in: path required: true description: The ID of the Instance type: string - name: start_line in: query required: false description: Start line of logs to read from instance type: int - name: end_line in: query required: false description: End line of logs to read from instance type: int - name: timeout in: query required: false description: Max seconds to wait for request completion. (-1 = wait forever) type: float default: -1 responses: 200: description: Instance with the given ID schema: $ref: '#/definitions/Instance' 404: $ref: '#/definitions/404Error' 50x: $ref: '#/definitions/50xError' tags: - Instances """ start_line = self.get_query_argument("start_line", default=None) if start_line == "": start_line = None elif start_line: start_line = int(start_line) end_line = self.get_query_argument("end_line", default=None) if end_line == "": end_line = None elif end_line: end_line = int(end_line) wait_event = Event() response = await self.client( Operation( operation_type="INSTANCE_LOGS", args=[instance_id], kwargs={ "wait_event": wait_event, "start_line": start_line, "end_line": end_line, }, ), serialize_kwargs={"to_string": False}, ) wait_timeout = float(self.get_argument("timeout", default="-1")) if not await event_wait(wait_event, wait_timeout): raise TimeoutExceededError("Timeout exceeded") response = await self.client( Operation(operation_type="REQUEST_READ", args=[response["id"]]), serialize_kwargs={"to_string": False}, ) if response["status"] == "ERROR": raise RequestProcessingError(response["output"]) self.set_header("request_id", response["id"]) self.set_header("Content-Type", "text/plain; charset=UTF-8") self.write(response["output"])