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

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

Introduction

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

Prototype

public static HostAndPort fromParts(String host, int port) 

Source Link

Document

Build a HostAndPort instance from separate host and port values.

Usage

From source file:brooklyn.entity.mesos.task.marathon.MarathonTaskImpl.java

@Override
public MarathonTaskLocation createLocation(Map<String, ?> flags) {
    Entity entity = getRunningEntity();
    MesosSlave slave = getMesosCluster().getMesosSlave(getHostname());
    SubnetTier subnet = slave.getSubnetTier();
    Boolean sdn = config().get(MesosCluster.SDN_ENABLE);

    // Configure the entity subnet
    LOG.info("Configuring entity {} via subnet {}", entity, subnet);
    entity.config().set(SubnetTier.PORT_FORWARDING_MANAGER, subnet.getPortForwardManager());
    entity.config().set(SubnetTier.PORT_FORWARDER, subnet.getPortForwarder());
    entity.config().set(SubnetTier.SUBNET_CIDR, Cidr.UNIVERSAL);
    DockerUtils.configureEnrichers(subnet, entity);

    // Lookup mapped ports
    List<Map.Entry<Integer, Integer>> portBindings = (List) flags.get("portBindings");
    Map<Integer, String> tcpMappings = MutableMap.of();
    Optional<JsonElement> application = getApplicationJson();
    if (application.isPresent()) {
        JsonArray tasks = application.get().getAsJsonObject().get("app").getAsJsonObject().get("tasks")
                .getAsJsonArray();/*  w  w w.j  a  v a  2s  .co m*/
        for (JsonElement each : tasks) {
            JsonElement ports = each.getAsJsonObject().get("ports");
            if (ports != null && !ports.isJsonNull()) {
                JsonArray array = ports.getAsJsonArray();
                if (array.size() > 0) {
                    for (int i = 0; i < array.size(); i++) {
                        int hostPort = array.get(i).getAsInt();
                        int containerPort = portBindings.get(i).getKey();
                        String address = sdn ? sensors().get(Attributes.SUBNET_ADDRESS) : getId();
                        String target = address + ":" + containerPort;
                        tcpMappings.put(hostPort, target);
                        if (containerPort == 22) { // XXX should be a better way?
                            sensors().set(DockerAttributes.DOCKER_MAPPED_SSH_PORT,
                                    HostAndPort.fromParts(getHostname(), hostPort).toString());
                        }
                    }
                }
            }
        }
    } else {
        throw new IllegalStateException(
                "Cannot retrieve application details for " + sensors().get(APPLICATION_ID));
    }

    // Create our wrapper location around the task
    Boolean useSsh = config().get(DockerAttributes.DOCKER_USE_SSH);
    LocationSpec<MarathonTaskLocation> spec = LocationSpec.create(MarathonTaskLocation.class)
            .parent(getMarathonFramework().getDynamicLocation()).configure(flags)
            .configure(DynamicLocation.OWNER, this).configure("entity", getRunningEntity())
            .configure(CloudLocationConfig.WAIT_FOR_SSHABLE, "false")
            .configure(SshMachineLocation.DETECT_MACHINE_DETAILS, useSsh)
            .configure(SshMachineLocation.TCP_PORT_MAPPINGS, tcpMappings).displayName(getShortName());
    if (useSsh) {
        spec.configure(SshMachineLocation.SSH_HOST, getHostname())
                .configure(SshMachineLocation.SSH_PORT, getSshPort()).configure("address", getAddress())
                .configure(LocationConfigKeys.USER, "root") // TODO from slave
                .configure(LocationConfigKeys.PASSWORD, "p4ssw0rd").configure(SshTool.PROP_PASSWORD, "p4ssw0rd")
                .configure(SshTool.PROP_HOST, getHostname()).configure(SshTool.PROP_PORT, getSshPort())
                .configure(LocationConfigKeys.PRIVATE_KEY_DATA, (String) null) // TODO used to generate authorized_keys
                .configure(LocationConfigKeys.PRIVATE_KEY_FILE, (String) null);
    }
    MarathonTaskLocation location = getManagementContext().getLocationManager().createLocation(spec);
    sensors().set(DYNAMIC_LOCATION, location);
    sensors().set(LOCATION_NAME, location.getId());

    // Record port mappings
    LOG.debug("Recording port mappings for {} at {}: {}", new Object[] { entity, location, tcpMappings });
    for (Integer hostPort : tcpMappings.keySet()) {
        HostAndPort target = HostAndPort.fromString(tcpMappings.get(hostPort));
        subnet.getPortForwarder().openPortForwarding(location, target.getPort(), Optional.of(hostPort),
                Protocol.TCP, Cidr.UNIVERSAL);
        subnet.getPortForwarder().openFirewallPort(entity, hostPort, Protocol.TCP, Cidr.UNIVERSAL);
        LOG.debug("Forwarded port: {} => {}", hostPort, target.getPort());
    }

    LOG.info("New task location {} created", location);
    if (useSsh) {
        DockerUtils.addExtraPublicKeys(entity, location);
    }
    return location;
}

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

