Example usage for com.amazonaws.services.ec2.model InstanceBlockDeviceMapping getDeviceName

List of usage examples for com.amazonaws.services.ec2.model InstanceBlockDeviceMapping getDeviceName

Introduction

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

Prototype


public String getDeviceName() 

Source Link

Document

The device name (for example, /dev/sdh or xvdh).

Usage

From source file:com.cloudera.director.aws.ec2.ebs.EBSAllocator.java

License:Apache License

/**
 * Adds a delete on termination flag to all volumes in an {@code InstanceEbsVolumes} list
 * that have the ATTACHED status. This makes sure that the volumes associated with the
 * instance will be automatically cleaned up upon instance termination.
 *
 * @param instanceEbsVolumesList list of instances along with their associated volumes
 *//*  ww w .  j  a  v a  2s . co  m*/
public void addDeleteOnTerminationFlag(List<InstanceEbsVolumes> instanceEbsVolumesList) {
    Set<String> volumesToFlag = getAllVolumeIdsWithStatus(instanceEbsVolumesList,
            InstanceEbsVolumes.Status.ATTACHED);

    if (!volumesToFlag.isEmpty()) {
        for (InstanceEbsVolumes instanceEbsVolumes : instanceEbsVolumesList) {
            String ec2InstanceId = instanceEbsVolumes.getEc2InstanceId();

            DescribeInstanceAttributeRequest instanceAttributeRequest = new DescribeInstanceAttributeRequest()
                    .withAttribute(InstanceAttributeName.BlockDeviceMapping).withInstanceId(ec2InstanceId);

            List<InstanceBlockDeviceMapping> blockDeviceMappings = client
                    .describeInstanceAttribute(instanceAttributeRequest).getInstanceAttribute()
                    .getBlockDeviceMappings();

            for (InstanceBlockDeviceMapping blockDeviceMapping : blockDeviceMappings) {
                String volumeId = blockDeviceMapping.getEbs().getVolumeId();

                // The block device mapping may have volumes associated with it that were not
                // provisioned by us. We skip marking those volumes for deletion.

                if (!volumesToFlag.contains(volumeId)) {
                    continue;
                }

                InstanceBlockDeviceMappingSpecification updatedSpec = new InstanceBlockDeviceMappingSpecification()
                        .withEbs(new EbsInstanceBlockDeviceSpecification().withDeleteOnTermination(true)
                                .withVolumeId(volumeId))
                        .withDeviceName(blockDeviceMapping.getDeviceName());

                ModifyInstanceAttributeRequest modifyRequest = new ModifyInstanceAttributeRequest()
                        .withBlockDeviceMappings(updatedSpec).withInstanceId(ec2InstanceId);

                client.modifyInstanceAttribute(modifyRequest);
            }
        }
    }
}

From source file:com.netflix.simianarmy.client.aws.AWSClient.java

License:Apache License

@Override
public List<String> listAttachedVolumes(String instanceId, boolean includeRoot) {
    Validate.notEmpty(instanceId);/*w  w w  .  java  2 s  . c o  m*/
    LOGGER.info(String.format("Listing volumes attached to instance %s in region %s.", instanceId, region));
    try {
        List<String> volumeIds = new ArrayList<String>();
        for (Instance instance : describeInstances(instanceId)) {
            String rootDeviceName = instance.getRootDeviceName();

            for (InstanceBlockDeviceMapping ibdm : instance.getBlockDeviceMappings()) {
                EbsInstanceBlockDevice ebs = ibdm.getEbs();
                if (ebs == null) {
                    continue;
                }

                String volumeId = ebs.getVolumeId();
                if (Strings.isNullOrEmpty(volumeId)) {
                    continue;
                }

                if (!includeRoot && rootDeviceName != null && rootDeviceName.equals(ibdm.getDeviceName())) {
                    continue;
                }

                volumeIds.add(volumeId);
            }
        }
        return volumeIds;
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidInstanceID.NotFound")) {
            throw new NotFoundException("AWS instance " + instanceId + " not found", e);
        }
        throw e;
    }
}

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();

    /*// ww w  . ja v  a 2s . c o m
     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:com.vb.aws.services.compute.ec2.EC2UtilsImpl.java

/**
 * This method returns all EBS root volumes.
 * @return /*from w  ww  .  j  a va 2 s.  co  m*/
 */
