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

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

Introduction

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

Prototype


public String getInstanceId() 

Source Link

Document

The ID of the instance.

Usage

From source file:virtualIT.java

License:Open Source License

private void defaultVolumeRequest(int userId) {
    String instanceId = null;/*  w  ww.j  a v  a  2 s. c om*/
    String volumeId;
    DescribeVolumesResult describeVolumeResult = ec2.describeVolumes();
    List<Volume> volumeData = describeVolumeResult.getVolumes();
    for (Volume item : volumeData) {
        volumeId = item.getVolumeId();
        List<VolumeAttachment> volumeAttachment = item.getAttachments();
        for (VolumeAttachment data : volumeAttachment) {
            instanceId = data.getInstanceId();
        }
        //instanceIdList.add(instanceId);
        rootvolumeIdList.add(volumeId);
        mapUserRootVol.put(userId, volumeId);
    }
}

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  .  j ava2  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  w  w  .  j  av 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:com.pearson.eidetic.driver.threads.subthreads.TagChecker.java

private List<Volume> checkVolumes(AmazonEC2Client ec2Client, List<Instance> instances) {
    List<Volume> volumesWithNoTags = new ArrayList();

    /*/*w  w w  . ja  v a  2 s  . com*/
     Here is what we have:
     Instance:
     -instance.id -> String
     -instance.blockdevicemapping -> InstanceBlockDeviceMapping
     -BlockDeviceMapping:
     --BlockDeviceMapping.deviceName -> String == (/dev/xvdk)
     --BlockDeviceMapping.getEbs -> EbsInstanceBlockDevice
     --EbsInstanceBLockDevice:
     ---EbsInstanceBLockDevice.id - String == (volume.id)
            
     For instances
     Set<HashMap<instance.id,HashMap<deviceName,volume.id>>>
            
     Volume:
     -volume.id -> String
     -volume.getAttachments -> volumeAttachments
     -volumeAttachments:
     --getDevice -> String == (/dev/xvdk)
     --getInstanceId -> String == instance.id
            
     For vols
     Set<HashMap<instance.id,HashMap<deviceName,volume.id>>>
                
     If anything remians in the instance set 
            
     */
    List<Volume> volumes = getEideticVolumes(ec2Client);
    //if (volumes.isEmpty()) {
    //return volumesWithNoTags;
    //}

    //Set<HashMap<instance.id,HashMap<deviceName,volume.id>>>
    Set<HashMap<String, HashMap<String, String>>> volumeSet = new HashSet();
    for (Volume volume : volumes) {
        String volumeId = volume.getVolumeId();
        List<VolumeAttachment> attachments = volume.getAttachments();
        for (VolumeAttachment attach : attachments) {
            HashMap<String, HashMap<String, String>> setMember = new HashMap();
            HashMap<String, String> internalHash = new HashMap();

            if (volumeId == null || attach.getDevice() == null || attach.getInstanceId() == null) {
                continue;
            }

            internalHash.put(attach.getDevice(), volumeId);
            setMember.put(attach.getInstanceId(), internalHash);

            volumeSet.add(setMember);
        }
    }

    //Set<HashMap<instance.id,HashMap<deviceName,volume.id>>>
    Set<HashMap<String, HashMap<String, String>>> instanceSet = new HashSet();
    for (Instance instance : instances) {
        String instanceId = instance.getInstanceId();
        List<InstanceBlockDeviceMapping> blocks = instance.getBlockDeviceMappings();

        List<Tag> tags = instance.getTags();

        Tag tagz = null;
        for (Tag t : tags) {
            if (t.getKey().equalsIgnoreCase("Data")) {
                tagz = t;
                break;
            }
        }
        if (tagz == null) {
            continue;
        }

        String[] stringtags = tagz.getValue().split(",");

        for (String tag : stringtags) {

            String deviceName;
            String volumeId;
            for (InstanceBlockDeviceMapping instanceBlockDeviceMapping : blocks) {

                deviceName = instanceBlockDeviceMapping.getDeviceName();

                if (!deviceName.equalsIgnoreCase(tag)) {
                    continue;
                }

                volumeId = instanceBlockDeviceMapping.getEbs().getVolumeId();

                HashMap<String, HashMap<String, String>> setMember = new HashMap();
                HashMap<String, String> internalHash = new HashMap();

                if (volumeId == null | volumeId == null) {
                    continue;
                }

                internalHash.put(deviceName, volumeId);
                setMember.put(instanceId, internalHash);

                instanceSet.add(setMember);
            }
        }
    }
    if (instanceSet.isEmpty()) {
        return volumesWithNoTags;
    }

    //Need to validate
    instanceSet.removeAll(volumeSet);
    if (instanceSet.isEmpty()) {
        return volumesWithNoTags;
    }

    //Instance keys, to get volumeIds
    Set<String> volumeIds = new HashSet();
    for (HashMap<String, HashMap<String, String>> i : instanceSet) {
        Set<String> curSet = i.keySet();
        for (String s : curSet) {
            Set<String> dvns = i.get(s).keySet();
            for (String dvn : dvns) {
                if (!volumeIds.contains(dvn)) {
                    volumeIds.add(i.get(s).get(dvn));
                }
            }
        }
    }

    for (String i : volumeIds) {

        DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest().withVolumeIds(i);
        DescribeVolumesResult describeVolumeResult = EC2ClientMethods.describeVolumes(ec2Client,
                describeVolumesRequest, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_);

        Volume volume = null;
        try {
            volume = describeVolumeResult.getVolumes().get(0);
        } catch (Exception e) {
            logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_
                    + "\",Event\"Error\", Error=\"volume id does not exist\", stacktrace=\"" + e.toString()
                    + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\"");
        }

        volumesWithNoTags.add(volume);
    }

    return volumesWithNoTags;
}