protected JcloudsSshMachineLocation obtainOnce(ConfigBag setup) throws NoMachinesAvailableException {
    AccessController.Response access = getManagementContext().getAccessController().canProvisionLocation(this);
    if (!access.isAllowed()) {
        throw new IllegalStateException(
                "Access controller forbids provisioning in " + this + ": " + access.getMsg());
    }/*from  w w  w.  j a va2s. c  om*/

    setCreationString(setup);
    boolean waitForSshable = !"false".equalsIgnoreCase(setup.get(WAIT_FOR_SSHABLE));
    boolean usePortForwarding = setup.get(USE_PORT_FORWARDING);
    boolean skipJcloudsSshing = Boolean.FALSE.equals(setup.get(USE_JCLOUDS_SSH_INIT)) || usePortForwarding;
    JcloudsPortForwarderExtension portForwarder = setup.get(PORT_FORWARDER);
    if (usePortForwarding)
        checkNotNull(portForwarder, "portForwarder, when use-port-forwarding enabled");

    final ComputeService computeService = getConfig(COMPUTE_SERVICE_REGISTRY).findComputeService(setup, true);
    CloudMachineNamer cloudMachineNamer = getCloudMachineNamer(setup);
    String groupId = elvis(setup.get(GROUP_ID), cloudMachineNamer.generateNewGroupId());
    NodeMetadata node = null;
    JcloudsSshMachineLocation sshMachineLocation = null;

    try {
        LOG.info("Creating VM " + setup.getDescription() + " in " + this);

        Semaphore machineCreationSemaphore = getMachineCreationSemaphore();
        boolean acquired = machineCreationSemaphore.tryAcquire(0, TimeUnit.SECONDS);
        if (!acquired) {
            LOG.info("Waiting in {} for machine-creation permit ({} other queuing requests already)",
                    new Object[] { this, machineCreationSemaphore.getQueueLength() });
            Stopwatch blockStopwatch = Stopwatch.createStarted();
            machineCreationSemaphore.acquire();
            LOG.info("Acquired in {} machine-creation permit, after waiting {}", this,
                    Time.makeTimeStringRounded(blockStopwatch));
        } else {
            LOG.debug("Acquired in {} machine-creation permit immediately", this);
        }

        Stopwatch provisioningStopwatch = Stopwatch.createStarted();
        Duration templateTimestamp, provisionTimestamp, usableTimestamp, customizedTimestamp;

        LoginCredentials userCredentials = null;
        Set<? extends NodeMetadata> nodes;
        Template template;
        try {
            // Setup the template
            template = buildTemplate(computeService, setup);
            if (waitForSshable && !skipJcloudsSshing) {
                userCredentials = initTemplateForCreateUser(template, setup);
            }

            //FIXME initialCredentials = initUserTemplateOptions(template, setup);
            for (JcloudsLocationCustomizer customizer : getCustomizers(setup)) {
                customizer.customize(this, computeService, template);
                customizer.customize(this, computeService, template.getOptions());
            }
            LOG.debug("jclouds using template {} / options {} to provision machine in {}",
                    new Object[] { template, template.getOptions(), setup.getDescription() });

            if (!setup.getUnusedConfig().isEmpty())
                LOG.debug("NOTE: unused flags passed to obtain VM in " + setup.getDescription() + ": "
                        + setup.getUnusedConfig());

            templateTimestamp = Duration.of(provisioningStopwatch);
            template.getOptions().getUserMetadata().put("Name",
                    cloudMachineNamer.generateNewMachineUniqueNameFromGroupId(groupId));

            nodes = computeService.createNodesInGroup(groupId, 1, template);
            provisionTimestamp = Duration.of(provisioningStopwatch);
        } finally {
            machineCreationSemaphore.release();
        }

        node = Iterables.getOnlyElement(nodes, null);
        LOG.debug("jclouds created {} for {}", node, setup.getDescription());
        if (node == null)
            throw new IllegalStateException(
                    "No nodes returned by jclouds create-nodes in " + setup.getDescription());

        // Setup port-forwarding, if required
        Optional<HostAndPort> sshHostAndPortOverride;
        if (usePortForwarding) {
            sshHostAndPortOverride = Optional.of(portForwarder.openPortForwarding(node, node.getLoginPort(),
                    Optional.<Integer>absent(), Protocol.TCP, Cidr.UNIVERSAL));
        } else {
            sshHostAndPortOverride = Optional.absent();
        }

        if (waitForSshable && skipJcloudsSshing) {
            // once that host:port is definitely reachable, we can create the user
            waitForReachable(computeService, node, sshHostAndPortOverride, node.getCredentials(), setup);
            userCredentials = createUser(computeService, node, sshHostAndPortOverride, setup);
        }

        // Figure out which login-credentials to use
        LoginCredentials customCredentials = setup.get(CUSTOM_CREDENTIALS);
        if (customCredentials != null) {
            userCredentials = customCredentials;
            //set userName and other data, from these credentials
            Object oldUsername = setup.put(USER, customCredentials.getUser());
            LOG.debug("node {} username {} / {} (customCredentials)",
                    new Object[] { node, customCredentials.getUser(), oldUsername });
            if (customCredentials.getOptionalPassword().isPresent())
                setup.put(PASSWORD, customCredentials.getOptionalPassword().get());
            if (customCredentials.getOptionalPrivateKey().isPresent())
                setup.put(PRIVATE_KEY_DATA, customCredentials.getOptionalPrivateKey().get());
        }
        if (userCredentials == null) {
            userCredentials = extractVmCredentials(setup, node);
        }
        if (userCredentials != null) {
            node = NodeMetadataBuilder.fromNodeMetadata(node).credentials(userCredentials).build();
        } else {
            // only happens if something broke above...
            userCredentials = LoginCredentials.fromCredentials(node.getCredentials());
        }
        // store the credentials, in case they have changed
        setup.putIfNotNull(JcloudsLocationConfig.PASSWORD, userCredentials.getOptionalPassword().orNull());
        setup.putIfNotNull(JcloudsLocationConfig.PRIVATE_KEY_DATA,
                userCredentials.getOptionalPrivateKey().orNull());

        // Wait for the VM to be reachable over SSH
        if (waitForSshable) {
            waitForReachable(computeService, node, sshHostAndPortOverride, userCredentials, setup);
        } else {
            LOG.debug("Skipping ssh check for {} ({}) due to config waitForSshable=false", node,
                    setup.getDescription());
        }
        usableTimestamp = Duration.of(provisioningStopwatch);

        // Create a JcloudsSshMachineLocation, and register it
        sshMachineLocation = registerJcloudsSshMachineLocation(computeService, node, userCredentials,
                sshHostAndPortOverride, setup);
        if (template != null && sshMachineLocation.getTemplate() == null) {
            sshMachineLocation.template = template;
        }

        if (usePortForwarding && sshHostAndPortOverride.isPresent()) {
            // Now that we have the sshMachineLocation, we can associate the port-forwarding address with it.
            PortForwardManager portForwardManager = setup.get(PORT_FORWARDING_MANAGER);
            if (portForwardManager != null) {
                portForwardManager.associate(node.getId(), sshHostAndPortOverride.get(), sshMachineLocation,
                        node.getLoginPort());
            } else {
                LOG.warn("No port-forward manager for {} so could not associate {} -> {} for {}",
                        new Object[] { this, node.getLoginPort(), sshHostAndPortOverride, sshMachineLocation });
            }
        }

        if ("docker".equals(this.getProvider())) {
            Map<Integer, Integer> portMappings = JcloudsUtil.dockerPortMappingsFor(this, node.getId());
            PortForwardManager portForwardManager = setup.get(PORT_FORWARDING_MANAGER);
            if (portForwardManager != null) {
                for (Integer containerPort : portMappings.keySet()) {
                    Integer hostPort = portMappings.get(containerPort);
                    String dockerHost = sshMachineLocation.getSshHostAndPort().getHostText();
                    portForwardManager.associate(node.getId(), HostAndPort.fromParts(dockerHost, hostPort),
                            sshMachineLocation, containerPort);
                }
            } else {
                LOG.warn("No port-forward manager for {} so could not associate docker port-mappings for {}",
                        this, sshMachineLocation);
            }
        }

        List<String> customisationForLogging = new ArrayList<String>();
        // Apply same securityGroups rules to iptables, if iptables is running on the node
        if (waitForSshable) {

            String setupScript = setup.get(JcloudsLocationConfig.CUSTOM_MACHINE_SETUP_SCRIPT_URL);
            if (Strings.isNonBlank(setupScript)) {
                customisationForLogging.add("custom setup script " + setupScript);

                String setupVarsString = setup.get(JcloudsLocationConfig.CUSTOM_MACHINE_SETUP_SCRIPT_VARS);
                Map<String, String> substitutions = (setupVarsString != null)
                        ? Splitter.on(",").withKeyValueSeparator(":").split(setupVarsString)
                        : ImmutableMap.<String, String>of();
                String scriptContent = ResourceUtils.create(this).getResourceAsString(setupScript);
                String script = TemplateProcessor.processTemplateContents(scriptContent, getManagementContext(),
                        substitutions);
                sshMachineLocation.execCommands("Customizing node " + this, ImmutableList.of(script));
            }

            if (setup.get(JcloudsLocationConfig.MAP_DEV_RANDOM_TO_DEV_URANDOM)) {
                customisationForLogging.add("point /dev/random to urandom");

                sshMachineLocation.execCommands("using urandom instead of random", Arrays
                        .asList("sudo mv /dev/random /dev/random-real", "sudo ln -s /dev/urandom /dev/random"));
            }

            if (setup.get(GENERATE_HOSTNAME)) {
                customisationForLogging.add("configure hostname");

                sshMachineLocation.execCommands("Generate hostname " + node.getName(),
                        Arrays.asList("sudo hostname " + node.getName(),
                                "sudo sed -i \"s/HOSTNAME=.*/HOSTNAME=" + node.getName()
                                        + "/g\" /etc/sysconfig/network",
                                "sudo bash -c \"echo 127.0.0.1   `hostname` >> /etc/hosts\""));
            }

            if (setup.get(OPEN_IPTABLES)) {
                @SuppressWarnings("unchecked")
                Iterable<Integer> inboundPorts = (Iterable<Integer>) setup.get(INBOUND_PORTS);

                if (inboundPorts == null || Iterables.isEmpty(inboundPorts)) {
                    LOG.info("No ports to open in iptables (no inbound ports) for {} at {}", sshMachineLocation,
                            this);
                } else {
                    customisationForLogging.add("open iptables");

                    List<String> iptablesRules = createIptablesRulesForNetworkInterface(inboundPorts);
                    iptablesRules.add(IptablesCommands.saveIptablesRules());
                    List<String> batch = Lists.newArrayList();
                    // Some entities, such as Riak (erlang based) have a huge range of ports, which leads to a script that
                    // is too large to run (fails with a broken pipe). Batch the rules into batches of 50
                    for (String rule : iptablesRules) {
                        batch.add(rule);
                        if (batch.size() == 50) {
                            sshMachineLocation.execCommands("Inserting iptables rules, 50 command batch",
                                    batch);
                            batch.clear();
                        }
                    }
                    if (batch.size() > 0) {
                        sshMachineLocation.execCommands("Inserting iptables rules", batch);
                    }
                    sshMachineLocation.execCommands("List iptables rules",
                            ImmutableList.of(IptablesCommands.listIptablesRule()));
                }
            }

            if (setup.get(STOP_IPTABLES)) {
                customisationForLogging.add("stop iptables");

                List<String> cmds = ImmutableList.of(IptablesCommands.iptablesServiceStop(),
                        IptablesCommands.iptablesServiceStatus());
                sshMachineLocation.execCommands("Stopping iptables", cmds);
            }

            List<String> extraKeyUrlsToAuth = setup.get(EXTRA_PUBLIC_KEY_URLS_TO_AUTH);
            if (extraKeyUrlsToAuth != null && !extraKeyUrlsToAuth.isEmpty()) {
                List<String> extraKeyDataToAuth = MutableList.of();
                for (String keyUrl : extraKeyUrlsToAuth) {
                    extraKeyDataToAuth.add(ResourceUtils.create().getResourceAsString(keyUrl));
                }
                sshMachineLocation.execCommands("Authorizing ssh keys",
                        ImmutableList.of(new AuthorizeRSAPublicKeys(extraKeyDataToAuth)
                                .render(org.jclouds.scriptbuilder.domain.OsFamily.UNIX)));
            }

        } else {
            // Otherwise we have deliberately not waited to be ssh'able, so don't try now to 
            // ssh to exec these commands!
        }

        // Apply any optional app-specific customization.
        for (JcloudsLocationCustomizer customizer : getCustomizers(setup)) {
            customizer.customize(this, computeService, sshMachineLocation);
        }

        customizedTimestamp = Duration.of(provisioningStopwatch);

        LOG.info("Finished VM " + setup.getDescription() + " creation:" + " " + sshMachineLocation.getUser()
                + "@" + sshMachineLocation.getAddress() + ":" + sshMachineLocation.getPort()
                + (Boolean.TRUE.equals(setup.get(LOG_CREDENTIALS))
                        ? "password=" + userCredentials.getOptionalPassword().or("<absent>") + " && key="
                                + userCredentials.getOptionalPrivateKey().or("<absent>")
                        : "")
                + " ready after " + Duration.of(provisioningStopwatch).toStringRounded() + " (" + template
                + " template built in " + Duration.of(templateTimestamp).toStringRounded() + ";" + " " + node
                + " provisioned in "
                + Duration.of(provisionTimestamp).subtract(templateTimestamp).toStringRounded() + ";" + " "
                + sshMachineLocation + " ssh usable in "
                + Duration.of(usableTimestamp).subtract(provisionTimestamp).toStringRounded() + ";"
                + " and os customized in "
                + Duration.of(customizedTimestamp).subtract(usableTimestamp).toStringRounded() + " - "
                + Joiner.on(", ").join(customisationForLogging) + ")");

        return sshMachineLocation;
    } catch (Exception e) {
        if (e instanceof RunNodesException && ((RunNodesException) e).getNodeErrors().size() > 0) {
            node = Iterables.get(((RunNodesException) e).getNodeErrors().keySet(), 0);
        }
        // sometimes AWS nodes come up busted (eg ssh not allowed); just throw it back (and maybe try for another one)
        boolean destroyNode = (node != null) && Boolean.TRUE.equals(setup.get(DESTROY_ON_FAILURE));

        LOG.error("Failed to start VM for {}{}: {}", new Object[] { setup.getDescription(),
                (destroyNode ? " (destroying " + node + ")" : ""), e.getMessage() });
        LOG.debug(Throwables.getStackTraceAsString(e));

        if (destroyNode) {
            if (sshMachineLocation != null) {
                releaseSafely(sshMachineLocation);
            } else {
                releaseNodeSafely(node);
            }
        }

        throw Exceptions.propagate(e);
    }
}

