def _execute(self, cprefix, command): if not command: raise iex.BadRequest("execute argument 'command' not present") if not command.command: raise iex.BadRequest("command not set") cmd_res = IonObject("AgentCommandResult", command_id=command.command_id, command=command.command) cmd_func = getattr(self, cprefix + str(command.command), None) if cmd_func: cmd_res.ts_execute = get_ion_ts() try: res = cmd_func(*command.args, **command.kwargs) cmd_res.status = 0 cmd_res.result = res except Exception as ex: # TODO: Distinguish application vs. uncaught exception cmd_res.status = getattr(ex, 'status_code', -1) cmd_res.result = str(ex) log.info("Agent function failed with ex=%s msg=%s" % (type(ex), str(ex))) else: log.info("Agent command not supported: %s" % (command.command)) ex = iex.NotFound("Command not supported: %s" % command.command) cmd_res.status = iex.NotFound.status_code cmd_res.result = str(ex) return cmd_res
def _execute(self, cprefix, command): if not command: raise iex.BadRequest("execute argument 'command' not present") if not command.command: raise iex.BadRequest("command not set") cmd_res = IonObject("AgentCommandResult", command_id=command.command_id, command=command.command) cmd_func = getattr(self, cprefix + str(command.command), None) if cmd_func: cmd_res.ts_execute = get_ion_ts() try: res = cmd_func(*command.args, **command.kwargs) cmd_res.status = 0 cmd_res.result = res except iex.IonException as ex: # TODO: Distinguish application vs. uncaught exception cmd_res.status = getattr(ex, 'status_code', -1) cmd_res.result = str(ex) log.warn("Agent command %s failed with trace=%s" % (command.command, traceback.format_exc())) else: log.info("Agent command not supported: %s" % (command.command)) ex = iex.NotFound("Command not supported: %s" % command.command) cmd_res.status = iex.NotFound.status_code cmd_res.result = str(ex) sub_type = "%s.%s" % (command.command, cmd_res.status) post_event = self._event_publisher._create_event(event_type=self.COMMAND_EVENT_TYPE, origin=self.resource_id, origin_type=self.ORIGIN_TYPE, sub_type=sub_type, command=command, result=cmd_res) post_event = self._post_execute_event_hook(post_event) success = self._event_publisher._publish_event(post_event, origin=post_event.origin) return cmd_res
def execute_resource(self, resource_id='', command=None): """ """ if not command: iex = BadRequest('Execute argument "command" not set.') self._on_command_error('execute_resource', None, None, None, iex) raise iex # Grab command syntax. id = command.command_id cmd = command.command args = command.args or [] kwargs = command.kwargs or {} if not command.command: iex = BadRequest('Command name not set.') self._on_command_error('execute_resource', cmd, args, kwargs, iex) raise iex # Construct a command result object. cmd_result = IonObject("AgentCommandResult", command_id=id, command=cmd, ts_execute=get_ion_ts(), status=0) try: result = self._fsm.on_event( ResourceAgentEvent.EXECUTE_RESOURCE, cmd, *args, **kwargs) cmd_result.result = result self._on_command('execute_resource', cmd, args, kwargs, result) except FSMStateError as ex: iex = Conflict(*(ex.args)) self._on_command_error('execute_resource', cmd, args, kwargs, iex) raise iex except FSMCommandUnknownError as ex: iex = BadRequest(*(ex.args)) self._on_command_error('execute_resource', cmd, args, kwargs, iex) raise iex except IonException as iex: self._on_command_error('execute_resource', cmd, args, kwargs, iex) raise iex except Exception as ex: iex = ServerError(*(ex.args)) self._on_command_error('execute_resource', cmd, args, kwargs, iex) raise iex return cmd_result
def _execute(self, cprefix, command): if not command: raise iex.BadRequest("execute argument 'command' not present") if not command.command: raise iex.BadRequest("command not set") cmd_res = IonObject("AgentCommandResult", command_id=command.command_id, command=command.command) cmd_func = getattr(self, cprefix + str(command.command), None) if cmd_func: cmd_res.ts_execute = get_ion_ts() try: res = cmd_func(*command.args, **command.kwargs) cmd_res.status = 0 cmd_res.result = res except Exception as ex: cmd_res.status = getattr(ex, 'status_code', -1) cmd_res.result = str(ex) else: ex = iex.NotFound("Command not supported: %s" % command.command) cmd_res.status = iex.NotFound.status_code cmd_res.result = str(ex) return cmd_res