Search in sources :

Example 11 with InstanceType

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

the class TestAWSSetupUtils method provisionAWSVMWithEC2Client.

/**
 * Method to directly provision instances on the AWS endpoint without the knowledge of the local
 * system. This is used to spawn instances and to test that the discovery of items not
 * provisioned by Xenon happens correctly.
 *
 * @throws Throwable
 */
public static List<String> provisionAWSVMWithEC2Client(AmazonEC2AsyncClient client, VerificationHost host, int numberOfInstance, String instanceType, String subnetId, String securityGroupId) throws Throwable {
    host.log("Provisioning %d instances on the AWS endpoint using the EC2 client.", numberOfInstance);
    RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withSubnetId(subnetId).withImageId(EC2_LINUX_AMI).withInstanceType(instanceType).withMinCount(numberOfInstance).withMaxCount(numberOfInstance).withSecurityGroupIds(securityGroupId);
    // handler invoked once the EC2 runInstancesAsync commands completes
    AWSRunInstancesAsyncHandler creationHandler = new AWSRunInstancesAsyncHandler(host);
    client.runInstancesAsync(runInstancesRequest, creationHandler);
    host.waitFor("Waiting for instanceIds to be returned from AWS", () -> {
        return checkInstanceIdsReturnedFromAWS(numberOfInstance, creationHandler.instanceIds);
    });
    return creationHandler.instanceIds;
}
Also used : RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest)

Example 12 with InstanceType

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

the class AWSInstanceService method createInstance.

