use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsLaunchService method launch.
public List<CloudResourceStatus> launch(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, AdjustmentTypeWithThreshold adjustmentTypeWithThreshold) throws Exception {
LOGGER.info("Launch AWS cluster with adjustment and threshold: {}", adjustmentTypeWithThreshold);
createKeyPair(ac, stack);
String cFStackName = cfStackUtil.getCfStackName(ac);
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
String regionName = ac.getCloudContext().getLocation().getRegion().value();
AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
AmazonEc2Client amazonEC2Client = awsClient.createEc2Client(credentialView, regionName);
Network network = stack.getNetwork();
AwsNetworkView awsNetworkView = new AwsNetworkView(network);
boolean mapPublicIpOnLaunch = awsNetworkService.isMapPublicOnLaunch(awsNetworkView, amazonEC2Client);
DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
ModelContext modelContext = null;
try {
cfClient.describeStacks(describeStacksRequest);
LOGGER.debug("Stack already exists: {}", cFStackName);
} catch (AmazonServiceException ignored) {
boolean existingVPC = awsNetworkView.isExistingVPC();
boolean existingSubnet = awsNetworkView.isExistingSubnet();
CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).availabilityZone(ac.getCloudContext().getLocation().getAvailabilityZone().value()).name(cFStackName).build();
resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());
String cidr = network.getSubnet().getCidr();
String subnet = isNoCIDRProvided(existingVPC, existingSubnet, cidr) ? awsNetworkService.findNonOverLappingCIDR(ac, stack) : cidr;
modelContext = awsModelService.buildDefaultModelContext(ac, stack, resourceNotifier);
String cfTemplate = cloudFormationTemplateBuilder.build(modelContext);
LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
cfClient.createStack(awsStackRequestHelper.createCreateStackRequest(ac, stack, cFStackName, subnet, cfTemplate));
}
LOGGER.debug("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());
Waiter<DescribeStacksRequest> creationWaiter = cfClient.waiters().stackCreateComplete();
StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
run(creationWaiter, describeStacksRequest, stackCancellationCheck, String.format("CloudFormation stack %s creation failed.", cFStackName), () -> awsCloudFormationErrorMessageProvider.getErrorReason(ac, cFStackName, ResourceStatus.CREATE_FAILED));
List<CloudResource> networkResources = saveGeneratedSubnet(ac, stack, cFStackName, cfClient, resourceNotifier);
suspendAutoscalingGoupsWhenNewInstancesAreReady(ac, stack);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
List<CloudResource> instances = cfStackUtil.getInstanceCloudResources(ac, cfClient, amazonASClient, stack.getGroups());
if (mapPublicIpOnLaunch) {
associatePublicIpsToGatewayInstances(stack, cFStackName, cfClient, amazonEC2Client, instances);
}
awsComputeResourceService.buildComputeResourcesForLaunch(ac, stack, adjustmentTypeWithThreshold, instances, networkResources);
awsTaggingService.tagRootVolumes(ac, amazonEC2Client, instances, stack.getTags());
awsCloudWatchService.addCloudWatchAlarmsForSystemFailures(instances, regionName, credentialView);
return awsResourceConnector.check(ac, instances);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsTerminateService method terminate.
public List<CloudResourceStatus> terminate(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) {
LOGGER.debug("Deleting stack: {}", ac.getCloudContext().getId());
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
AuthenticatedContextView authenticatedContextView = new AuthenticatedContextView(ac);
String regionName = authenticatedContextView.getRegion();
AmazonEc2Client amazonEC2Client = authenticatedContextView.getAmazonEC2Client();
AmazonCloudFormationClient amazonCloudFormationClient = awsClient.createCloudFormationClient(credentialView, regionName);
LOGGER.debug("Calling deleteCloudWatchAlarmsForSystemFailures from AwsTerminateService");
awsCloudWatchService.deleteAllCloudWatchAlarmsForSystemFailures(stack, regionName, credentialView);
waitAndDeleteCloudformationStack(ac, stack, resources, amazonCloudFormationClient);
awsComputeResourceService.deleteComputeResources(ac, stack, resources);
deleteKeyPair(ac, stack, amazonEC2Client, credentialView, regionName);
deleteLaunchConfiguration(resources, ac);
LOGGER.debug("Deleting stack finished");
return awsResourceConnector.check(ac, resources);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsTaggingServiceTest method tesTagRootVolumesForInstancesMoreThanSingleRequestLimitAndNotAllVolumesFound.
@Test
public void tesTagRootVolumesForInstancesMoreThanSingleRequestLimitAndNotAllVolumesFound() {
int instanceCount = 1200;
CloudResource instance = CloudResource.builder().type(ResourceType.AWS_INSTANCE).instanceId(INSTANCE_ID).name(INSTANCE_ID).status(CommonStatus.CREATED).build();
Instance awsInstance = new Instance().withInstanceId(INSTANCE_ID).withBlockDeviceMappings(new InstanceBlockDeviceMapping().withDeviceName("/dev/sda1").withEbs(new EbsInstanceBlockDevice().withVolumeId(VOLUME_ID))).withRootDeviceName("/dev/sda1");
Instance awsInstanceWithInvalidRootDisk = new Instance().withInstanceId(INSTANCE_ID).withBlockDeviceMappings(new InstanceBlockDeviceMapping().withDeviceName("/dev/sdb1").withEbs(new EbsInstanceBlockDevice().withVolumeId(VOLUME_ID))).withRootDeviceName("/dev/sda1");
List<CloudResource> instanceList = new ArrayList<>(instanceCount);
for (int i = 0; i < instanceCount; i++) {
instanceList.add(instance);
}
List<Instance> awsInstances = new ArrayList<>(instanceCount);
for (int i = 0; i < 1100; i++) {
awsInstances.add(awsInstance);
}
for (int i = 0; i < 100; i++) {
awsInstances.add(awsInstanceWithInvalidRootDisk);
}
DescribeInstancesResult describeResult = new DescribeInstancesResult().withReservations(new Reservation().withInstances(awsInstances));
AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
when(ec2Client.describeInstances(any())).thenReturn(describeResult);
awsTaggingService.tagRootVolumes(authenticatedContext(), ec2Client, instanceList, Map.of());
verify(ec2Client, times(2)).createTags(tagRequestCaptor.capture());
List<CreateTagsRequest> requests = tagRequestCaptor.getAllValues();
assertEquals(1000, requests.get(0).getResources().size());
assertEquals(100, requests.get(1).getResources().size());
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsNetworkServiceTest method testFindNonOverLappingCIDRWit20Vpc2.
@Test
public void testFindNonOverLappingCIDRWit20Vpc2() {
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity, createGroupNetwork(), emptyMap());
Map<String, Object> networkParameters = new HashMap<>();
networkParameters.put("vpcId", "vpc-12345678");
networkParameters.put("internetGatewayId", "igw-12345678");
Network network = new Network(new Subnet(null), networkParameters);
CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
CloudContext cloudContext = mock(CloudContext.class);
Location location = mock(Location.class);
Vpc vpc = mock(Vpc.class);
DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);
when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
when(cloudContext.getLocation()).thenReturn(location);
when(cloudContext.getName()).thenReturn(new String(new byte[] { (byte) 16 }));
when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
when(awsClient.createEc2Client(any(), any())).thenReturn(ec2Client);
when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
when(vpc.getCidrBlock()).thenReturn("10.0.0.0/20");
when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4));
when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/24");
when(subnet2.getCidrBlock()).thenReturn("10.0.1.0/24");
when(subnet3.getCidrBlock()).thenReturn("10.0.2.0/24");
when(subnet4.getCidrBlock()).thenReturn("10.0.3.0/24");
String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
Assert.assertEquals("10.0.4.0/24", cidr);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsNetworkServiceTest method testFindNonOverLappingCIDRForOneSpot.
@Test
public void testFindNonOverLappingCIDRForOneSpot() {
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity, createGroupNetwork(), emptyMap());
Map<String, Object> networkParameters = new HashMap<>();
networkParameters.put("vpcId", "vpc-12345678");
networkParameters.put("internetGatewayId", "igw-12345678");
Network network = new Network(new Subnet(null), networkParameters);
CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
CloudContext cloudContext = mock(CloudContext.class);
Location location = mock(Location.class);
Vpc vpc = mock(Vpc.class);
DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);
when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
when(cloudContext.getLocation()).thenReturn(location);
when(cloudContext.getName()).thenReturn("");
when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
when(awsClient.createEc2Client(any(), any())).thenReturn(ec2Client);
when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
when(vpc.getCidrBlock()).thenReturn("172.14.0.0/16");
when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
List<com.amazonaws.services.ec2.model.Subnet> subnetList = new ArrayList<>();
String startRange = "172.14.0.0";
for (int i = 0; i < 254; i++) {
startRange = incrementIp(startRange);
com.amazonaws.services.ec2.model.Subnet subnetMock = mock(com.amazonaws.services.ec2.model.Subnet.class);
when(subnetMock.getCidrBlock()).thenReturn(startRange + "/24");
subnetList.add(subnetMock);
}
when(subnetsResult.getSubnets()).thenReturn(subnetList);
String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
Assert.assertEquals("172.14.255.0/24", cidr);
}
Aggregations