Search in sources :

Example 1 with ComputeMetadata

use of org.jclouds.compute.domain.ComputeMetadata in project whirr by apache.

the class ByonClusterAction method doAction.

@Override
protected void doAction(Map<InstanceTemplate, ClusterActionEvent> eventMap) throws IOException, InterruptedException {
    final Collection<Future<ExecResponse>> futures = Sets.newHashSet();
    List<NodeMetadata> nodes = Lists.newArrayList();
    List<NodeMetadata> usedNodes = Lists.newArrayList();
    int numberAllocated = 0;
    Set<Instance> allInstances = Sets.newLinkedHashSet();
    for (Entry<InstanceTemplate, ClusterActionEvent> entry : eventMap.entrySet()) {
        final ClusterSpec clusterSpec = entry.getValue().getClusterSpec();
        final StatementBuilder statementBuilder = entry.getValue().getStatementBuilder();
        if (statementBuilder.isEmpty()) {
            // skip
            continue;
        }
        final ComputeServiceContext computeServiceContext = getCompute().apply(clusterSpec);
        final ComputeService computeService = computeServiceContext.getComputeService();
        LoginCredentials credentials = LoginCredentials.builder().user(clusterSpec.getClusterUser()).privateKey(clusterSpec.getPrivateKey()).build();
        final RunScriptOptions options = overrideLoginCredentials(credentials);
        if (numberAllocated == 0) {
            for (ComputeMetadata compute : computeService.listNodes()) {
                if (!(compute instanceof NodeMetadata)) {
                    throw new IllegalArgumentException("Not an instance of NodeMetadata: " + compute);
                }
                nodes.add((NodeMetadata) compute);
            }
        }
        int num = entry.getKey().getNumberOfInstances();
        Predicate<NodeMetadata> unused = not(in(usedNodes));
        // TODO: This seems very fragile and a bug.  It is not required that someone passes a hardware id,
        // so this is likely to break badly. Even if there was, why do we assume it is splittable?!
        // this logic should be refactored or removed ASAP
        Predicate<NodeMetadata> instancePredicate = Predicates.alwaysTrue();
        if (entry.getKey().getTemplate() != null) {
            String hardwareId = entry.getKey().getTemplate().getHardwareId();
            if (hardwareId != null)
                instancePredicate = new TagsPredicate(StringUtils.split(hardwareId));
        }
        List<NodeMetadata> templateNodes = Lists.newArrayList(filter(nodes, and(unused, instancePredicate)));
        if (templateNodes.size() < num) {
            LOG.warn("Not enough nodes available for template " + StringUtils.join(entry.getKey().getRoles(), "+"));
        }
        templateNodes = templateNodes.subList(0, num);
        usedNodes.addAll(templateNodes);
        numberAllocated = usedNodes.size();
        Set<Instance> templateInstances = getInstances(credentials, entry.getKey().getRoles(), templateNodes);
        allInstances.addAll(templateInstances);
        for (final Instance instance : templateInstances) {
            futures.add(runStatementOnInstanceInCluster(statementBuilder, instance, clusterSpec, options));
        }
    }
    for (Future<ExecResponse> future : futures) {
        try {
            future.get();
        } catch (ExecutionException e) {
            throw new IOException(e.getCause());
        }
    }
    if (action.equals(ClusterActionHandler.BOOTSTRAP_ACTION)) {
        Cluster cluster = new Cluster(allInstances);
        for (ClusterActionEvent event : eventMap.values()) {
            event.setCluster(cluster);
        }
    }
}
Also used : RunScriptOptions(org.jclouds.compute.options.RunScriptOptions) Instance(org.apache.whirr.Cluster.Instance) ExecResponse(org.jclouds.compute.domain.ExecResponse) ClusterActionEvent(org.apache.whirr.service.ClusterActionEvent) ComputeMetadata(org.jclouds.compute.domain.ComputeMetadata) LoginCredentials(org.jclouds.domain.LoginCredentials) Builder.overrideLoginCredentials(org.jclouds.compute.options.RunScriptOptions.Builder.overrideLoginCredentials) ExecutionException(java.util.concurrent.ExecutionException) Cluster(org.apache.whirr.Cluster) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) ClusterSpec(org.apache.whirr.ClusterSpec) IOException(java.io.IOException) ComputeService(org.jclouds.compute.ComputeService) NodeMetadata(org.jclouds.compute.domain.NodeMetadata) StatementBuilder(org.apache.whirr.service.jclouds.StatementBuilder) Future(java.util.concurrent.Future) InstanceTemplate(org.apache.whirr.InstanceTemplate)