private void createInstance(AWSInstanceContext aws) {
    if (aws.computeRequest.isMockRequest) {
        aws.taskManager.finishTask();
        return;
    }
    final DiskState bootDisk = aws.bootDisk;
    if (bootDisk == null) {
        aws.taskManager.patchTaskToFailure(new IllegalStateException("AWS bootDisk not specified"));
        return;
    }
    if (bootDisk.bootConfig != null && bootDisk.bootConfig.files.length > 1) {
        aws.taskManager.patchTaskToFailure(new IllegalStateException("Only 1 configuration file allowed"));
        return;
    }
    // This a single disk state with a bootConfig. There's no expectation
    // that it does exists, but if it does, we only support cloud configs at
    // this point.
    String cloudConfig = null;
    if (bootDisk.bootConfig != null && bootDisk.bootConfig.files.length > CLOUD_CONFIG_DEFAULT_FILE_INDEX) {
        cloudConfig = bootDisk.bootConfig.files[CLOUD_CONFIG_DEFAULT_FILE_INDEX].contents;
    }
    String instanceType = aws.child.description.instanceType;
    if (instanceType == null) {
        // fallback to legacy usage of name
        instanceType = aws.child.description.name;
    }
    if (instanceType == null) {
        aws.error = new IllegalStateException("AWS Instance type not specified");
        aws.stage = AWSInstanceStage.ERROR;
        handleAllocation(aws);
        return;
    }
    RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId(aws.bootDiskImageNativeId).withInstanceType(instanceType).withMinCount(1).withMaxCount(1).withMonitoring(true).withTagSpecifications(new TagSpecification().withResourceType(ResourceType.Instance).withTags(aws.getAWSTags()));
    if (aws.placement != null) {
        runInstancesRequest.withPlacement(new Placement(aws.placement));
    }
    if (aws.child.customProperties != null && aws.child.customProperties.containsKey(CUSTOM_PROP_SSH_KEY_NAME)) {
        runInstancesRequest = runInstancesRequest.withKeyName(aws.child.customProperties.get(CUSTOM_PROP_SSH_KEY_NAME));
    }
    if (!aws.dataDisks.isEmpty() || bootDisk.capacityMBytes > 0 || bootDisk.customProperties != null) {
        DescribeImagesRequest imagesDescriptionRequest = new DescribeImagesRequest();
        imagesDescriptionRequest.withImageIds(aws.bootDiskImageNativeId);
        DescribeImagesResult imagesDescriptionResult = aws.amazonEC2Client.describeImages(imagesDescriptionRequest);
        if (imagesDescriptionResult.getImages().size() != 1) {
            handleError(aws, new IllegalStateException("AWS ImageId is not available"));
            return;
        }
        Image image = imagesDescriptionResult.getImages().get(0);
        AssertUtil.assertNotNull(aws.instanceTypeInfo, "instanceType cannot be null");
        List<BlockDeviceMapping> blockDeviceMappings = image.getBlockDeviceMappings();
        String rootDeviceType = image.getRootDeviceType();
        String bootDiskType = bootDisk.customProperties.get(DEVICE_TYPE);
        boolean hasHardConstraint = containsHardConstraint(bootDisk);
        BlockDeviceMapping rootDeviceMapping = null;
        try {
            // The number of instance-store disks that will be provisioned is limited by the instance-type.
            suppressExcessInstanceStoreDevices(blockDeviceMappings, aws.instanceTypeInfo);
            for (BlockDeviceMapping blockDeviceMapping : blockDeviceMappings) {
                EbsBlockDevice ebs = blockDeviceMapping.getEbs();
                String diskType = getDeviceType(ebs);
                if (hasHardConstraint) {
                    validateIfDeviceTypesAreMatching(diskType, bootDiskType);
                }
                if (blockDeviceMapping.getNoDevice() != null) {
                    continue;
                }
                if (rootDeviceType.equals(AWSStorageType.EBS.getName()) && blockDeviceMapping.getDeviceName().equals(image.getRootDeviceName())) {
                    rootDeviceMapping = blockDeviceMapping;
                    continue;
                }
                DiskState diskState = new DiskState();
                copyCustomProperties(diskState, bootDisk);
                addMandatoryProperties(diskState, blockDeviceMapping, aws);
                updateDeviceMapping(diskType, bootDiskType, blockDeviceMapping.getDeviceName(), ebs, diskState);
                // update disk state with final volume-type and iops
                if (diskType.equals(AWSStorageType.EBS.getName())) {
                    diskState.customProperties.put(VOLUME_TYPE, ebs.getVolumeType());
                    diskState.customProperties.put(DISK_IOPS, String.valueOf(ebs.getIops()));
                }
                aws.imageDisks.add(diskState);
            }
            customizeBootDiskProperties(bootDisk, rootDeviceType, rootDeviceMapping, hasHardConstraint, aws);
            List<DiskState> ebsDisks = new ArrayList<>();
            List<DiskState> instanceStoreDisks = new ArrayList<>();
            if (!aws.dataDisks.isEmpty()) {
                if (!rootDeviceType.equals(AWSStorageType.EBS.name().toLowerCase())) {
                    instanceStoreDisks = aws.dataDisks;
                    assertAndResetPersistence(instanceStoreDisks);
                    validateSupportForAdditionalInstanceStoreDisks(instanceStoreDisks, blockDeviceMappings, aws.instanceTypeInfo, rootDeviceType);
                } else {
                    splitDataDisks(aws.dataDisks, instanceStoreDisks, ebsDisks);
                    setEbsDefaultsIfNotSpecified(ebsDisks, Boolean.FALSE);
                    if (!instanceStoreDisks.isEmpty()) {
                        assertAndResetPersistence(instanceStoreDisks);
                        validateSupportForAdditionalInstanceStoreDisks(instanceStoreDisks, blockDeviceMappings, aws.instanceTypeInfo, rootDeviceType);
                    }
                }
            }
            // get the available attach paths for new disks and external disks
            List<String> usedDeviceNames = null;
            if (!instanceStoreDisks.isEmpty() || !ebsDisks.isEmpty() || !aws.externalDisks.isEmpty()) {
                usedDeviceNames = getUsedDeviceNames(blockDeviceMappings);
            }
            if (!instanceStoreDisks.isEmpty()) {
                List<String> usedVirtualNames = getUsedVirtualNames(blockDeviceMappings);
                blockDeviceMappings.addAll(createInstanceStoreMappings(instanceStoreDisks, usedDeviceNames, usedVirtualNames, aws.instanceTypeInfo.id, aws.instanceTypeInfo.dataDiskSizeInMB, image.getPlatform(), image.getVirtualizationType()));
            }
            if (!ebsDisks.isEmpty() || !aws.externalDisks.isEmpty()) {
                aws.availableEbsDiskNames = AWSBlockDeviceNameMapper.getAvailableNames(AWSSupportedOS.get(image.getPlatform()), AWSSupportedVirtualizationTypes.get(image.getVirtualizationType()), AWSStorageType.EBS, instanceType, usedDeviceNames);
            }
            if (!ebsDisks.isEmpty()) {
                blockDeviceMappings.addAll(createEbsDeviceMappings(ebsDisks, aws.availableEbsDiskNames));
            }
            runInstancesRequest.withBlockDeviceMappings(blockDeviceMappings);
        } catch (Exception e) {
            aws.error = e;
            aws.stage = AWSInstanceStage.ERROR;
            handleAllocation(aws);
            return;
        }
    }
    AWSNicContext primaryNic = aws.getPrimaryNic();
    if (primaryNic != null && primaryNic.nicSpec != null) {
        runInstancesRequest.withNetworkInterfaces(primaryNic.nicSpec);
    } else {
        runInstancesRequest.withSecurityGroupIds(AWSUtils.getOrCreateSecurityGroups(aws, null));
    }
    if (cloudConfig != null) {
        try {
            runInstancesRequest.setUserData(Base64.getEncoder().encodeToString(cloudConfig.getBytes(Utils.CHARSET)));
        } catch (UnsupportedEncodingException e) {
            handleError(aws, new IllegalStateException("Error encoding user data"));
            return;
        }
    }
    String message = "[AWSInstanceService] Sending run instance request for instance id: " + aws.bootDiskImageNativeId + ", instance type: " + instanceType + ", parent task id: " + aws.computeRequest.taskReference;
    this.logInfo(() -> message);
    // handler invoked once the EC2 runInstancesAsync commands completes
    AsyncHandler<RunInstancesRequest, RunInstancesResult> creationHandler = new AWSCreationHandler(this, aws);
    aws.amazonEC2Client.runInstancesAsync(runInstancesRequest, creationHandler);
}
Also used : DescribeImagesRequest(com.amazonaws.services.ec2.model.DescribeImagesRequest) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TagSpecification(com.amazonaws.services.ec2.model.TagSpecification) Image(com.amazonaws.services.ec2.model.Image) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Placement(com.amazonaws.services.ec2.model.Placement) DescribeImagesResult(com.amazonaws.services.ec2.model.DescribeImagesResult) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) EbsBlockDevice(com.amazonaws.services.ec2.model.EbsBlockDevice) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) AWSNicContext(com.vmware.photon.controller.model.adapters.awsadapter.AWSInstanceContext.AWSNicContext)

