Package org.apache.whirr

Source Code of org.apache.whirr.ClusterController

* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.

package org.apache.whirr;


import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

import org.apache.whirr.actions.BootstrapClusterAction;
import org.apache.whirr.actions.ConfigureClusterAction;
import org.apache.whirr.actions.DestroyClusterAction;
import org.apache.whirr.service.ClusterStateStore;
import org.apache.whirr.service.ClusterStateStoreFactory;
import org.apache.whirr.service.ClusterActionHandler;
import org.apache.whirr.service.ComputeCache;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.RunScriptOnNodesException;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeState;
import org.jclouds.domain.Credentials;
import org.jclouds.scriptbuilder.domain.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.whirr.RolePredicates.withIds;
import static org.jclouds.compute.options.RunScriptOptions.Builder.overrideCredentialsWith;

* This class is used to start and stop clusters.
public class ClusterController {

  private static final Logger LOG = LoggerFactory.getLogger(ClusterController.class);

  private final Function<ClusterSpec, ComputeServiceContext> getCompute;
  private final ClusterStateStoreFactory stateStoreFactory;

  public ClusterController() {
    this(ComputeCache.INSTANCE, new ClusterStateStoreFactory());

  public ClusterController(Function<ClusterSpec, ComputeServiceContext> getCompute,
      ClusterStateStoreFactory stateStoreFactory) {
    this.getCompute = getCompute;
    this.stateStoreFactory = stateStoreFactory;

   * @return the unique name of the service.
  public String getName() {
    throw new UnsupportedOperationException("No service name");
   * @return compute service contexts for use in managing the service
  protected Function<ClusterSpec, ComputeServiceContext> getCompute() {
    return getCompute;
   * Start the cluster described by <code>clusterSpec</code> and block until the
   * cluster is
   * available. It is not guaranteed that the service running on the cluster
   * has started when this method returns.
   * @param clusterSpec
   * @return an object representing the running cluster
   * @throws IOException if there is a problem while starting the cluster. The
   * cluster may or may not have started.
   * @throws InterruptedException if the thread is interrupted.
  public Cluster launchCluster(ClusterSpec clusterSpec)
      throws IOException, InterruptedException {
    Map<String, ClusterActionHandler> handlerMap = new HandlerMapFactory().create();

    BootstrapClusterAction bootstrapper = new BootstrapClusterAction(getCompute(), handlerMap);
    Cluster cluster = bootstrapper.execute(clusterSpec, null);

    ConfigureClusterAction configurer = new ConfigureClusterAction(getCompute(), handlerMap);
    cluster = configurer.execute(clusterSpec, cluster);


    return cluster;

   * Stop the cluster and destroy all resources associated with it.
   * @throws IOException if there is a problem while stopping the cluster. The
   * cluster may or may not have been stopped.
   * @throws InterruptedException if the thread is interrupted.
  public void destroyCluster(ClusterSpec clusterSpec) throws IOException,
      InterruptedException {
    DestroyClusterAction destroyer = new DestroyClusterAction(getCompute());
    destroyer.execute(clusterSpec, null);


  public void destroyInstance(ClusterSpec clusterSpec, String instanceId) throws IOException {"Destroying instance {}", instanceId);

    /* Destroy the instance */
    ComputeService computeService = getCompute().apply(clusterSpec).getComputeService();

    /* .. and update the cluster state storage */
    ClusterStateStore store = stateStoreFactory.create(clusterSpec);
    Cluster cluster = store.load();
    cluster.removeInstancesMatching(withIds(instanceId));;"Instance {} destroyed", instanceId);

  public Map<? extends NodeMetadata, ExecResponse> runScriptOnNodesMatching(ClusterSpec spec,
        Predicate<NodeMetadata> condition, Statement statement) throws IOException, RunScriptOnNodesException {

    Credentials credentials = new Credentials(spec.getClusterUser(), spec.getPrivateKey());
    ComputeServiceContext context = getCompute().apply(spec);

    condition = Predicates.and(runningInGroup(spec.getClusterName()), condition);
    return context.getComputeService().runScriptOnNodesMatching(condition,
      statement, overrideCredentialsWith(credentials).wrapInInitScript(false).runAsRoot(false));

  public Set<? extends NodeMetadata> getNodes(ClusterSpec clusterSpec)
    throws IOException, InterruptedException {
    ComputeService computeService = getCompute().apply(clusterSpec).getComputeService();
    return computeService.listNodesDetailsMatching(

  public Set<Cluster.Instance> getInstances(ClusterSpec spec)
      throws IOException, InterruptedException {
    return getInstances(spec, null);

  public Set<Cluster.Instance> getInstances(ClusterSpec spec, ClusterStateStore stateStore)
      throws IOException, InterruptedException {

    Set<Cluster.Instance> instances = Sets.newLinkedHashSet();
    Cluster cluster = (stateStore != null) ? stateStore.load() : null;

    for(NodeMetadata node : getNodes(spec)) {
      instances.add(toInstance(node, cluster, spec));

    return instances;

  private Cluster.Instance toInstance(NodeMetadata metadata, Cluster cluster, ClusterSpec spec) {
    Credentials credentials = new Credentials(spec.getClusterUser(), spec.getPrivateKey());

    Set<String> roles = Sets.newHashSet();
    try {
      if (cluster != null) {
        roles = cluster.getInstanceMatching(withIds(metadata.getId())).getRoles();
    } catch(NoSuchElementException e) {}

    return new Cluster.Instance(credentials, roles,
      Iterables.getFirst(metadata.getPublicAddresses(), null),
      Iterables.getFirst(metadata.getPrivateAddresses(), null),
      metadata.getId(), metadata);
  public static Predicate<ComputeMetadata> runningInGroup(final String group) {
    return new Predicate<ComputeMetadata>() {
      public boolean apply(ComputeMetadata computeMetadata) {
        // Not all list calls return NodeMetadata (e.g. VCloud)
        if (computeMetadata instanceof NodeMetadata) {
          NodeMetadata nodeMetadata = (NodeMetadata) computeMetadata;
          return group.equals(nodeMetadata.getGroup())
            && nodeMetadata.getState() == NodeState.RUNNING;
        return false;
      public String toString() {
        return "runningInGroup(" + group + ")";


Related Classes of org.apache.whirr.ClusterController

Copyright © 2018 All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact