Search in sources :

Example 1 with CloudResourceStatus

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

the class AwsResourceConnector method upscale.

@Override
public List<CloudResourceStatus> upscale(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) {
    resumeAutoScaling(ac, stack);
    AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    List<Group> scaledGroups = getScaledGroups(stack);
    for (Group group : scaledGroups) {
        String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, cloudFormationClient, group.getName());
        amazonASClient.updateAutoScalingGroup(new UpdateAutoScalingGroupRequest().withAutoScalingGroupName(asGroupName).withMaxSize(group.getInstancesSize()).withDesiredCapacity(group.getInstancesSize()));
        LOGGER.info("Updated Auto Scaling group's desiredCapacity: [stack: '{}', to: '{}']", ac.getCloudContext().getId(), resources.size());
    }
    scheduleStatusChecks(stack, ac, cloudFormationClient);
    suspendAutoScaling(ac, stack);
    boolean mapPublicIpOnLaunch = isMapPublicOnLaunch(new AwsNetworkView(stack.getNetwork()), amazonEC2Client);
    List<Group> gateways = getGatewayGroups(scaledGroups);
    if (mapPublicIpOnLaunch && !gateways.isEmpty()) {
        String cFStackName = getCloudFormationStackResource(resources).getName();
        Map<String, String> eipAllocationIds = getElasticIpAllocationIds(cFStackName, cloudFormationClient);
        for (Group gateway : gateways) {
            List<String> eips = getEipsForGatewayGroup(eipAllocationIds, gateway);
            List<String> freeEips = getFreeIps(eips, amazonEC2Client);
            List<String> instanceIds = getInstancesForGroup(ac, amazonASClient, cloudFormationClient, gateway);
            List<String> newInstances = instanceIds.stream().filter(iid -> gateway.getInstances().stream().noneMatch(inst -> iid.equals(inst.getInstanceId()))).collect(Collectors.toList());
            associateElasticIpsToInstances(amazonEC2Client, freeEips, newInstances);
        }
    }
    return singletonList(new CloudResourceStatus(getCloudFormationStackResource(resources), ResourceStatus.UPDATED));
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) CreateStackRequest(com.amazonaws.services.cloudformation.model.CreateStackRequest) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) ReleaseAddressRequest(com.amazonaws.services.ec2.model.ReleaseAddressRequest) ROLLBACK_IN_PROGRESS(com.amazonaws.services.cloudformation.model.StackStatus.ROLLBACK_IN_PROGRESS) SuspendProcessesRequest(com.amazonaws.services.autoscaling.model.SuspendProcessesRequest) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) ROLLBACK_FAILED(com.amazonaws.services.cloudformation.model.StackStatus.ROLLBACK_FAILED) AdjustmentType(com.sequenceiq.cloudbreak.api.model.AdjustmentType) SubnetInfo(org.apache.commons.net.util.SubnetUtils.SubnetInfo) AmazonServiceException(com.amazonaws.AmazonServiceException) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Retry(com.sequenceiq.cloudbreak.service.Retry) SyncPollingScheduler(com.sequenceiq.cloudbreak.cloud.scheduler.SyncPollingScheduler) AwsInstanceProfileView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) DELETE_FAILED(com.amazonaws.services.cloudformation.model.StackStatus.DELETE_FAILED) AwsPollTaskFactory(com.sequenceiq.cloudbreak.cloud.aws.task.AwsPollTaskFactory) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) StackStatus(com.amazonaws.services.cloudformation.model.StackStatus) DescribeKeyPairsRequest(com.amazonaws.services.ec2.model.DescribeKeyPairsRequest) InstanceGroupType(com.sequenceiq.cloudbreak.api.model.InstanceGroupType) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Lists(com.google.common.collect.Lists) UpdateAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) Service(org.springframework.stereotype.Service) Image(com.amazonaws.services.ec2.model.Image) Filter(com.amazonaws.services.ec2.model.Filter) Network(com.sequenceiq.cloudbreak.cloud.model.Network) DeleteStackRequest(com.amazonaws.services.cloudformation.model.DeleteStackRequest) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DELETE_COMPLETE(com.amazonaws.services.cloudformation.model.StackStatus.DELETE_COMPLETE) CREATE_FAILED(com.amazonaws.services.cloudformation.model.StackStatus.CREATE_FAILED) DetachInstancesRequest(com.amazonaws.services.autoscaling.model.DetachInstancesRequest) Parameter(com.amazonaws.services.cloudformation.model.Parameter) ROLLBACK_COMPLETE(com.amazonaws.services.cloudformation.model.StackStatus.ROLLBACK_COMPLETE) IOException(java.io.IOException) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) ImportKeyPairRequest(com.amazonaws.services.ec2.model.ImportKeyPairRequest) Configuration(freemarker.template.Configuration) InetAddresses(com.google.common.net.InetAddresses) SubnetUtils(org.apache.commons.net.util.SubnetUtils) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) LoggerFactory(org.slf4j.LoggerFactory) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) OnFailure(com.amazonaws.services.cloudformation.model.OnFailure) TlsInfo(com.sequenceiq.cloudbreak.cloud.model.TlsInfo) AmazonCloudFormation(com.amazonaws.services.cloudformation.AmazonCloudFormation) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DescribeImagesRequest(com.amazonaws.services.ec2.model.DescribeImagesRequest) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) CREATE_COMPLETE(com.amazonaws.services.cloudformation.model.StackStatus.CREATE_COMPLETE) Collection(java.util.Collection) Output(com.amazonaws.services.cloudformation.model.Output) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) Collectors(java.util.stream.Collectors) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) ModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Entry(java.util.Map.Entry) Optional(java.util.Optional) AmazonAutoScaling(com.amazonaws.services.autoscaling.AmazonAutoScaling) HashMap(java.util.HashMap) Inject(javax.inject.Inject) DeleteKeyPairRequest(com.amazonaws.services.ec2.model.DeleteKeyPairRequest) DisassociateAddressRequest(com.amazonaws.services.ec2.model.DisassociateAddressRequest) Charset(java.nio.charset.Charset) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ResumeProcessesRequest(com.amazonaws.services.autoscaling.model.ResumeProcessesRequest) PollTask(com.sequenceiq.cloudbreak.cloud.task.PollTask) DescribeImagesResult(com.amazonaws.services.ec2.model.DescribeImagesResult) Subnet(com.amazonaws.services.ec2.model.Subnet) ResourceConnector(com.sequenceiq.cloudbreak.cloud.ResourceConnector) Logger(org.slf4j.Logger) ResourceType(com.sequenceiq.cloudbreak.common.type.ResourceType) Address(com.amazonaws.services.ec2.model.Address) Vpc(com.amazonaws.services.ec2.model.Vpc) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) Collections(java.util.Collections) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) UpdateAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient)

