Example usage for com.amazonaws.services.ec2.model Image getRootDeviceType

List of usage examples for com.amazonaws.services.ec2.model Image getRootDeviceType

Introduction

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

Prototype


public String getRootDeviceType() 

Source Link

Document

The type of root device used by the AMI.

Usage

From source file:com.cloudera.director.aws.ec2.EC2InstanceTemplateConfigurationValidator.java

License:Apache License

/**
 * Validates the configured AMI.// w  w w  .  j  av a2s .  c  om
 *
 * @param client              the EC2 client
 * @param configuration       the configuration to be validated
 * @param accumulator         the exception condition accumulator
 * @param localizationContext the localization context
 */
@VisibleForTesting
@SuppressWarnings("PMD.CollapsibleIfStatements")
void checkImage(AmazonEC2Client client, Configured configuration,
        PluginExceptionConditionAccumulator accumulator, LocalizationContext localizationContext) {

    String imageName = configuration.getConfigurationValue(IMAGE, localizationContext);
    String type = configuration.getConfigurationValue(TYPE, localizationContext);

    int conditionCount = accumulator.getConditionsByKey().size();

    if (!imageName.startsWith("ami-")) {
        addError(accumulator, IMAGE, localizationContext, null, INVALID_AMI_NAME_MSG, imageName);
        return;
    }

    LOG.info(">> Describing AMI '{}'", imageName);
    DescribeImagesResult result = null;
    try {
        result = client.describeImages(new DescribeImagesRequest().withImageIds(imageName));
        checkCount(accumulator, IMAGE, localizationContext, imageName, result.getImages());
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().startsWith(INVALID_AMI_ID)) {
            addError(accumulator, IMAGE, localizationContext, null, INVALID_AMI_MSG, imageName);
        } else {
            throw Throwables.propagate(e);
        }
    }

    if ((result == null) || (accumulator.getConditionsByKey().size() > conditionCount)) {
        return;
    }

    Image image = Iterables.getOnlyElement(result.getImages());
    if (!SIXTY_FOUR_BIT_ARCHITECTURE.equals(image.getArchitecture())) {
        addError(accumulator, IMAGE, localizationContext, null, INVALID_AMI_ARCHITECTURE_MSG, imageName,
                image.getArchitecture());
    }

    AWSFilters imageFilters = templateFilters.getSubfilters(IMAGE.unwrap().getConfigKey());
    boolean useSpotInstances = Boolean
            .parseBoolean(configuration.getConfigurationValue(USE_SPOT_INSTANCES, localizationContext));

    String ownerId = image.getOwnerId();
    if (ownerId != null) {
        String blacklistValue = imageFilters.getBlacklistValue(IMAGE_OWNER_ID_BLACKLIST_KEY,
                ownerId.toLowerCase());
        if (blacklistValue != null) {
            addError(accumulator, IMAGE, localizationContext, null, INVALID_AMI_OWNER_MSG, imageName, ownerId,
                    blacklistValue);
        } else {
            if (useSpotInstances) {
                blacklistValue = imageFilters.getBlacklistValue(IMAGE_SPOT_OWNER_ID_BLACKLIST_KEY,
                        ownerId.toLowerCase());
                if (blacklistValue != null) {
                    addError(accumulator, IMAGE, localizationContext, null, INVALID_AMI_OWNER_SPOT_MSG,
                            imageName, ownerId, blacklistValue);
                }
            }
        }
    }

    String platform = image.getPlatform();
    if (platform != null) {
        String blacklistValue = imageFilters.getBlacklistValue(IMAGE_PLATFORM_BLACKLIST_KEY,
                platform.toLowerCase());
        if (blacklistValue != null) {
            addError(accumulator, IMAGE, localizationContext, null, INVALID_AMI_PLATFORM_MSG, imageName,
                    platform, blacklistValue);
        } else {
            if (useSpotInstances) {
                blacklistValue = imageFilters.getBlacklistValue(IMAGE_SPOT_PLATFORM_BLACKLIST_KEY,
                        platform.toLowerCase());
                if (blacklistValue != null) {
                    addError(accumulator, IMAGE, localizationContext, null, INVALID_AMI_PLATFORM_SPOT_MSG,
                            imageName, platform, blacklistValue);
                }
            }
        }
    }

    if (!AVAILABLE_STATE.equals(image.getState())) {
        addError(accumulator, IMAGE, localizationContext, null, INVALID_AMI_STATE_MSG, imageName,
                image.getState());
    }

    if (!provider.getVirtualizationMappings().apply(image.getVirtualizationType()).contains(type)) {
        addError(accumulator, IMAGE, localizationContext, null, INVALID_AMI_INSTANCE_TYPE_COMPATIBILITY_MSG,
                type, image.getVirtualizationType(), imageName);
    }

    if (!ROOT_DEVICE_TYPE.equals(image.getRootDeviceType())) {
        addError(accumulator, IMAGE, localizationContext, null, INVALID_AMI_ROOT_DEVICE_TYPE_MSG, imageName,
                image.getRootDeviceType());
    }
}

From source file:com.cloudera.director.aws.ec2.EC2Provider.java

License:Apache License

/**
 * Creates block device mappings based on the specified instance template.
 *
 * @param template the instance template
 * @return the block device mappings//from   w  w  w .j av  a 2s  .  c o  m
 */
private List<BlockDeviceMapping> getBlockDeviceMappings(EC2InstanceTemplate template) {

    // Query the AMI about the root device name & mapping information
    DescribeImagesResult result = client
            .describeImages(new DescribeImagesRequest().withImageIds(template.getImage()));
    if (result.getImages().isEmpty()) {
        throw new IllegalArgumentException("The description for image " + template.getImage() + " is empty");
    }
    Image templateImage = result.getImages().get(0);
    String rootDeviceType = templateImage.getRootDeviceType();
    if (!DEVICE_TYPE_EBS.equals(rootDeviceType)) {
        throw new IllegalArgumentException("The root device for image " + template.getImage() + " must be \""
                + DEVICE_TYPE_EBS + "\", found: " + rootDeviceType);
    }
    List<BlockDeviceMapping> originalMappings = templateImage.getBlockDeviceMappings();
    LOG.info(">> Original image block device mappings: {}", originalMappings);
    if (originalMappings.isEmpty()) {
        throw new IllegalArgumentException(
                "The image " + template.getImage() + " has no block device mappings");
    }
    BlockDeviceMapping rootDevice = selectRootDevice(originalMappings, templateImage.getRootDeviceName());
    if (rootDevice == null) {
        throw new IllegalArgumentException("Could not determine root device for image " + template.getImage()
                + " based on root device name " + templateImage.getRootDeviceName());
    }

    // The encrypted property was added to the block device mapping in version 1.8 of the SDK.
    // It is a Boolean, but defaults to false instead of being unset, so we set it to null here.
    rootDevice.getEbs().setEncrypted(null);
    rootDevice.getEbs().setVolumeSize(template.getRootVolumeSizeGB());
    rootDevice.getEbs().setVolumeType(template.getRootVolumeType());
    rootDevice.getEbs().setDeleteOnTermination(true);

    List<BlockDeviceMapping> deviceMappings = Lists.newArrayList(rootDevice);

    int ebsVolumeCount = template.getEbsVolumeCount();

    EBSAllocationStrategy ebsAllocationStrategy = EBSAllocationStrategy.get(template);

    switch (ebsAllocationStrategy) {
    case NO_EBS_VOLUMES:
        // The volumes within an instance should be homogeneous. So we only add
        // instance store volumes when additional EBS volumes aren't mounted.
        deviceMappings.addAll(ephemeralDeviceMappings.apply(template.getType()));
        break;
    case AS_INSTANCE_REQUEST:
        LOG.info("EBS volumes will be allocated as part of instance launch request");
        List<BlockDeviceMapping> ebsDeviceMappings = getEbsBlockDeviceMapping(ebsVolumeCount,
                template.getEbsVolumeType(), template.getEbsVolumeSizeGiB(), template.isEnableEbsEncryption());
        deviceMappings.addAll(ebsDeviceMappings);
        break;
    case AS_SEPARATE_REQUESTS:
        LOG.info("EBS volumes will be separately allocated after instance launch request");
        break;
    default:
        throw new IllegalStateException("Invalid EBS allocation strategy " + ebsAllocationStrategy);
    }

    LOG.info(">> Block device mappings: {}", deviceMappings);
    return deviceMappings;
}

From source file:com.zotoh.cloudapi.aws.AMImage.java

License:Open Source License

private MachineImage toMI(Image i) {
    MachineImage m = null;/*  w w  w  .  j  av a  2  s.c  o  m*/
    if (i != null) {
        m = new MachineImage();
        m.setProviderRegionId(_svc.getCloud().getContext().getRegionId());
        m.setArchitecture(toArch(i.getArchitecture()));
        m.setCurrentState(toImageState(i.getState()));

        m.setProviderMachineImageId(i.getImageId());
        m.setName(i.getName());
        m.setProviderOwnerId(i.getOwnerId());
        m.setSoftware("");
        m.setType(toImageType(i.getRootDeviceType()));

        m.addTag("manifest-location", nsb(i.getImageLocation()));
        m.addTag("hypervisor", nsb(i.getHypervisor()));
        m.addTag("alias", nsb(i.getImageOwnerAlias()));
        m.addTag("kernel", nsb(i.getKernelId()));
        m.addTag("public", i.getPublic() ? "true" : "false");
        m.addTag("ramdisk", nsb(i.getRamdiskId()));
        m.addTag("root-dev-name", nsb(i.getRootDeviceName()));
        m.addTag("state-reason", nsb(i.getStateReason()));
        m.addTag("virtualization-type", nsb(i.getVirtualizationType()));

        m.setDescription(i.getDescription());
        m.setPlatform(nsb(i.getPlatform()).toLowerCase().indexOf("windows") >= 0 ? Platform.WINDOWS
                : Platform.UBUNTU);
    }

    return m;
}

From source file:jp.aws.test.ec2.EC2Instance.java

License:Apache License

/**
 * AMI?//from w w w .j  a v a 2s.  co m
 *
 * @param HashMap
 *            <String,String> filterMap : ID: ownerid => self, amazon,
 *            redhat, 00000000  root-device-type => ebs, instance-store
 *            architecture => i386, x86_64 name => amzn-ami*
 * @return
 * @throws Exception
 * @note - ????????
 */
public List<AMIItem> ami_list(HashMap<String, String> filterMap) {

    List<AMIItem> amiItemList = new ArrayList<AMIItem>();

    // 
    this.clientManager.changeRegion();

    // AMI??
    DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest();
    List<String> ownersList = new ArrayList<String>();

    // OwnerID?(self, amazon, redhat, 00000000)
    ownersList.add(filterMap.get("ownerid"));
    describeImagesRequest.setOwners(ownersList);

    // AMI????????Filter?
    ArrayList<Filter> filters = new ArrayList<Filter>();

    Filter filter = new Filter();
    filter.setName("image-type");

    // machine
    List<String> valueList = new ArrayList<String>();
    valueList.add("machine");
    filter.setValues(valueList);

    // Filter
    filters.add(filter);

    // 
    for (Iterator<String> it = filterMap.keySet().iterator(); it.hasNext();) {
        String key = it.next();
        Log.d("ami_list", String.format("key:%s, value:%s", key, filterMap.get(key)));
        if (key.toLowerCase().equals("ownerid"))
            continue;
        filters.add(new Filter().withName(key).withValues(filterMap.get(key)));
    }

    // Filter
    describeImagesRequest.setFilters(filters);

    // ????AMI??
    DescribeImagesResult describeImagesResult = this.clientManager.ec2().describeImages(describeImagesRequest);

    // AMI?List?(Image?com.amazonaws.services.ec2.model?Image???)
    List<Image> amiList = describeImagesResult.getImages();

    // ?AMI??
    for (Image image : amiList) {
        // http://docs.amazonwebservices.com/AWSAndroidSDK/latest/javadoc/com/amazonaws/services/ec2/model/Image.html
        AMIItem amiItem = new AMIItem();

        StringBuilder builder = new StringBuilder();

        amiItem.imageId = image.getImageId();
        amiItem.imageType = image.getImageType();
        amiItem.imageLocation = image.getImageLocation();
        amiItem.name = image.getName();
        amiItem.architecture = image.getArchitecture();
        amiItem.platform = image.getPlatform();
        amiItem.state = image.getState();
        amiItem.ownerId = image.getOwnerId();
        amiItem.rootDeviceType = image.getRootDeviceType();
        amiItem.rootDeviceName = image.getRootDeviceName();
        amiItem.description = image.getDescription();
        builder.setLength(0); // ?
        List<Tag> tags = image.getTags(); // tag
        for (Tag tag : tags) {
            builder.append(tag.getValue());
            builder.append(", ");
        }

        // ?
        amiItemList.add(amiItem);
    }

    return amiItemList;
}