Search in sources :

Example 41 with CloudResource

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource 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 42 with CloudResource

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource in project cloudbreak by hortonworks.

the class AwsResourceConnector method releaseReservedIp.

private void releaseReservedIp(AmazonEC2 client, Iterable<CloudResource> resources) {
    CloudResource elasticIpResource = getReservedIp(resources);
    if (elasticIpResource != null && elasticIpResource.getName() != null) {
        Address address;
        try {
            DescribeAddressesResult describeResult = client.describeAddresses(new DescribeAddressesRequest().withAllocationIds(elasticIpResource.getName()));
            address = describeResult.getAddresses().get(0);
        } catch (AmazonServiceException e) {
            if (e.getErrorMessage().equals("The allocation ID '" + elasticIpResource.getName() + "' does not exist")) {
                LOGGER.warn("Elastic IP with allocation ID '{}' not found. Ignoring IP release.", elasticIpResource.getName());
                return;
            } else {
                throw e;
            }
        }
        if (address.getAssociationId() != null) {
            client.disassociateAddress(new DisassociateAddressRequest().withAssociationId(elasticIpResource.getName()));
        }
        client.releaseAddress(new ReleaseAddressRequest().withAllocationId(elasticIpResource.getName()));
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) DisassociateAddressRequest(com.amazonaws.services.ec2.model.DisassociateAddressRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ReleaseAddressRequest(com.amazonaws.services.ec2.model.ReleaseAddressRequest)

Example 43 with CloudResource

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource in project cloudbreak by hortonworks.

the class AwsResourceConnector method saveGeneratedSubnet.

private void saveGeneratedSubnet(AuthenticatedContext ac, CloudStack stack, String cFStackName, AmazonCloudFormation client, PersistenceNotifier resourceNotifier) {
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    if (awsNetworkView.isExistingVPC()) {
        String vpcId = awsNetworkView.getExistingVPC();
        CloudResource vpc = new Builder().type(ResourceType.AWS_VPC).name(vpcId).build();
        resourceNotifier.notifyAllocation(vpc, ac.getCloudContext());
    } else {
        String vpcId = getCreatedVpc(cFStackName, client);
        CloudResource vpc = new Builder().type(ResourceType.AWS_VPC).name(vpcId).build();
        resourceNotifier.notifyAllocation(vpc, ac.getCloudContext());
    }
    if (awsNetworkView.isExistingSubnet()) {
        String subnetId = awsNetworkView.getExistingSubnet();
        CloudResource subnet = new Builder().type(ResourceType.AWS_SUBNET).name(subnetId).build();
        resourceNotifier.notifyAllocation(subnet, ac.getCloudContext());
    } else {
        String subnetId = getCreatedSubnet(cFStackName, client);
        CloudResource subnet = new Builder().type(ResourceType.AWS_SUBNET).name(subnetId).build();
        resourceNotifier.notifyAllocation(subnet, ac.getCloudContext());
    }
}
Also used : AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 44 with CloudResource

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource 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)

Example 45 with CloudResource

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource in project cloudbreak by hortonworks.

the class AwsResourceConnector method getCloudResources.

private List<CloudResource> getCloudResources(AuthenticatedContext ac, CloudStack stack, String cFStackName, AmazonCloudFormation client, AmazonEC2 amazonEC2Client, AmazonAutoScaling amazonASClient, boolean mapPublicIpOnLaunch) {
    List<CloudResource> cloudResources = new ArrayList<>();
    AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    scheduleStatusChecks(stack, ac, cloudFormationClient);
    suspendAutoScaling(ac, stack);
    if (mapPublicIpOnLaunch) {
        Map<String, String> eipAllocationIds = getElasticIpAllocationIds(cFStackName, client);
        List<Group> gateways = getGatewayGroups(stack.getGroups());
        for (Group gateway : gateways) {
            List<String> eips = getEipsForGatewayGroup(eipAllocationIds, gateway);
            List<String> instanceIds = getInstancesForGroup(ac, amazonASClient, client, gateway);
            associateElasticIpsToInstances(amazonEC2Client, eips, instanceIds);
        }
    }
    return cloudResources;
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) ArrayList(java.util.ArrayList) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient)

Aggregations

CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)79 ArrayList (java.util.ArrayList)33 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)29 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)20 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)17 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)16 Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)13 List (java.util.List)11 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)9 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)8 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)8 Location (com.sequenceiq.cloudbreak.cloud.model.Location)8 Group (com.sequenceiq.cloudbreak.cloud.model.Group)7 Operation (com.google.api.services.compute.model.Operation)6 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)6 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)6 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)6 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)6 Map (java.util.Map)6 CloudConnector (com.sequenceiq.cloudbreak.cloud.CloudConnector)5