/** * Try to load the cluster state or return an empty instance * * @return */ public Cluster tryLoadOrEmpty() { try { return load(); } catch (Exception e) { LOG.info("Unable to load cluster state, assuming it has no running nodes.", e); return Cluster.empty(); } }
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); }
/** * 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(); }
/** * Provision the hardware resources needed for running services */ public Cluster bootstrapCluster(ClusterSpec clusterSpec) throws IOException, InterruptedException { BootstrapClusterAction bootstrapper = new BootstrapClusterAction(getCompute(), handlerMapFactory.create()); Cluster cluster = bootstrapper.execute(clusterSpec, null); getClusterStateStore(clusterSpec).save(cluster); return cluster; }
/** * Provision the hardware resources needed for running services */ public Cluster bootstrapCluster(ClusterSpec clusterSpec) throws IOException, InterruptedException { BootstrapClusterAction bootstrapper = new BootstrapClusterAction(getCompute(), handlerMapFactory.create()); Cluster cluster = bootstrapper.execute(clusterSpec, null); getClusterStateStore(clusterSpec).save(cluster); return cluster; }
/** * Try to load the cluster state or return an empty instance * * @return */ public Cluster tryLoadOrEmpty() { try { return load(); } catch (Exception e) { LOG.info("Unable to load cluster state, assuming it has no running nodes.", e); return Cluster.empty(); } }
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); }
/** * Provisions the hardware for a BYON cluster. */ @Override public Cluster bootstrapCluster(ClusterSpec clusterSpec) throws IOException, InterruptedException { LoadingCache<String, ClusterActionHandler> handlerMap = handlerMapFactory .create(); ClusterAction bootstrapper = new ByonClusterAction(BOOTSTRAP_ACTION, getCompute(), handlerMap); Cluster cluster = bootstrapper.execute(clusterSpec, null); getClusterStateStore(clusterSpec).save(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 { ClusterStateStore stateStore = getClusterStateStore(clusterSpec); destroyCluster(clusterSpec, stateStore.tryLoadOrEmpty()); stateStore.destroy(); }
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; }
/** * Provisions the hardware for a BYON cluster. */ @Override public Cluster bootstrapCluster(ClusterSpec clusterSpec) throws IOException, InterruptedException { LoadingCache<String, ClusterActionHandler> handlerMap = handlerMapFactory .create(); ClusterAction bootstrapper = new ByonClusterAction(BOOTSTRAP_ACTION, getCompute(), handlerMap); Cluster cluster = bootstrapper.execute(clusterSpec, null); getClusterStateStore(clusterSpec).save(cluster); return cluster; }
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; }
/** * Configures cluster services for a BYON cluster. */ @Override public Cluster configureServices(ClusterSpec clusterSpec, Cluster cluster, Set<String> targetRoles, Set<String> targetInstanceIds) throws IOException, InterruptedException { LoadingCache<String, ClusterActionHandler> handlerMap = handlerMapFactory .create(); ClusterAction configurer = new ByonClusterAction(CONFIGURE_ACTION, getCompute(), handlerMap); cluster = configurer.execute(clusterSpec, cluster); getClusterStateStore(clusterSpec).save(cluster); return cluster; }
private Predicate<NodeMetadata> buildFilterPredicate(String[] ids, String[] roles, ClusterSpec spec) throws IOException { Predicate<NodeMetadata> condition = Predicates.alwaysTrue(); if (ids != null && ids.length > 0) { return Predicates.and(condition, withIds(ids)); } else if (roles != null && roles.length > 0) { List<String> instanceIds = Lists.newArrayList(); Cluster cluster = createClusterStateStore(spec).load(); for (Cluster.Instance instance : cluster.getInstancesMatching( anyRoleIn(Sets.<String>newHashSet(roles)))) { instanceIds.add(instance.getId()); } condition = Predicates.and(condition, withIds(instanceIds.toArray(new String[0]))); return condition; } else { return condition; } }
/** * Configures cluster services for a BYON cluster. */ @Override public Cluster configureServices(ClusterSpec clusterSpec, Cluster cluster, Set<String> targetRoles, Set<String> targetInstanceIds) throws IOException, InterruptedException { LoadingCache<String, ClusterActionHandler> handlerMap = handlerMapFactory .create(); ClusterAction configurer = new ByonClusterAction(CONFIGURE_ACTION, getCompute(), handlerMap); cluster = configurer.execute(clusterSpec, cluster); getClusterStateStore(clusterSpec).save(cluster); return cluster; }
private Predicate<NodeMetadata> buildFilterPredicate(String[] ids, String[] roles, ClusterSpec spec) throws IOException { Predicate<NodeMetadata> condition = Predicates.alwaysTrue(); if (ids != null && ids.length > 0) { return Predicates.and(condition, withIds(ids)); } else if (roles != null && roles.length > 0) { List<String> instanceIds = Lists.newArrayList(); Cluster cluster = createClusterStateStore(spec).load(); for (Cluster.Instance instance : cluster.getInstancesMatching( anyRoleIn(Sets.<String>newHashSet(roles)))) { instanceIds.add(instance.getId()); } condition = Predicates.and(condition, withIds(instanceIds.toArray(new String[0]))); return condition; } else { return condition; } }
@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())); }
public Map<? extends NodeMetadata, ExecResponse> runScriptOnNodesMatching(final ClusterSpec spec, Predicate<NodeMetadata> condition, final Statement statement) throws IOException, RunScriptOnNodesException { ComputeServiceContext computeServiceContext = getCompute().apply(spec); ComputeService computeService = computeServiceContext.getComputeService(); Cluster cluster = getClusterStateStore(spec).load(); RunScriptOptions options = RunScriptOptions.Builder.runAsRoot(false).wrapInInitScript(false); return computeService.runScriptOnNodesMatching(Predicates.<NodeMetadata>and(condition, runningIn(cluster)), statement, options); }
memStore.save(createTestCluster( new String[]{"id1", "id2"}, new String[]{"role1", "role2"}));
public Map<? extends NodeMetadata, ExecResponse> runScriptOnNodesMatching(final ClusterSpec spec, Predicate<NodeMetadata> condition, final Statement statement) throws IOException, RunScriptOnNodesException { ComputeServiceContext computeServiceContext = getCompute().apply(spec); ComputeService computeService = computeServiceContext.getComputeService(); Cluster cluster = getClusterStateStore(spec).load(); RunScriptOptions options = RunScriptOptions.Builder.runAsRoot(false).wrapInInitScript(false); return computeService.runScriptOnNodesMatching(Predicates.<NodeMetadata>and(condition, runningIn(cluster)), statement, options); }