Example 2 with ComputeMetadata

use of org.jclouds.compute.domain.ComputeMetadata in project ignite by apache.

the class TcpDiscoveryCloudIpFinder method initComputeService.

/**
 * Initializes Apache jclouds compute service.
 */
private void initComputeService() {
    if (initGuard.compareAndSet(false, true))
        try {
            if (provider == null)
                throw new IgniteSpiException("Cloud provider is not set.");
            if (identity == null)
                throw new IgniteSpiException("Cloud identity is not set.");
            if (credential != null && credentialPath != null)
                throw new IgniteSpiException("Both credential and credentialPath are set. Use only one method.");
            if (credentialPath != null)
                credential = getCredentialFromFile();
            try {
                ContextBuilder ctxBuilder = ContextBuilder.newBuilder(provider);
                ctxBuilder.credentials(identity, credential);
                Properties properties = new Properties();
                properties.setProperty(Constants.PROPERTY_SO_TIMEOUT, JCLOUD_CONNECTION_TIMEOUT);
                properties.setProperty(Constants.PROPERTY_CONNECTION_TIMEOUT, JCLOUD_CONNECTION_TIMEOUT);
                if (!F.isEmpty(regions))
                    properties.setProperty(LocationConstants.PROPERTY_REGIONS, keysSetToStr(regions));
                if (!F.isEmpty(zones))
                    properties.setProperty(LocationConstants.PROPERTY_ZONES, keysSetToStr(zones));
                ctxBuilder.overrides(properties);
                computeService = ctxBuilder.buildView(ComputeServiceContext.class).getComputeService();
                if (!F.isEmpty(zones) || !F.isEmpty(regions)) {
                    nodesFilter = new Predicate<ComputeMetadata>() {

                        @Override
                        public boolean apply(ComputeMetadata computeMetadata) {
                            String region = null;
                            String zone = null;
                            Location location = computeMetadata.getLocation();
                            while (location != null) {
                                switch(location.getScope()) {
                                    case ZONE:
                                        zone = location.getId();
                                        break;
                                    case REGION:
                                        region = location.getId();
                                        break;
                                }
                                location = location.getParent();
                            }
                            if (regions != null && region != null && !regions.contains(region))
                                return false;
                            if (zones != null && zone != null && !zones.contains(zone))
                                return false;
                            return true;
                        }
                    };
                }
            } catch (Exception e) {
                throw new IgniteSpiException("Failed to connect to the provider: " + provider, e);
            }
        } finally {
            initLatch.countDown();
        }
    else {
        try {
            U.await(initLatch);
        } catch (IgniteInterruptedCheckedException e) {
            throw new IgniteSpiException("Thread has been interrupted.", e);
        }
        if (computeService == null)
            throw new IgniteSpiException("Ip finder has not been initialized properly.");
    }
}
Also used : IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) ContextBuilder(org.jclouds.ContextBuilder) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) ComputeMetadata(org.jclouds.compute.domain.ComputeMetadata) Properties(java.util.Properties) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IOException(java.io.IOException) Predicate(com.google.common.base.Predicate) Location(org.jclouds.domain.Location)

Example 3 with ComputeMetadata

use of org.jclouds.compute.domain.ComputeMetadata in project hive by apache.

the class CloudComputeService method createFilterPTestPredicate.

