Exemplo n.º 1
0
    def migration_handle_import(self, state, agent_type=None, blackbox=None):

        def render_response(resp):
            return spec.HandleImportResponse()

        factory = descriptor.lookup(agent_type)
        if factory is None:
            raise ValueError('Unknown agent type: %r' % (agent_type, ))
        doc = factory()
        kwargs = dict()
        if blackbox:
            kwargs['blackbox'] == blackbox
        f = self.save_document(doc)
        f.add_callback(fiber.inject_param, 2, self.initiate_protocol,
                       start_agent.GloballyStartAgent, **kwargs)
        f.add_callback(fiber.call_param, 'notify_finish')
        f.add_callback(render_response)
        return f
Exemplo n.º 2
0
 def _flush_agents_body(self):
     medium = self._get_host_medium()
     if medium is None:
         msg = "Host Agent not ready yet, agent will be spawned later."
         defer.returnValue(msg)
     yield medium.wait_for_state(AgencyAgentState.ready)
     agent = medium.get_agent()
     while True:
         try:
             to_spawn = self._to_spawn.pop(0)
         except IndexError:
             break
         desc, kwargs = to_spawn
         if not isinstance(desc, descriptor.Descriptor):
             factory = descriptor.lookup(desc)
             if factory is None:
                 raise ValueError(
                     'No descriptor factory found for agent %r' % desc)
             desc = factory()
         desc = yield medium.save_document(desc)
         yield agent.start_agent(desc, **kwargs)
Exemplo n.º 3
0
    def spawn_next_agent(self, state):

        def handle_success(entry):
            f = self.remove_import_entry(entry)
            f.add_callback(fiber.drop_param, self.spawn_next_agent)
            return f

        def handle_failure(fail, entry):
            msg = ("Failed to start agent, import entry: %r" % entry)
            self.raise_alert(msg, alert.Severity.high)

        if state.spawn_running:
            return

        self._set_semaphore(True)

        entry = self.get_top_import_entry()
        if entry is not None:
            factory = descriptor.lookup(entry.agent_type)
            if factory is None:
                raise ValueError('Unknown agent type: %r' %
                                 (entry.agent_type, ))
            doc = factory()
            kwargs = dict()
            if entry.blackbox:
                kwargs['blackbox'] == entry.blackbox
            f = self.save_document(doc)
            f.add_callback(fiber.inject_param, 2, self.initiate_protocol,
                           start_agent.GloballyStartAgent, **kwargs)
            f.add_callback(fiber.call_param, 'notify_finish')
            f.add_callbacks(fiber.drop_param, handle_failure,
                            cbargs=(handle_success, entry),
                            ebargs=(entry, ))
            f.add_both(fiber.bridge_param, self._set_semaphore, False)

            return f
Exemplo n.º 4
0
def bootstrap(parser=None, args=None, descriptors=None):
    """Bootstrap a feat process, handling command line arguments.
    @param parser: the option parser to use; more options will be
        added to the parser; if not specified or None
        a new one will be created
    @type  parser: optparse.OptionParser or None
    @param args: the command line arguments to parse; if not specified
        or None, sys.argv[1:] will be used
    @type  args: [str()] or None
    @param descriptors: the descriptors of the agent to starts in addition
        of the host agent; if not specified or None
        no additional agents will be started
    @type  descriptors: [Descriptor()] or None
    @return: the deferred of the bootstrap chain
    @rtype:  defer.Deferred()"""

    parser = parser or optparse.OptionParser()
    add_options(parser)
    net_agency.add_options(parser)

    with _Bootstrap(parser=parser, args=args) as bootstrap:
        agency = bootstrap.agency
        opts = bootstrap.opts
        args = bootstrap.args
        opts, args = check_options(opts, args)

        descriptors = descriptors or []
        d = agency.initiate()

        if not opts.standalone:
            # specific to running normal agency
            for name in opts.agents:
                factory = descriptor.lookup(name)
                if factory is None:
                    msg = "No descriptor factory found for agent %s" % name
                    raise OptionError(msg)
                descriptors.append(factory())

            hostdef = opts.hostdef
            if opts.hostres or opts.hostcat:
                hostdef = host.HostDef()
                for resdef in opts.hostres:
                    parts = resdef.split(":", 1)
                    name = parts[0]
                    value = 1
                    if len(parts) > 1:
                        try:
                            value = int(parts[1])
                        except ValueError:
                            raise OptionError("Invalid host resource: %s"
                                              % resdef)
                    hostdef.resources[name] = value

                for catdef in opts.hostcat:
                    name, value = check_category(catdef)
                    hostdef.categories[name] = value

            agency.set_host_def(hostdef)

            for desc in descriptors:
                log.debug("feat", "Starting agent with descriptor %r", desc)
                d.addCallback(defer.drop_param, agency.spawn_agent, desc)
        else:
            # standalone specific
            kwargs = opts.standalone_kwargs or dict()
            d.addCallback(defer.drop_param, agency.spawn_agent,
                          opts.agents[0], **kwargs)
        return d