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;
}
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);
}
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);
}
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);
}
Aggregations