Search in sources :

Example 1 with AwsInstanceProfileView

use of com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView in project cloudbreak by hortonworks.

the class AwsSetup method prerequisites.

@Override
public void prerequisites(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier persistenceNotifier) {
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String region = ac.getCloudContext().getLocation().getRegion().value();
    verifySpotInstances(stack);
    AwsCredentialView awsCredentialView = new AwsCredentialView(ac.getCloudCredential());
    AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack);
    if (awsClient.roleBasedCredential(awsCredentialView) && awsInstanceProfileView.isCreateInstanceProfile()) {
        validateInstanceProfileCreation(awsCredentialView);
    }
    if (awsNetworkView.isExistingVPC()) {
        try {
            AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, region);
            validateExistingIGW(awsNetworkView, amazonEC2Client);
            validateExistingSubnet(awsNetworkView, amazonEC2Client);
        } catch (AmazonServiceException e) {
            throw new CloudConnectorException(e.getErrorMessage());
        } catch (AmazonClientException e) {
            throw new CloudConnectorException(e.getMessage());
        }
    }
    validateExistingKeyPair(stack.getInstanceAuthentication(), credentialView, region);
    LOGGER.debug("setup has been executed");
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonClientException(com.amazonaws.AmazonClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) AwsInstanceProfileView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)

Example 2 with AwsInstanceProfileView

use of com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView in project cloudbreak by hortonworks.

the class AwsResourceConnector method getStackParameters.

private Collection<Parameter> getStackParameters(AuthenticatedContext ac, CloudStack stack, String stackName, String newSubnetCidr) {
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack);
    String keyPairName = awsClient.getKeyPairName(ac);
    if (awsClient.existingKeyPairNameSpecified(stack.getInstanceAuthentication())) {
        keyPairName = awsClient.getExistingKeyPairName(stack.getInstanceAuthentication());
    }
    Collection<Parameter> parameters = new ArrayList<>(asList(new Parameter().withParameterKey("CBUserData").withParameterValue(stack.getImage().getUserDataByType(InstanceGroupType.CORE)), new Parameter().withParameterKey("CBGateWayUserData").withParameterValue(stack.getImage().getUserDataByType(InstanceGroupType.GATEWAY)), new Parameter().withParameterKey("StackName").withParameterValue(stackName), new Parameter().withParameterKey("StackOwner").withParameterValue(ac.getCloudContext().getOwner()), new Parameter().withParameterKey("KeyName").withParameterValue(keyPairName), new Parameter().withParameterKey("AMI").withParameterValue(stack.getImage().getImageName()), new Parameter().withParameterKey("RootDeviceName").withParameterValue(getRootDeviceName(ac, stack))));
    if (awsInstanceProfileView.isUseExistingInstanceProfile() && awsInstanceProfileView.isEnableInstanceProfileStrategy()) {
        parameters.add(new Parameter().withParameterKey("InstanceProfile").withParameterValue(awsInstanceProfileView.getInstanceProfile()));
    }
    if (ac.getCloudContext().getLocation().getAvailabilityZone().value() != null) {
        parameters.add(new Parameter().withParameterKey("AvailabilitySet").withParameterValue(ac.getCloudContext().getLocation().getAvailabilityZone().value()));
    }
    if (awsNetworkView.isExistingVPC()) {
        parameters.add(new Parameter().withParameterKey("VPCId").withParameterValue(awsNetworkView.getExistingVPC()));
        if (awsNetworkView.isExistingIGW()) {
            parameters.add(new Parameter().withParameterKey("InternetGatewayId").withParameterValue(awsNetworkView.getExistingIGW()));
        }
        if (awsNetworkView.isExistingSubnet()) {
            parameters.add(new Parameter().withParameterKey("SubnetId").withParameterValue(awsNetworkView.getExistingSubnet()));
        } else {
            parameters.add(new Parameter().withParameterKey("SubnetCIDR").withParameterValue(newSubnetCidr));
        }
    }
    return parameters;
}
Also used : AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) ArrayList(java.util.ArrayList) Parameter(com.amazonaws.services.cloudformation.model.Parameter) AwsInstanceProfileView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)

Example 3 with AwsInstanceProfileView

use of com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView in project cloudbreak by hortonworks.

the class AwsResourceConnector method launch.

