Search in sources :

Example 16 with ComputeService

use of org.jclouds.compute.ComputeService in project whirr by apache.

the class ClusterController method destroyInstance.

public void destroyInstance(ClusterSpec clusterSpec, String instanceId) throws IOException {
    LOG.info("Destroying instance {}", instanceId);
    /* Destroy the instance */
    ComputeService computeService = getCompute().apply(clusterSpec).getComputeService();
    computeService.destroyNode(instanceId);
    /* .. and update the cluster state storage */
    ClusterStateStore store = getClusterStateStore(clusterSpec);
    Cluster cluster = store.load();
    cluster.removeInstancesMatching(withIds(instanceId));
    store.save(cluster);
    LOG.info("Instance {} destroyed", instanceId);
}
Also used : ClusterStateStore(org.apache.whirr.state.ClusterStateStore) ComputeService(org.jclouds.compute.ComputeService)

Example 17 with ComputeService

use of org.jclouds.compute.ComputeService in project whirr by apache.

the class BootstrapClusterAction method doAction.

@Override
protected void doAction(Map<InstanceTemplate, ClusterActionEvent> eventMap) throws IOException, InterruptedException {
    LOG.info("Bootstrapping cluster");
    ExecutorService executorService = Executors.newCachedThreadPool();
    Map<InstanceTemplate, Future<Set<? extends NodeMetadata>>> futures = Maps.newHashMap();
    // initialize startup processes per InstanceTemplates
    for (Entry<InstanceTemplate, ClusterActionEvent> entry : eventMap.entrySet()) {
        final InstanceTemplate instanceTemplate = entry.getKey();
        final ClusterSpec clusterSpec = entry.getValue().getClusterSpec();
        final int maxNumberOfRetries = clusterSpec.getMaxStartupRetries();
        StatementBuilder statementBuilder = entry.getValue().getStatementBuilder();
        ComputeServiceContext computeServiceContext = getCompute().apply(clusterSpec);
        final ComputeService computeService = computeServiceContext.getComputeService();
        final Template template = BootstrapTemplate.build(clusterSpec, computeService, statementBuilder, entry.getKey());
        Future<Set<? extends NodeMetadata>> nodesFuture = executorService.submit(new StartupProcess(clusterSpec.getClusterName(), instanceTemplate.getNumberOfInstances(), instanceTemplate.getMinNumberOfInstances(), maxNumberOfRetries, instanceTemplate.getRoles(), computeService, template, executorService, nodeStarterFactory));
        futures.put(instanceTemplate, nodesFuture);
    }
    Set<Instance> instances = Sets.newLinkedHashSet();
    for (Entry<InstanceTemplate, Future<Set<? extends NodeMetadata>>> entry : futures.entrySet()) {
        Set<? extends NodeMetadata> nodes;
        try {
            nodes = entry.getValue().get();
        } catch (ExecutionException e) {
            // nodes after retries
            throw new IOException(e);
        }
        Set<String> roles = entry.getKey().getRoles();
        instances.addAll(getInstances(roles, nodes));
    }
    Cluster cluster = new Cluster(instances);
    for (ClusterActionEvent event : eventMap.values()) {
        event.setCluster(cluster);
    }
}
Also used : Set(java.util.Set) Instance(org.apache.whirr.Cluster.Instance) ClusterActionEvent(org.apache.whirr.service.ClusterActionEvent) InstanceTemplate(org.apache.whirr.InstanceTemplate) Template(org.jclouds.compute.domain.Template) BootstrapTemplate(org.apache.whirr.compute.BootstrapTemplate) StartupProcess(org.apache.whirr.compute.StartupProcess) 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) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) InstanceTemplate(org.apache.whirr.InstanceTemplate)

Example 18 with ComputeService

use of org.jclouds.compute.ComputeService 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 19 with ComputeService

use of org.jclouds.compute.ComputeService in project whirr by apache.

the class DestroyClusterAction method postRunScriptsActions.

@Override
protected void postRunScriptsActions(Map<InstanceTemplate, ClusterActionEvent> eventMap) throws IOException {
    ClusterSpec clusterSpec = eventMap.values().iterator().next().getClusterSpec();
    LOG.info("Destroying " + clusterSpec.getClusterName() + " cluster");
    ComputeService computeService = getCompute().apply(clusterSpec).getComputeService();
    computeService.destroyNodesMatching(inGroup(clusterSpec.getClusterName()));
    LOG.info("Cluster {} destroyed", clusterSpec.getClusterName());
}
Also used : ClusterSpec(org.apache.whirr.ClusterSpec) ComputeService(org.jclouds.compute.ComputeService)

Example 20 with ComputeService