From source file:google.registry.config.RegistryConfig.java

/**
 * Returns the address of the Nomulus app HTTP server.
 *
 * <p>This is used by the {@code nomulus} tool to connect to the App Engine remote API.
 *///  ww w .  java  2s  .co  m
public static HostAndPort getServer() {
    switch (RegistryEnvironment.get()) {
    case LOCAL:
        return HostAndPort.fromParts("localhost", 8080);
    case UNITTEST:
        throw new UnsupportedOperationException("Unit tests can't spin up a full server");
    default:
        return HostAndPort.fromParts(String.format("tools-dot-%s.appspot.com", getProjectId()), 443);
    }
}

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

protected MachineLocation obtainOnce(ConfigBag setup) throws NoMachinesAvailableException {
    AccessController.Response access = getManagementContext().getAccessController().canProvisionLocation(this);
    if (!access.isAllowed()) {
        throw new IllegalStateException(
                "Access controller forbids provisioning in " + this + ": " + access.getMsg());
    }/* ww w.j  a v a 2 s . com*/

    setCreationString(setup);
    boolean waitForSshable = !"false".equalsIgnoreCase(setup.get(WAIT_FOR_SSHABLE));
    boolean waitForWinRmable = !"false".equalsIgnoreCase(setup.get(WAIT_FOR_WINRM_AVAILABLE));
    boolean usePortForwarding = setup.get(USE_PORT_FORWARDING);
    boolean skipJcloudsSshing = Boolean.FALSE.equals(setup.get(USE_JCLOUDS_SSH_INIT)) || usePortForwarding;
    JcloudsPortForwarderExtension portForwarder = setup.get(PORT_FORWARDER);
    if (usePortForwarding)
        checkNotNull(portForwarder, "portForwarder, when use-port-forwarding enabled");

    final ComputeService computeService = getConfig(COMPUTE_SERVICE_REGISTRY).findComputeService(setup, true);
    CloudMachineNamer cloudMachineNamer = getCloudMachineNamer(setup);
    String groupId = elvis(setup.get(GROUP_ID), cloudMachineNamer.generateNewGroupId(setup));
    NodeMetadata node = null;
    JcloudsMachineLocation machineLocation = null;
    Duration semaphoreTimestamp = null;
    Duration templateTimestamp = null;
    Duration provisionTimestamp = null;
    Duration usableTimestamp = null;
    Duration customizedTimestamp = null;
    Stopwatch provisioningStopwatch = Stopwatch.createStarted();

    try {
        LOG.info("Creating VM " + setup.getDescription() + " in " + this);

        Semaphore machineCreationSemaphore = getMachineCreationSemaphore();
        boolean acquired = machineCreationSemaphore.tryAcquire(0, TimeUnit.SECONDS);
        if (!acquired) {
            LOG.info("Waiting in {} for machine-creation permit ({} other queuing requests already)",
                    new Object[] { this, machineCreationSemaphore.getQueueLength() });
            Stopwatch blockStopwatch = Stopwatch.createStarted();
            machineCreationSemaphore.acquire();
            LOG.info("Acquired in {} machine-creation permit, after waiting {}", this,
                    Time.makeTimeStringRounded(blockStopwatch));
        } else {
            LOG.debug("Acquired in {} machine-creation permit immediately", this);
        }
        semaphoreTimestamp = Duration.of(provisioningStopwatch);

        LoginCredentials userCredentials = null;
        Set<? extends NodeMetadata> nodes;
        Template template;
        try {
            // Setup the template
            template = buildTemplate(computeService, setup);
            boolean expectWindows = isWindows(template, setup);
            if (!skipJcloudsSshing) {
                if (expectWindows) {
                    // TODO Was this too early to look at template.getImage? e.g. customizeTemplate could subsequently modify it.
                    LOG.warn("Ignoring invalid configuration for Windows provisioning of " + template.getImage()
                            + ": " + USE_JCLOUDS_SSH_INIT.getName() + " should be false");
                    skipJcloudsSshing = true;
                } else if (waitForSshable) {
                    userCredentials = initTemplateForCreateUser(template, setup);
                }
            }

            templateTimestamp = Duration.of(provisioningStopwatch);
            // "Name" metadata seems to set the display name; at least in AWS
            // TODO it would be nice if this salt comes from the location's ID (but we don't know that yet as the ssh machine location isn't created yet)
            // TODO in softlayer we want to control the suffix of the hostname which is 3 random hex digits
            template.getOptions().getUserMetadata().put("Name",
                    cloudMachineNamer.generateNewMachineUniqueNameFromGroupId(setup, groupId));

            if (setup.get(JcloudsLocationConfig.INCLUDE_BROOKLYN_USER_METADATA)) {
                template.getOptions().getUserMetadata().put("brooklyn-user", System.getProperty("user.name"));

                Object context = setup.get(CALLER_CONTEXT);
                if (context instanceof Entity) {
                    Entity entity = (Entity) context;
                    template.getOptions().getUserMetadata().put("brooklyn-app-id", entity.getApplicationId());
                    template.getOptions().getUserMetadata().put("brooklyn-app-name",
                            entity.getApplication().getDisplayName());
                    template.getOptions().getUserMetadata().put("brooklyn-entity-id", entity.getId());
                    template.getOptions().getUserMetadata().put("brooklyn-entity-name",
                            entity.getDisplayName());
                    template.getOptions().getUserMetadata().put("brooklyn-server-creation-date",
                            Time.makeDateSimpleStampString());
                }
            }

            customizeTemplate(setup, computeService, template);

            LOG.debug("jclouds using template {} / options {} to provision machine in {}",
                    new Object[] { template, template.getOptions(), setup.getDescription() });

            if (!setup.getUnusedConfig().isEmpty())
                if (LOG.isDebugEnabled())
                    LOG.debug("NOTE: unused flags passed to obtain VM in " + setup.getDescription() + ": "
                            + Sanitizer.sanitize(setup.getUnusedConfig()));

            nodes = computeService.createNodesInGroup(groupId, 1, template);
            provisionTimestamp = Duration.of(provisioningStopwatch);
        } finally {
            machineCreationSemaphore.release();
        }

        node = Iterables.getOnlyElement(nodes, null);
        LOG.debug("jclouds created {} for {}", node, setup.getDescription());
        if (node == null)
            throw new IllegalStateException(
                    "No nodes returned by jclouds create-nodes in " + setup.getDescription());

        boolean windows = isWindows(node, setup);
        if (windows) {
            int newLoginPort = node.getLoginPort() == 22 ? 5985 : node.getLoginPort();
            String newLoginUser = "root".equals(node.getCredentials().getUser()) ? "Administrator"
                    : node.getCredentials().getUser();
            LOG.debug(
                    "jclouds created Windows VM {}; transforming connection details: loginPort from {} to {}; loginUser from {} to {}",
                    new Object[] { node, node.getLoginPort(), newLoginPort, node.getCredentials().getUser(),
                            newLoginUser });

            node = NodeMetadataBuilder.fromNodeMetadata(node).loginPort(newLoginPort)
                    .credentials(LoginCredentials.builder(node.getCredentials()).user(newLoginUser).build())
                    .build();
        }
        // FIXME How do we influence the node.getLoginPort, so it is set correctly for Windows?
        // Setup port-forwarding, if required
        Optional<HostAndPort> sshHostAndPortOverride;
        if (usePortForwarding) {
            sshHostAndPortOverride = Optional.of(portForwarder.openPortForwarding(node, node.getLoginPort(),
                    Optional.<Integer>absent(), Protocol.TCP, Cidr.UNIVERSAL));
        } else {
            sshHostAndPortOverride = Optional.absent();
        }

        LoginCredentials initialCredentials = node.getCredentials();
        if (skipJcloudsSshing) {
            boolean waitForConnectable = (windows) ? waitForWinRmable : waitForSshable;
            if (waitForConnectable) {
                if (windows) {
                    // TODO Does jclouds support any windows user setup?
                    initialCredentials = waitForWinRmAvailable(computeService, node, sshHostAndPortOverride,
                            setup);
                } else {
                    initialCredentials = waitForSshable(computeService, node, sshHostAndPortOverride, setup);
                }
                userCredentials = createUser(computeService, node, sshHostAndPortOverride, initialCredentials,
                        setup);
            }
        }

        // Figure out which login-credentials to use
        LoginCredentials customCredentials = setup.get(CUSTOM_CREDENTIALS);
        if (customCredentials != null) {
            userCredentials = customCredentials;
            //set userName and other data, from these credentials
            Object oldUsername = setup.put(USER, customCredentials.getUser());
            LOG.debug("node {} username {} / {} (customCredentials)",
                    new Object[] { node, customCredentials.getUser(), oldUsername });
            if (customCredentials.getOptionalPassword().isPresent())
                setup.put(PASSWORD, customCredentials.getOptionalPassword().get());
            if (customCredentials.getOptionalPrivateKey().isPresent())
                setup.put(PRIVATE_KEY_DATA, customCredentials.getOptionalPrivateKey().get());
        }
        if (userCredentials == null || (!userCredentials.getOptionalPassword().isPresent()
                && !userCredentials.getOptionalPrivateKey().isPresent())) {
            // We either don't have any userCredentials, or it is missing both a password/key.
            // TODO See waitForSshable, which now handles if the node.getLoginCredentials has both a password+key
            userCredentials = extractVmCredentials(setup, node, initialCredentials);
        }
        if (userCredentials == null) {
            // TODO See waitForSshable, which now handles if the node.getLoginCredentials has both a password+key
            userCredentials = extractVmCredentials(setup, node, initialCredentials);
        }
        if (userCredentials != null) {
            node = NodeMetadataBuilder.fromNodeMetadata(node).credentials(userCredentials).build();
        } else {
            // only happens if something broke above...
            userCredentials = LoginCredentials.fromCredentials(node.getCredentials());
        }
        // store the credentials, in case they have changed
        setup.putIfNotNull(JcloudsLocationConfig.PASSWORD, userCredentials.getOptionalPassword().orNull());
        setup.putIfNotNull(JcloudsLocationConfig.PRIVATE_KEY_DATA,
                userCredentials.getOptionalPrivateKey().orNull());

        // Wait for the VM to be reachable over SSH
        if (waitForSshable && !windows) {
            waitForSshable(computeService, node, sshHostAndPortOverride, ImmutableList.of(userCredentials),
                    setup);
        } else {
            LOG.debug("Skipping ssh check for {} ({}) due to config waitForSshable=false", node,
                    setup.getDescription());
        }
        usableTimestamp = Duration.of(provisioningStopwatch);

        //            JcloudsSshMachineLocation jcloudsSshMachineLocation = null;
        //            WinRmMachineLocation winRmMachineLocation = null;
        // Create a JcloudsSshMachineLocation, and register it
        if (windows) {
            machineLocation = registerWinRmMachineLocation(computeService, node, userCredentials,
                    sshHostAndPortOverride, setup);
        } else {
            machineLocation = registerJcloudsSshMachineLocation(computeService, node,
                    Optional.fromNullable(template), userCredentials, sshHostAndPortOverride, setup);
        }

        if (usePortForwarding && sshHostAndPortOverride.isPresent()) {
            // Now that we have the sshMachineLocation, we can associate the port-forwarding address with it.
            PortForwardManager portForwardManager = setup.get(PORT_FORWARDING_MANAGER);
            if (portForwardManager != null) {
                portForwardManager.associate(node.getId(), sshHostAndPortOverride.get(), machineLocation,
                        node.getLoginPort());
            } else {
                LOG.warn("No port-forward manager for {} so could not associate {} -> {} for {}",
                        new Object[] { this, node.getLoginPort(), sshHostAndPortOverride, machineLocation });
            }
        }

        if ("docker".equals(this.getProvider())) {
            if (windows) {
                throw new UnsupportedOperationException("Docker not supported on Windows");
            }
            Map<Integer, Integer> portMappings = JcloudsUtil.dockerPortMappingsFor(this, node.getId());
            PortForwardManager portForwardManager = setup.get(PORT_FORWARDING_MANAGER);
            if (portForwardManager != null) {
                for (Integer containerPort : portMappings.keySet()) {
                    Integer hostPort = portMappings.get(containerPort);
                    String dockerHost = ((JcloudsSshMachineLocation) machineLocation).getSshHostAndPort()
                            .getHostText();
                    portForwardManager.associate(node.getId(), HostAndPort.fromParts(dockerHost, hostPort),
                            machineLocation, containerPort);
                }
            } else {
                LOG.warn("No port-forward manager for {} so could not associate docker port-mappings for {}",
                        this, machineLocation);
            }
        }

        List<String> customisationForLogging = new ArrayList<String>();
        // Apply same securityGroups rules to iptables, if iptables is running on the node
        if (waitForSshable) {

            String setupScript = setup.get(JcloudsLocationConfig.CUSTOM_MACHINE_SETUP_SCRIPT_URL);
            List<String> setupScripts = setup.get(JcloudsLocationConfig.CUSTOM_MACHINE_SETUP_SCRIPT_URL_LIST);
            Collection<String> allScripts = new MutableList<String>().appendIfNotNull(setupScript)
                    .appendAll(setupScripts);
            for (String setupScriptItem : allScripts) {
                if (Strings.isNonBlank(setupScriptItem)) {
                    customisationForLogging.add("custom setup script " + setupScriptItem);

                    String setupVarsString = setup.get(JcloudsLocationConfig.CUSTOM_MACHINE_SETUP_SCRIPT_VARS);
                    Map<String, String> substitutions = (setupVarsString != null)
                            ? Splitter.on(",").withKeyValueSeparator(":").split(setupVarsString)
                            : ImmutableMap.<String, String>of();
                    String scriptContent = ResourceUtils.create(this).getResourceAsString(setupScriptItem);
                    String script = TemplateProcessor.processTemplateContents(scriptContent,
                            getManagementContext(), substitutions);
                    if (windows) {
                        ((WinRmMachineLocation) machineLocation)
                                .executeCommand(ImmutableList.copyOf((script.replace("\r", "").split("\n"))));
                    } else {
                        ((SshMachineLocation) machineLocation).execCommands("Customizing node " + this,
                                ImmutableList.of(script));
                    }
                }
            }

            if (setup.get(JcloudsLocationConfig.MAP_DEV_RANDOM_TO_DEV_URANDOM)) {
                if (windows) {
                    LOG.warn("Ignoring flag MAP_DEV_RANDOM_TO_DEV_URANDOM on Windows location {}",
                            machineLocation);
                } else {
                    customisationForLogging.add("point /dev/random to urandom");

                    ((SshMachineLocation) machineLocation).execCommands("using urandom instead of random",
                            Arrays.asList("sudo mv /dev/random /dev/random-real",
                                    "sudo ln -s /dev/urandom /dev/random"));
                }
            }

            if (setup.get(GENERATE_HOSTNAME)) {
                if (windows) {
                    // TODO: Generate Windows Hostname
                    LOG.warn("Ignoring flag GENERATE_HOSTNAME on Windows location {}", machineLocation);
                } else {
                    customisationForLogging.add("configure hostname");

                    ((SshMachineLocation) machineLocation).execCommands("Generate hostname " + node.getName(),
                            Arrays.asList("sudo hostname " + node.getName(),
                                    "sudo sed -i \"s/HOSTNAME=.*/HOSTNAME=" + node.getName()
                                            + "/g\" /etc/sysconfig/network",
                                    "sudo bash -c \"echo 127.0.0.1   `hostname` >> /etc/hosts\""));
                }
            }

            if (setup.get(OPEN_IPTABLES)) {
                if (windows) {
                    LOG.warn("Ignoring DEPRECATED flag OPEN_IPTABLES on Windows location {}", machineLocation);
                } else {
                    LOG.warn(
                            "Using DEPRECATED flag OPEN_IPTABLES (will not be supported in future versions) for {} at {}",
                            machineLocation, this);

                    @SuppressWarnings("unchecked")
                    Iterable<Integer> inboundPorts = (Iterable<Integer>) setup.get(INBOUND_PORTS);

                    if (inboundPorts == null || Iterables.isEmpty(inboundPorts)) {
                        LOG.info("No ports to open in iptables (no inbound ports) for {} at {}",
                                machineLocation, this);
                    } else {
                        customisationForLogging.add("open iptables");

                        List<String> iptablesRules = Lists.newArrayList();

                        if (isLocationFirewalldEnabled((SshMachineLocation) machineLocation)) {
                            for (Integer port : inboundPorts) {
                                iptablesRules.add(IptablesCommands.addFirewalldRule(Chain.INPUT, Protocol.TCP,
                                        port, Policy.ACCEPT));
                            }
                        } else {
                            iptablesRules = createIptablesRulesForNetworkInterface(inboundPorts);
                            iptablesRules.add(IptablesCommands.saveIptablesRules());
                        }
                        List<String> batch = Lists.newArrayList();
                        // Some entities, such as Riak (erlang based) have a huge range of ports, which leads to a script that
                        // is too large to run (fails with a broken pipe). Batch the rules into batches of 50
                        for (String rule : iptablesRules) {
                            batch.add(rule);
                            if (batch.size() == 50) {
                                ((SshMachineLocation) machineLocation)
                                        .execCommands("Inserting iptables rules, 50 command batch", batch);
                                batch.clear();
                            }
                        }
                        if (batch.size() > 0) {
                            ((SshMachineLocation) machineLocation).execCommands("Inserting iptables rules",
                                    batch);
                        }
                        ((SshMachineLocation) machineLocation).execCommands("List iptables rules",
                                ImmutableList.of(IptablesCommands.listIptablesRule()));
                    }
                }
            }

            if (setup.get(STOP_IPTABLES)) {
                if (windows) {
                    LOG.warn("Ignoring DEPRECATED flag OPEN_IPTABLES on Windows location {}", machineLocation);
                } else {
                    LOG.warn(
                            "Using DEPRECATED flag STOP_IPTABLES (will not be supported in future versions) for {} at {}",
                            machineLocation, this);

                    customisationForLogging.add("stop iptables");

                    List<String> cmds = ImmutableList.<String>of();
                    if (isLocationFirewalldEnabled((SshMachineLocation) machineLocation)) {
                        cmds = ImmutableList.of(IptablesCommands.firewalldServiceStop(),
                                IptablesCommands.firewalldServiceStatus());
                    } else {
                        cmds = ImmutableList.of(IptablesCommands.iptablesServiceStop(),
                                IptablesCommands.iptablesServiceStatus());
                    }
                    ((SshMachineLocation) machineLocation).execCommands("Stopping iptables", cmds);
                }
            }

            List<String> extraKeyUrlsToAuth = setup.get(EXTRA_PUBLIC_KEY_URLS_TO_AUTH);
            if (extraKeyUrlsToAuth != null && !extraKeyUrlsToAuth.isEmpty()) {
                if (windows) {
                    LOG.warn("Ignoring flag EXTRA_PUBLIC_KEY_URLS_TO_AUTH on Windows location",
                            machineLocation);
                } else {
                    List<String> extraKeyDataToAuth = MutableList.of();
                    for (String keyUrl : extraKeyUrlsToAuth) {
                        extraKeyDataToAuth.add(ResourceUtils.create().getResourceAsString(keyUrl));
                    }
                    ((SshMachineLocation) machineLocation).execCommands("Authorizing ssh keys",
                            ImmutableList.of(new AuthorizeRSAPublicKeys(extraKeyDataToAuth)
                                    .render(org.jclouds.scriptbuilder.domain.OsFamily.UNIX)));
                }
            }

        } else {
            // Otherwise we have deliberately not waited to be ssh'able, so don't try now to
            // ssh to exec these commands!
        }

        // Apply any optional app-specific customization.
        for (JcloudsLocationCustomizer customizer : getCustomizers(setup)) {
            LOG.debug("Customizing machine {}, using customizer {}", machineLocation, customizer);
            customizer.customize(this, computeService, machineLocation);
        }
        for (MachineLocationCustomizer customizer : getMachineCustomizers(setup)) {
            LOG.debug("Customizing machine {}, using customizer {}", machineLocation, customizer);
            customizer.customize(machineLocation);
        }

        customizedTimestamp = Duration.of(provisioningStopwatch);

        try {
            String logMessage = "Finished VM " + setup.getDescription() + " creation:" + " "
                    + machineLocation.getUser() + "@" + machineLocation.getAddress() + ":"
                    + machineLocation.getPort()
                    + (Boolean.TRUE.equals(setup.get(LOG_CREDENTIALS))
                            ? "password=" + userCredentials.getOptionalPassword().or("<absent>") + " && key="
                                    + userCredentials.getOptionalPrivateKey().or("<absent>")
                            : "")
                    + " ready after " + Duration.of(provisioningStopwatch).toStringRounded() + " ("
                    + "semaphore obtained in " + Duration.of(semaphoreTimestamp).toStringRounded() + ";"
                    + template + " template built in "
                    + Duration.of(templateTimestamp).subtract(semaphoreTimestamp).toStringRounded() + ";" + " "
                    + node + " provisioned in "
                    + Duration.of(provisionTimestamp).subtract(templateTimestamp).toStringRounded() + ";" + " "
                    + machineLocation + " connection usable in "
                    + Duration.of(usableTimestamp).subtract(provisionTimestamp).toStringRounded() + ";"
                    + " and os customized in "
                    + Duration.of(customizedTimestamp).subtract(usableTimestamp).toStringRounded() + " - "
                    + Joiner.on(", ").join(customisationForLogging) + ")";
            LOG.info(logMessage);
        } catch (Exception e) {
            // TODO Remove try-catch! @Nakomis: why did you add it? What exception happened during logging?
            Exceptions.propagateIfFatal(e);
            LOG.warn("Problem generating log message summarising completion of jclouds machine provisioning "
                    + machineLocation + " by " + this, e);
        }

        return machineLocation;

    } catch (Exception e) {
        if (e instanceof RunNodesException && ((RunNodesException) e).getNodeErrors().size() > 0) {
            node = Iterables.get(((RunNodesException) e).getNodeErrors().keySet(), 0);
        }
        // sometimes AWS nodes come up busted (eg ssh not allowed); just throw it back (and maybe try for another one)
        boolean destroyNode = (node != null) && Boolean.TRUE.equals(setup.get(DESTROY_ON_FAILURE));

        if (e.toString().contains("VPCResourceNotSpecified")) {
            LOG.error(
                    "Detected that your EC2 account is a legacy 'classic' account, but the recommended instance type requires VPC. "
                            + "You can specify the 'eu-central-1' region to avoid this problem, or you can specify a classic-compatible instance type, "
                            + "or you can specify a subnet to use with 'networkName' "
                            + "(taking care that the subnet auto-assigns public IP's and allows ingress on all ports, "
                            + "as Brooklyn does not currently configure security groups for non-default VPC's; "
                            + "or setting up Brooklyn to be in the subnet or have a jump host or other subnet access configuration). "
                            + "For more information on VPC vs classic see http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-vpc.html.");
        }

        LOG.error(
                "Failed to start VM for " + setup.getDescription() + (destroyNode ? " (destroying)" : "")
                        + (node != null ? "; node " + node : "") + " after "
                        + Duration.of(provisioningStopwatch).toStringRounded()
                        + (semaphoreTimestamp != null
                                ? " (" + "semaphore obtained in "
                                        + Duration.of(semaphoreTimestamp).toStringRounded() + ";"
                                        + (templateTimestamp != null && semaphoreTimestamp != null
                                                ? " template built in " + Duration.of(templateTimestamp)
                                                        .subtract(semaphoreTimestamp).toStringRounded() + ";"
                                                : "")
                                        + (provisionTimestamp != null && templateTimestamp != null
                                                ? " node provisioned in " + Duration.of(provisionTimestamp)
                                                        .subtract(templateTimestamp).toStringRounded() + ";"
                                                : "")
                                        + (usableTimestamp != null && provisioningStopwatch != null
                                                ? " connection usable in "
                                                        + Duration.of(usableTimestamp)
                                                                .subtract(provisionTimestamp).toStringRounded()
                                                        + ";"
                                                : "")
                                        + (customizedTimestamp != null && usableTimestamp != null
                                                ? " and OS customized in " + Duration.of(customizedTimestamp)
                                                        .subtract(usableTimestamp).toStringRounded()
                                                : "")
                                        + ")"
                                : "")
                        + ": " + e.getMessage());
        LOG.debug(Throwables.getStackTraceAsString(e));

        if (destroyNode) {
            Stopwatch destroyingStopwatch = Stopwatch.createStarted();
            if (machineLocation != null) {
                releaseSafely(machineLocation);
            } else {
                releaseNodeSafely(node);
            }
            LOG.info("Destroyed " + (machineLocation != null ? "machine " + machineLocation : "node " + node)
                    + " in " + Duration.of(destroyingStopwatch).toStringRounded());
        }

        throw Exceptions.propagate(e);
    }
}

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

