Search in sources :

Example 1 with Image

use of com.amazonaws.services.ec2.model.Image in project h2o-2 by h2oai.

the class EC2 method resize.

//@formatter:on
/**
   * Create or terminate EC2 instances. Uses their Name tag to find existing ones.
   */
public Cloud resize() throws Exception {
    AmazonEC2Client ec2 = new AmazonEC2Client(new PersistS3.H2OAWSCredentialsProviderChain());
    ec2.setEndpoint("ec2." + region + ".amazonaws.com");
    DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
    List<Reservation> reservations = describeInstancesResult.getReservations();
    List<Instance> instances = new ArrayList<Instance>();
    for (Reservation reservation : reservations) {
        for (Instance instance : reservation.getInstances()) {
            String ip = ip(instance);
            if (ip != null) {
                String name = null;
                if (instance.getTags().size() > 0)
                    name = instance.getTags().get(0).getValue();
                if (NAME.equals(name))
                    instances.add(instance);
            }
        }
    }
    System.out.println("Found " + instances.size() + " EC2 instances for user " + USER);
    if (instances.size() > boxes) {
        for (int i = 0; i < instances.size() - boxes; i++) {
        // TODO terminate?
        }
    } else if (instances.size() < boxes) {
        int launchCount = boxes - instances.size();
        System.out.println("Creating " + launchCount + " EC2 instances.");
        if (confirm) {
            System.out.println("Please confirm [y/n]");
            String s = Utils.readConsole();
            if (s == null || !s.equalsIgnoreCase("y"))
                throw new Exception("Aborted");
        }
        CreatePlacementGroupRequest group = new CreatePlacementGroupRequest();
        group.withGroupName(USER);
        group.withStrategy(PlacementStrategy.Cluster);
        try {
            ec2.createPlacementGroup(group);
        } catch (AmazonServiceException ex) {
            if (!"InvalidPlacementGroup.Duplicate".equals(ex.getErrorCode()))
                throw ex;
        }
        RunInstancesRequest run = new RunInstancesRequest();
        run.withInstanceType(type);
        run.withImageId(image);
        run.withMinCount(launchCount).withMaxCount(launchCount);
        run.withSecurityGroupIds(securityGroup);
        Placement placement = new Placement();
        placement.setGroupName(USER);
        run.withPlacement(placement);
        BlockDeviceMapping map = new BlockDeviceMapping();
        map.setDeviceName("/dev/sdb");
        map.setVirtualName("ephemeral0");
        run.withBlockDeviceMappings(map);
        run.withUserData(new String(Base64.encodeBase64(cloudConfig.getBytes())));
        RunInstancesResult runRes = ec2.runInstances(run);
        ArrayList<String> ids = new ArrayList<String>();
        for (Instance instance : runRes.getReservation().getInstances()) ids.add(instance.getInstanceId());
        List<Instance> created = wait(ec2, ids);
        System.out.println("Created " + created.size() + " EC2 instances.");
        instances.addAll(created);
    }
    String[] pub = new String[boxes];
    String[] prv = new String[boxes];
    for (int i = 0; i < boxes; i++) {
        pub[i] = instances.get(i).getPublicIpAddress();
        prv[i] = instances.get(i).getPrivateIpAddress();
    }
    System.out.println("EC2 public IPs: " + Utils.join(' ', pub));
    System.out.println("EC2 private IPs: " + Utils.join(' ', prv));
    Cloud cloud = new Cloud();
    cloud.publicIPs.addAll(Arrays.asList(pub));
    cloud.privateIPs.addAll(Arrays.asList(prv));
    return cloud;
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) PersistS3(water.persist.PersistS3) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonServiceException(com.amazonaws.AmazonServiceException)

Example 2 with Image

use of com.amazonaws.services.ec2.model.Image in project camel by apache.

the class AmazonEC2ClientMock method startInstances.

@Override
public StartInstancesResult startInstances(StartInstancesRequest startInstancesRequest) {
    StartInstancesResult result = new StartInstancesResult();
    if (startInstancesRequest.getInstanceIds().get(0).equals("test-1")) {
        Collection<InstanceStateChange> coll = new ArrayList<InstanceStateChange>();
        InstanceStateChange sc = new InstanceStateChange();
        InstanceState previousState = new InstanceState();
        previousState.setCode(80);
        previousState.setName(InstanceStateName.Stopped);
        InstanceState newState = new InstanceState();
        newState.setCode(16);
        newState.setName(InstanceStateName.Running);
        sc.setPreviousState(previousState);
        sc.setCurrentState(newState);
        sc.setInstanceId("test-1");
        coll.add(sc);
        result.setStartingInstances(coll);
    } else {
        throw new AmazonServiceException("The image-id doesn't exists");
    }
    return result;
}
Also used : InstanceState(com.amazonaws.services.ec2.model.InstanceState) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) InstanceStateChange(com.amazonaws.services.ec2.model.InstanceStateChange)

Example 3 with Image

use of com.amazonaws.services.ec2.model.Image in project java-docs-samples by GoogleCloudPlatform.

the class DetectLandmark method identifyLandmark.

/**
 * Gets up to {@code maxResults} landmarks for an image stored at {@code uri}.
 */
