Search in sources :

Example 1 with ModelContext

use of com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext 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 2 with ModelContext

use of com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext in project cloudbreak by hortonworks.

the class CloudFormationTemplateBuilderTest method buildTestWithVPCAndIGWAndMultiSG.

@Test
public void buildTestWithVPCAndIGWAndMultiSG() {
    // GIVEN
    List<Group> groups = new ArrayList<>();
    Security security = new Security(emptyList(), List.of("multi-sg-id1", "multi-sg-id2"));
    groups.add(new Group("master", InstanceGroupType.CORE, emptyList(), security, instance, instanceAuthentication, instanceAuthentication.getLoginUserName(), "publickey", ROOT_VOLUME_SIZE, Optional.empty(), createGroupNetwork(), emptyMap()));
    CloudStack cloudStack = new CloudStack(groups, new Network(new Subnet(CIDR)), image, emptyMap(), emptyMap(), "template", instanceAuthentication, instanceAuthentication.getLoginUserName(), "publicKey", null);
    // WHEN
    modelContext = new ModelContext().withAuthenticatedContext(authenticatedContext).withStack(cloudStack).withExistingVpc(true).withExistingIGW(true).withExistingSubnetCidr(singletonList(existingSubnetCidr)).withExistinVpcCidr(List.of(existingSubnetCidr)).withStack(cloudStack).mapPublicIpOnLaunch(false).withOutboundInternetTraffic(OutboundInternetTraffic.ENABLED).withTemplate(awsCloudFormationTemplate);
    String templateString = cloudFormationTemplateBuilder.build(modelContext);
    // THEN
    Assertions.assertThat(JsonUtil.isValid(templateString)).overridingErrorMessage("Invalid JSON: " + templateString).isTrue();
    assertThat(templateString, containsString("\"multi-sg-id1\",\"multi-sg-id2\""));
    assertThat(templateString, not(containsString("SecurityGroupIngress")));
    assertThat(templateString, containsString("VPCId"));
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext) Group(com.sequenceiq.cloudbreak.cloud.model.Group) GroupNetwork(com.sequenceiq.cloudbreak.cloud.model.GroupNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) Security(com.sequenceiq.cloudbreak.cloud.model.Security) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Test(org.junit.jupiter.api.Test)

Example 3 with ModelContext

use of com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext in project cloudbreak by hortonworks.

the class CloudFormationTemplateBuilderTest method buildTestNoOutboundInternetTrafficButVpcCidrsAndPrefixListsAreEmpty.