Example 2 with CloudResourceStatus

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

the class AzureResourceConnector method upscale.

@Override
public List<CloudResourceStatus> upscale(AuthenticatedContext authenticatedContext, CloudStack stack, List<CloudResource> resources) {
    AzureClient client = authenticatedContext.getParameter(AzureClient.class);
    AzureCredentialView azureCredentialView = new AzureCredentialView(authenticatedContext.getCloudCredential());
    String stackName = azureUtils.getStackName(authenticatedContext.getCloudContext());
    AzureStackView azureStackView = getAzureStack(azureCredentialView, authenticatedContext.getCloudContext(), stack, getNumberOfAvailableIPsInSubnets(client, stack.getNetwork()));
    String customImageId = azureStorage.getCustomImageId(client, authenticatedContext, stack);
    String template = azureTemplateBuilder.build(stackName, customImageId, azureCredentialView, azureStackView, authenticatedContext.getCloudContext(), stack);
    String parameters = azureTemplateBuilder.buildParameters(authenticatedContext.getCloudCredential(), stack.getNetwork(), stack.getImage());
    String resourceGroupName = azureUtils.getResourceGroupName(authenticatedContext.getCloudContext());
    try {
        String region = authenticatedContext.getCloudContext().getLocation().getRegion().value();
        Map<String, AzureDiskType> storageAccounts = azureStackView.getStorageAccounts();
        for (Entry<String, AzureDiskType> entry : storageAccounts.entrySet()) {
            azureStorage.createStorage(client, entry.getKey(), entry.getValue(), resourceGroupName, region, azureStorage.isEncrytionNeeded(stack.getParameters()), stack.getTags());
        }
        Deployment templateDeployment = client.createTemplateDeployment(stackName, stackName, template, parameters);
        LOGGER.info("created template deployment for upscale: {}", templateDeployment.exportTemplate().template());
        CloudResource armTemplate = resources.stream().filter(r -> r.getType() == ResourceType.ARM_TEMPLATE).findFirst().orElseThrow(() -> new CloudConnectorException(String.format("Arm Template not found for: %s  ", stackName)));
        return Collections.singletonList(new CloudResourceStatus(armTemplate, ResourceStatus.IN_PROGRESS));
    } catch (CloudException e) {
        LOGGER.error("Upscale error, cloud exception happened: ", e);
        if (e.body() != null && e.body().details() != null) {
            String details = e.body().details().stream().map(CloudError::message).collect(Collectors.joining(", "));
            throw new CloudConnectorException(String.format("Stack upscale failed, status code %s, error message: %s, details: %s", e.body().code(), e.body().message(), details));
        } else {
            throw new CloudConnectorException(String.format("Stack upscale failed: '%s', please go to Azure Portal for detailed message", e));
        }
    } catch (Exception e) {
        throw new CloudConnectorException(String.format("Could not upscale: %s  ", stackName), e);
    }
}
Also used : CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Deployment(com.microsoft.azure.management.resources.Deployment) AzureStackView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureStackView) CloudError(com.microsoft.azure.CloudError) CloudException(com.microsoft.azure.CloudException) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudException(com.microsoft.azure.CloudException) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) AzureCredentialView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 3 with CloudResourceStatus

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

