def connect(address, port, tunnel=False, authkey='PublicKey', pubkey=None,
            logfile=None):
    """
    Connects to the server at `address` and `port` using `key` and returns
    a (shared) proxy for the associated :class:`ObjServerFactory`.

    address: string
        IP address for server or pipe filename.

    port: int
        Server port.  If < 0, `address` is a pipe filename.

    tunnel: bool
        Connect via SSH tunnel.

    authkey:
        Server authorization key.

    pubkey:
        Server public key; required if `authkey` is 'PublicKey'.

    logfile:
        Location of server's log file, if known.
    """
    if port < 0:
        key = address
    else:
        key = (address, port)
    try:
        return _PROXIES[key]
    except KeyError:
        # Requires ssh setup.
        if tunnel:  # pragma no cover
            location, cleanup = setup_tunnel(address, port)
            atexit.register(*cleanup)
        else:
            location = key
        via = ' (via tunnel)' if tunnel else ''
        log = ' at %s' % logfile if logfile else ''
        if not OpenMDAO_Proxy.manager_is_alive(location):
            raise RuntimeError("Can't connect to server at %s:%s%s. It appears"
                               " to be offline. Please check the server log%s."
                               % (address, port, via, log))

        mgr = _FactoryManager(location, authkey, pubkey=pubkey)
        try:
            mgr.connect()
        except EOFError:
            raise RuntimeError("Can't connect to server at %s:%s%s. It appears"
                               " to be rejecting the connection. Please check"
                               " the server log%s." % (address, port, via, log))

        proxy = mgr.openmdao_main_objserverfactory_ObjServerFactory()
        if proxy.version != __version__:
            logging.warning('Server version %r different than local version %r',
                            proxy.version, __version__)
        _PROXIES[key] = proxy
        return proxy
Example #2
0
    def from_address(cls, address, authkey, host):
        """
        Return manager given an address.

        address: (ip_addr, port) or string referring to pipe.
            Address to connect to.

        authkey: string
            Authorization key.

        host: :class:`Host`
            Host we're managing.
        """
        if host.tunnel_outgoing:
            _LOGGER.debug('Client setting up tunnel for %s:%s', host.hostname,
                          address[1])
            address, cleanup = setup_tunnel(host.hostname,
                                            address[1],
                                            identity=host.identity_filename)
        else:
            cleanup = None

        manager = cls(address, authkey)
        _LOGGER.debug('Client connecting to server at %s' % (address, ))
        conn = connection.Client(address, authkey=authkey)
        try:
            managers.dispatch(conn, None, 'dummy')
        finally:
            conn.close()
        manager._state.value = managers.State.STARTED
        manager._name = 'Host-%s:%s' % manager.address
        manager.shutdown = util.Finalize(manager,
                                         HostManager._finalize_host,
                                         args=(manager._address,
                                               manager._authkey, cleanup,
                                               host.reverse_cleanup),
                                         exitpriority=-10)
        return manager
    def from_address(cls, address, authkey, host):
        """
        Return manager given an address.

        address: (ip_addr, port) or string referring to pipe.
            Address to connect to.

        authkey: string
            Authorization key.

        host: :class:`Host`
            Host we're managing.
        """
        if host.tunnel_outgoing:
            _LOGGER.debug('Client setting up tunnel for %s:%s',
                          host.hostname, address[1])
            address, cleanup = setup_tunnel(host.hostname, address[1],
                                            identity=host.identity_filename)
        else:
            cleanup = None

        manager = cls(address, authkey)
        _LOGGER.debug('Client connecting to server at %s' % (address,))
        conn = connection.Client(address, authkey=authkey)
        try:
            managers.dispatch(conn, None, 'dummy')
        finally:
            conn.close()
        manager._state.value = managers.State.STARTED
        manager._name = 'Host-%s:%s' % manager.address
        manager.shutdown = util.Finalize(
            manager, HostManager._finalize_host,
            args=(manager._address, manager._authkey,
                  cleanup, host.reverse_cleanup),
            exitpriority=-10)
        return manager