private void blockUntilRunningAndAssignElasticIpsToInstancesOrPutIntoBadMap( Set<RunningInstance> input, Map<NodeMetadata, Exception> badNodes) { Map<RegionAndName, RunningInstance> instancesById = Maps.uniqueIndex(input, instanceToRegionAndName); for (Map.Entry<RegionAndName, RunningInstance> entry : instancesById.entrySet()) { RegionAndName id = entry.getKey(); RunningInstance instance = entry.getValue(); try { logger.debug("<< allocating elastic IP instance(%s)", id); String ip = client.getElasticIPAddressServices().allocateAddressInRegion(id.getRegion()); // block until instance is running logger.debug(">> awaiting status running instance(%s)", id); AtomicReference<NodeMetadata> node = newReference(runningInstanceToNodeMetadata.apply(instance)); nodeRunning.apply(node); logger.trace("<< running instance(%s)", id); logger.debug(">> associating elastic IP %s to instance %s", ip, id); client .getElasticIPAddressServices() .associateAddressInRegion(id.getRegion(), ip, id.getName()); logger.trace("<< associated elastic IP %s to instance %s", ip, id); // add mapping of instance to ip into the cache elasticIpCache.put(id, ip); } catch (RuntimeException e) { badNodes.put(runningInstanceToNodeMetadata.apply(instancesById.get(id)), e); } } }
// Fires up an instance, finds its root volume ID, takes a snapshot, then // terminates the instance. private Snapshot createSnapshot() throws RunNodesException { String instanceId = null; try { RunningInstance instance = getOnlyElement( concat( ec2Client .getInstanceServices() .runInstancesInRegion(regionId, null, imageId, 1, 1))); instanceId = instance.getId(); assertTrue(runningTester.apply(instance), instanceId + "didn't achieve the state running!"); instance = getOnlyElement( concat( ec2Client.getInstanceServices().describeInstancesInRegion(regionId, instanceId))); BlockDevice device = instance.getEbsBlockDevices().get("/dev/sda1"); assertNotNull(device, "device: /dev/sda1 not present on: " + instance); Snapshot snapshot = ec2Client .getElasticBlockStoreServices() .createSnapshotInRegion(regionId, device.getVolumeId()); snapshotsToDelete.add(snapshot.getId()); return snapshot; } finally { if (instanceId != null) ec2Client.getInstanceServices().terminateInstancesInRegion(regionId, instanceId); } }
@Override @AfterClass(groups = {"integration", "live"}) protected void tearDownContext() { for (String imageId : imagesToDeregister) client.deregisterImageInRegion(regionId, imageId); for (String snapshotId : snapshotsToDelete) ec2Client.getElasticBlockStoreServices().deleteSnapshotInRegion(regionId, snapshotId); super.tearDownContext(); }
public void testDescribeImages() { for (String region : ec2Client.getConfiguredRegions()) { Set<? extends Image> allResults = client.describeImagesInRegion(region); assertNotNull(allResults); assert allResults.size() >= 2 : allResults.size(); Iterator<? extends Image> iterator = allResults.iterator(); String id1 = iterator.next().getId(); String id2 = iterator.next().getId(); Set<? extends Image> twoResults = client.describeImagesInRegion(region, imageIds(id1, id2)); assertNotNull(twoResults); assertEquals(twoResults.size(), 2); iterator = twoResults.iterator(); assertEquals(iterator.next().getId(), id1); assertEquals(iterator.next().getId(), id2); } }
@Override @BeforeClass(groups = {"integration", "live"}) public void setupContext() { super.setupContext(); ec2Client = view.unwrap(EC2ApiMetadata.CONTEXT_TOKEN).getApi(); runningTester = retry(new InstanceStateRunning(ec2Client), 600, 5, SECONDS); client = ec2Client.getAMIServices(); if (ebsTemplate != null) { Template template = view.getComputeService().templateBuilder().from(ebsTemplate).build(); regionId = template.getLocation().getId(); imageId = template.getImage().getProviderId(); for (Image image : client.describeImagesInRegion(regionId)) { if (ebsBackedImageName.equals(image.getName())) client.deregisterImageInRegion(regionId, image.getId()); } } }
protected Set<RunningInstance> createNodesInRegionAndZone( String region, String zone, String group, int count, Template template, RunInstancesOptions instanceOptions) { int countStarted = 0; int tries = 0; Set<RunningInstance> started = ImmutableSet.<RunningInstance>of(); while (countStarted < count && tries++ < count) { if (logger.isDebugEnabled()) logger.debug( ">> running %d instance region(%s) zone(%s) ami(%s) params(%s)", count - countStarted, region, zone, template.getImage().getProviderId(), instanceOptions.buildFormParameters()); started = ImmutableSet.copyOf( concat( started, client .getInstanceServices() .runInstancesInRegion( region, zone, template.getImage().getProviderId(), 1, count - countStarted, instanceOptions))); countStarted = size(started); if (countStarted < count) logger.debug( ">> not enough instances (%d/%d) started, attempting again", countStarted, count); } return started; }
public Image getImageInRegion(String region, String id) { return getOnlyElement( client .getAMIServices() .describeImagesInRegion(region, DescribeImagesOptions.Builder.imageIds(id))); }