@Test
public void buildTestNoOutboundInternetTrafficButVpcCidrsAndPrefixListsAreEmpty() {
    // GIVEN
    // WHEN
    modelContext = new ModelContext().withAuthenticatedContext(authenticatedContext).withStack(cloudStack).withExistingVpc(true).withExistingIGW(true).withExistingSubnetCidr(singletonList(existingSubnetCidr)).withExistinVpcCidr(List.of(existingSubnetCidr)).mapPublicIpOnLaunch(true).withEnableInstanceProfile(true).withInstanceProfileAvailable(true).withOutboundInternetTraffic(OutboundInternetTraffic.DISABLED).withVpcCidrs(List.of()).withPrefixListIds(List.of()).withTemplate(awsCloudFormationTemplate);
    String templateString = cloudFormationTemplateBuilder.build(modelContext);
    // THEN
    Assertions.assertThat(JsonUtil.isValid(templateString)).overridingErrorMessage("Invalid JSON: " + templateString).isTrue();
    assertThat(templateString, containsString("SecurityGroupIngress"));
    assertThat(templateString, not(containsString("SecurityGroupEgress")));
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Example 4 with ModelContext

use of com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext in project cloudbreak by hortonworks.

the class CloudFormationTemplateBuilderTest method buildTestNoOutboundInternetTrafficJustPrefixLists.

@Test
public void buildTestNoOutboundInternetTrafficJustPrefixLists() {
    // GIVEN
    // WHEN
    modelContext = new ModelContext().withAuthenticatedContext(authenticatedContext).withStack(cloudStack).withExistingVpc(true).withExistingIGW(true).withExistingSubnetCidr(singletonList(existingSubnetCidr)).withExistinVpcCidr(List.of(existingSubnetCidr)).mapPublicIpOnLaunch(true).withEnableInstanceProfile(true).withInstanceProfileAvailable(true).withOutboundInternetTraffic(OutboundInternetTraffic.DISABLED).withVpcCidrs(List.of()).withPrefixListIds(List.of("prefix1", "prefix2")).withTemplate(awsCloudFormationTemplate);
    String templateString = cloudFormationTemplateBuilder.build(modelContext);
    // THEN
    Assertions.assertThat(JsonUtil.isValid(templateString)).overridingErrorMessage("Invalid JSON: " + templateString).isTrue();
    assertThat(templateString, containsString("SecurityGroupIngress"));
    assertThat(templateString, stringContainsInOrder("SecurityGroupEgress", "prefix1", "prefix2"));
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Example 5 with ModelContext

use of com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext in project cloudbreak by hortonworks.

the class CloudFormationTemplateBuilderTest method buildTestWithRoleWithoutVPCAndIGWAndPublicIpOnLaunchAndInstanceProfile.

@Test
public void buildTestWithRoleWithoutVPCAndIGWAndPublicIpOnLaunchAndInstanceProfile() {
    CloudStack cloudStack = initCloudStackWithInstanceProfile();
    // WHEN
    modelContext = new ModelContext().withAuthenticatedContext(authenticatedContext).withStack(cloudStack).withExistingVpc(false).withExistingIGW(false).withExistingSubnetCidr(singletonList(existingSubnetCidr)).withExistinVpcCidr(List.of(existingSubnetCidr)).mapPublicIpOnLaunch(false).withEnableInstanceProfile(false).withInstanceProfileAvailable(true).withOutboundInternetTraffic(OutboundInternetTraffic.ENABLED).withTemplate(awsCloudFormationTemplate);
    String templateString = cloudFormationTemplateBuilder.build(modelContext);
    // THEN
    Assertions.assertThat(JsonUtil.isValid(templateString)).overridingErrorMessage("Invalid JSON: " + templateString).isTrue();
    assertThat(templateString, containsString("InstanceProfile"));
    assertThat(templateString, not(containsString("VPCId")));
    assertThat(templateString, not(containsString("SubnetCIDR")));
    assertThat(templateString, containsString("SubnetId"));
    assertThat(templateString, containsString("SubnetConfig"));
    assertThat(templateString, containsString("\"AttachGateway\""));
    assertThat(templateString, containsString("\"InternetGateway\""));
    assertThat(templateString, containsString("AvailabilitySet"));
    assertThat(templateString, containsString("SecurityGroupIngress"));
    assertThat(templateString, not(containsString("EIP")));
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext) Matchers.containsString(org.hamcrest.Matchers.containsString) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Test(org.junit.jupiter.api.Test)

Aggregations

ModelContext (com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext)47 Matchers.containsString (org.hamcrest.Matchers.containsString)44 Test (org.junit.jupiter.api.Test)43 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)20 Group (com.sequenceiq.cloudbreak.cloud.model.Group)9 JsonUtil (com.sequenceiq.cloudbreak.common.json.JsonUtil)9 Security (com.sequenceiq.cloudbreak.cloud.model.Security)8 ArrayList (java.util.ArrayList)6 AwsLoadBalancer (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer)5 Network (com.sequenceiq.cloudbreak.cloud.model.Network)5 AwsEfsFileSystem (com.sequenceiq.cloudbreak.cloud.aws.common.efs.AwsEfsFileSystem)3 GroupNetwork (com.sequenceiq.cloudbreak.cloud.model.GroupNetwork)3 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)3 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)3 AwsInstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.instance.AwsInstanceTemplate)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)2 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)2 AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView)2 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)2