Example usage for com.google.common.net HostAndPort getHostText

List of usage examples for com.google.common.net HostAndPort getHostText

Introduction

In this page you can find the example usage for com.google.common.net HostAndPort getHostText.

Prototype

public String getHostText() 

Source Link

Document

Returns the portion of this HostAndPort instance that should represent the hostname or IPv4/IPv6 literal.

Usage

From source file:org.apache.brooklyn.location.jclouds.JcloudsLocation.java

/**
 * Creates a temporary ssh machine location (i.e. will not be persisted), which uses the given credentials.
 * It ignores any credentials (e.g. password, key-phrase, etc) that are supplied in the config.
 *//*w ww .j av a2 s. com*/
protected SshMachineLocation createTemporarySshMachineLocation(HostAndPort hostAndPort, LoginCredentials creds,
        ConfigBag config) {
    String initialUser = creds.getUser();
    Optional<String> initialPassword = creds.getOptionalPassword();
    Optional<String> initialPrivateKey = creds.getOptionalPrivateKey();

    Map<String, Object> sshProps = Maps.newLinkedHashMap(config.getAllConfig());
    sshProps.put("user", initialUser);
    sshProps.put("address", hostAndPort.getHostText());
    sshProps.put("port", hostAndPort.getPort());
    sshProps.put(AbstractLocation.TEMPORARY_LOCATION.getName(), true);
    sshProps.put(LocalLocationManager.CREATE_UNMANAGED.getName(), true);
    sshProps.remove("password");
    sshProps.remove("privateKeyData");
    sshProps.remove("privateKeyFile");
    sshProps.remove("privateKeyPassphrase");

    if (initialPassword.isPresent())
        sshProps.put("password", initialPassword.get());
    if (initialPrivateKey.isPresent())
        sshProps.put("privateKeyData", initialPrivateKey.get());

    if (isManaged()) {
        return getManagementContext().getLocationManager().createLocation(sshProps, SshMachineLocation.class);
    } else {
        return new SshMachineLocation(sshProps);
    }
}

From source file:org.apache.brooklyn.location.jclouds.JcloudsLocation.java

/**
 * Creates a temporary WinRM machine location (i.e. will not be persisted), which uses the given credentials.
 * It ignores any credentials (e.g. password, key-phrase, etc) that are supplied in the config.
 *///from  w w  w  .  j  av  a  2s .  co m
protected WinRmMachineLocation createTemporaryWinRmMachineLocation(HostAndPort hostAndPort,
        LoginCredentials creds, ConfigBag config) {
    String initialUser = creds.getUser();
    Optional<String> initialPassword = creds.getOptionalPassword();
    Optional<String> initialPrivateKey = creds.getOptionalPrivateKey();

    Map<String, Object> winrmProps = Maps.newLinkedHashMap(config.getAllConfig());
    winrmProps.put("user", initialUser);
    winrmProps.put("address", hostAndPort.getHostText());
    winrmProps.put("port", hostAndPort.getPort());
    winrmProps.put(AbstractLocation.TEMPORARY_LOCATION.getName(), true);
    winrmProps.put(LocalLocationManager.CREATE_UNMANAGED.getName(), true);
    winrmProps.remove("password");
    winrmProps.remove("privateKeyData");
    winrmProps.remove("privateKeyFile");
    winrmProps.remove("privateKeyPassphrase");

    if (initialPassword.isPresent())
        winrmProps.put("password", initialPassword.get());
    if (initialPrivateKey.isPresent())
        winrmProps.put("privateKeyData", initialPrivateKey.get());

    if (isManaged()) {
        return getManagementContext().getLocationManager().createLocation(winrmProps,
                WinRmMachineLocation.class);
    } else {
        throw new UnsupportedOperationException(
                "Cannot create temporary WinRmMachineLocation because " + this + " is not managed");
    }
}

From source file:brooklyn.location.jclouds.JcloudsLocation.java

/**
 * Attempts to obtain the hostname or IP of the node, as advertised by the cloud provider.
 * Prefers public, reachable IPs. /*from  ww w  . jav  a 2s  . c  om*/
 * For some clouds (e.g. aws-ec2), it will attempt to find the public hostname.
 */
protected String getPublicHostname(NodeMetadata node, Optional<HostAndPort> sshHostAndPort, ConfigBag setup) {
    String provider = (setup != null) ? setup.get(CLOUD_PROVIDER) : null;
    if (provider == null)
        provider = getProvider();

    if ("aws-ec2".equals(provider)) {
        HostAndPort inferredHostAndPort = null;
        if (!sshHostAndPort.isPresent()) {
            try {
                String vmIp = JcloudsUtil.getFirstReachableAddress(this.getComputeService().getContext(), node);
                int port = node.getLoginPort();
                inferredHostAndPort = HostAndPort.fromParts(vmIp, port);
            } catch (Exception e) {
                LOG.warn("Error reaching aws-ec2 instance " + node.getId() + "@" + node.getLocation()
                        + " on port " + node.getLoginPort() + "; falling back to jclouds metadata for address",
                        e);
            }
        }
        if (sshHostAndPort.isPresent() || inferredHostAndPort != null) {
            HostAndPort hostAndPortToUse = sshHostAndPort.isPresent() ? sshHostAndPort.get()
                    : inferredHostAndPort;
            try {
                return getPublicHostnameAws(hostAndPortToUse, setup);
            } catch (Exception e) {
                LOG.warn("Error querying aws-ec2 instance instance " + node.getId() + "@" + node.getLocation()
                        + " over ssh for its hostname; falling back to first reachable IP", e);
                // We've already found a reachable address so settle for that, rather than doing it again
                if (inferredHostAndPort != null)
                    return inferredHostAndPort.getHostText();
            }
        }
    }

    return getPublicHostnameGeneric(node, setup);
}

From source file:org.apache.brooklyn.location.jclouds.JcloudsLocation.java

/**
 * Attempts to obtain the hostname or IP of the node, as advertised by the cloud provider.
 * Prefers public, reachable IPs./*from   w w w  .ja v a  2  s  .  c o m*/
 * For some clouds (e.g. aws-ec2), it will attempt to find the public hostname.
 */
protected String getPublicHostname(NodeMetadata node, Optional<HostAndPort> sshHostAndPort,
        LoginCredentials userCredentials, ConfigBag setup) {
    String provider = (setup != null) ? setup.get(CLOUD_PROVIDER) : null;
    if (provider == null)
        provider = getProvider();

    if ("aws-ec2".equals(provider)) {
        HostAndPort inferredHostAndPort = null;
        if (!sshHostAndPort.isPresent()) {
            try {
                String vmIp = getFirstReachableAddress(node, setup);
                int port = node.getLoginPort();
                inferredHostAndPort = HostAndPort.fromParts(vmIp, port);
            } catch (Exception e) {
                LOG.warn("Error reaching aws-ec2 instance " + node.getId() + "@" + node.getLocation()
                        + " on port " + node.getLoginPort() + "; falling back to jclouds metadata for address",
                        e);
            }
        }
        if (sshHostAndPort.isPresent() || inferredHostAndPort != null) {
            if (isWindows(node, setup)) {
                if (inferredHostAndPort != null) {
                    LOG.warn("Cannot querying aws-ec2 Windows instance " + node.getId() + "@"
                            + node.getLocation()
                            + " over ssh for its hostname; falling back to first reachable IP");
                    return inferredHostAndPort.getHostText();
                }
            } else {
                HostAndPort hostAndPortToUse = sshHostAndPort.isPresent() ? sshHostAndPort.get()
                        : inferredHostAndPort;
                try {
                    return getPublicHostnameAws(hostAndPortToUse, userCredentials, setup);
                } catch (Exception e) {
                    if (inferredHostAndPort != null) {
                        LOG.warn("Error querying aws-ec2 instance " + node.getId() + "@" + node.getLocation()
                                + " over ssh for its hostname; falling back to first reachable IP", e);
                        // We've already found a reachable address so settle for that, rather than doing it again
                        return inferredHostAndPort.getHostText();
                    } else {
                        LOG.warn("Error querying aws-ec2 instance " + node.getId() + "@" + node.getLocation()
                                + " over ssh for its hostname; falling back to jclouds metadata for address",
                                e);
                    }
                }
            }
        }
    }

    return getPublicHostnameGeneric(node, setup);
}