Example 13 with InstanceType

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

the class AWSInstanceTypeService method getInstanceTypes.

/**
 * Return the instance types by loading them from AWS SDK {@link InstanceType}.
 */
private DeferredResult<Context> getInstanceTypes(Context context) {
    AssertUtil.assertNotNull(context.endpointState, "Endpoint state was not retrieved.");
    context.instanceTypes = new InstanceTypeList();
    // Set tenant links as specified in the endpoint.
    context.instanceTypes.tenantLinks = context.endpointState.tenantLinks;
    context.instanceTypes.instanceTypes = // Use AWS SDK InstanceType enum as primary source of instance type data.
    Arrays.stream(InstanceType.values()).map(instanceType -> {
        InstanceTypeList.InstanceType result = new InstanceTypeList.InstanceType(instanceType.toString(), instanceType.toString());
        InstanceTypeList.InstanceType instanceTypeInfo = this.instanceTypeInfo.get(instanceType.toString());
        if (instanceTypeInfo != null) {
            // We have additional information -> populate additional fields.
            result.cpuCount = instanceTypeInfo.cpuCount;
            result.memoryInMB = instanceTypeInfo.memoryInMB;
            result.networkType = instanceTypeInfo.networkType;
            result.storageType = instanceTypeInfo.storageType;
            result.dataDiskMaxCount = instanceTypeInfo.dataDiskMaxCount;
            result.dataDiskSizeInMB = instanceTypeInfo.dataDiskSizeInMB;
        }
        return result;
    }).filter(instanceType -> !Integer.valueOf(-1).equals(instanceType.cpuCount)).collect(Collectors.toList());
    return DeferredResult.completed(context);
}
Also used : Arrays(java.util.Arrays) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) File(java.io.File) URI_PARAM_ENDPOINT(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.URI_PARAM_ENDPOINT) IOUtils(org.apache.commons.io.IOUtils) FileUtils(com.vmware.xenon.common.FileUtils) Utils(com.vmware.xenon.common.Utils) URI_PARAM_INSTANCE_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.URI_PARAM_INSTANCE_TYPE) Map(java.util.Map) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) InstanceTypeList(com.vmware.photon.controller.model.support.InstanceTypeList) InstanceType(com.amazonaws.services.ec2.model.InstanceType) AssertUtil(com.vmware.photon.controller.model.util.AssertUtil) InputStream(java.io.InputStream) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) InstanceType(com.amazonaws.services.ec2.model.InstanceType) InstanceTypeList(com.vmware.photon.controller.model.support.InstanceTypeList)

