Search in sources :

Example 1 with BlockDeviceMapping

use of com.amazonaws.services.ec2.model.BlockDeviceMapping 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 BlockDeviceMapping

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

the class AWSInstanceService method createInstanceStoreMappings.

/**
 * Creates device mappings for the instance-store disks.
 */
private List<BlockDeviceMapping> createInstanceStoreMappings(List<DiskState> instanceStoreDisks, List<String> usedDeviceNames, List<String> usedVirtualNames, String instanceType, Integer capacityMBytes, String platform, String virtualizationType) {
    List<BlockDeviceMapping> deviceMappings = new ArrayList<>();
    if (!instanceStoreDisks.isEmpty()) {
        this.logInfo(() -> String.format("[AWSInstanceService] Ignores the size and type of the " + "additional disk. Instance-store type of additional disks are " + "provisioned with the capacity supported by %s", instanceType));
        List<String> availableDeviceNames = AWSBlockDeviceNameMapper.getAvailableNames(AWSSupportedOS.get(platform), AWSSupportedVirtualizationTypes.get(virtualizationType), AWSStorageType.INSTANCE_STORE, instanceType, usedDeviceNames);
        List<String> availableVirtualNames = getAvailableVirtualNames(usedVirtualNames);
        if (availableDeviceNames.size() >= instanceStoreDisks.size() && availableVirtualNames.size() >= instanceStoreDisks.size()) {
            for (DiskState diskState : instanceStoreDisks) {
                BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping();
                String deviceName = availableDeviceNames.get(0);
                blockDeviceMapping.setDeviceName(deviceName);
                availableDeviceNames.remove(0);
                usedDeviceNames.add(deviceName);
                String virtualName = availableVirtualNames.get(0);
                blockDeviceMapping.setVirtualName(virtualName);
                availableVirtualNames.remove(0);
                deviceMappings.add(blockDeviceMapping);
                if (diskState.customProperties == null) {
                    diskState.customProperties = new HashMap<>();
                }
                diskState.customProperties.put(DEVICE_NAME, deviceName);
                diskState.customProperties.put(DEVICE_TYPE, AWSStorageType.INSTANCE_STORE.getName());
                diskState.capacityMBytes = capacityMBytes;
            }
        } else {
            String message = "Additional instance-store disks cannot be attached. " + "Not sufficient device names are available.";
            this.logWarning(() -> "[AWSInstanceService] " + message);
            throw new IllegalArgumentException(message);
        }
    }
    return deviceMappings;
}
Also used : DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ArrayList(java.util.ArrayList) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)

Example 3 with BlockDeviceMapping

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

the class AWSInstanceService method addMandatoryProperties.

/**
 * Add the disk information to disk state so that the disk state reflects the volume
 * information
 */
private void addMandatoryProperties(DiskState diskState, BlockDeviceMapping deviceMapping, AWSInstanceContext instanceType) {
    if (diskState.customProperties == null) {
        diskState.customProperties = new HashMap<>();
    }
    String deviceName = deviceMapping.getDeviceName();
    diskState.customProperties.put(DEVICE_NAME, deviceName);
    diskState.persistent = Boolean.FALSE;
    EbsBlockDevice ebs = deviceMapping.getEbs();
    if (ebs != null) {
        diskState.capacityMBytes = ebs.getVolumeSize() * 1024;
        diskState.customProperties.put(DEVICE_TYPE, AWSStorageType.EBS.getName());
    } else {
        diskState.capacityMBytes = instanceType.instanceTypeInfo.dataDiskSizeInMB;
        diskState.customProperties.put(DEVICE_TYPE, AWSStorageType.INSTANCE_STORE.getName());
    }
    // add the endpointLinks
    diskState.endpointLinks = instanceType.child.endpointLinks;
}
Also used : EbsBlockDevice(com.amazonaws.services.ec2.model.EbsBlockDevice)

Example 4 with BlockDeviceMapping

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

the class AWSInstanceService method createEbsDeviceMappings.

/**
 * Creates the device mappings for the ebs disks.
 */