use of org.jclouds.compute.ComputeService in project whirr by apache.

the class Activator method start.

/**
 * Called when this bundle is started so the Framework can perform the
 * bundle-specific activities necessary to start this bundle. This method
 * can be used to register services or to allocate any resources that this
 * bundle needs.
 * <p/>
 * <p/>
 * This method must complete and return to its caller in a timely manner.
 *
 * @param context The execution context of the bundle being started.
 * @throws Exception If this method throws an exception, this
 *                   bundle is marked as stopped and the Framework will remove this
 *                   bundle's listeners, unregister all services registered by this
 *                   bundle, and release all services used by this bundle.
 */
@Override
public void start(BundleContext context) throws Exception {
    // Initialize OSGi based FunctionLoader
    functionLoader = new BundleFunctionLoader(context);
    functionLoader.start();
    defaultClusterController.setHandlerMapFactory(handlerMapFactory);
    byonClusterController.setHandlerMapFactory(handlerMapFactory);
    // Register services
    clusterControllerFactoryRegistration = context.registerService(ClusterControllerFactory.class.getName(), clusterControllerFactory, null);
    handlerMapFactoryRegistration = context.registerService(DynamicHandlerMapFactory.class.getName(), handlerMapFactory, null);
    // Start tracking
    clusterControllerTracker = new ServiceTracker(context, ClusterController.class.getName(), null) {

        @Override
        public Object addingService(ServiceReference reference) {
            Object service = super.addingService(reference);
            clusterControllerFactory.bind((ClusterController) service);
            return service;
        }

        @Override
        public void removedService(ServiceReference reference, Object service) {
            clusterControllerFactory.unbind((ClusterController) service);
            super.removedService(reference, service);
        }
    };
    clusterControllerTracker.open();
    computeServiceTracker = new ServiceTracker(context, ComputeService.class.getName(), null) {

        @Override
        public Object addingService(ServiceReference reference) {
            Object service = context.getService(reference);
            dynamicComputeCache.bind((ComputeService) service);
            return service;
        }

        @Override
        public void removedService(ServiceReference reference, Object service) {
            dynamicComputeCache.unbind((ComputeService) service);
            super.removedService(reference, service);
        }
    };
    computeServiceTracker.open();
    handlerTracker = new ServiceTracker(context, ClusterActionHandler.class.getName(), null) {

        @Override
        public Object addingService(ServiceReference reference) {
            Object service = context.getService(reference);
            handlerMapFactory.bind((ClusterActionHandler) service);
            return service;
        }

        @Override
        public void removedService(ServiceReference reference, Object service) {
            handlerMapFactory.unbind((ClusterActionHandler) service);
            super.removedService(reference, service);
        }
    };
    handlerTracker.open();
    Properties defaultClusterControllerProperties = new Properties();
    defaultClusterControllerProperties.setProperty("name", "default");
    defaultClusterControllerRegistration = context.registerService(ClusterController.class.getName(), defaultClusterController, defaultClusterControllerProperties);
    Properties byonClusterControllerProperties = new Properties();
    byonClusterControllerProperties.setProperty("name", "byon");
    byonClusterControllerRegistration = context.registerService(ClusterController.class.getName(), byonClusterController, byonClusterControllerProperties);
}
Also used : BundleFunctionLoader(org.jclouds.scriptbuilder.functionloader.osgi.BundleFunctionLoader) ClusterController(org.apache.whirr.ClusterController) ByonClusterController(org.apache.whirr.ByonClusterController) ClusterActionHandler(org.apache.whirr.service.ClusterActionHandler) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Properties(java.util.Properties) ComputeService(org.jclouds.compute.ComputeService) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ComputeService (org.jclouds.compute.ComputeService)44 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)13 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)13 Template (org.jclouds.compute.domain.Template)13 TemplateBuilder (org.jclouds.compute.domain.TemplateBuilder)13 ResourceContainerCloud (org.palladiosimulator.pcm.cloud.pcmcloud.resourceenvironmentcloud.ResourceContainerCloud)10 ResourceContainer (org.palladiosimulator.pcm.resourceenvironment.ResourceContainer)10 RunNodesException (org.jclouds.compute.RunNodesException)8 IOException (java.io.IOException)7 ClusterSpec (org.apache.whirr.ClusterSpec)7 TemplateOptions (org.jclouds.compute.options.TemplateOptions)7 Set (java.util.Set)6 Test (org.junit.Test)6 ClusterActionHandler (org.apache.whirr.service.ClusterActionHandler)5 Statement (org.jclouds.scriptbuilder.domain.Statement)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 HashSet (java.util.HashSet)4 Stack (java.util.Stack)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)4