Example #1
0
    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
Example #2
0
    def __init__(self, resource_id, *args, **kwargs):
        assert resource_id, "resource_id must be set for an agent"
        self.resource_id = resource_id

        if not 'name' in kwargs:
            process_id = self._get_agent_process_id(self.resource_id)
            if process_id:
                kwargs['name'] = process_id
                log.debug("Use agent process %s for resource_id=%s" % (process_id, self.resource_id))
            else:
                # TODO: Check if there is a service for this type of resource
                log.debug("No agent process found for resource_id %s" % self.resource_id)
                raise iex.NotFound("No agent process found for resource_id %s" % self.resource_id)

        assert "name" in kwargs, "Name argument for agent target not set"
        ResourceAgentProcessClient.__init__(self, *args, **kwargs)
Example #3
0
 def get_agent_param(self, resource_id="", name=''):
     try:
         return getattr(self, "apar_%s" % name)
     except AttributeError:
         raise iex.NotFound('Agent parameter not found: %s' % name)
Example #4
0
 def set_agent_param(self, resource_id="", name='', value=''):
     if not hasattr(self, "apar_%s" % name):
         raise iex.NotFound('Agent parameter not existing: %s' % name)
     pvalue = getattr(self, "apar_%s" % name)
     setattr(self, "apar_%s" % name, value)
     return pvalue