the class AzureResourceConnector method check.

@Override
public List<CloudResourceStatus> check(AuthenticatedContext authenticatedContext, List<CloudResource> resources) {
    List<CloudResourceStatus> result = new ArrayList<>();
    AzureClient client = authenticatedContext.getParameter(AzureClient.class);
    String stackName = azureUtils.getStackName(authenticatedContext.getCloudContext());
    for (CloudResource resource : resources) {
        switch(resource.getType()) {
            case ARM_TEMPLATE:
                LOGGER.info("Checking Azure group stack status of: {}", stackName);
                try {
                    CloudResourceStatus templateResourceStatus;
                    if (client.templateDeploymentExists(stackName, stackName)) {
                        Deployment resourceGroupDeployment = client.getTemplateDeployment(stackName, stackName);
                        templateResourceStatus = azureUtils.getTemplateStatus(resource, resourceGroupDeployment, client, stackName);
                    } else {
                        templateResourceStatus = new CloudResourceStatus(resource, ResourceStatus.DELETED);
                    }
                    result.add(templateResourceStatus);
                } catch (CloudException e) {
                    if (e.response().code() == AzureConstants.NOT_FOUND) {
                        result.add(new CloudResourceStatus(resource, ResourceStatus.DELETED));
                    } else {
                        throw new CloudConnectorException(e.body().message(), e);
                    }
                } catch (RuntimeException e) {
                    throw new CloudConnectorException(String.format("Invalid resource exception: %s", e.getMessage()), e);
                }
                break;
            case AZURE_NETWORK:
            case AZURE_SUBNET:
                break;
            default:
                throw new CloudConnectorException(String.format("Invalid resource type: %s", resource.getType()));
        }
    }
    return result;
}
Also used : AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) ArrayList(java.util.ArrayList) Deployment(com.microsoft.azure.management.resources.Deployment) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudException(com.microsoft.azure.CloudException)