/**
 * Create the user immediately - executing ssh commands as required.
 *///from w ww .  j  a v  a 2 s.  com
protected LoginCredentials createUser(ComputeService computeService, NodeMetadata node,
        Optional<HostAndPort> hostAndPortOverride, LoginCredentials initialCredentials, ConfigBag config) {
    Image image = (node.getImageId() != null) ? computeService.getImage(node.getImageId()) : null;
    UserCreation userCreation = createUserStatements(image, config);

    if (!userCreation.statements.isEmpty()) {
        // If unsure of OS family, default to unix for rendering statements.
        org.jclouds.scriptbuilder.domain.OsFamily scriptOsFamily;
        if (isWindows(node, config)) {
            scriptOsFamily = org.jclouds.scriptbuilder.domain.OsFamily.WINDOWS;
        } else {
            scriptOsFamily = org.jclouds.scriptbuilder.domain.OsFamily.UNIX;
        }

        boolean windows = isWindows(node, config);

        if (windows) {
            LOG.warn("Unable to execute statements on WinRM in JcloudsLocation; skipping for " + node + ": "
                    + userCreation.statements);

        } else {
            List<String> commands = Lists.newArrayList();
            for (Statement statement : userCreation.statements) {
                InitAdminAccess initAdminAccess = new InitAdminAccess(new AdminAccessConfiguration.Default());
                initAdminAccess.visit(statement);
                commands.add(statement.render(scriptOsFamily));
            }

            String initialUser = initialCredentials.getUser();
            String address = hostAndPortOverride.isPresent() ? hostAndPortOverride.get().getHostText()
                    : getFirstReachableAddress(node, config);
            int port = hostAndPortOverride.isPresent() ? hostAndPortOverride.get().getPort()
                    : node.getLoginPort();

            // TODO Retrying lots of times as workaround for vcloud-director. There the guest customizations
            // can cause the VM to reboot shortly after it was ssh'able.
            Map<String, Object> execProps = Maps.newLinkedHashMap();
            execProps.put(ShellTool.PROP_RUN_AS_ROOT.getName(), true);
            execProps.put(SshTool.PROP_SSH_TRIES.getName(), 50);
            execProps.put(SshTool.PROP_SSH_TRIES_TIMEOUT.getName(), 10 * 60 * 1000);

            if (LOG.isDebugEnabled()) {
                LOG.debug("VM {}: executing user creation/setup via {}@{}:{}; commands: {}",
                        new Object[] { config.getDescription(), initialUser, address, port, commands });
            }

            HostAndPort hostAndPort = hostAndPortOverride.isPresent() ? hostAndPortOverride.get()
                    : HostAndPort.fromParts(address, port);
            SshMachineLocation sshLoc = createTemporarySshMachineLocation(hostAndPort, initialCredentials,
                    config);
            try {
                // BROOKLYN-188: for SUSE, need to specify the path (for groupadd, useradd, etc)
                Map<String, ?> env = ImmutableMap.of("PATH", sbinPath());

                int exitcode = sshLoc.execScript(execProps, "create-user", commands, env);

                if (exitcode != 0) {
                    LOG.warn("exit code {} when creating user for {}; usage may subsequently fail", exitcode,
                            node);
                }
            } finally {
                getManagementContext().getLocationManager().unmanage(sshLoc);
                Streams.closeQuietly(sshLoc);
            }
        }
    }

    return userCreation.createdUserCredentials;
}

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.  j  a  va2s.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.accumulo.tserver.TabletServer.java

