Search in sources :

Example 46 with CloudResource

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

the class AwsResourceConnector method terminate.

@Override
public List<CloudResourceStatus> terminate(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) {
    LOGGER.info("Deleting stack: {}", ac.getCloudContext().getId());
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    if (resources != null && !resources.isEmpty()) {
        AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
        CloudResource stackResource = getCloudFormationStackResource(resources);
        if (stackResource == null) {
            return Collections.emptyList();
        }
        String cFStackName = stackResource.getName();
        LOGGER.info("Deleting CloudFormation stack for stack: {} [cf stack id: {}]", cFStackName, ac.getCloudContext().getId());
        DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
        try {
            retryService.testWith2SecDelayMax5Times(() -> {
                try {
                    cfClient.describeStacks(describeStacksRequest);
                } catch (AmazonServiceException e) {
                    if (!e.getErrorMessage().contains(cFStackName + " does not exist")) {
                        throw e;
                    }
                    throw new ActionWentFailException("Stack not exists");
                }
                return Boolean.TRUE;
            });
        } catch (ActionWentFailException ignored) {
            LOGGER.info(String.format("Stack not found with name: %s", cFStackName));
            AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
            releaseReservedIp(amazonEC2Client, resources);
            return Collections.emptyList();
        }
        resumeAutoScalingPolicies(ac, stack);
        DeleteStackRequest deleteStackRequest = new DeleteStackRequest().withStackName(cFStackName);
        cfClient.deleteStack(deleteStackRequest);
        PollTask<Boolean> task = awsPollTaskFactory.newAwsTerminateStackStatusCheckerTask(ac, cfClient, DELETE_COMPLETE, DELETE_FAILED, ERROR_STATUSES, cFStackName);
        try {
            Boolean statePollerResult = task.call();
            if (!task.completed(statePollerResult)) {
                syncPollingScheduler.schedule(task);
            }
        } catch (Exception e) {
            throw new CloudConnectorException(e.getMessage(), e);
        }
        AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
        releaseReservedIp(amazonEC2Client, resources);
        deleteKeyPair(ac, stack);
    } else if (resources != null) {
        AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
        releaseReservedIp(amazonEC2Client, resources);
        LOGGER.info("No CloudFormation stack saved for stack.");
    } else {
        LOGGER.info("No resources to release.");
    }
    return check(ac, resources);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) DeleteStackRequest(com.amazonaws.services.cloudformation.model.DeleteStackRequest) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) IOException(java.io.IOException) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient)

Example 47 with CloudResource

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

the class StackCreationActions method provisioningFinishedAction.

@Bean(name = "PROVISIONING_FINISHED_STATE")
public Action<?, ?> provisioningFinishedAction() {
    return new AbstractStackCreationAction<LaunchStackResult>(LaunchStackResult.class) {

        @Override
        protected void doExecute(StackContext context, LaunchStackResult payload, Map<Object, Object> variables) {
            Stack stack = stackCreationService.provisioningFinished(context, payload, variables);
            StackContext newContext = new StackContext(context.getFlowId(), stack, context.getCloudContext(), context.getCloudCredential(), context.getCloudStack());
            sendEvent(newContext);
        }

        @Override
        protected Selectable createRequest(StackContext context) {
            List<CloudInstance> cloudInstances = cloudStackConverter.buildInstances(context.getStack());
            List<CloudResource> cloudResources = cloudResourceConverter.convert(context.getStack().getResources());
            return new CollectMetadataRequest(context.getCloudContext(), context.getCloudCredential(), cloudResources, cloudInstances);
        }
    };
}
Also used : CollectMetadataRequest(com.sequenceiq.cloudbreak.cloud.event.instance.CollectMetadataRequest) StackContext(com.sequenceiq.cloudbreak.core.flow2.stack.StackContext) LaunchStackResult(com.sequenceiq.cloudbreak.cloud.event.resource.LaunchStackResult) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Map(java.util.Map) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Stack(com.sequenceiq.cloudbreak.domain.Stack) Bean(org.springframework.context.annotation.Bean)

Example 48 with CloudResource

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

the class StackDownscaleActions method stackDownscaleCollectResourcesAction.

