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

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

Introduction

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

Prototype


public EbsInstanceBlockDevice getEbs() 

Source Link

Document

Parameters used to automatically set up EBS volumes when the instance is launched.

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
 *//*  w ww . j  ava  2 s  .  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.cloudera.director.aws.ec2.EC2Provider.java

License:Apache License

/**
 * Get EBS volumes attached to the specified virtual instance id.
 *
 * @return list of ebs volumes//from   ww  w.  j a  va2s  .co  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.cloudera.director.aws.ec2.EC2Provider.java

License:Apache License

/**
 * Tags an EC2 instance. Expects that the instance already exists or is in the process of
 * being created. This may also tag EBS volumes depending on template configurations.
 *
 * @param template          the instance template
 * @param userDefinedTags   the user-defined tags
 * @param virtualInstanceId the virtual instance id
 * @param ec2InstanceId     the EC2 instance id
 * @return true if the instance was successfully tagged, false otherwise
 * @throws InterruptedException if the operation is interrupted
 *//*from w w  w . j  av a 2s  .  c o  m*/
private boolean tagInstance(EC2InstanceTemplate template, List<Tag> userDefinedTags, String virtualInstanceId,
        String ec2InstanceId) throws InterruptedException {
    LOG.info(">> Tagging instance {} / {}", ec2InstanceId, virtualInstanceId);
    List<Tag> tags = Lists.newArrayList(
            new Tag(ResourceTags.RESOURCE_NAME.getTagKey(),
                    String.format("%s-%s", template.getInstanceNamePrefix(), virtualInstanceId)),
            new Tag(ResourceTags.CLOUDERA_DIRECTOR_ID.getTagKey(), virtualInstanceId),
            new Tag(ResourceTags.CLOUDERA_DIRECTOR_TEMPLATE_NAME.getTagKey(), template.getName()));
    tags.addAll(userDefinedTags);

    // Wait for the instance to be started. If it is terminating, skip tagging.
    if (!waitUntilInstanceHasStarted(ec2InstanceId)) {
        return false;
    }

    client.createTags(new CreateTagsRequest().withTags(tags).withResources(ec2InstanceId));

    // Tag EBS volumes if they were part of instance launch request
    if (EBSAllocationStrategy.get(template) == EBSAllocationStrategy.AS_INSTANCE_REQUEST) {
        DescribeInstancesResult result = client.describeInstances(
                new DescribeInstancesRequest().withInstanceIds(Collections.singletonList(ec2InstanceId)));
        List<InstanceBlockDeviceMapping> instanceBlockDeviceMappings = getOnlyElement(
                getOnlyElement(result.getReservations()).getInstances()).getBlockDeviceMappings();
        for (InstanceBlockDeviceMapping instanceBlockDeviceMapping : instanceBlockDeviceMappings) {
            String volumeId = instanceBlockDeviceMapping.getEbs().getVolumeId();
            ebsAllocator.tagVolume(template, userDefinedTags, virtualInstanceId, volumeId);
        }
    }

    return true;
}

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);/*from   w ww.  jav  a  2 s. co  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();

    /*//from w  w  w . j  a v  a2s .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   www  . j  a va2  s .c o 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:eu.optimis.monitoring.amazoncollector.MeasurementsHelper.java

License:Apache License

public List<Measurement> getMeasurements() {
    AmazonEC2 ec2 = getAmazonEC2Client();
    List<Measurement> measurements = new LinkedList<Measurement>();

    List<Filter> filters = new LinkedList<Filter>();
    List<String> tags = new LinkedList<String>();
    tags.add(SERVICE_ID_TAG);/*  w w w.j a v a 2  s .c  om*/
    Filter f = new Filter("tag-key", tags);
    filters.add(f);
    DescribeInstancesRequest req = new DescribeInstancesRequest();
    req.setFilters(filters);

    DescribeInstancesResult res;
    try {
        res = ec2.describeInstances(req);
    } catch (AmazonServiceException se) {
        printServiceException(se);
        throw new RuntimeException("Exception while trying to get information about running instances");
    }
    List<Instance> instances = new ArrayList<Instance>();
    for (Reservation r : res.getReservations()) {
        instances.addAll(r.getInstances());
    }

    for (Instance i : instances) {
        String instance_id = i.getInstanceId();
        InstanceType type = InstanceType.fromValue(i.getInstanceType());
        String ami_id = i.getImageId();
        String architecture = i.getArchitecture();
        String state = i.getState().getName();

        List<String> volume_ids = new LinkedList<String>();
        for (InstanceBlockDeviceMapping mapping : i.getBlockDeviceMappings()) {
            volume_ids.add(mapping.getEbs().getVolumeId());
        }

        String service_id = "";
        List<Tag> itags = i.getTags();
        for (Tag t : itags) {
            if (t.getKey().equals(SERVICE_ID_TAG)) {
                service_id = t.getValue();
                break;
            }
        }

        try {
            measurements.add(
                    new Measurement("machine_type", architecture, "", new Date(), instance_id, service_id));
            measurements.add(new Measurement("vm_status", state, "", new Date(), instance_id, service_id));

            Measurement m_mem = getTotalMemory(type, instance_id, service_id);
            Measurement m_cpu = getCPUSpeed(type, instance_id, service_id);
            Measurement m_cores = getNumVCores(type, instance_id, service_id);
            Measurement m_os = getOSRelease(ami_id, instance_id, service_id);
            Measurement m_disk = getTotalDiskSize(volume_ids, instance_id, service_id);
            Measurement m_memused = getCWMeasurement("MemoryUsed", "mem_used", true, instance_id, service_id);
            Measurement m_cpuutil = getCWMeasurement("CPUUtilization", "cpu_used", false, instance_id,
                    service_id);
            Measurement m_nout = getCWMeasurement("NetworkOut", "network_out", false, instance_id, service_id);
            Measurement m_nin = getCWMeasurement("NetworkIn", "network_in", false, instance_id, service_id);

            if (m_mem != null)
                measurements.add(m_mem);
            if (m_cpu != null)
                measurements.add(m_cpu);
            if (m_cores != null)
                measurements.add(m_cores);
            if (m_os != null)
                measurements.add(m_os);
            if (m_disk != null)
                measurements.add(m_disk);
            if (m_memused != null)
                measurements.add(m_memused);
            if (m_cpuutil != null)
                measurements.add(m_cpuutil);
            if (m_nout != null)
                measurements.add(m_nout);
            if (m_nin != null)
                measurements.add(m_nin);

        } catch (AmazonServiceException se) {
            printServiceException(se);
            throw new RuntimeException("Exception while trying to retrieve some metrics");
        }
    }

    return measurements;
}

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;/*ww  w . j  a v a2 s .  c  o m*/
    }

    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  ww  .ja  va  2 s .  c o  m*/
 * @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   ww  w  . java2  s  . c  o m
}