Example 4 with CloudResourceStatus

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

the class AzureUtils method getTemplateStatus.

public CloudResourceStatus getTemplateStatus(CloudResource resource, Deployment templateDeployment, AzureClient access, String stackName) {
    String status = templateDeployment.provisioningState();
    LOGGER.info("Azure stack status of: {}  is: {}", resource.getName(), status);
    ResourceStatus resourceStatus = AzureStackStatus.mapResourceStatus(status);
    CloudResourceStatus armResourceStatus = null;
    if (ResourceStatus.FAILED.equals(resourceStatus)) {
        LOGGER.debug("Cloud resource status: {}", resourceStatus);
        try {
            // TODO: discuss with Doktorics why this is needed
            DeploymentOperations templateDeploymentOperations = access.getTemplateDeploymentOperations(stackName, stackName);
            for (DeploymentOperation deploymentOperation : templateDeploymentOperations.list()) {
                if ("Failed".equals(deploymentOperation.provisioningState())) {
                    String statusMessage = (String) deploymentOperation.statusMessage();
                    armResourceStatus = new CloudResourceStatus(resource, AzureStackStatus.mapResourceStatus(status), statusMessage);
                    break;
                }
            }
        } catch (RuntimeException e) {
            armResourceStatus = new CloudResourceStatus(resource, AzureStackStatus.mapResourceStatus(status), e.getMessage());
        }
    } else {
        LOGGER.debug("Cloud resource status: {}", resourceStatus);
        armResourceStatus = new CloudResourceStatus(resource, AzureStackStatus.mapResourceStatus(status));
    }
    return armResourceStatus;
}
Also used : CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) DeploymentOperation(com.microsoft.azure.management.resources.DeploymentOperation) DeploymentOperations(com.microsoft.azure.management.resources.DeploymentOperations)

Example 5 with CloudResourceStatus

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

the class OpenStackResourceConnector method checkResourceStatus.

private CloudResourceStatus checkResourceStatus(AuthenticatedContext authenticatedContext, String stackName, Collection<CloudResource> list, CloudResource resource, ResourceType openstackNetwork) {
    CloudResourceStatus result;
    result = getCloudResourceStatus(list, openstackNetwork, resource);
    if (result.getStatus().isPermanent()) {
        persistenceNotifier.notifyAllocation(result.getCloudResource(), authenticatedContext.getCloudContext());
    }
    return result;
}
Also used : CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)

Aggregations

CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)43 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)29 ArrayList (java.util.ArrayList)18 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)14 List (java.util.List)11 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)9 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)7 Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)6 CloudConnector (com.sequenceiq.cloudbreak.cloud.CloudConnector)5 ResourceStatus (com.sequenceiq.cloudbreak.cloud.model.ResourceStatus)5 ResourcesStatePollerResult (com.sequenceiq.cloudbreak.cloud.task.ResourcesStatePollerResult)5 Group (com.sequenceiq.cloudbreak.cloud.model.Group)4 Platform (com.sequenceiq.cloudbreak.cloud.model.Platform)4 NetworkResourceBuilder (com.sequenceiq.cloudbreak.cloud.template.NetworkResourceBuilder)4 CloudException (com.microsoft.azure.CloudException)3 Deployment (com.microsoft.azure.management.resources.Deployment)3 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)3 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)3 ComputeResourceBuilder (com.sequenceiq.cloudbreak.cloud.template.ComputeResourceBuilder)3 ResourceBuilderContext (com.sequenceiq.cloudbreak.cloud.template.context.ResourceBuilderContext)3