List of usage examples for com.amazonaws.services.ec2.model DescribeVolumesRequest DescribeVolumesRequest
public DescribeVolumesRequest()
From source file:HW1.java
License:Open Source License
public static String getVolumeState(String volumeId) { DescribeVolumesRequest dv = new DescribeVolumesRequest(); List<String> v_list = new ArrayList<String>(); v_list.add(volumeId);//from www . ja v a 2 s . co m dv.setVolumeIds(v_list); DescribeVolumesResult dr = ec2.describeVolumes(dv); String state = dr.getVolumes().get(0).getState(); return state; }
From source file:com.cloudera.director.aws.ec2.ebs.EBSAllocator.java
License:Apache License
/** * Waits for the volumes in a list of {@code InstanceEbsVolumes} to reach an available state. * Returns an updated list of {@code InstanceEbsVolumes} with the volumes that became * available marked as AVAILABLE and volumes that failed or timed out marked as FAILED. * * @param createdInstanceVolumes list of instances with their created ebs volumes * @return updated list of instances EBS volumes */// ww w .ja v a2s .c om public List<InstanceEbsVolumes> waitUntilVolumesAvailable(List<InstanceEbsVolumes> createdInstanceVolumes) throws InterruptedException { Set<String> volumesToCheck = getAllVolumeIdsWithStatus(createdInstanceVolumes, InstanceEbsVolumes.Status.CREATED); int numRequestedVolumes = volumesToCheck.size(); Set<String> volumesAvailable = Sets.newHashSetWithExpectedSize(numRequestedVolumes); if (numRequestedVolumes > 0) { LOG.info("Waiting for a maximum of {} seconds for volumes to become available", availableTimeoutSeconds); Stopwatch watch = Stopwatch.createStarted(); while (watch.elapsed(TimeUnit.SECONDS) < availableTimeoutSeconds) { DescribeVolumesRequest volumeRequest = new DescribeVolumesRequest().withVolumeIds(volumesToCheck); try { List<Volume> volumes = client.describeVolumes(volumeRequest).getVolumes(); for (Volume volume : volumes) { String id = volume.getVolumeId(); VolumeState state = VolumeState.fromValue(volume.getState()); switch (state) { case Creating: break; case Available: volumesToCheck.remove(id); volumesAvailable.add(id); break; case Error: // TODO log why the volume failed which may need a separate api call volumesToCheck.remove(id); break; default: String err = String .format("A requested volume went into an unexpected state %s while waiting " + "for volume to become available", state); throw new IllegalStateException(String.format(err, state)); } } if (volumesToCheck.isEmpty()) { break; } } catch (AmazonServiceException ex) { // ignore exception when volume isn't found, newly created volumes may not be found right away if (ex.getErrorCode().equals("InvalidVolume.NotFound")) { LOG.info("Requested volume(s) not yet found"); } else { throw AWSExceptions.propagate(ex); } } LOG.info("Waiting on {} out of {} volumes to reach a final state, next check in {} seconds", volumesToCheck.size(), numRequestedVolumes, WAIT_UNTIL_AVAILABLE_INTERVAL_SECONDS); TimeUnit.SECONDS.sleep(WAIT_UNTIL_AVAILABLE_INTERVAL_SECONDS); } if (volumesToCheck.size() > 0) { LOG.error( "Timed out while waiting for volumes to be created, {} out of {} volumes became available", volumesAvailable.size(), numRequestedVolumes); } } else { LOG.info("Skipping wait for availability because no EBS volumes were created"); } // Update the status of each volume to AVAILABLE or FAILED based on the result List<InstanceEbsVolumes> updated = Lists.newArrayList(); for (InstanceEbsVolumes instanceEbsVolumes : createdInstanceVolumes) { Map<String, InstanceEbsVolumes.Status> updatedVolumes = Maps.newHashMap(); for (String volumeId : instanceEbsVolumes.getVolumeStatuses().keySet()) { InstanceEbsVolumes.Status updatedStatus = volumesAvailable.contains(volumeId) ? InstanceEbsVolumes.Status.AVAILABLE : InstanceEbsVolumes.Status.FAILED; updatedVolumes.put(volumeId, updatedStatus); } updated.add(new InstanceEbsVolumes(instanceEbsVolumes.getVirtualInstanceId(), instanceEbsVolumes.getEc2InstanceId(), updatedVolumes)); } return updated; }
From source file:com.cloudera.director.aws.ec2.ebs.EBSAllocator.java
License:Apache License
/** * Wait for the specified list of volumes to be attached within a timeout. * * @param volumeIds collection of volume ids to wait for * @return collection of volumes that were successfully attached * @throws InterruptedException if the operation is interrupted *///from w w w . java 2s .c o m private Collection<String> waitUntilVolumesAttached(Collection<String> volumeIds) throws InterruptedException { checkNotNull(volumeIds); if (volumeIds.isEmpty()) { LOG.info("No volumes are being attached, skipping wait"); return Collections.emptySet(); } Set<String> unattachedVolumes = Sets.newHashSet(volumeIds); Set<String> attachedVolumes = Sets.newHashSet(); LOG.info("Waiting for a maximum of {} seconds for volumes to be attached", attachTimeoutSeconds); Stopwatch watch = Stopwatch.createStarted(); while (watch.elapsed(TimeUnit.SECONDS) < attachTimeoutSeconds) { DescribeVolumesRequest volumeRequest = new DescribeVolumesRequest().withVolumeIds(unattachedVolumes); List<Volume> volumes = client.describeVolumes(volumeRequest).getVolumes(); for (Volume volume : volumes) { VolumeAttachment attachment = Iterables.getOnlyElement(volume.getAttachments()); VolumeAttachmentState state = VolumeAttachmentState.fromValue(attachment.getState()); if (state == VolumeAttachmentState.Attached) { unattachedVolumes.remove(volume.getVolumeId()); attachedVolumes.add(volume.getVolumeId()); } } if (unattachedVolumes.isEmpty()) { return attachedVolumes; } LOG.info("Waiting on {} out of {} volumes to be attached, next check in {} seconds", unattachedVolumes.size(), volumeIds.size(), WAIT_UNTIL_ATTACHED_INTERVAL_SECONDS); TimeUnit.SECONDS.sleep(WAIT_UNTIL_ATTACHED_INTERVAL_SECONDS); } LOG.error("Timed out while waiting for all volumes to be attached, {} out of {} volumes were attached", attachedVolumes.size(), volumeIds.size()); return attachedVolumes; }
From source file:com.cloudera.director.aws.ec2.EC2Provider.java
License:Apache License
/** * Get EBS volumes attached to the specified virtual instance id. * * @return list of ebs volumes/* w ww .j a v a2 s .c o m*/ */ @VisibleForTesting List<Volume> getVolumes(String virtualInstanceId) { String ec2InstanceId = getOnlyElement( getEC2InstanceIdsByVirtualInstanceId(Collections.singletonList(virtualInstanceId)).values()); DescribeInstanceAttributeResult results = client .describeInstanceAttribute(new DescribeInstanceAttributeRequest().withInstanceId(ec2InstanceId) .withAttribute(InstanceAttributeName.BlockDeviceMapping)); List<InstanceBlockDeviceMapping> blockDeviceMappings = results.getInstanceAttribute() .getBlockDeviceMappings(); List<String> volumeIds = Lists.newArrayList(); for (InstanceBlockDeviceMapping mapping : blockDeviceMappings) { volumeIds.add(mapping.getEbs().getVolumeId()); } DescribeVolumesResult volumeResults = client .describeVolumes(new DescribeVolumesRequest().withVolumeIds(volumeIds)); return volumeResults.getVolumes(); }
From source file:com.dowdandassociates.gentoo.bootstrap.AbstractBootstrapInstanceInformationProvider.java
License:Apache License
protected void attachVolume(Optional<Instance> instance, Optional<Volume> volume) { if (instance.isPresent() && volume.isPresent()) { AttachVolumeResult result = ec2Client.attachVolume(new AttachVolumeRequest() .withInstanceId(instance.get().getInstanceId()).withVolumeId(volume.get().getVolumeId()) .withDevice(blockDeviceInformation.getSDevice())); try {// w w w. j av a 2 s .c om DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest() .withVolumeIds(volume.get().getVolumeId()); String instanceId = instance.get().getInstanceId(); boolean waiting = true; do { log.info("Sleeping for " + sleep.get() + " ms"); Thread.sleep(sleep.get()); DescribeVolumesResult describeVolumesResult = ec2Client.describeVolumes(describeVolumesRequest); if (describeVolumesResult.getVolumes().isEmpty()) { return; } Volume bootstrapVolume = describeVolumesResult.getVolumes().get(0); List<VolumeAttachment> attachments = bootstrapVolume.getAttachments(); for (VolumeAttachment attachment : attachments) { if (!instanceId.equals(attachment.getInstanceId())) { continue; } String attachmentState = attachment.getState(); log.info("Attachment state = " + attachmentState); if ("attaching".equals(attachmentState)) { break; } if (!"attached".equals(attachmentState)) { return; } waiting = false; break; } } while (waiting); } catch (InterruptedException e) { } } }
From source file:com.dowdandassociates.gentoo.bootstrap.DefaultTestSnapshotProvider.java
License:Apache License
public Optional<Snapshot> get() { BootstrapInstanceInformation instanceInfo = bootstrapResultInformation.getInstanceInfo(); Optional<Instance> instance = instanceInfo.getInstance(); Optional<Volume> volume = instanceInfo.getVolume(); Optional<Integer> exitStatus = bootstrapResultInformation.getExitStatus(); if (!instance.isPresent()) { log.info("Instance is absent"); return Optional.absent(); }/*w ww . j a v a2s . co m*/ String instanceId = instance.get().getInstanceId(); TerminateInstancesResult terminateInstancesResult = ec2Client .terminateInstances(new TerminateInstancesRequest().withInstanceIds(instanceId)); if (!volume.isPresent()) { log.info("Volume is absent"); return Optional.absent(); } String volumeId = volume.get().getVolumeId(); DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() .withInstanceIds(instanceId); DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest().withVolumeIds(volumeId); try { while (true) { log.info("Sleeping for " + instanceSleep.get() + " ms"); Thread.sleep(instanceSleep.get()); DescribeInstancesResult describeInstancesResult = ec2Client .describeInstances(describeInstancesRequest); String state = describeInstancesResult.getReservations().get(0).getInstances().get(0).getState() .getName(); log.info("Instance State = " + state); if ("terminated".equals(state)) { break; } } CreateSnapshotResult createSnapshotResult = ec2Client .createSnapshot(new CreateSnapshotRequest().withVolumeId(volumeId)); log.info("SnapshotId = " + createSnapshotResult.getSnapshot().getSnapshotId()); DescribeSnapshotsRequest describeSnapshotsRequest = new DescribeSnapshotsRequest() .withSnapshotIds(createSnapshotResult.getSnapshot().getSnapshotId()); Snapshot snapshot; while (true) { log.info("Sleeping for " + snapshotSleep.get() + " ms"); Thread.sleep(snapshotSleep.get()); DescribeSnapshotsResult describeSnapshotsResult = ec2Client .describeSnapshots(describeSnapshotsRequest); String state = describeSnapshotsResult.getSnapshots().get(0).getState(); log.info("Snapshot State = " + state); if ("error".equals(state)) { return Optional.absent(); } if ("completed".equals(state)) { snapshot = describeSnapshotsResult.getSnapshots().get(0); break; } } ec2Client.deleteVolume(new DeleteVolumeRequest().withVolumeId(volumeId)); if (!exitStatus.isPresent()) { log.info("Exit status is not present"); return Optional.absent(); } log.info("exit status = " + exitStatus.get()); if (0 != exitStatus.get()) { return Optional.absent(); } return Optional.fromNullable(snapshot); } catch (InterruptedException e) { return Optional.absent(); } }
From source file:com.dowdandassociates.gentoo.bootstrap.EbsOnDemandBootstrapInstanceInformationProvider.java
License:Apache License
@Override protected Optional<Volume> generateVolume(Optional<Instance> instance) { if (!instance.isPresent()) { return Optional.absent(); }/*from ww w . ja v a2s . co m*/ log.info("AvailabilityZone=" + instance.get().getPlacement().getAvailabilityZone()); log.info("Size=" + volumeSize.get().toString()); CreateVolumeResult createVolumeResult = getEc2Client().createVolume( new CreateVolumeRequest().withAvailabilityZone(instance.get().getPlacement().getAvailabilityZone()) .withSize(volumeSize.get())); log.info("volume id = " + createVolumeResult.getVolume().getVolumeId()); DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest() .withVolumeIds(createVolumeResult.getVolume().getVolumeId()); try { while (true) { log.info("Sleeping for " + sleep.get().toString() + " ms"); Thread.sleep(sleep.get()); DescribeVolumesResult describeVolumesResult = getEc2Client() .describeVolumes(describeVolumesRequest); Volume volume = describeVolumesResult.getVolumes().get(0); String state = volume.getState(); log.info("volume state = " + state); if ("creating".equals(state)) { continue; } if (!"available".equals(state)) { return Optional.absent(); } return Optional.fromNullable(volume); } } catch (InterruptedException e) { return Optional.absent(); } }
From source file:com.dowdandassociates.gentoo.bootstrap.SimpleBootstrapInstanceInformationProvider.java
License:Apache License
@Override protected Optional<Volume> generateVolume(Optional<Instance> instance) { log.info("Get Bootstrap Volume"); if (null == volumeId.get()) { return Optional.absent(); }//from w ww. j ava 2 s. com DescribeVolumesResult result = getEc2Client().describeVolumes(new DescribeVolumesRequest() .withFilters(new Filter().withName("volume-id").withValues(volumeId.get()))); if (result.getVolumes().isEmpty()) { return Optional.absent(); } return Optional.fromNullable(result.getVolumes().get(0)); }
From source file:com.dowdandassociates.gentoo.bootstrap.SnapshotOnDemandBootstrapInstanceInformationProvider.java
License:Apache License
@Override protected Optional<Volume> generateVolume(Optional<Instance> instance) { if (!instance.isPresent()) { log.error("Instance is not present"); return Optional.absent(); }//from w w w .j av a 2 s . c om if (null == snapshotId.get()) { log.error("SnapshotId not set"); return Optional.absent(); } log.info("AvailabilityZone=" + instance.get().getPlacement().getAvailabilityZone()); log.info("SnapshotId = " + snapshotId.get().toString()); CreateVolumeResult createVolumeResult = getEc2Client().createVolume( new CreateVolumeRequest().withAvailabilityZone(instance.get().getPlacement().getAvailabilityZone()) .withSnapshotId(snapshotId.get())); DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest() .withVolumeIds(createVolumeResult.getVolume().getVolumeId()); try { while (true) { log.info("Sleeping for " + sleep.get().toString() + " ms"); Thread.sleep(sleep.get()); DescribeVolumesResult describeVolumesResult = getEc2Client() .describeVolumes(describeVolumesRequest); Volume volume = describeVolumesResult.getVolumes().get(0); String state = volume.getState(); log.info("volume state = " + state); if ("creating".equals(state)) { continue; } if (!"available".equals(state)) { return Optional.absent(); } return Optional.fromNullable(volume); } } catch (InterruptedException e) { return Optional.absent(); } }
From source file:com.liferay.amazontools.AMICleaner.java
License:Open Source License
protected void deleteAvailableVolumes() { DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest(); Filter filter = new Filter(); filter.setName("status"); filter.withValues("available"); describeVolumesRequest.withFilters(filter); DescribeVolumesResult describeVolumesResult = amazonEC2Client.describeVolumes(describeVolumesRequest); List<Volume> volumes = describeVolumesResult.getVolumes(); for (int i = 0; i < volumes.size(); i++) { DeleteVolumeRequest deleteVolumeRequest = new DeleteVolumeRequest(); Volume volume = volumes.get(i);//from w w w . ja v a 2 s . c o m deleteVolumeRequest.setVolumeId(volume.getVolumeId()); amazonEC2Client.deleteVolume(deleteVolumeRequest); } }