@Bean(name = "DOWNSCALE_COLLECT_RESOURCES_STATE")
public Action<?, ?> stackDownscaleCollectResourcesAction() {
    return new AbstractStackDownscaleAction<StackDownscaleTriggerEvent>(StackDownscaleTriggerEvent.class) {

        @Override
        protected void doExecute(StackScalingFlowContext context, StackDownscaleTriggerEvent payload, Map<Object, Object> variables) {
            stackDownscaleService.startStackDownscale(context, payload);
            Stack stack = context.getStack();
            LOGGER.debug("Assembling downscale stack event for stack: {}", stack);
            List<CloudResource> resources = cloudResourceConverter.convert(stack.getResources());
            variables.put(RESOURCES, resources);
            List<CloudInstance> instances = new ArrayList<>();
            InstanceGroup group = stack.getInstanceGroupByInstanceGroupName(context.getInstanceGroupName());
            for (InstanceMetaData metaData : group.getAllInstanceMetaData()) {
                if (context.getInstanceIds().contains(metaData.getInstanceId())) {
                    CloudInstance cloudInstance = metadataConverter.convert(metaData);
                    instances.add(cloudInstance);
                }
            }
            variables.put(INSTANCES, instances);
            Selectable request = new DownscaleStackCollectResourcesRequest(context.getCloudContext(), context.getCloudCredential(), context.getCloudStack(), resources, instances);
            sendEvent(context.getFlowId(), request);
        }
    };
}
Also used : DownscaleStackCollectResourcesRequest(com.sequenceiq.cloudbreak.cloud.event.resource.DownscaleStackCollectResourcesRequest) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Stack(com.sequenceiq.cloudbreak.domain.Stack) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup) InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) Selectable(com.sequenceiq.cloudbreak.cloud.event.Selectable) StackDownscaleTriggerEvent(com.sequenceiq.cloudbreak.core.flow2.event.StackDownscaleTriggerEvent) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Map(java.util.Map) Bean(org.springframework.context.annotation.Bean)

Example 49 with CloudResource

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

the class AbstractStackTerminationAction method createFlowContext.

@Override
protected StackTerminationContext createFlowContext(String flowId, StateContext<StackTerminationState, StackTerminationEvent> stateContext, P payload) {
    Stack stack = stackService.getByIdWithLists(payload.getStackId());
    MDCBuilder.buildMdcContext(stack);
    Location location = location(region(stack.getRegion()), availabilityZone(stack.getAvailabilityZone()));
    CloudContext cloudContext = new CloudContext(stack.getId(), stack.getName(), stack.cloudPlatform(), stack.getOwner(), stack.getPlatformVariant(), location);
    CloudCredential cloudCredential = credentialConverter.convert(stack.getCredential());
    CloudStack cloudStack = cloudStackConverter.convert(stack);
    List<CloudResource> resources = cloudResourceConverter.convert(stack.getResources());
    return new StackTerminationContext(flowId, stack, cloudContext, cloudCredential, cloudStack, resources);
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Stack(com.sequenceiq.cloudbreak.domain.Stack) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Example 50 with CloudResource

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

the class AbstractInstanceTerminationAction method createFlowContext.

@Override
protected InstanceTerminationContext createFlowContext(String flowId, StateContext<InstanceTerminationState, InstanceTerminationEvent> stateContext, P payload) {
    Stack stack = stackService.getByIdWithLists(payload.getStackId());
    MDCBuilder.buildMdcContext(stack);
    Location location = location(region(stack.getRegion()), availabilityZone(stack.getAvailabilityZone()));
    CloudContext cloudContext = new CloudContext(stack.getId(), stack.getName(), stack.cloudPlatform(), stack.getOwner(), stack.getPlatformVariant(), location);
    CloudCredential cloudCredential = credentialConverter.convert(stack.getCredential());
    Set<String> instanceIds = payload.getInstanceIds();
    CloudStack cloudStack = cloudStackConverter.convert(stack, instanceIds);
    List<CloudResource> cloudResources = cloudResourceConverter.convert(stack.getResources());
    List<InstanceMetaData> instanceMetaDataList = new ArrayList<>();
    List<CloudInstance> cloudInstances = new ArrayList<>();
    for (String instanceId : instanceIds) {
        InstanceMetaData instanceMetaData = instanceMetaDataRepository.findByInstanceId(stack.getId(), instanceId);
        CloudInstance cloudInstance = metadataConverter.convert(instanceMetaData);
        instanceMetaDataList.add(instanceMetaData);
        cloudInstances.add(cloudInstance);
    }
    return new InstanceTerminationContext(flowId, stack, cloudContext, cloudCredential, cloudStack, cloudResources, cloudInstances, instanceMetaDataList);
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Stack(com.sequenceiq.cloudbreak.domain.Stack) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

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