@Override
public List<CloudResourceStatus> launch(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, AdjustmentType adjustmentType, Long threshold) throws Exception {
    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.createAccess(credentialView, regionName);
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    boolean existingVPC = awsNetworkView.isExistingVPC();
    boolean existingSubnet = awsNetworkView.isExistingSubnet();
    boolean mapPublicIpOnLaunch = isMapPublicOnLaunch(awsNetworkView, amazonEC2Client);
    try {
        cfClient.describeStacks(new DescribeStacksRequest().withStackName(cFStackName));
        LOGGER.info("Stack already exists: {}", cFStackName);
    } catch (AmazonServiceException ignored) {
        CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).name(cFStackName).build();
        resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());
        String cidr = stack.getNetwork().getSubnet().getCidr();
        String subnet = isNoCIDRProvided(existingVPC, existingSubnet, cidr) ? findNonOverLappingCIDR(ac, stack) : cidr;
        AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack);
        ModelContext modelContext = new ModelContext().withAuthenticatedContext(ac).withStack(stack).withExistingVpc(existingVPC).withSnapshotId(getEbsSnapshotIdIfNeeded(ac, stack)).withExistingIGW(awsNetworkView.isExistingIGW()).withExistingSubnetCidr(existingSubnet ? getExistingSubnetCidr(ac, stack) : null).withExistingSubnetIds(existingSubnet ? awsNetworkView.getSubnetList() : null).mapPublicIpOnLaunch(mapPublicIpOnLaunch).withEnableInstanceProfile(awsInstanceProfileView.isEnableInstanceProfileStrategy()).withInstanceProfileAvailable(awsInstanceProfileView.isInstanceProfileAvailable()).withTemplate(stack.getTemplate()).withDefaultSubnet(subnet);
        String cfTemplate = cloudFormationTemplateBuilder.build(modelContext);
        LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
        cfClient.createStack(createCreateStackRequest(ac, stack, cFStackName, subnet, cfTemplate));
    }
    LOGGER.info("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());
    AmazonAutoScalingClient asClient = awsClient.createAutoScalingClient(credentialView, regionName);
    PollTask<Boolean> task = awsPollTaskFactory.newAwsCreateStackStatusCheckerTask(ac, cfClient, asClient, CREATE_COMPLETE, CREATE_FAILED, ERROR_STATUSES, cFStackName);
    try {
        Boolean statePollerResult = task.call();
        if (!task.completed(statePollerResult)) {
            syncPollingScheduler.schedule(task);
        }
    } catch (RuntimeException e) {
        throw new CloudConnectorException(e.getMessage(), e);
    }
    AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
    saveS3AccessRoleArn(ac, stack, cFStackName, cfClient, resourceNotifier);
    saveGeneratedSubnet(ac, stack, cFStackName, cfClient, resourceNotifier);
    List<CloudResource> cloudResources = getCloudResources(ac, stack, cFStackName, cfClient, amazonEC2Client, amazonASClient, mapPublicIpOnLaunch);
    return check(ac, cloudResources);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) ModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) AwsInstanceProfileView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)

Example 4 with AwsInstanceProfileView

use of com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView in project cloudbreak by hortonworks.

the class AwsResourceConnector method saveS3AccessRoleArn.

private void saveS3AccessRoleArn(AuthenticatedContext ac, CloudStack stack, String cFStackName, AmazonCloudFormation client, PersistenceNotifier resourceNotifier) {
    AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack);
    if (awsInstanceProfileView.isEnableInstanceProfileStrategy() && !awsInstanceProfileView.isInstanceProfileAvailable()) {
        String s3AccessRoleArn = getCreatedS3AccessRoleArn(cFStackName, client);
        CloudResource s3AccessRoleArnCloudResource = new Builder().type(ResourceType.S3_ACCESS_ROLE_ARN).name(s3AccessRoleArn).build();
        resourceNotifier.notifyAllocation(s3AccessRoleArnCloudResource, ac.getCloudContext());
    }
}
Also used : Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AwsInstanceProfileView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)

Aggregations

AwsInstanceProfileView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)4 AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)2 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)2 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)1 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)1 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)1 Parameter (com.amazonaws.services.cloudformation.model.Parameter)1 ModelContext (com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext)1 ArrayList (java.util.ArrayList)1