List of usage examples for com.google.common.net HostAndPort getPort
public int getPort()
From source file:io.airlift.jmx.JmxAgent.java
@Inject public JmxAgent(JmxConfig config) throws IOException { // first, see if the jmx agent is already started (e.g., via command line properties passed to the jvm) HostAndPort address = getRunningAgentAddress(config.getRmiRegistryPort(), config.getRmiServerPort()); if (address != null) { log.info("JMX agent already running and listening on %s", address); } else {// w w w . j a v a 2 s. c o m // otherwise, start it manually int registryPort; if (config.getRmiRegistryPort() == null) { registryPort = NetUtils.findUnusedPort(); } else { registryPort = config.getRmiRegistryPort(); } int serverPort = 0; if (config.getRmiServerPort() != null) { serverPort = config.getRmiServerPort(); } // This is somewhat of a hack, but the jmx agent in Oracle/OpenJDK doesn't // have a programmatic API for starting it and controlling its parameters System.setProperty("com.sun.management.jmxremote", "true"); System.setProperty("com.sun.management.jmxremote.port", Integer.toString(registryPort)); System.setProperty("com.sun.management.jmxremote.rmi.port", Integer.toString(serverPort)); System.setProperty("com.sun.management.jmxremote.authenticate", "false"); System.setProperty("com.sun.management.jmxremote.ssl", "false"); try { Agent.startAgent(); } catch (Exception e) { throw Throwables.propagate(e); } try { // This is how the jdk jmx agent constructs its url JMXServiceURL url = new JMXServiceURL("rmi", null, registryPort); address = HostAndPort.fromParts(url.getHost(), url.getPort()); } catch (MalformedURLException e) { // should not happen... throw new AssertionError(e); } log.info("JMX agent started and listening on %s", address); } this.url = new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", address.getHostText(), address.getPort())); }
From source file:io.airlift.jmx.JmxAgent8.java
@Inject public JmxAgent8(JmxConfig config) throws IOException { // first, see if the jmx agent is already started (e.g., via command line properties passed to the jvm) HostAndPort address = getRunningAgentAddress(config.getRmiRegistryPort(), config.getRmiServerPort()); if (address != null) { log.info("JMX agent already running and listening on %s", address); } else {/*from w w w .j a v a 2 s . c o m*/ // otherwise, start it manually int registryPort; if (config.getRmiRegistryPort() == null) { registryPort = NetUtils.findUnusedPort(); } else { registryPort = config.getRmiRegistryPort(); } int serverPort = 0; if (config.getRmiServerPort() != null) { serverPort = config.getRmiServerPort(); } // This is somewhat of a hack, but the jmx agent in Oracle/OpenJDK doesn't // have a programmatic API for starting it and controlling its parameters System.setProperty("com.sun.management.jmxremote", "true"); System.setProperty("com.sun.management.jmxremote.port", Integer.toString(registryPort)); System.setProperty("com.sun.management.jmxremote.rmi.port", Integer.toString(serverPort)); System.setProperty("com.sun.management.jmxremote.authenticate", "false"); System.setProperty("com.sun.management.jmxremote.ssl", "false"); try { Agent.startAgent(); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } try { // This is how the jdk jmx agent constructs its url JMXServiceURL url = new JMXServiceURL("rmi", null, registryPort); address = HostAndPort.fromParts(url.getHost(), url.getPort()); } catch (MalformedURLException e) { // should not happen... throw new AssertionError(e); } log.info("JMX agent started and listening on %s", address); } this.url = new JMXServiceURL( String.format("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", address.getHost(), address.getPort())); }
From source file:com.proofpoint.jmx.JmxAgent8.java
@Inject public JmxAgent8(JmxConfig config) throws IOException { // first, see if the jmx agent is already started (e.g., via command line properties passed to the jvm) HostAndPort address = getRunningAgentAddress(config.getRmiRegistryPort(), config.getRmiServerPort()); if (address != null) { log.info("JMX agent already running and listening on %s", address); } else if (config.isEnabled()) { // otherwise, start it manually int registryPort; if (config.getRmiRegistryPort() == null) { registryPort = NetUtils.findUnusedPort(); } else {/* w w w .j a v a2 s.com*/ registryPort = config.getRmiRegistryPort(); } int serverPort = 0; if (config.getRmiServerPort() != null) { serverPort = config.getRmiServerPort(); } // This is somewhat of a hack, but the jmx agent in Oracle/OpenJDK doesn't // have a programmatic API for starting it and controlling its parameters System.setProperty("com.sun.management.jmxremote", "true"); System.setProperty("com.sun.management.jmxremote.port", Integer.toString(registryPort)); System.setProperty("com.sun.management.jmxremote.rmi.port", Integer.toString(serverPort)); System.setProperty("com.sun.management.jmxremote.authenticate", "false"); System.setProperty("com.sun.management.jmxremote.ssl", "false"); try { Agent.startAgent(); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } try { // This is how the jdk jmx agent constructs its url JMXServiceURL url = new JMXServiceURL("rmi", null, registryPort); address = HostAndPort.fromParts(url.getHost(), url.getPort()); } catch (MalformedURLException e) { // should not happen... throw new AssertionError(e); } log.info("JMX agent started and listening on %s", address); } if (config.isEnabled()) { this.url = new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", address.getHost(), address.getPort())); } else { this.url = null; } }
From source file:brooklyn.location.jclouds.JcloudsLocation.java
protected SshMachineLocation createTemporarySshMachineLocation(HostAndPort hostAndPort, LoginCredentials creds, ConfigBag config) {// w ww. j a va 2 s. c o m Optional<String> initialPassword = creds.getOptionalPassword(); Optional<String> initialPrivateKey = creds.getOptionalPrivateKey(); String initialUser = creds.getUser(); Map<String, Object> sshProps = Maps.newLinkedHashMap(config.getAllConfig()); sshProps.put("user", initialUser); sshProps.put("address", hostAndPort.getHostText()); sshProps.put("port", hostAndPort.getPort()); if (initialPassword.isPresent()) sshProps.put("password", initialPassword.get()); if (initialPrivateKey.isPresent()) sshProps.put("privateKeyData", initialPrivateKey.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:brooklyn.location.jclouds.JcloudsLocation.java
private String getPublicHostnameAws(HostAndPort sshHostAndPort, ConfigBag setup) { SshMachineLocation sshLocByIp = null; try {//from www . j a va 2 s . c om ConfigBag sshConfig = extractSshConfig(setup, new ConfigBag()); // TODO messy way to get an SSH session if (isManaged()) { sshLocByIp = getManagementContext().getLocationManager() .createLocation(LocationSpec.create(SshMachineLocation.class) .configure("address", sshHostAndPort.getHostText()) .configure("port", sshHostAndPort.getPort()).configure("user", getUser(setup)) .configure(sshConfig.getAllConfig())); } else { MutableMap<Object, Object> locationProps = MutableMap.builder() .put("address", sshHostAndPort.getHostText()).put("port", sshHostAndPort.getPort()) .put("user", getUser(setup)).putAll(sshConfig.getAllConfig()).build(); sshLocByIp = new SshMachineLocation(locationProps); } ByteArrayOutputStream outStream = new ByteArrayOutputStream(); ByteArrayOutputStream errStream = new ByteArrayOutputStream(); int exitcode = sshLocByIp.execCommands(MutableMap.of("out", outStream, "err", errStream), "get public AWS hostname", ImmutableList.of(BashCommands.INSTALL_CURL, "echo `curl --silent --retry 20 http://169.254.169.254/latest/meta-data/public-hostname`; exit")); String outString = new String(outStream.toByteArray()); String[] outLines = outString.split("\n"); for (String line : outLines) { if (line.startsWith("ec2-")) return line.trim(); } throw new IllegalStateException( "Could not obtain aws-ec2 hostname for vm " + sshHostAndPort + "; exitcode=" + exitcode + "; stdout=" + outString + "; stderr=" + new String(errStream.toByteArray())); } finally { Streams.closeQuietly(sshLocByIp); } }
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. *//*from w w w . j a va 2s.c o m*/ 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 a v a 2s.c o 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:org.apache.brooklyn.location.jclouds.DefaultConnectivityResolver.java
/** * Combines the given resolve options with the customiser's configuration to determine the * best address and credential pair for management. In particular, if the resolve options * allow it will check that the credential is actually valid for the address. *///from w w w. ja v a2s. c om @Override public ManagementAddressResolveResult resolve(JcloudsLocation location, NodeMetadata node, ConfigBag config, ConnectivityResolverOptions options) { LOG.debug("{} resolving management parameters for {}, node={}, config={}, options={}", new Object[] { this, location, node, config, options }); final Stopwatch timer = Stopwatch.createStarted(); // Should only be null in tests. final Entity contextEntity = getContextEntity(config); if (shouldPublishNetworks() && !options.isRebinding() && contextEntity != null) { publishNetworks(node, contextEntity); } HostAndPort hapChoice = null; LoginCredentials credChoice = null; final Iterable<HostAndPort> managementCandidates = getManagementCandidates(location, node, config, options); Iterable<LoginCredentials> credentialCandidates = Collections.emptyList(); if (!Iterables.isEmpty(managementCandidates)) { credentialCandidates = getCredentialCandidates(location, node, options, config); // Try each pair of address and credential until one succeeds. if (shouldCheckCredentials() && options.pollForReachableAddresses()) { for (HostAndPort hap : managementCandidates) { for (LoginCredentials cred : credentialCandidates) { LOG.trace("Testing host={} with credential={}", hap, cred); if (checkCredential(location, hap, cred, config, options.isWindows())) { hapChoice = hap; credChoice = cred; break; } } if (hapChoice != null) break; } } else if (shouldCheckCredentials()) { LOG.debug("{} set on {} but pollForFirstReachableAddress={}", new Object[] { CHECK_CREDENTIALS.getName(), this, options.pollForReachableAddresses() }); } } if (hapChoice == null) { LOG.trace("Choosing first management candidate given node={} and mode={}", node, getNetworkMode()); hapChoice = Iterables.getFirst(managementCandidates, null); } if (hapChoice == null) { LOG.trace("Choosing first address of node={} in mode={}", node, getNetworkMode()); final Iterator<String> hit = getResolvableAddressesWithMode(node).iterator(); if (hit.hasNext()) HostAndPort.fromHost(hit.next()); } if (hapChoice == null) { LOG.error("None of the addresses of node {} are reachable in mode {}", new Object[] { node, getNetworkMode() }); throw new IllegalStateException( "Could not determine management address for node: " + node + " in mode: " + getNetworkMode()); } if (credChoice == null) { credChoice = Iterables.getFirst(credentialCandidates, null); if (credChoice == null) { throw new IllegalStateException("No credentials configured for " + location); } } if (contextEntity != null) { contextEntity.sensors().set(Attributes.ADDRESS, hapChoice.getHostText()); } // Treat AWS as a special case because the DNS fully qualified hostname in AWS is // (normally?!) a good way to refer to the VM from both inside and outside of the region. if (!isNetworkModeSet() && !options.isWindows()) { final boolean lookupAwsHostname = Boolean.TRUE .equals(config.get(JcloudsLocationConfig.LOOKUP_AWS_HOSTNAME)); String provider = config.get(JcloudsLocationConfig.CLOUD_PROVIDER); if (provider == null) { provider = location.getProvider(); } if (options.waitForConnectable() && "aws-ec2".equals(provider) && lookupAwsHostname) { // getHostnameAws sshes to the machine and curls 169.254.169.254/latest/meta-data/public-hostname. try { LOG.debug("Resolving AWS hostname of {}", location); String result = location.getHostnameAws(hapChoice, credChoice, config); hapChoice = HostAndPort.fromParts(result, hapChoice.getPort()); LOG.debug("Resolved AWS hostname of {}: {}", location, result); } catch (Exception e) { LOG.debug("Failed to resolve AWS hostname of " + location, e); } } } ManagementAddressResolveResult result = new ManagementAddressResolveResult(hapChoice, credChoice); LOG.debug("{} resolved management parameters for {} in {}: {}", new Object[] { this, location, Duration.of(timer), result }); return result; }