From source file:jp.primecloud.auto.process.aws.AwsVolumeProcess.java

License:Open Source License

public void attachVolume(AwsProcessClient awsProcessClient, Long instanceNo, Long volumeNo) {
    AwsInstance awsInstance = awsInstanceDao.read(instanceNo);
    AwsVolume awsVolume = awsVolumeDao.read(volumeNo);
    String volumeId = awsVolume.getVolumeId();

    //// w  w  w .j a  va 2s . c  om
    Component component = null;
    if (awsVolume.getComponentNo() != null) {
        component = componentDao.read(awsVolume.getComponentNo());
    }
    Instance instance = instanceDao.read(instanceNo);
    processLogger.debug(component, instance, "AwsEbsAttach",
            new Object[] { instance.getInstanceName(), awsVolume.getVolumeId(), awsVolume.getDevice() });

    // ??
    AttachVolumeRequest request = new AttachVolumeRequest();
    request.withVolumeId(volumeId);
    request.withInstanceId(awsInstance.getInstanceId());
    request.withDevice(awsVolume.getDevice());
    AttachVolumeResult result = awsProcessClient.getEc2Client().attachVolume(request);
    VolumeAttachment attachment = result.getAttachment();

    // 
    if (log.isInfoEnabled()) {
        log.info(MessageUtils.getMessage("IPROCESS-100123", volumeId, attachment.getInstanceId()));
    }

    // ?
    awsVolume.setInstanceId(attachment.getInstanceId());
    awsVolumeDao.update(awsVolume);
}

From source file:jp.primecloud.auto.process.aws.AwsVolumeProcess.java

License:Open Source License

public void detachVolume(AwsProcessClient awsProcessClient, Long instanceNo, Long volumeNo) {
    AwsVolume awsVolume = awsVolumeDao.read(volumeNo);
    String volumeId = awsVolume.getVolumeId();

    ////from w  w w  .  j a va  2s  .  co  m
    Component component = null;
    if (awsVolume.getComponentNo() != null) {
        component = componentDao.read(awsVolume.getComponentNo());
    }
    Instance instance = instanceDao.read(instanceNo);
    processLogger.debug(component, instance, "AwsEbsDetach",
            new Object[] { instance.getInstanceName(), awsVolume.getVolumeId(), awsVolume.getDevice() });

    // ??
    DetachVolumeRequest request = new DetachVolumeRequest();
    request.withVolumeId(volumeId);
    request.withInstanceId(awsVolume.getInstanceId());
    request.withDevice(awsVolume.getDevice());
    DetachVolumeResult result = awsProcessClient.getEc2Client().detachVolume(request);
    VolumeAttachment attachment = result.getAttachment();

    // 
    if (log.isInfoEnabled()) {
        log.info(MessageUtils.getMessage("IPROCESS-100125", volumeId, attachment.getInstanceId()));
    }
}

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 ww  w .ja va  2 s . c om*/
}