Search in sources :

Example 1 with ActionWentFailException

use of com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException 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 2 with ActionWentFailException

use of com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException in project cloudbreak by hortonworks.

the class HeartbeatService method heartbeat.

@Scheduled(cron = "${cb.ha.heartbeat.rate:0/30 * * * * *}")
public void heartbeat() {
    if (cloudbreakNodeConfig.isNodeIdSpecified()) {
        String nodeId = cloudbreakNodeConfig.getId();
        try {
            retryService.testWith2SecDelayMax5Times(() -> {
                try {
                    CloudbreakNode self = cloudbreakNodeRepository.findOne(nodeId);
                    if (self == null) {
                        self = new CloudbreakNode(nodeId);
                    }
                    self.setLastUpdated(clock.getCurrentTime());
                    cloudbreakNodeRepository.save(self);
                    return true;
                } catch (RuntimeException e) {
                    LOGGER.error("Failed to update the heartbeat timestamp", e);
                    metricService.incrementMetricCounter(MetricType.HEARTBEAT_UPDATE_FAILED);
                    throw new ActionWentFailException(e.getMessage());
                }
            });
        } catch (ActionWentFailException af) {
            LOGGER.error(String.format("Failed to update the heartbeat timestamp 5 times for node %s: %s", nodeId, af.getMessage()));
            cancelEveryFlowWithoutDbUpdate();
        }
        cancelInvalidFlows();
    }
}
Also used : CloudbreakNode(com.sequenceiq.cloudbreak.domain.CloudbreakNode) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 3 with ActionWentFailException

use of com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException in project cloudbreak by hortonworks.

the class AzureResourceConnector method terminate.

@Override
public List<CloudResourceStatus> terminate(AuthenticatedContext authenticatedContext, CloudStack stack, List<CloudResource> resources) {
    AzureClient client = authenticatedContext.getParameter(AzureClient.class);
    for (CloudResource resource : resources) {
        try {
            try {
                retryService.testWith2SecDelayMax5Times(() -> {
                    if (!client.resourceGroupExists(resource.getName())) {
                        throw new ActionWentFailException("Resource group not exists");
                    }
                    return true;
                });
                client.deleteResourceGroup(resource.getName());
            } catch (ActionWentFailException ignored) {
                LOGGER.info(String.format("Resource group not found with name: %s", resource.getName()));
            }
            if (azureStorage.isPersistentStorage(azureStorage.getPersistentStorageName(stack))) {
                CloudContext cloudCtx = authenticatedContext.getCloudContext();
                AzureCredentialView azureCredentialView = new AzureCredentialView(authenticatedContext.getCloudCredential());
                String imageStorageName = azureStorage.getImageStorageName(azureCredentialView, cloudCtx, stack);
                String imageResourceGroupName = azureStorage.getImageResourceGroupName(cloudCtx, stack);
                String diskContainer = azureStorage.getDiskContainerName(cloudCtx);
                deleteContainer(client, imageResourceGroupName, imageStorageName, diskContainer);
            }
        } catch (CloudException e) {
            if (e.response().code() != AzureConstants.NOT_FOUND) {
                throw new CloudConnectorException(String.format("Could not delete resource group: %s", resource.getName()), e);
            } else {
                return check(authenticatedContext, Collections.emptyList());
            }
        }
    }
    return check(authenticatedContext, resources);
}
Also used : AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) AzureCredentialView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudException(com.microsoft.azure.CloudException) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)

Aggregations

ActionWentFailException (com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)3 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)1 DeleteStackRequest (com.amazonaws.services.cloudformation.model.DeleteStackRequest)1 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)1 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 CloudException (com.microsoft.azure.CloudException)1 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)1 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)1 AzureCredentialView (com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView)1 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)1 CloudbreakNode (com.sequenceiq.cloudbreak.domain.CloudbreakNode)1 IOException (java.io.IOException)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1