Search in sources :

Example 11 with AmazonEc2Client

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);
}
Also used : DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) CloudFormationTemplateBuilder(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) ModelContext(com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Example 12 with AmazonEc2Client

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);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Example 13 with AmazonEc2Client

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());
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) EbsInstanceBlockDevice(com.amazonaws.services.ec2.model.EbsInstanceBlockDevice) Test(org.junit.Test)

Example 14 with AmazonEc2Client

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);
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) HashMap(java.util.HashMap) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Vpc(com.amazonaws.services.ec2.model.Vpc) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) GroupNetwork(com.sequenceiq.cloudbreak.cloud.model.GroupNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) Location(com.sequenceiq.cloudbreak.cloud.model.Location) Test(org.junit.Test)

Example 15 with AmazonEc2Client

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);
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) Group(com.sequenceiq.cloudbreak.cloud.model.Group) HashMap(java.util.HashMap) Vpc(com.amazonaws.services.ec2.model.Vpc) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) GroupNetwork(com.sequenceiq.cloudbreak.cloud.model.GroupNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Location(com.sequenceiq.cloudbreak.cloud.model.Location) Test(org.junit.Test)

Aggregations

AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)97 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)44 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)41 Test (org.junit.Test)31 ArrayList (java.util.ArrayList)30 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)29 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)29 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)28 HashMap (java.util.HashMap)28 Group (com.sequenceiq.cloudbreak.cloud.model.Group)24 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)24 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)23 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)23 Network (com.sequenceiq.cloudbreak.cloud.model.Network)23 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)22 AmazonServiceException (com.amazonaws.AmazonServiceException)21 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)21 List (java.util.List)21 Vpc (com.amazonaws.services.ec2.model.Vpc)20 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)20