public void config(String hostname) {
    log.info("Tablet server starting on " + hostname);
    majorCompactorThread = new Daemon(new LoggingRunnable(log, new MajorCompactor(getConfiguration())));
    majorCompactorThread.setName("Split/MajC initiator");
    majorCompactorThread.start();/*  w  w  w  .  j  av a 2 s . c  om*/

    clientAddress = HostAndPort.fromParts(hostname, 0);
    try {
        AccumuloVFSClassLoader.getContextManager().setContextConfig(
                new ContextManager.DefaultContextsConfig(new Iterable<Entry<String, String>>() {
                    @Override
                    public Iterator<Entry<String, String>> iterator() {
                        return getConfiguration().iterator();
                    }
                }));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    // A task that cleans up unused classloader contexts
    Runnable contextCleaner = new Runnable() {
        @Override
        public void run() {
            Set<String> contextProperties = getServerConfigurationFactory().getConfiguration()
                    .getAllPropertiesWithPrefix(Property.VFS_CONTEXT_CLASSPATH_PROPERTY).keySet();
            Set<String> configuredContexts = new HashSet<String>();
            for (String prop : contextProperties) {
                configuredContexts.add(prop.substring(Property.VFS_CONTEXT_CLASSPATH_PROPERTY.name().length()));
            }

            try {
                AccumuloVFSClassLoader.getContextManager().removeUnusedContexts(configuredContexts);
            } catch (IOException e) {
                log.warn("{}", e.getMessage(), e);
            }
        }
    };

    AccumuloConfiguration aconf = getConfiguration();
    SimpleTimer.getInstance(aconf).schedule(contextCleaner, 60000, 60000);

    FileSystemMonitor.start(aconf, Property.TSERV_MONITOR_FS);

    Runnable gcDebugTask = new Runnable() {
        @Override
        public void run() {
            gcLogger.logGCInfo(getConfiguration());
        }
    };

    SimpleTimer.getInstance(aconf).schedule(gcDebugTask, 0, TIME_BETWEEN_GC_CHECKS);

    Runnable constraintTask = new Runnable() {

        @Override
        public void run() {
            ArrayList<Tablet> tablets;

            synchronized (onlineTablets) {
                tablets = new ArrayList<Tablet>(onlineTablets.values());
            }

            for (Tablet tablet : tablets) {
                tablet.checkConstraints();
            }
        }
    };

    SimpleTimer.getInstance(aconf).schedule(constraintTask, 0, 1000);
}

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

protected void releasePortForwarding(final JcloudsMachineLocation machine) {
    // TODO Implementation needs revisisted. It relies on deprecated PortForwardManager methods.

    boolean usePortForwarding = Boolean.TRUE.equals(machine.getConfig(USE_PORT_FORWARDING));
    final JcloudsPortForwarderExtension portForwarder = machine.getConfig(PORT_FORWARDER);
    PortForwardManager portForwardManager = machine.getConfig(PORT_FORWARDING_MANAGER);
    final String nodeId = machine.getJcloudsId();
    final Map<String, Runnable> subtasks = Maps.newLinkedHashMap();

    if (portForwarder == null) {
        LOG.debug("No port-forwarding to close (because portForwarder null) on release of " + machine);
    } else {//from  w ww .j av a2 s. co m
        final Optional<NodeMetadata> node = machine.getOptionalNode();
        // Release the port-forwarding for the login-port, which was explicitly created by JcloudsLocation
        if (usePortForwarding && node.isPresent()) {
            final HostAndPort hostAndPortOverride;
            if (machine instanceof SshMachineLocation) {
                hostAndPortOverride = ((SshMachineLocation) machine).getSshHostAndPort();
            } else if (machine instanceof WinRmMachineLocation) {
                String host = ((WinRmMachineLocation) machine).getAddress().getHostAddress();
                int port = ((WinRmMachineLocation) machine).config().get(WinRmMachineLocation.WINRM_PORT);
                hostAndPortOverride = HostAndPort.fromParts(host, port);
            } else {
                LOG.warn("Unexpected machine {} of type {}; expected SSH or WinRM", machine,
                        (machine != null ? machine.getClass() : null));
                hostAndPortOverride = null;
            }
            if (hostAndPortOverride != null) {
                final int loginPort = node.get().getLoginPort();
                subtasks.put("Close port-forward " + hostAndPortOverride + "->" + loginPort, new Runnable() {
                    public void run() {
                        LOG.debug("Closing port-forwarding at {} for machine {}: {}->{}",
                                new Object[] { this, machine, hostAndPortOverride, loginPort });
                        portForwarder.closePortForwarding(node.get(), loginPort, hostAndPortOverride,
                                Protocol.TCP);
                    }
                });
            }
        }

        // Get all the other port-forwarding mappings for this VM, and release all of those
        Set<PortMapping> mappings;
        if (portForwardManager != null) {
            mappings = Sets.newLinkedHashSet();
            mappings.addAll(portForwardManager.getLocationPublicIpIds(machine));
            if (nodeId != null) {
                mappings.addAll(portForwardManager.getPortMappingWithPublicIpId(nodeId));
            }
        } else {
            mappings = ImmutableSet.of();
        }

        for (final PortMapping mapping : mappings) {
            final HostAndPort publicEndpoint = mapping.getPublicEndpoint();
            final int targetPort = mapping.getPrivatePort();
            final Protocol protocol = Protocol.TCP;
            if (publicEndpoint != null && node.isPresent()) {
                subtasks.put("Close port-forward " + publicEndpoint + "->" + targetPort, new Runnable() {
                    public void run() {
                        LOG.debug("Closing port-forwarding at {} for machine {}: {}->{}",
                                new Object[] { this, machine, publicEndpoint, targetPort });
                        portForwarder.closePortForwarding(node.get(), targetPort, publicEndpoint, protocol);
                    }
                });
            }
        }

        if (subtasks.size() > 0) {
            final TaskBuilder<Void> builder = TaskBuilder.<Void>builder().parallel(true)
                    .displayName("close port-forwarding at " + machine);
            for (Map.Entry<String, Runnable> entry : subtasks.entrySet()) {
                builder.add(TaskBuilder.builder().displayName(entry.getKey()).body(entry.getValue()).build());
            }
            final Task<Void> task = builder.build();
            final DynamicTasks.TaskQueueingResult<Void> queueResult = DynamicTasks.queueIfPossible(task);
            if (queueResult.isQueuedOrSubmitted()) {
                final String origDetails = Tasks
                        .setBlockingDetails("waiting for closing port-forwarding of " + machine);
                try {
                    task.blockUntilEnded();
                } finally {
                    Tasks.setBlockingDetails(origDetails);
                }
            } else {
                LOG.warn("Releasing port-forwarding of " + machine + " not executing in execution-context "
                        + "(e.g. not invoked inside effector); falling back to executing sequentially");
                for (Runnable subtask : subtasks.values()) {
                    subtask.run();
                }
            }
        }
    }

    // Forget all port mappings associated with this VM
    if (portForwardManager != null) {
        portForwardManager.forgetPortMappings(machine);
        if (nodeId != null) {
            portForwardManager.forgetPortMappings(nodeId);
        }
    }
}

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

protected LoginCredentials waitForWinRmAvailable(final ComputeService computeService, final NodeMetadata node,
        Optional<HostAndPort> hostAndPortOverride, List<LoginCredentials> credentialsToTry, ConfigBag setup) {
    String waitForWinrmAvailable = setup.get(WAIT_FOR_WINRM_AVAILABLE);
    checkArgument(!"false".equalsIgnoreCase(waitForWinrmAvailable),
            "waitForWinRmAvailable called despite waitForWinRmAvailable=%s", waitForWinrmAvailable);
    Duration timeout = null;/*  w ww  .  j a v  a2  s  . c  o m*/
    try {
        timeout = Duration.parse(waitForWinrmAvailable);
    } catch (Exception e) {
        // TODO will this just be a NumberFormatException? If so, catch that specificially
        // normal if 'true'; just fall back to default
        Exceptions.propagateIfFatal(e);
    }
    if (timeout == null) {
        timeout = Duration.parse(WAIT_FOR_WINRM_AVAILABLE.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(5985) : 5985;

    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).
    // The createTemporaryWinRmMachineLocation deals with removing that.
    ConfigBag winrmProps = ConfigBag.newInstanceCopying(setup);

    final Map<WinRmMachineLocation, LoginCredentials> machinesToTry = Maps.newLinkedHashMap();
    for (LoginCredentials creds : credentialsToTry) {
        machinesToTry.put(createTemporaryWinRmMachineLocation(hostAndPort, creds, winrmProps), creds);
    }
    try {
        Callable<Boolean> checker = new Callable<Boolean>() {
            public Boolean call() {
                for (Map.Entry<WinRmMachineLocation, LoginCredentials> entry : machinesToTry.entrySet()) {
                    WinRmMachineLocation machine = entry.getKey();
                    WinRmToolResponse response = machine.executeCommand(
                            ImmutableMap.of(WinRmTool.PROP_EXEC_TRIES.getName(), 1),
                            ImmutableList.of("echo testing"));
                    boolean success = (response.getStatusCode() == 0);
                    if (success) {
                        credsSuccessful.set(entry.getValue());
                        return true;
                    }
                }
                return false;
            }
        };

        waitForReachable(checker, connectionDetails, credentialsToTry, setup, timeout);
    } finally {
        for (WinRmMachineLocation machine : machinesToTry.keySet()) {
            getManagementContext().getLocationManager().unmanage(machine);
        }
    }

    return credsSuccessful.get();
}

From source file:org.apache.accumulo.server.tabletserver.ScanRunState.java

public void config(String hostname) {
    log.info("Tablet server starting on " + hostname);
    security = AuditedSecurityOperation.getInstance();
    clientAddress = HostAndPort.fromParts(hostname, 0);
    logger = new TabletServerLogger(this,
            getSystemConfiguration().getMemoryInBytes(Property.TSERV_WALOG_MAX_SIZE));

    try {//  w ww  .  ja v  a  2s  .co  m
        AccumuloVFSClassLoader.getContextManager().setContextConfig(
                new ContextManager.DefaultContextsConfig(new Iterable<Entry<String, String>>() {
                    @Override
                    public Iterator<Entry<String, String>> iterator() {
                        return getSystemConfiguration().iterator();
                    }
                }));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    // A task that cleans up unused classloader contexts
    Runnable contextCleaner = new Runnable() {
        @Override
        public void run() {
            ArrayList<KeyExtent> extents;

            synchronized (onlineTablets) {
                extents = new ArrayList<KeyExtent>(onlineTablets.keySet());
            }

            Set<Text> tables = new HashSet<Text>();

            for (KeyExtent keyExtent : extents) {
                tables.add(keyExtent.getTableId());
            }

            HashSet<String> contexts = new HashSet<String>();

            for (Text tableid : tables) {
                String context = getTableConfiguration(new KeyExtent(tableid, null, null))
                        .get(Property.TABLE_CLASSPATH);
                if (!context.equals("")) {
                    contexts.add(context);
                }
            }

            try {
                AccumuloVFSClassLoader.getContextManager().removeUnusedContexts(contexts);
            } catch (IOException e) {
                log.warn(e.getMessage(), e);
            }
        }
    };

    SimpleTimer.getInstance().schedule(contextCleaner, 60000, 60000);

    FileSystemMonitor.start(getSystemConfiguration(), Property.TSERV_MONITOR_FS);

    Runnable gcDebugTask = new Runnable() {
        @Override
        public void run() {
            logGCInfo(getSystemConfiguration());
        }
    };

    SimpleTimer.getInstance().schedule(gcDebugTask, 0, 1000);

    Runnable constraintTask = new Runnable() {

        @Override
        public void run() {
            ArrayList<Tablet> tablets;

            synchronized (onlineTablets) {
                tablets = new ArrayList<Tablet>(onlineTablets.values());
            }

            for (Tablet tablet : tablets) {
                tablet.checkConstraints();
            }
        }
    };

    SimpleTimer.getInstance().schedule(constraintTask, 0, 1000);

    this.resourceManager = new TabletServerResourceManager(instance, fs);

    lastPingTime = System.currentTimeMillis();

    currentMaster = null;

    statsKeeper = new TabletStatsKeeper();

    // start major compactor
    majorCompactorThread = new Daemon(new LoggingRunnable(log, new MajorCompactor(getSystemConfiguration())));
    majorCompactorThread.setName("Split/MajC initiator");
    majorCompactorThread.start();
}