Example usage for com.amazonaws.services.ec2.model VolumeAttachment getState

List of usage examples for com.amazonaws.services.ec2.model VolumeAttachment getState

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model VolumeAttachment getState.

Prototype


public String getState() 

Source Link

Document

The attachment state of the volume.

Usage

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
 *//*  ww  w.  ja va 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.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 {/*from  w w  w. ja  v  a  2  s . com*/
            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.netflix.simianarmy.aws.janitor.VolumeTaggingMonkey.java

License:Apache License

private void tagVolumesWithLatestAttachment(AWSClient awsClient) {
    List<Volume> volumes = awsClient.describeVolumes();
    LOGGER.info(String.format("Trying to tag %d volumes for Janitor Monkey meta data.", volumes.size()));
    Date now = calendar.now().getTime();
    for (Volume volume : volumes) {
        String owner = null, instanceId = null;
        Date lastDetachTime = null;
        List<VolumeAttachment> attachments = volume.getAttachments();
        List<Tag> tags = volume.getTags();

        // The volume can have a special tag is it does not want to be changed/tagged
        // by Janitor monkey.
        if ("donotmark".equals(getTagValue(JanitorMonkey.JANITOR_TAG, tags))) {
            LOGGER.info(// w  ww  .  j  a v  a 2 s  .co m
                    String.format("The volume %s is tagged as not handled by Janitor", volume.getVolumeId()));
            continue;
        }

        Map<String, String> janitorMetadata = parseJanitorTag(tags);
        // finding the instance attached most recently.
        VolumeAttachment latest = null;
        for (VolumeAttachment attachment : attachments) {
            if (latest == null || latest.getAttachTime().before(attachment.getAttachTime())) {
                latest = attachment;
            }
        }
        if (latest != null) {
            instanceId = latest.getInstanceId();
            owner = getOwnerEmail(instanceId, janitorMetadata, tags, awsClient);
        }

        if (latest == null || "detached".equals(latest.getState())) {
            if (janitorMetadata.get(JanitorMonkey.DETACH_TIME_TAG_KEY) == null) {
                // There is no attached instance and the last detached time is not set.
                // Use the current time as the last detached time.
                LOGGER.info(String.format("Setting the last detached time to %s for volume %s", now,
                        volume.getVolumeId()));
                lastDetachTime = now;
            } else {
                LOGGER.debug(String.format("The volume %s was already marked as detached at time %s",
                        volume.getVolumeId(), janitorMetadata.get(JanitorMonkey.DETACH_TIME_TAG_KEY)));
            }
        } else {
            // The volume is currently attached to an instance
            lastDetachTime = null;
        }
        String existingOwner = janitorMetadata.get(JanitorMonkey.OWNER_TAG_KEY);
        if (owner == null && existingOwner != null) {
            // Save the current owner in the tag when we are not able to find a owner.
            owner = existingOwner;
        }
        if (needsUpdate(janitorMetadata, owner, instanceId, lastDetachTime)) {
            Event evt = updateJanitorMetaTag(volume, instanceId, owner, lastDetachTime, awsClient);
            if (evt != null) {
                context().recorder().recordEvent(evt);
            }
        }
    }
}

From source file:org.xmlsh.aws.util.AWSEC2Command.java

License:BSD License

protected void writeAttachment(VolumeAttachment attachment) throws XMLStreamException {
    startElement("attachment");
    attribute("delete-on-termination", AWSUtil.parseBoolean(attachment.getDeleteOnTermination()));
    attribute("attach-date", Util.formatXSDateTime(attachment.getAttachTime()));
    attribute("device", attachment.getDevice());
    attribute("instance-id", attachment.getInstanceId());
    attribute("volume-id", attachment.getVolumeId());
    attribute("state", attachment.getState());
    endElement();/*from www  . ja v  a  2  s.c  om*/
}