Example 14 with InstanceType

use of com.amazonaws.services.ec2.model.InstanceType in project ice by Netflix.

the class ReservationCapacityPoller method poll.

@Override
protected void poll() throws Exception {
    ProcessorConfig config = ProcessorConfig.getInstance();
    // read from s3 if not exists
    File file = new File(config.localDir, "reservation_capacity.txt");
    if (!file.exists()) {
        logger.info("downloading " + file + "...");
        AwsUtils.downloadFileIfNotExist(config.workS3BucketName, config.workS3BucketPrefix, file);
        logger.info("downloaded " + file);
    }
    // read from file
    Map<String, ReservedInstances> reservations = Maps.newTreeMap();
    if (file.exists()) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = reader.readLine()) != null) {
                String[] tokens = line.split(",");
                String accountId = tokens[0];
                String region = tokens[1];
                String reservationId = tokens[2];
                String zone = tokens[3];
                Long start = Long.parseLong(tokens[4]);
                long duration = Long.parseLong(tokens[5]);
                String instanceType = tokens[6];
                String productDescription = tokens[7];
                int instanceCount = Integer.parseInt(tokens[8]);
                String offeringType = tokens[9];
                String state = tokens[10];
                Long end = tokens.length > 11 ? Long.parseLong(tokens[11]) : null;
                float fixedPrice = tokens.length > 12 ? Float.parseFloat(tokens[12]) : 0;
                float usagePrice = tokens.length > 13 ? Float.parseFloat(tokens[13]) : 0;
                ReservedInstances reservation = new ReservedInstances().withAvailabilityZone(zone).withStart(new Date(start)).withDuration(duration).withInstanceType(instanceType).withProductDescription(productDescription).withInstanceCount(instanceCount).withOfferingType(offeringType).withState(state).withFixedPrice(fixedPrice).withUsagePrice(usagePrice);
                if (end != null)
                    reservation.setEnd(new Date(end));
                else
                    reservation.setEnd(new Date(start + duration * 1000));
                reservations.put(accountId + "," + region + "," + reservationId, reservation);
            }
        } catch (Exception e) {
            logger.error("error in reading " + file, e);
        } finally {
            if (reader != null)
                try {
                    reader.close();
                } catch (Exception e) {
                }
        }
    }
    logger.info("read " + reservations.size() + " reservations.");
    for (Account account : config.accountService.getReservationAccounts().keySet()) {
        try {
            AmazonEC2Client ec2Client;
            String assumeRole = config.accountService.getReservationAccessRoles().get(account);
            if (assumeRole != null) {
                String externalId = config.accountService.getReservationAccessExternalIds().get(account);
                final Credentials credentials = AwsUtils.getAssumedCredentials(account.id, assumeRole, externalId);
                ec2Client = new AmazonEC2Client(new AWSSessionCredentials() {

                    public String getAWSAccessKeyId() {
                        return credentials.getAccessKeyId();
                    }

                    public String getAWSSecretKey() {
                        return credentials.getSecretAccessKey();
                    }

                    public String getSessionToken() {
                        return credentials.getSessionToken();
                    }
                });
            } else
                ec2Client = new AmazonEC2Client(AwsUtils.awsCredentialsProvider.getCredentials(), AwsUtils.clientConfig);
            for (Region region : Region.getAllRegions()) {
                // just ignore GovCloud when polling for RIs in order to prevent AuthFailure errors.
                if (region == Region.US_GOV_WEST_1) {
                    continue;
                }
                ec2Client.setEndpoint("ec2." + region.name + ".amazonaws.com");
                try {
                    DescribeReservedInstancesResult result = ec2Client.describeReservedInstances();
                    for (ReservedInstances reservation : result.getReservedInstances()) {
                        String key = account.id + "," + region.name + "," + reservation.getReservedInstancesId();
                        reservations.put(key, reservation);
                        if (reservation.getEnd() == null)
                            reservation.setEnd(new Date(reservation.getStart().getTime() + reservation.getDuration() * 1000L));
                        if (reservation.getFixedPrice() == null)
                            reservation.setFixedPrice(0f);
                        if (reservation.getUsagePrice() == null)
                            reservation.setUsagePrice(0f);
                    }
                } catch (Exception e) {
                    logger.error("error in describeReservedInstances for " + region.name + " " + account.name, e);
                }
            }
            ec2Client.shutdown();
        } catch (Exception e) {
            logger.error("Error in describeReservedInstances for " + account.name, e);
        }
    }
    config.reservationService.updateEc2Reservations(reservations);
    updatedConfig = true;
    // archive to disk
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(file));
        for (String key : reservations.keySet()) {
            ReservedInstances reservation = reservations.get(key);
            String[] line = new String[] { key, reservation.getAvailabilityZone(), reservation.getStart().getTime() + "", reservation.getDuration().toString(), reservation.getInstanceType(), reservation.getProductDescription(), reservation.getInstanceCount().toString(), reservation.getOfferingType(), reservation.getState(), reservation.getEnd().getTime() + "", reservation.getFixedPrice() + "", reservation.getUsagePrice() + "" };
            writer.write(StringUtils.join(line, ","));
            writer.newLine();
        }
    } catch (Exception e) {
        logger.error("", e);
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (Exception e) {
            }
    }
    logger.info("archived " + reservations.size() + " reservations.");
    // archive to s3
    logger.info("uploading " + file + "...");
    AwsUtils.upload(config.workS3BucketName, config.workS3BucketPrefix, config.localDir, file.getName());
    logger.info("uploaded " + file);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) Account(com.netflix.ice.tag.Account) ReservedInstances(com.amazonaws.services.ec2.model.ReservedInstances) Date(java.util.Date) AWSSessionCredentials(com.amazonaws.auth.AWSSessionCredentials) Region(com.netflix.ice.tag.Region) DescribeReservedInstancesResult(com.amazonaws.services.ec2.model.DescribeReservedInstancesResult) AWSSessionCredentials(com.amazonaws.auth.AWSSessionCredentials) Credentials(com.amazonaws.services.securitytoken.model.Credentials)

Aggregations

RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)5 ArrayList (java.util.ArrayList)5 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)4 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)4 HashMap (java.util.HashMap)4 Collectors (java.util.stream.Collectors)4 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)3 BlockDeviceMapping (com.amazonaws.services.ec2.model.BlockDeviceMapping)3 EbsBlockDevice (com.amazonaws.services.ec2.model.EbsBlockDevice)3 InstanceBlockDeviceMapping (com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)3 Tag (com.amazonaws.services.ec2.model.Tag)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)2 AttachVolumeRequest (com.amazonaws.services.ec2.model.AttachVolumeRequest)2 CreateVolumeRequest (com.amazonaws.services.ec2.model.CreateVolumeRequest)2 CreateVolumeResult (com.amazonaws.services.ec2.model.CreateVolumeResult)2 DeleteVolumeRequest (com.amazonaws.services.ec2.model.DeleteVolumeRequest)2 DetachVolumeRequest (com.amazonaws.services.ec2.model.DetachVolumeRequest)2 Instance (com.amazonaws.services.ec2.model.Instance)2 InstanceType (com.amazonaws.services.ec2.model.InstanceType)2