public List<Volume> getAllEBSRootVolumes() {

    List<Instance> allInstances = getAllInstances();
    List<Volume> allEBSRootVolumes = new ArrayList<>();

    for (Instance instance : allInstances) {

        //We need volumes of type only EBS.
        if (instance.getRootDeviceType().equalsIgnoreCase(DeviceType.Ebs.toString())) {
            String rootDeviceName = instance.getRootDeviceName();
            List<InstanceBlockDeviceMapping> instanceBlockDeviceMappings = instance.getBlockDeviceMappings();
            for (InstanceBlockDeviceMapping instanceBlockDeviceMapping : instanceBlockDeviceMappings) {
                if (instanceBlockDeviceMapping.getDeviceName().equalsIgnoreCase(rootDeviceName)) {
                    String volumeId = instanceBlockDeviceMapping.getEbs().getVolumeId();
                    Volume volume = new Volume().withVolumeId(volumeId);
                    allEBSRootVolumes.add(volume);
                }
            }
        }
    }

    System.out.println("INFO: Number of EBS Root Volumes : " + allEBSRootVolumes.size());
    List<String> volumeIds = allEBSRootVolumes.stream().map(e -> e.getVolumeId()).collect(Collectors.toList());
    System.out.println("INFO: EBS Root Volumes : " + volumeIds);

    return allEBSRootVolumes;
}

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

License:Open Source License

public void createTag(AwsProcessClient awsProcessClient, Long instanceNo) {
    // Eucalyptus??????
    PlatformAws platformAws = awsProcessClient.getPlatformAws();
    if (BooleanUtils.isTrue(platformAws.getEuca())) {
        return;/*from   w  w  w  .  jav a2s  . c  om*/
    }

    Instance instance = instanceDao.read(instanceNo);
    AwsInstance awsInstance = awsInstanceDao.read(instanceNo);
    User user = userDao.read(awsProcessClient.getUserNo());
    Farm farm = farmDao.read(instance.getFarmNo());

    // ??
    List<Tag> tags = new ArrayList<Tag>();
    tags.add(new Tag("Name", instance.getFqdn()));
    tags.add(new Tag("UserName", user.getUsername()));
    tags.add(new Tag("CloudName", farm.getDomainName()));
    tags.add(new Tag("ServerName", instance.getFqdn()));
    awsCommonProcess.createTag(awsProcessClient, awsInstance.getInstanceId(), tags);

    com.amazonaws.services.ec2.model.Instance instance2 = awsCommonProcess.describeInstance(awsProcessClient,
            awsInstance.getInstanceId());

    // EBS??
    for (InstanceBlockDeviceMapping mapping : instance2.getBlockDeviceMappings()) {
        if (mapping.getEbs() == null) {
            continue;
        }

        String deviceName = mapping.getDeviceName();
        if (deviceName.lastIndexOf("/") != -1) {
            deviceName = deviceName.substring(deviceName.lastIndexOf("/") + 1);
        }

        tags = new ArrayList<Tag>();
        tags.add(new Tag("Name", instance.getFqdn() + "_" + deviceName));
        tags.add(new Tag("UserName", user.getUsername()));
        tags.add(new Tag("CloudName", farm.getDomainName()));
        tags.add(new Tag("ServerName", instance.getFqdn()));
        awsCommonProcess.createTag(awsProcessClient, mapping.getEbs().getVolumeId(), tags);
    }
}

From source file:org.occiware.clouddriver.util.InstanceDataFactory.java

License:Apache License

/**
 *
 * @param instance//from w  w  w  . j  a  va2  s  .  c om
 * @return
 */
private static List<InstanceVolumeDO> BuildInstanceVolumeDOs(Instance instance) {
    List<InstanceBlockDeviceMapping> blockDeviceMappings = instance.getBlockDeviceMappings();
    String deviceName;
    InstanceVolumeDO instVolumeDO;
    EbsInstanceBlockDevice ebs;
    List<InstanceVolumeDO> instanceVolumeDOs = new ArrayList<>();
    for (InstanceBlockDeviceMapping blockDeviceMapping : blockDeviceMappings) {
        deviceName = blockDeviceMapping.getDeviceName();
        ebs = blockDeviceMapping.getEbs();

        if (ebs != null) {
            instVolumeDO = new InstanceVolumeDO();
            instVolumeDO.setAttachTime(ebs.getAttachTime());
            instVolumeDO.setDeleteOnTermination(ebs.getDeleteOnTermination());
            instVolumeDO.setStatus(ebs.getStatus());
            instVolumeDO.setVolumeId(ebs.getVolumeId());
            instVolumeDO.setDeviceName(deviceName);
            instanceVolumeDOs.add(instVolumeDO);
        }

    }
    return instanceVolumeDOs;
}

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

License:BSD License

public void writeInstanceDeviceMapping(InstanceBlockDeviceMapping device) throws XMLStreamException {
    startElement("device");
    attribute("name", device.getDeviceName());

    EbsInstanceBlockDevice ebs = device.getEbs();
    attribute("status", ebs.getStatus());
    attribute("volume-id", ebs.getVolumeId());
    attribute("attach-date", Util.formatXSDateTime(ebs.getAttachTime()));
    attribute("delete-on-termination", ebs.getDeleteOnTermination().toString());
    endElement();//from   w  w w  . j  a va 2s .co  m
}