Search in sources :

Example 21 with Image

use of com.amazonaws.services.ec2.model.Image in project cloudbreak by hortonworks.

the class AwsResourceConnector method getRootDeviceName.

private String getRootDeviceName(AuthenticatedContext ac, CloudStack cloudStack) {
    AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    DescribeImagesResult images = ec2Client.describeImages(new DescribeImagesRequest().withImageIds(cloudStack.getImage().getImageName()));
    if (images.getImages().isEmpty()) {
        throw new CloudConnectorException(String.format("AMI is not available: '%s'.", cloudStack.getImage().getImageName()));
    }
    Image image = images.getImages().get(0);
    if (image == null) {
        throw new CloudConnectorException(String.format("Couldn't describe AMI '%s'.", cloudStack.getImage().getImageName()));
    }
    return image.getRootDeviceName();
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) DescribeImagesResult(com.amazonaws.services.ec2.model.DescribeImagesResult) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) DescribeImagesRequest(com.amazonaws.services.ec2.model.DescribeImagesRequest) Image(com.amazonaws.services.ec2.model.Image)

Example 22 with Image

use of com.amazonaws.services.ec2.model.Image in project TOSCAna by StuPro-TOSCAna.

the class CapabilityMapper method mapOsCapabilityToImageId.

/**
 *     Requests the AWS server for ImageIds with filters which are filled based on the values of the {@link OsCapability}.
 *     <br>
 *     The image with the latest creation date is picked and its imageId returned.
 *
 *     @param osCapability the OsCapability to map
 *     @return a {@link String} that contains a valid ImageId that can be added to the properties of an EC2
 */
public String mapOsCapabilityToImageId(OsCapability osCapability) throws SdkClientException, ParseException, IllegalArgumentException {
    AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).withRegion(awsRegion).build();
    // need to set these
    DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withFilters(new Filter("virtualization-type").withValues("hvm"), new Filter("root-device-type").withValues("ebs")).withOwners(// this is the ownerId of amazon itself
    "099720109477");
    if (osCapability.getType().isPresent() && osCapability.getType().get().equals(OsCapability.Type.WINDOWS)) {
        describeImagesRequest.withFilters(new Filter("platform").withValues("windows"));
    }
    if (osCapability.getDistribution().isPresent()) {
        if (osCapability.getDistribution().get().equals(OsCapability.Distribution.UBUNTU)) {
            // */ubuntu/images/* gets better results than plain *ubuntu*
            describeImagesRequest.withFilters(new Filter("name").withValues("*ubuntu/images/*"));
        } else {
            // just search for the string
            describeImagesRequest.withFilters(new Filter("name").withValues("*" + osCapability.getDistribution().toString() + "*"));
        }
    }
    if (osCapability.getVersion().isPresent()) {
        describeImagesRequest.withFilters(new Filter("name").withValues("*" + osCapability.getVersion().get() + "*"));
    }
    if (osCapability.getArchitecture().isPresent()) {
        if (osCapability.getArchitecture().get().equals(OsCapability.Architecture.x86_64)) {
            describeImagesRequest.withFilters(new Filter("architecture").withValues(ARCH_x86_64));
        } else if (osCapability.getArchitecture().get().equals(OsCapability.Architecture.x86_32)) {
            describeImagesRequest.withFilters(new Filter("architecture").withValues(ARCH_x86_32));
        } else {
            throw new UnsupportedOperationException("This architecture is not supported " + osCapability.getArchitecture());
        }
    } else {
        // defaulting to 64 bit architecture
        describeImagesRequest.withFilters(new Filter("architecture").withValues(ARCH_x86_64));
    }
    try {
        DescribeImagesResult describeImagesResult = ec2.describeImages(describeImagesRequest);
        String imageId = processResult(describeImagesResult);
        logger.debug("ImageId is: '{}'", imageId);
        return imageId;
    } catch (SdkClientException se) {
        logger.error("Cannot connect to AWS to request image Ids");
        throw se;
    } catch (ParseException pe) {
        logger.error("Error parsing date format of image creation dates");
        throw pe;
    } catch (IllegalArgumentException ie) {
        logger.error("With the filters created from the OsCapability there are no valid images received");
        throw ie;
    }
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) SdkClientException(com.amazonaws.SdkClientException) Filter(com.amazonaws.services.ec2.model.Filter) DescribeImagesResult(com.amazonaws.services.ec2.model.DescribeImagesResult) DescribeImagesRequest(com.amazonaws.services.ec2.model.DescribeImagesRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) ParseException(java.text.ParseException)

Example 23 with Image

use of com.amazonaws.services.ec2.model.Image in project TOSCAna by StuPro-TOSCAna.

the class CapabilityMapper method processResult.

/**
 *     Processes the result of an {@link DescribeImagesRequest} and returns the imageId of the latest image.
 *
 *     @param describeImagesResult the result received from aws
 *     @return returns the latest imageId
 */
private String processResult(DescribeImagesResult describeImagesResult) throws ParseException, IllegalArgumentException {
    Integer numReceivedImages = describeImagesResult.getImages().size();
    logger.debug("Got '{}' images from aws", numReceivedImages);
    if (numReceivedImages > 0) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Map<Date, Image> creationDateMap = new HashMap<>();
        for (Image image : describeImagesResult.getImages()) {
            Date date = dateFormat.parse(image.getCreationDate());
            creationDateMap.put(date, image);
        }
        Image latest = creationDateMap.get(Collections.max(creationDateMap.keySet()));
        logger.debug("Latest image received: '{}'", latest);
        return latest.getImageId();
    } else {
        throw new IllegalArgumentException("No images received");
    }
}
Also used : HashMap(java.util.HashMap) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Image(com.amazonaws.services.ec2.model.Image) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

DescribeImagesRequest (com.amazonaws.services.ec2.model.DescribeImagesRequest)8 DescribeImagesResult (com.amazonaws.services.ec2.model.DescribeImagesResult)8 Filter (com.amazonaws.services.ec2.model.Filter)7 AmazonServiceException (com.amazonaws.AmazonServiceException)6 Image (com.amazonaws.services.ec2.model.Image)5 ArrayList (java.util.ArrayList)5 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)4 AnnotateImageRequest (com.google.api.services.vision.v1.model.AnnotateImageRequest)4 AnnotateImageResponse (com.google.api.services.vision.v1.model.AnnotateImageResponse)4 BatchAnnotateImagesRequest (com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest)4 BatchAnnotateImagesResponse (com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)4 Feature (com.google.api.services.vision.v1.model.Feature)4 Image (com.google.api.services.vision.v1.model.Image)4 IOException (java.io.IOException)4 BlockDeviceMapping (com.amazonaws.services.ec2.model.BlockDeviceMapping)3 EbsBlockDevice (com.amazonaws.services.ec2.model.EbsBlockDevice)3 InstanceState (com.amazonaws.services.ec2.model.InstanceState)3 InstanceStateChange (com.amazonaws.services.ec2.model.InstanceStateChange)3 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)3 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)2