public List<EntityAnnotation> identifyLandmark(String uri, int maxResults) throws IOException {
    AnnotateImageRequest request = new AnnotateImageRequest().setImage(new Image().setSource(new ImageSource().setGcsImageUri(uri))).setFeatures(ImmutableList.of(new Feature().setType("LANDMARK_DETECTION").setMaxResults(maxResults)));
    Vision.Images.Annotate annotate = vision.images().annotate(new BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request)));
    // Due to a bug: requests to Vision API containing large images fail when GZipped.
    annotate.setDisableGZipContent(true);
    BatchAnnotateImagesResponse batchResponse = annotate.execute();
    assert batchResponse.getResponses().size() == 1;
    AnnotateImageResponse response = batchResponse.getResponses().get(0);
    if (response.getLandmarkAnnotations() == null) {
        throw new IOException(response.getError() != null ? response.getError().getMessage() : "Unknown error getting image annotations");
    }
    return response.getLandmarkAnnotations();
}
Also used : BatchAnnotateImagesRequest(com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest) AnnotateImageRequest(com.google.api.services.vision.v1.model.AnnotateImageRequest) AnnotateImageResponse(com.google.api.services.vision.v1.model.AnnotateImageResponse) ImageSource(com.google.api.services.vision.v1.model.ImageSource) IOException(java.io.IOException) Image(com.google.api.services.vision.v1.model.Image) Feature(com.google.api.services.vision.v1.model.Feature) BatchAnnotateImagesResponse(com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)

Example 4 with Image

use of com.amazonaws.services.ec2.model.Image in project java-docs-samples by GoogleCloudPlatform.

the class TextApp method detectText.

/**
 * Gets up to {@code maxResults} text annotations for images stored at {@code paths}.
 */
public ImmutableList<ImageText> detectText(List<Path> paths) {
    ImmutableList.Builder<AnnotateImageRequest> requests = ImmutableList.builder();
    try {
        for (Path path : paths) {
            byte[] data;
            data = Files.readAllBytes(path);
            requests.add(new AnnotateImageRequest().setImage(new Image().encodeContent(data)).setFeatures(ImmutableList.of(new Feature().setType("TEXT_DETECTION").setMaxResults(MAX_RESULTS))));
        }
        Vision.Images.Annotate annotate = vision.images().annotate(new BatchAnnotateImagesRequest().setRequests(requests.build()));
        // Due to a bug: requests to Vision API containing large images fail when GZipped.
        annotate.setDisableGZipContent(true);
        BatchAnnotateImagesResponse batchResponse = annotate.execute();
        assert batchResponse.getResponses().size() == paths.size();
        ImmutableList.Builder<ImageText> output = ImmutableList.builder();
        for (int i = 0; i < paths.size(); i++) {
            Path path = paths.get(i);
            AnnotateImageResponse response = batchResponse.getResponses().get(i);
            output.add(ImageText.builder().path(path).textAnnotations(MoreObjects.firstNonNull(response.getTextAnnotations(), ImmutableList.<EntityAnnotation>of())).error(response.getError()).build());
        }
        return output.build();
    } catch (IOException ex) {
        // Got an exception, which means the whole batch had an error.
        ImmutableList.Builder<ImageText> output = ImmutableList.builder();
        for (Path path : paths) {
            output.add(ImageText.builder().path(path).textAnnotations(ImmutableList.<EntityAnnotation>of()).error(new Status().setMessage(ex.getMessage())).build());
        }
        return output.build();
    }
}
Also used : Path(java.nio.file.Path) Status(com.google.api.services.vision.v1.model.Status) BatchAnnotateImagesRequest(com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) Image(com.google.api.services.vision.v1.model.Image) Feature(com.google.api.services.vision.v1.model.Feature) AnnotateImageRequest(com.google.api.services.vision.v1.model.AnnotateImageRequest) AnnotateImageResponse(com.google.api.services.vision.v1.model.AnnotateImageResponse) BatchAnnotateImagesResponse(com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)

Example 5 with Image

use of com.amazonaws.services.ec2.model.Image in project photon-model by vmware.

the class TestAWSImageEnumerationTask method beforeTest.

@Before
public final void beforeTest() throws Throwable {
    CommandLineArgumentParser.parseFromProperties(this);
    if (this.isMock) {
        return;
    }
    if (AMAZON_PARAVIRTUAL_IMAGE_NAME == null || AMAZON_HVM_IMAGE_NAME == null) {
        // create credentials
        AuthCredentialsServiceState creds = new AuthCredentialsServiceState();
        creds.privateKey = this.secretKey;
        creds.privateKeyId = this.accessKey;
        AmazonEC2AsyncClient client = AWSUtils.getAsyncClient(creds, this.regionId, getExecutor());
        // Get arbitrary PARAVIRTUAL image name
        AMAZON_PARAVIRTUAL_IMAGE_NAME = lookupAwsImage(client, AWSConstants.AWS_IMAGE_VIRTUALIZATION_TYPE_PARAVIRTUAL);
        // Get arbitrary HVM image name
        AMAZON_HVM_IMAGE_NAME = lookupAwsImage(client, AWSConstants.AWS_IMAGE_VIRTUALIZATION_TYPE_HVM);
        {
            Filter nameFilter = new Filter("name").withValues(AMAZON_PARAVIRTUAL_IMAGE_NAME);
            // Serialize the list of filters to JSON string
            AMAZON_PUBLIC_IMAGE_FILTER_SINGLE = Utils.toJson(Arrays.asList(nameFilter));
        }
        {
            Filter nameFilter = new Filter("name").withValues(AMAZON_PARAVIRTUAL_IMAGE_NAME, AMAZON_HVM_IMAGE_NAME);
            // Serialize the list of filters to JSON string
            AMAZON_PUBLIC_IMAGE_FILTER_PARTITIONING = Utils.toJson(Arrays.asList(nameFilter));
        }
    }
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) Filter(com.amazonaws.services.ec2.model.Filter) Before(org.junit.Before)

Aggregations

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 DescribeImagesRequest (com.amazonaws.services.ec2.model.DescribeImagesRequest)4 DescribeImagesResult (com.amazonaws.services.ec2.model.DescribeImagesResult)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 Filter (com.amazonaws.services.ec2.model.Filter)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