public void execute(Iterable<? extends NodeMetadata> deadOnes) { for (NodeMetadata node : deadOnes){ credentialStore.remove("node#" + node.getId()); } Iterable<OrgAndName> orgGroups = filter(transform(deadOnes, nodeToOrgAndName), notNull()); for (OrgAndName orgGroup : orgGroups) { Iterable<? extends NodeMetadata> nodesInOrg = listNodes.listDetailsOnNodesMatching(parentLocationId(orgGroup .getOrg().toASCIIString())); Iterable<? extends NodeMetadata> nodesInGroup = filter(nodesInOrg, inGroup(orgGroup.getName())); if (size(nodesInGroup) == 0 || all(nodesInGroup, TERMINATED)){ deleteKeyPair.execute(orgGroup); } } }
@Override public boolean apply(String groupName) { return all(filter(computeService.listNodesDetailsMatching(all()), inGroup(groupName)), TERMINATED); } }
public Multimap<String, String> apply(Set<? extends NodeMetadata> deadNodes) { Iterable<? extends NodeMetadata> nodesWithGroup = filter(deadNodes, NodePredicates.hasGroup()); Set<ZoneAndName> zoneAndGroupNames = ImmutableSet.copyOf(filter(transform(nodesWithGroup, new Function<NodeMetadata, ZoneAndName>() { @Override public ZoneAndName apply(NodeMetadata input) { String zoneId = input.getLocation().getScope() == LocationScope.HOST ? input.getLocation() .getParent().getId() : input.getLocation().getId(); return ZoneAndName.fromZoneAndName(zoneId, input.getGroup()); } }), allNodesInGroupTerminated)); Multimap<String, String> zoneToZoneAndGroupNames = Multimaps.transformValues(Multimaps.index(zoneAndGroupNames, ZoneAndName.ZONE_FUNCTION), ZoneAndName.NAME_FUNCTION); return zoneToZoneAndGroupNames; }
public Set<? extends NodeMetadata> listRunningNodesInGroup(String group) { return filter(compute.listNodesDetailsMatching(all()), runningInGroup(group)); }
@Override public Set<? extends NodeMetadata> destroyNodesMatching(Predicate<NodeMetadata> filter) { // GCE does not return TERMINATED nodes, so in practice no node will never reach the TERMINATED // state, and the deleted nodes will never be returned. // In order to be able to clean up the resources associated to the deleted nodes, we have to retrieve // the details of the nodes before deleting them. Set<? extends NodeMetadata> nodes = newHashSet(filter(listNodesDetailsMatching(all()), filter)); super.destroyNodesMatching(filter); // This returns an empty list (a list of null elements) in GCE, as the api does not return deleted nodes cleanUpIncidentalResourcesOfDeadNodes(nodes); return nodes; }
@Test public void testWeCanCancelTasks() throws Exception { String group = this.group + "w"; try { client.destroyNodesMatching(inGroup(group)); } catch (Exception e) { } template = buildTemplate(templateBuilder()); try { Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, template); NodeMetadata node = getOnlyElement(nodes); weCanCancelTasks(node); } finally { client.destroyNodesMatching(inGroup(group)); } }
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; } }
public Multimap<String, String> apply(Set<? extends NodeMetadata> deadNodes) { Iterable<? extends NodeMetadata> nodesWithGroup = filter(deadNodes, NodePredicates.hasGroup()); Set<DatacenterAndName> datacenterAndGroupNames = ImmutableSet.copyOf(filter(transform(nodesWithGroup, new Function<NodeMetadata, DatacenterAndName>() { @Override public DatacenterAndName apply(NodeMetadata input) { String datacenterId = input.getLocation().getId(); return DatacenterAndName.fromDatacenterAndName(datacenterId, input.getGroup()); } }), allNodesInGroupTerminated)); Multimap<String, String> datacenterToDatacenterAndGroupNames = Multimaps.transformValues(Multimaps.index(datacenterAndGroupNames, DatacenterAndName.DATACENTER_FUNCTION), DatacenterAndName.NAME_FUNCTION); return datacenterToDatacenterAndGroupNames; }
@Test(groups = { "integration", "live" }, dependsOnMethods = "testCreateSecurityGroup") public void testCreateNodeWithSecurityGroup() throws RunNodesException, InterruptedException, ExecutionException { ComputeService computeService = view.getComputeService(); Optional<SecurityGroupExtension> securityGroupExtension = computeService.getSecurityGroupExtension(); assertTrue(securityGroupExtension.isPresent(), "security group extension was not present"); NodeMetadata node = getOnlyElement(computeService.createNodesInGroup(nodeGroup, 1, options().securityGroups(groupId))); try { Set<SecurityGroup> groups = securityGroupExtension.get().listSecurityGroupsForNode(node.getId()); assertEquals(groups.size(), 1, "node has " + groups.size() + " groups"); assertEquals(getOnlyElement(groups).getId(), groupId); } finally { computeService.destroyNodesMatching(inGroup(node.getGroup())); } }
@Test(enabled = true, dependsOnMethods = "testConcurrentUseOfComputeServiceToCreateNodes") public void testCreateTwoNodesWithRunScript() throws Exception { try { client.destroyNodesMatching(inGroup(group)); } catch (NoSuchElementException e) { } refreshTemplate(); try { nodes = newTreeSet(client.createNodesInGroup(group, 2, template)); } catch (RunNodesException e) { nodes = newTreeSet(concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet())); throw e; } assertEquals(nodes.size(), 2); checkNodes(nodes, group, "bootstrap"); NodeMetadata node1 = nodes.first(); NodeMetadata node2 = nodes.last(); // credentials aren't always the same // assertEquals(node1.getCredentials(), node2.getCredentials()); assertLocationSameOrChild(checkNotNull(node1.getLocation(), "location of %s", node1), template.getLocation()); assertLocationSameOrChild(checkNotNull(node2.getLocation(), "location of %s", node2), template.getLocation()); checkImageIdMatchesTemplate(node1); checkImageIdMatchesTemplate(node2); checkOsMatchesTemplate(node1); checkOsMatchesTemplate(node2); }
@Test(enabled = true, dependsOnMethods = { "testCompareSizes" }) public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception { String group = this.group + "r"; try { client.destroyNodesMatching(inGroup(group)); } catch (Exception e) { TemplateOptions options = client.templateOptions().blockOnPort(22, 120); try { Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, options); NodeMetadata node = get(nodes, 0); LoginCredentials good = node.getCredentials(); assert good.identity != null : nodes; assert good.credential != null : nodes; for (Entry<? extends NodeMetadata, ExecResponse> response : client.runScriptOnNodesMatching( runningInGroup(group), "hostname", wrapInInitScript(false).runAsRoot(false).overrideLoginCredentials(good)).entrySet()) { checkResponseEqualsHostname(response.getValue(), response.getKey()); ExecResponse response = client.runScriptOnNode(node.getId(), "hostname", wrapInInitScript(false).runAsRoot(false)); checkResponseEqualsHostname(response, node); OperatingSystem os = node.getOperatingSystem(); client.destroyNodesMatching(inGroup(group));
@Test public void testWindowsAdminWorks() throws Exception { String group = "winadm"; // Spin up a new node. Make sure to open the RDP port 3389 Template template = view.getComputeService().templateBuilder().from(windowsTemplate).options(inboundPorts(3389)) .build(); try { NodeMetadata node = Iterables.getOnlyElement(view.getComputeService().createNodesInGroup(group, 1, template)); assertEquals(node.getCredentials().getUser(), "Administrator"); assertFalse(Strings.isNullOrEmpty(node.getCredentials().getPassword())); } finally { view.getComputeService().destroyNodesMatching(NodePredicates.inGroup(group)); } }
public void testResizeRam() throws Exception { String group = this.group + "ram"; GoGridClient api = view.utils().injector().getInstance(GoGridClient.class); try { client.destroyNodesMatching(inGroup(group)); } catch (Exception e) { } Predicate<Server> serverLatestJobCompleted = retry(new ServerLatestJobCompleted(api .getJobServices()), 800, 20, SECONDS); String ram = get(api.getServerServices().getRamSizes(), 1).getName(); try { NodeMetadata node = getOnlyElement(client.createNodesInGroup(group, 1)); Server updatedServer = api.getServerServices().editServerRam(Long.valueOf(node.getId()), ram); assertNotNull(updatedServer); assert serverLatestJobCompleted.apply(updatedServer); assertEquals(getLast(api.getServerServices().getServersById(Long.valueOf(node.getId()))).getRam().getName(), ram); } finally { client.destroyNodesMatching(inGroup(group)); } } }
@Test(enabled = true, dependsOnMethods = { "testListNodes", "testGetNodesWithDetails", "testListNodesByIds" }) public void testDestroyNodes() { int toDestroy = refreshNodes().size(); Set<? extends NodeMetadata> destroyed = client.destroyNodesMatching(inGroup(group)); assertEquals(toDestroy, destroyed.size()); for (NodeMetadata node : filter(client.listNodesDetailsMatching(all()), inGroup(group))) { assert node.getStatus() == Status.TERMINATED : node; assert view.utils().credentialStore().get("node#" + node.getId()) == null : "credential should have been null for " + "node#" + node.getId(); } }
@Test(enabled = false) protected void tryBadPassword(String group, Credentials good) throws AssertionError { try { Map<? extends NodeMetadata, ExecResponse> responses = client.runScriptOnNodesMatching( runningInGroup(group), "echo I put a bad password", wrapInInitScript(false).runAsRoot(false).overrideLoginCredentials( LoginCredentials.builder().user(good.identity).noPrivateKey().password("romeo").build())); assert responses.size() == 0 : "shouldn't pass with a bad password\n" + responses; } catch (AssertionError e) { throw e; } catch (RunScriptOnNodesException e) { assert Iterables.any(e.getNodeErrors().values(), Predicates.instanceOf(AuthorizationException.class)) : e + " not authexception!"; } }
@Test(enabled = true) public void testCreateAndRunAService() throws Exception { String group = this.group + "s"; try { client.destroyNodesMatching(inGroup(group)); } catch (Exception e) { } try { createAndRunAServiceInGroup(group); } finally { client.destroyNodesMatching(inGroup(group)); } }
@Test(enabled = true, dependsOnMethods = "testGet") public void testReboot() throws Exception { client.rebootNodesMatching(inGroup(group));// TODO test // validation testGet(); }
@Test(enabled = true, expectedExceptions = NoSuchElementException.class) public void testCorrectExceptionRunningNodesNotFound() throws Exception { client.runScriptOnNodesMatching(runningInGroup("zebras-are-awesome"), InstallJDK.fromOpenJDK()); }
@Test(enabled = true, dependsOnMethods = "testSuspendResume") public void testGetNodesWithDetails() throws Exception { for (NodeMetadata node : client.listNodesDetailsMatching(all())) { assert node.getProviderId() != null : node; assert node.getLocation() != null : node; assertEquals(node.getType(), ComputeType.NODE); assert node instanceof NodeMetadata; NodeMetadata nodeMetadata = node; assert nodeMetadata.getProviderId() != null : nodeMetadata; // nullable // assert nodeMetadata.getImage() != null : node; // user specified name is not always supported // assert nodeMetadata.getName() != null : nodeMetadata; if (nodeMetadata.getStatus() == Status.RUNNING) { assert nodeMetadata.getPublicAddresses() != null : nodeMetadata; assert nodeMetadata.getPublicAddresses().size() > 0 || nodeMetadata.getPrivateAddresses().size() > 0 : nodeMetadata; assertNotNull(nodeMetadata.getPrivateAddresses()); } } }