use of org.apache.whirr.state.ClusterStateStore 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);
}
use of org.apache.whirr.state.ClusterStateStore in project whirr by apache.
the class ListClusterCommandTest method testAllOptions.
@Test
public void testAllOptions() throws Exception {
ClusterControllerFactory factory = mock(ClusterControllerFactory.class);
ClusterController controller = mock(ClusterController.class);
when(factory.create((String) any())).thenReturn(controller);
NodeMetadata node1 = new NodeMetadataBuilder().name("name1").ids("id1").location(new LocationBuilder().scope(LocationScope.PROVIDER).id("location-id1").description("location-desc1").build()).imageId("image-id").status(NodeMetadata.Status.RUNNING).publicAddresses(Lists.newArrayList("127.0.0.1")).privateAddresses(Lists.newArrayList("127.0.0.1")).build();
NodeMetadata node2 = new NodeMetadataBuilder().name("name2").ids("id2").location(new LocationBuilder().scope(LocationScope.PROVIDER).id("location-id2").description("location-desc2").build()).imageId("image-id").status(NodeMetadata.Status.RUNNING).publicAddresses(Lists.newArrayList("127.0.0.2")).privateAddresses(Lists.newArrayList("127.0.0.2")).build();
when(controller.getNodes((ClusterSpec) any())).thenReturn((Set) Sets.newLinkedHashSet(Lists.newArrayList(node1, node2)));
when(controller.getInstances((ClusterSpec) any(), (ClusterStateStore) any())).thenCallRealMethod();
ClusterStateStore memStore = new MemoryClusterStateStore();
memStore.save(createTestCluster(new String[] { "id1", "id2" }, new String[] { "role1", "role2" }));
ClusterStateStoreFactory stateStoreFactory = mock(ClusterStateStoreFactory.class);
when(stateStoreFactory.create((ClusterSpec) any())).thenReturn(memStore);
ListClusterCommand command = new ListClusterCommand(factory, stateStoreFactory);
Map<String, File> keys = KeyPair.generateTemporaryFiles();
int rc = command.run(null, out, null, Lists.newArrayList("--instance-templates", "1 noop", "--service-name", "test-service", "--cluster-name", "test-cluster", "--identity", "myusername", "--quiet", "--private-key-file", keys.get("private").getAbsolutePath()));
assertThat(rc, is(0));
assertThat(outBytes.toString(), is("id1\timage-id\t127.0.0.1\t127.0.0.1\tRUNNING\tlocation-id1\trole1\n" + "id2\timage-id\t127.0.0.2\t127.0.0.2\tRUNNING\tlocation-id2\trole2\n"));
verify(factory).create("test-service");
}
use of org.apache.whirr.state.ClusterStateStore in project whirr by apache.
the class RunScriptCommandTest method testRunScriptByRole.
@Test
public void testRunScriptByRole() throws Exception {
ClusterControllerFactory factory = mock(ClusterControllerFactory.class);
ClusterController controller = mock(ClusterController.class);
when(factory.create((String) any())).thenReturn(controller);
ClusterStateStore memStore = new MemoryClusterStateStore();
memStore.save(createTestCluster(new String[] { "reg/A", "reg/B" }, new String[] { "A", "B" }));
ClusterStateStoreFactory stateStoreFactory = mock(ClusterStateStoreFactory.class);
when(stateStoreFactory.create((ClusterSpec) any())).thenReturn(memStore);
RunScriptCommand command = new RunScriptCommand(factory, stateStoreFactory);
Map<String, File> keys = KeyPair.generateTemporaryFiles();
int rc = command.run(null, out, System.err, Lists.newArrayList("--instance-templates", "1 noop", "--script", "/dev/null", "--roles", "A", "--cluster-name", "test-cluster", "--provider", "provider", "--identity", "myusername", "--credential", "mypassword", "--private-key-file", keys.get("private").getAbsolutePath()));
assertThat(rc, is(0));
ArgumentCaptor<Predicate> predicate = ArgumentCaptor.forClass(Predicate.class);
verify(controller).runScriptOnNodesMatching((ClusterSpec) any(), predicate.capture(), (Statement) any());
// check predicate equality by using the object string representation
Predicate<NodeMetadata> expected = Predicates.and(Predicates.<NodeMetadata>alwaysTrue(), withIds("reg/A"));
assertThat(predicate.getValue().toString(), is(expected.toString()));
}
use of org.apache.whirr.state.ClusterStateStore in project whirr by apache.
the class ListClusterCommand method run.
public int run(InputStream in, PrintStream out, PrintStream err, ClusterSpec clusterSpec) throws Exception {
ClusterStateStore stateStore = createClusterStateStore(clusterSpec);
ClusterController controller = createClusterController(clusterSpec.getServiceName());
for (Cluster.Instance instance : controller.getInstances(clusterSpec, stateStore)) {
out.println(Joiner.on('\t').useForNull("-").join(instance.getId(), instance.getNodeMetadata().getImageId(), instance.getPublicIp(), instance.getPrivateIp(), instance.getNodeMetadata().getStatus(), instance.getNodeMetadata().getLocation().getId(), Joiner.on(",").join(instance.getRoles())));
}
return 0;
}
use of org.apache.whirr.state.ClusterStateStore in project whirr by apache.
the class ClusterController method destroyCluster.
/**
* 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 {
ClusterStateStore stateStore = getClusterStateStore(clusterSpec);
destroyCluster(clusterSpec, stateStore.tryLoadOrEmpty());
stateStore.destroy();
}
Aggregations