List of usage examples for com.google.common.net HostAndPort fromParts
public static HostAndPort fromParts(String host, int port)
From source file:org.apache.brooklyn.location.jclouds.JcloudsLocation.java
protected LoginCredentials waitForSshable(final ComputeService computeService, final NodeMetadata node, Optional<HostAndPort> hostAndPortOverride, List<LoginCredentials> credentialsToTry, ConfigBag setup) { String waitForSshable = setup.get(WAIT_FOR_SSHABLE); checkArgument(!"false".equalsIgnoreCase(waitForSshable), "waitForReachable called despite waitForSshable=%s for %s", waitForSshable, node); checkArgument(credentialsToTry.size() > 0, "waitForReachable called without credentials for %s", node); Duration timeout = null;//from w w w . j av a 2 s .com try { timeout = Duration.parse(waitForSshable); } catch (Exception e) { // normal if 'true'; just fall back to default } if (timeout == null) { timeout = Duration.parse(WAIT_FOR_SSHABLE.getDefaultValue()); } Set<String> users = Sets.newLinkedHashSet(); for (LoginCredentials creds : credentialsToTry) { users.add(creds.getUser()); } String user = (users.size() == 1) ? Iterables.getOnlyElement(users) : "{" + Joiner.on(",").join(users) + "}"; String vmIp = hostAndPortOverride.isPresent() ? hostAndPortOverride.get().getHostText() : getFirstReachableAddress(node, setup); if (vmIp == null) LOG.warn("Unable to extract IP for " + node + " (" + setup.getDescription() + "): subsequent connection attempt will likely fail"); int vmPort = hostAndPortOverride.isPresent() ? hostAndPortOverride.get().getPortOrDefault(22) : 22; String connectionDetails = user + "@" + vmIp + ":" + vmPort; final HostAndPort hostAndPort = hostAndPortOverride.isPresent() ? hostAndPortOverride.get() : HostAndPort.fromParts(vmIp, vmPort); final AtomicReference<LoginCredentials> credsSuccessful = new AtomicReference<LoginCredentials>(); // Don't use config that relates to the final user credentials (those have nothing to do // with the initial credentials of the VM returned by the cloud provider). ConfigBag sshProps = ConfigBag.newInstanceCopying(setup); sshProps.remove("password"); sshProps.remove("privateKeyData"); sshProps.remove("privateKeyFile"); sshProps.remove("privateKeyPassphrase"); final Map<SshMachineLocation, LoginCredentials> machinesToTry = Maps.newLinkedHashMap(); for (LoginCredentials creds : credentialsToTry) { machinesToTry.put(createTemporarySshMachineLocation(hostAndPort, creds, sshProps), creds); } try { Callable<Boolean> checker = new Callable<Boolean>() { public Boolean call() { for (Map.Entry<SshMachineLocation, LoginCredentials> entry : machinesToTry.entrySet()) { SshMachineLocation machine = entry.getKey(); int exitstatus = machine .execScript( ImmutableMap.of(SshTool.PROP_SSH_TRIES_TIMEOUT.getName(), Duration.THIRTY_SECONDS.toMilliseconds(), SshTool.PROP_SSH_TRIES.getName(), 1), "check-connectivity", ImmutableList.of("true")); boolean success = (exitstatus == 0); if (success) { credsSuccessful.set(entry.getValue()); return true; } } return false; } }; waitForReachable(checker, connectionDetails, credentialsToTry, setup, timeout); } finally { for (SshMachineLocation machine : machinesToTry.keySet()) { getManagementContext().getLocationManager().unmanage(machine); Streams.closeQuietly(machine); } } return credsSuccessful.get(); }
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.// ww w . ja v a2s . 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, 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); }
From source file:org.apache.brooklyn.location.jclouds.JcloudsLocation.java
private Maybe<String> getPrivateHostnameAws(NodeMetadata node, Optional<HostAndPort> sshHostAndPort, LoginCredentials userCredentials, ConfigBag setup) { // TODO Remove duplication from getPublicHostname. // TODO Don't like HostAndPort inferredHostAndPort = null; if (!sshHostAndPort.isPresent()) { try {//from w w w .j a v a 2 s. c o m 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) { HostAndPort hostAndPortToUse = sshHostAndPort.isPresent() ? sshHostAndPort.get() : inferredHostAndPort; try { return Maybe.of(getPublicHostnameAws(hostAndPortToUse, userCredentials, setup)); } catch (Exception e) { LOG.warn("Error querying aws-ec2 instance instance " + node.getId() + "@" + node.getLocation() + " over ssh for its hostname; falling back to jclouds metadata for address", e); } } return Maybe.absent(); }