private List<BlockDeviceMapping> createEbsDeviceMappings(List<DiskState> ebsDisks, List<String> availableDiskNames) {
    List<BlockDeviceMapping> additionalDiskMappings = new ArrayList<>();
    if (availableDiskNames.size() >= ebsDisks.size()) {
        for (DiskState diskState : ebsDisks) {
            if (diskState.capacityMBytes > 0) {
                BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping();
                EbsBlockDevice ebsBlockDevice = new EbsBlockDevice();
                int diskSize = (int) diskState.capacityMBytes / 1024;
                ebsBlockDevice.setVolumeSize(diskSize);
                ebsBlockDevice.setDeleteOnTermination(!diskState.persistent);
                if (diskState.customProperties != null) {
                    String requestedVolumeType = diskState.customProperties.get(VOLUME_TYPE);
                    if (requestedVolumeType != null) {
                        validateSizeSupportedByVolumeType(diskSize, requestedVolumeType);
                        ebsBlockDevice.setVolumeType(requestedVolumeType);
                    }
                    String diskIops = diskState.customProperties.get(DISK_IOPS);
                    if (diskIops != null && !diskIops.isEmpty()) {
                        int iops = Integer.parseInt(diskIops);
                        if (iops > diskSize * MAX_IOPS_PER_GiB) {
                            String info = String.format("[AWSInstanceService] Requested " + "IOPS (%s) exceeds the maximum value supported" + " by %sGiB disk. Continues provisioning the " + "disk with %s iops", iops, diskSize, diskSize * MAX_IOPS_PER_GiB);
                            this.logInfo(() -> info);
                            iops = diskSize * MAX_IOPS_PER_GiB;
                        }
                        ebsBlockDevice.setIops(iops);
                    }
                }
                diskState.encrypted = diskState.encrypted == null ? false : diskState.encrypted;
                ebsBlockDevice.setEncrypted(diskState.encrypted);
                String deviceName = availableDiskNames.get(0);
                availableDiskNames.remove(0);
                blockDeviceMapping.setDeviceName(deviceName);
                blockDeviceMapping.setEbs(ebsBlockDevice);
                additionalDiskMappings.add(blockDeviceMapping);
                if (diskState.customProperties == null) {
                    diskState.customProperties = new HashMap<>();
                }
                diskState.customProperties.put(DEVICE_NAME, deviceName);
            } else {
                String message = "Additional disk size capacity has to be positive";
                this.logWarning(() -> "[AWSInstanceService] " + message);
                throw new IllegalArgumentException(message);
            }
        }
    } else {
        String message = "Additional ebs disks cannot be attached. Not sufficient " + "device names are available.";
        this.logWarning(() -> "[AWSInstanceService] " + message);
        throw new IllegalArgumentException(message);
    }
    return additionalDiskMappings;
}
Also used : DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) EbsBlockDevice(com.amazonaws.services.ec2.model.EbsBlockDevice) ArrayList(java.util.ArrayList) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)

Example 5 with BlockDeviceMapping

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

the class TestAWSSetupUtils method provisionAWSEBSVMWithEC2Client.

public static String provisionAWSEBSVMWithEC2Client(VerificationHost host, AmazonEC2Client client, String ami, String subnetId, String securityGroupId, BlockDeviceMapping blockDeviceMapping) {
    RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withSubnetId(subnetId).withImageId(ami).withInstanceType(instanceType).withMinCount(1).withMaxCount(1).withSecurityGroupIds(securityGroupId).withBlockDeviceMappings(blockDeviceMapping);
    // handler invoked once the EC2 runInstancesAsync commands completes
    RunInstancesResult result = null;
    try {
        result = client.runInstances(runInstancesRequest);
    } catch (Exception e) {
        host.log(Level.SEVERE, "Error encountered in provisioning machine on AWS", Utils.toString(e));
    }
    assertNotNull(result);
    assertNotNull(result.getReservation());
    assertNotNull(result.getReservation().getInstances());
    assertEquals(1, result.getReservation().getInstances().size());
    String instanceId = result.getReservation().getInstances().get(0).getInstanceId();
    tagResources(client, Arrays.asList(instanceId), TAG_KEY_FOR_TEST_RESOURCES, TAG_VALUE_FOR_TEST_RESOURCES + TAG_INSTANCE);
    return instanceId;
}
Also used : RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) UnknownHostException(java.net.UnknownHostException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

EbsBlockDevice (com.amazonaws.services.ec2.model.EbsBlockDevice)7 BlockDeviceMapping (com.amazonaws.services.ec2.model.BlockDeviceMapping)6 InstanceBlockDeviceMapping (com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)4 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)4 ArrayList (java.util.ArrayList)4 Image (com.amazonaws.services.ec2.model.Image)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)2 DescribeImagesRequest (com.amazonaws.services.ec2.model.DescribeImagesRequest)2 DescribeImagesResult (com.amazonaws.services.ec2.model.DescribeImagesResult)2 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)2 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)2 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 DescribeVolumesRequest (com.amazonaws.services.ec2.model.DescribeVolumesRequest)1 DescribeVolumesResult (com.amazonaws.services.ec2.model.DescribeVolumesResult)1 Filter (com.amazonaws.services.ec2.model.Filter)1 Placement (com.amazonaws.services.ec2.model.Placement)1 Tag (com.amazonaws.services.ec2.model.Tag)1 TagSpecification (com.amazonaws.services.ec2.model.TagSpecification)1 AWSNicContext (com.vmware.photon.controller.model.adapters.awsadapter.AWSInstanceContext.AWSNicContext)1