use of com.amazonaws.services.ec2.model.BlockDeviceMapping in project SimianArmy by Netflix.
the class EBSSnapshotJanitorCrawler method refreshSnapshotToAMIs.
private void refreshSnapshotToAMIs() {
snapshotToAMIs.clear();
for (Image image : getAWSClient().describeImages()) {
for (BlockDeviceMapping bdm : image.getBlockDeviceMappings()) {
EbsBlockDevice ebd = bdm.getEbs();
if (ebd != null && ebd.getSnapshotId() != null) {
LOGGER.debug(String.format("Snapshot %s is used to generate AMI %s", ebd.getSnapshotId(), image.getImageId()));
Collection<String> amis = snapshotToAMIs.get(ebd.getSnapshotId());
if (amis == null) {
amis = new ArrayList<String>();
snapshotToAMIs.put(ebd.getSnapshotId(), amis);
}
amis.add(image.getImageId());
}
}
}
}
use of com.amazonaws.services.ec2.model.BlockDeviceMapping in project photon-model by vmware.
the class TestAWSEnumerationTask method testTagEnumeration.
@Test
public void testTagEnumeration() throws Throwable {
if (this.isMock) {
return;
}
setUpTestVolume(this.host, this.client, this.awsTestContext, this.isMock);
this.snapshotId = (String) this.awsTestContext.get(TestAWSSetupUtils.SNAPSHOT_KEY);
this.ebsBlockDevice = new EbsBlockDevice().withSnapshotId(this.snapshotId);
this.blockDeviceMapping = new BlockDeviceMapping().withDeviceName(BLOCK_DEVICE_NAME).withEbs(this.ebsBlockDevice);
this.diskId = (String) this.awsTestContext.get(TestAWSSetupUtils.DISK_KEY);
this.host.log("Running test: " + this.currentTestName.getMethodName());
// VM tags
Tag tag1 = new Tag(VM_TAG_KEY_1, VM_TAG_VALUE_1);
Tag tag2 = new Tag(VM_TAG_KEY_2, VM_TAG_VALUE_2);
Tag tag3 = new Tag(VM_TAG_KEY_3, VM_TAG_VALUE_3);
List<Tag> vmTags = Arrays.asList(tag1, tag2, tag3);
// SG tag
List<Tag> sgTags = new ArrayList<>();
sgTags.add(new Tag(INITIAL_SG_TAG, INITIAL_SG_TAG));
// Network tag
List<Tag> networkTags = new ArrayList<>();
networkTags.add(new Tag(INITIAL_VPC_TAG, INITIAL_VPC_TAG));
// Subnet tag
List<Tag> subnetTags = new ArrayList<>();
subnetTags.add(new Tag(INITIAL_SUBNET_TAG, INITIAL_SUBNET_TAG));
// Disk tag
List<Tag> diskTags = new ArrayList<>();
diskTags.add(new Tag(INITIAL_DISK_TAG, INITIAL_DISK_TAG));
try {
String linuxVMId1 = provisionAWSEBSVMWithEC2Client(this.host, this.client, EC2_LINUX_AMI, this.subnetId, this.securityGroupId, this.blockDeviceMapping);
this.instancesToCleanUp.add(linuxVMId1);
waitForProvisioningToComplete(this.instancesToCleanUp, this.host, this.client, ZERO);
// Tag the first VM with a name and add some additional tags
tagResourcesWithName(this.client, VM_NAME, linuxVMId1);
List<Tag> linuxVMId1Tags = Arrays.asList(tag1, tag2);
// tag vm, default SG, VPC, Subnet and Disk
tagResources(this.client, linuxVMId1Tags, linuxVMId1);
tagResources(this.client, sgTags, this.securityGroupId);
tagResources(this.client, networkTags, this.vpcId);
tagResources(this.client, subnetTags, this.subnetId);
tagResources(this.client, diskTags, this.diskId);
enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
String linuxVMId2 = provisionAWSEBSVMWithEC2Client(this.host, this.client, EC2_LINUX_AMI, this.subnetId, this.securityGroupId, this.blockDeviceMapping);
this.instancesToCleanUp.add(linuxVMId2);
waitForProvisioningToComplete(this.instancesToCleanUp, this.host, this.client, ZERO);
// Name the second VM and add some tags
tagResourcesWithName(this.client, VM_UPDATED_NAME, linuxVMId2);
List<Tag> linuxVMId2Tags = Arrays.asList(tag2, tag3);
tagResources(this.client, linuxVMId2Tags, linuxVMId2);
// Un-tag the resources
unTagResources(this.client, sgTags, this.securityGroupId);
unTagResources(this.client, networkTags, this.vpcId);
unTagResources(this.client, subnetTags, this.subnetId);
unTagResources(this.client, diskTags, this.diskId);
// re-init tag arrays
sgTags = new ArrayList<>();
networkTags = new ArrayList<>();
subnetTags = new ArrayList<>();
diskTags = new ArrayList<>();
// new key-value set remotely should result in a new tag state created locally
// and a new tag link added to the SecurityGroupState, NetworkState, SubnetState and
// DiskState
sgTags.add(new Tag(SECONDARY_SG_TAG, SECONDARY_SG_TAG));
networkTags.add(new Tag(SECONDARY_VPC_TAG, SECONDARY_VPC_TAG));
subnetTags.add(new Tag(SECONDARY_SUBNET_TAG, SECONDARY_SUBNET_TAG));
diskTags.add(new Tag(SECONDARY_DISK_TAG, SECONDARY_DISK_TAG));
// tag again default SG, VPC, Subnet and Disk
tagResources(this.client, diskTags, this.diskId);
tagResources(this.client, sgTags, this.securityGroupId);
tagResources(this.client, networkTags, this.vpcId);
tagResources(this.client, subnetTags, this.subnetId);
enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
validateComputeName(linuxVMId1, VM_NAME);
validateComputeName(linuxVMId2, VM_UPDATED_NAME);
// Validate tag states number
int allTagsNumber = vmTags.size() + sgTags.size() + networkTags.size() + subnetTags.size() + diskTags.size();
queryDocumentsAndAssertExpectedCount(this.host, allTagsNumber, TagService.FACTORY_LINK, false);
ServiceDocumentQueryResult serviceDocumentQueryResult = queryAllFactoryResources(this.host, TagService.FACTORY_LINK);
Map<String, TagState> tagsMap = new HashMap<>();
for (Entry<String, Object> entry : serviceDocumentQueryResult.documents.entrySet()) {
tagsMap.put(entry.getKey(), Utils.fromJson(entry.getValue(), TagState.class));
}
// validate security group tags
Map<String, SecurityGroupState> allSecurityGroupStatesMap = ProvisioningUtils.<SecurityGroupState>getResourceStates(this.host, SecurityGroupService.FACTORY_LINK, SecurityGroupState.class);
SecurityGroupState defaultSgState = allSecurityGroupStatesMap.get(this.securityGroupId);
// ensure one link is deleted and one new is added to the sg state. One additional
// link is an internal tag.
assertNotNull(defaultSgState.tagLinks);
assertEquals("Wrong number of security-group tag links found.", 1 + internalTagsCount1, defaultSgState.tagLinks.size());
// validate vpc tags
Map<String, NetworkState> allNetworkStatesMap = ProvisioningUtils.<NetworkState>getResourceStates(this.host, NetworkService.FACTORY_LINK, NetworkState.class);
NetworkState defaultNetworkState = allNetworkStatesMap.get(this.vpcId);
// ensure one link is deleted and one new is added to the network state. One additional
// link is an internal tag.
assertEquals("Wrong number of network tag links found.", 1 + internalTagsCount1, defaultNetworkState.tagLinks.size());
// validate subnet tags
Map<String, SubnetState> allSubnetStatesMap = ProvisioningUtils.<SubnetState>getResourceStates(this.host, SubnetService.FACTORY_LINK, SubnetState.class);
SubnetState defaultSubnetState = allSubnetStatesMap.get(this.subnetId);
// ensure one link is deleted and one new is added to the subnet state. One additional
// link is an internal tag.
assertEquals("Wrong number of subnet tag links found.", 1 + internalTagsCount1, defaultSubnetState.tagLinks.size());
// validate disk tags
Map<String, DiskState> allDiskStatesMap = ProvisioningUtils.<DiskState>getResourceStates(this.host, DiskService.FACTORY_LINK, DiskState.class);
DiskState defaultDiskState = allDiskStatesMap.get(this.diskId);
// ensure one link is deleted and one new is added to the disk state
assertEquals("Wrong number of disk tag links found.", 1 + internalTagsCount1, defaultDiskState.tagLinks.size());
// ensure EBS disk has an internal type tag set
assertTrue(defaultDiskState.tagLinks.contains(TagsUtil.newTagState(TAG_KEY_TYPE, AWSResourceType.ebs_block.toString(), false, this.endpointState.tenantLinks).documentSelfLink));
// validate vm tags
Map<Tag, String> vmTagLinks = new HashMap<>();
for (Tag tag : vmTags) {
for (TagState tagState : tagsMap.values()) {
if (tagState.key.equals(tag.getKey())) {
vmTagLinks.put(tag, tagState.documentSelfLink);
}
}
}
ComputeState linuxVMId1ComputeState = getComputeByAWSId(this.host, linuxVMId1);
// compute has 2 remote tags + 1 local tag
assertEquals(linuxVMId1Tags.size() + internalTagsCount1, linuxVMId1ComputeState.tagLinks.size());
for (Tag tag : linuxVMId1Tags) {
assertTrue(linuxVMId1ComputeState.tagLinks.contains(vmTagLinks.get(tag)));
}
ComputeState linuxVMId2ComputeState = getComputeByAWSId(this.host, linuxVMId2);
assertEquals(linuxVMId2Tags.size() + internalTagsCount1, linuxVMId2ComputeState.tagLinks.size());
for (Tag tag : linuxVMId2Tags) {
assertTrue(linuxVMId2ComputeState.tagLinks.contains(vmTagLinks.get(tag)));
}
} catch (Throwable t) {
this.host.log("Exception occurred during test execution: %s", t.getMessage());
if (t instanceof AssertionError) {
fail("Assert exception occurred during test execution: " + t.getMessage());
}
} finally {
// un-tag default SG
unTagResources(this.client, sgTags, this.securityGroupId);
// un-tag default VPC
unTagResources(this.client, networkTags, this.vpcId);
// un-tag default Subnet
unTagResources(this.client, subnetTags, this.subnetId);
// un-tag default Disk
unTagResources(this.client, diskTags, this.diskId);
tearDownTestDisk(this.client, this.host, this.awsTestContext, this.isMock);
}
}
use of com.amazonaws.services.ec2.model.BlockDeviceMapping in project photon-model by vmware.
the class TestAWSImageEnumerationTask method lookupAwsImage.
// Kind of overhead cause it loads almost all images just to get the first one.
// Still we need that to make the tests STABLE.
private String lookupAwsImage(AmazonEC2AsyncClient client, String virtualizationType) {
DescribeImagesRequest request = new DescribeImagesRequest().withFilters(new Filter(AWSConstants.AWS_IMAGE_STATE_FILTER).withValues(AWSConstants.AWS_IMAGE_STATE_AVAILABLE)).withFilters(new Filter(AWSConstants.AWS_IMAGE_IS_PUBLIC_FILTER).withValues(Boolean.TRUE.toString())).withFilters(new Filter("root-device-type").withValues("ebs")).withFilters(new Filter(AWSConstants.AWS_IMAGE_VIRTUALIZATION_TYPE_FILTER).withValues(virtualizationType));
DescribeImagesResult describeImages = client.describeImages(request);
Image image = describeImages.getImages().stream().filter(img -> {
for (BlockDeviceMapping blockDeviceMapping : img.getBlockDeviceMappings()) {
// blockDeviceMapping can be with noDevice
EbsBlockDevice ebs = blockDeviceMapping.getEbs();
if (ebs != null) {
return true;
}
}
return false;
}).findFirst().get();
getHost().log(Level.INFO, "AWS '%s' image loaded (out of %s): %s [%s]", virtualizationType, describeImages.getImages().size(), image.getName(), image);
return image.getName();
}
use of com.amazonaws.services.ec2.model.BlockDeviceMapping in project photon-model by vmware.
the class AWSInstanceService method customizeBootDiskProperties.
private void customizeBootDiskProperties(DiskState bootDisk, String rootDeviceType, BlockDeviceMapping rootDeviceMapping, boolean hasHardConstraint, AWSInstanceContext aws) {
bootDisk.persistent = Boolean.FALSE;
if (rootDeviceType.equals(AWSStorageType.EBS.name().toLowerCase())) {
String requestedType = bootDisk.customProperties.get(DEVICE_TYPE);
EbsBlockDevice ebs = rootDeviceMapping.getEbs();
if (hasHardConstraint) {
validateIfDeviceTypesAreMatching(rootDeviceType, requestedType);
}
bootDisk.capacityMBytes = ebs.getVolumeSize() * 1024;
updateDeviceMapping(rootDeviceType, requestedType, rootDeviceMapping.getDeviceName(), ebs, bootDisk);
bootDisk.customProperties.put(DEVICE_TYPE, AWSStorageType.EBS.getName());
bootDisk.customProperties.put(VOLUME_TYPE, ebs.getVolumeType());
if (ebs.getIops() != null) {
bootDisk.customProperties.put(DISK_IOPS, String.valueOf(ebs.getIops()));
}
} else {
if (aws.instanceTypeInfo.dataDiskSizeInMB != null) {
this.logInfo(() -> "[AWSInstanceService] Instance-Store boot disk size is set to the " + "value supported by instance-type.");
bootDisk.capacityMBytes = aws.instanceTypeInfo.dataDiskSizeInMB;
bootDisk.customProperties.put(DEVICE_TYPE, AWSStorageType.INSTANCE_STORE.getName());
}
}
}
use of com.amazonaws.services.ec2.model.BlockDeviceMapping 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);
}
Aggregations