static Predicate<ComputeMetadata> createFilterPTestPredicate(final String groupName, final String groupTag) {
    return new Predicate<ComputeMetadata>() {

        @Override
        public boolean apply(ComputeMetadata computeMetadata) {
            NodeMetadata nodeMetadata = (NodeMetadata) computeMetadata;
            return nodeMetadata.getStatus() == Status.RUNNING && isPTestHost(nodeMetadata);
        }

        private boolean isPTestHost(NodeMetadata node) {
            String result = "false non-ptest host";
            if (groupName.equalsIgnoreCase(node.getGroup())) {
                result = "true due to group " + groupName;
                return true;
            }
            if (Strings.nullToEmpty(node.getName()).startsWith(groupName)) {
                result = "true due to name " + groupName;
                return true;
            }
            if (node.getTags().contains(groupTag)) {
                result = "true due to tag " + groupName;
                return true;
            }
            LOG.debug("Found node: " + node + ", Result: " + result);
            return false;
        }
    };
}
Also used : NodeMetadata(org.jclouds.compute.domain.NodeMetadata) ComputeMetadata(org.jclouds.compute.domain.ComputeMetadata) Predicate(com.google.common.base.Predicate)

Example 4 with ComputeMetadata

use of org.jclouds.compute.domain.ComputeMetadata in project acceptance-test-harness by jenkinsci.

the class JcloudsMachineProvider method getRunningInstances.

private Set<NodeMetadata> getRunningInstances() {
    logger.info(String.format("Check if we already got running machines in the security group: %s... ", getGroupName()));
    Set<? extends NodeMetadata> nodeMetadatas = computeService.listNodesDetailsMatching(new Predicate<ComputeMetadata>() {

        @Override
        public boolean apply(ComputeMetadata computeMetadata) {
            return true;
        }
    });
    Set<NodeMetadata> filteredNodes = new HashSet<>();
    for (NodeMetadata nm : nodeMetadatas) {
        if (getGroupName().equals(nm.getGroup()) && nm.getStatus() == NodeMetadata.Status.RUNNING) {
            logger.info(String.format("Found running machine: %s", getGroupName()));
            filteredNodes.add(nm);
        }
    }
    return filteredNodes;
}
Also used : NodeMetadata(org.jclouds.compute.domain.NodeMetadata) ComputeMetadata(org.jclouds.compute.domain.ComputeMetadata)

Example 5 with ComputeMetadata

use of org.jclouds.compute.domain.ComputeMetadata in project SimianArmy by Netflix.

the class AWSClient method getJcloudsNode.

private NodeMetadata getJcloudsNode(ComputeService computeService, String jcloudsId) {
    // Work around a jclouds bug / documentation issue...
    // TODO: Figure out what's broken, and eliminate this function
    // This should work (?):
    // Set<NodeMetadata> nodes = computeService.listNodesByIds(Collections.singletonList(jcloudsId));
    Set<NodeMetadata> nodes = Sets.newHashSet();
    for (ComputeMetadata n : computeService.listNodes()) {
        if (jcloudsId.equals(n.getId())) {
            nodes.add((NodeMetadata) n);
        }
    }
    if (nodes.isEmpty()) {
        LOGGER.warn("Unable to find jclouds node: {}", jcloudsId);
        for (ComputeMetadata n : computeService.listNodes()) {
            LOGGER.info("Did find node: {}", n);
        }
        throw new IllegalStateException("Unable to find node using jclouds: " + jcloudsId);
    }
    NodeMetadata node = Iterables.getOnlyElement(nodes);
    return node;
}
Also used : NodeMetadata(org.jclouds.compute.domain.NodeMetadata) ComputeMetadata(org.jclouds.compute.domain.ComputeMetadata)

Aggregations

ComputeMetadata (org.jclouds.compute.domain.ComputeMetadata)7 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)5 IOException (java.io.IOException)3 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)3 Predicate (com.google.common.base.Predicate)2 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)2 InetSocketAddress (java.net.InetSocketAddress)1 DecimalFormat (java.text.DecimalFormat)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Properties (java.util.Properties)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 Cluster (org.apache.whirr.Cluster)1 Instance (org.apache.whirr.Cluster.Instance)1 ClusterSpec (org.apache.whirr.ClusterSpec)1 InstanceTemplate (org.apache.whirr.InstanceTemplate)1