Search in sources :

Example 16 with InstanceGroup

use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup in project cloudbreak by hortonworks.

the class EmbeddedDatabaseService method calculateVolumeCountOnGatewayGroup.

private int calculateVolumeCountOnGatewayGroup(Stack stack) {
    Optional<InstanceGroup> gatewayGroup = stack.getInstanceGroups().stream().filter(ig -> ig.getInstanceGroupType() == InstanceGroupType.GATEWAY).findFirst();
    Template template = gatewayGroup.map(InstanceGroup::getTemplate).orElse(null);
    return template == null ? 0 : template.getVolumeTemplates().stream().filter(volumeTemplate -> volumeTemplate.getUsageType() == VolumeUsageType.DATABASE).mapToInt(VolumeTemplate::getVolumeCount).sum();
}
Also used : Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) StringUtils(org.apache.commons.lang3.StringUtils) Template(com.sequenceiq.cloudbreak.domain.Template) InstanceGroupType(com.sequenceiq.common.api.type.InstanceGroupType) Inject(javax.inject.Inject) VolumeTemplate(com.sequenceiq.cloudbreak.domain.VolumeTemplate) Component(org.springframework.stereotype.Component) DatabaseAvailabilityType(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.database.DatabaseAvailabilityType) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Optional(java.util.Optional) CloudParameterCache(com.sequenceiq.cloudbreak.service.stack.CloudParameterCache) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup) VolumeUsageType(com.sequenceiq.cloudbreak.domain.VolumeUsageType) VolumeTemplate(com.sequenceiq.cloudbreak.domain.VolumeTemplate) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup) Template(com.sequenceiq.cloudbreak.domain.Template) VolumeTemplate(com.sequenceiq.cloudbreak.domain.VolumeTemplate)

Example 17 with InstanceGroup

use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup in project cloudbreak by hortonworks.

the class StackDecorator method prepareInstanceGroups.

private void prepareInstanceGroups(Stack subject, StackV4Request request, Credential credential, User user, DetailedEnvironmentResponse environment) {
    Map<String, InstanceGroupParameterResponse> instanceGroupParameterResponse = cloudParameterService.getInstanceGroupParameters(extendedCloudCredentialConverter.convert(credential), getInstanceGroupParameterRequests(subject));
    CloudbreakUser cloudbreakUser = legacyRestRequestThreadLocalService.getCloudbreakUser();
    subject.getInstanceGroups().parallelStream().forEach(instanceGroup -> {
        subject.getCluster().getHostGroups().stream().filter(hostGroup -> hostGroup.getName().equals(instanceGroup.getGroupName())).forEach(hostGroup -> hostGroup.setInstanceGroup(instanceGroup));
        legacyRestRequestThreadLocalService.setCloudbreakUser(cloudbreakUser);
        updateInstanceGroupParameters(instanceGroupParameterResponse, instanceGroup);
        if (instanceGroup.getTemplate() != null) {
            Template template = instanceGroup.getTemplate();
            if (template.getId() == null) {
                template.setCloudPlatform(credential.cloudPlatform());
                PlacementSettingsV4Request placement = request.getPlacement();
                String availabilityZone = placement != null ? placement.getAvailabilityZone() : subject.getAvailabilityZone();
                String region = placement != null ? placement.getRegion() : subject.getRegion();
                CdpResourceType cdpResourceType = cdpResourceTypeProvider.fromStackType(request.getType());
                template = templateDecorator.decorate(credential, template, region, availabilityZone, subject.getPlatformVariant(), cdpResourceType);
                template.setWorkspace(subject.getWorkspace());
                setupDatabaseAttachedVolume(subject, instanceGroup, template);
                template = templateService.create(user, template);
                instanceGroup.setTemplate(template);
            }
        }
        if (instanceGroup.getSecurityGroup() != null) {
            SecurityGroup securityGroup = instanceGroup.getSecurityGroup();
            if (securityGroup.getId() == null) {
                securityGroup.setCloudPlatform(credential.cloudPlatform());
                securityGroup.setWorkspace(subject.getWorkspace());
                securityGroup = securityGroupService.create(user, securityGroup);
                instanceGroup.setSecurityGroup(securityGroup);
            }
        }
        if (instanceGroup.getInstanceGroupNetwork() != null) {
            InstanceGroupNetwork ign = instanceGroup.getInstanceGroupNetwork();
            instanceGroup.setAvailabilityZones(getAvailabilityZoneFromEnv(instanceGroup, environment));
            if (ign.getId() == null) {
                ign.setCloudPlatform(credential.cloudPlatform());
                ign = instanceGroupNetworkService.create(ign);
                instanceGroup.setInstanceGroupNetwork(ign);
            }
        }
    });
}
Also used : InstanceGroupToInstanceGroupParameterRequestConverter(com.sequenceiq.cloudbreak.converter.v4.stacks.instancegroup.InstanceGroupToInstanceGroupParameterRequestConverter) ResourceGroupUsage(com.sequenceiq.environment.api.v1.environment.model.request.azure.ResourceGroupUsage) LoggerFactory(org.slf4j.LoggerFactory) Measure(com.sequenceiq.cloudbreak.aspect.Measure) PlatformParametersConsts(com.sequenceiq.cloudbreak.cloud.PlatformParametersConsts) AzureEnvironmentParameters(com.sequenceiq.environment.api.v1.environment.model.request.azure.AzureEnvironmentParameters) PlatformParameters(com.sequenceiq.cloudbreak.cloud.PlatformParameters) EmbeddedDatabaseService(com.sequenceiq.cloudbreak.service.cluster.EmbeddedDatabaseService) VolumeTemplate(com.sequenceiq.cloudbreak.domain.VolumeTemplate) CdpResourceType(com.sequenceiq.common.api.type.CdpResourceType) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult) Map(java.util.Map) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) CredentialToExtendedCloudCredentialConverter(com.sequenceiq.cloudbreak.converter.spi.CredentialToExtendedCloudCredentialConverter) NetworkService(com.sequenceiq.cloudbreak.service.network.NetworkService) CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) VolumeUsageType(com.sequenceiq.cloudbreak.domain.VolumeUsageType) CredentialConverter(com.sequenceiq.cloudbreak.service.environment.credential.CredentialConverter) SharedServiceValidator(com.sequenceiq.cloudbreak.service.stack.SharedServiceValidator) PlatformOrchestrators(com.sequenceiq.cloudbreak.cloud.model.PlatformOrchestrators) Platform(com.sequenceiq.cloudbreak.cloud.model.Platform) Set(java.util.Set) Objects(java.util.Objects) Json(com.sequenceiq.cloudbreak.common.json.Json) AdjustmentType(com.sequenceiq.common.api.type.AdjustmentType) List(java.util.List) EmbeddedDatabaseConfig(com.sequenceiq.cloudbreak.conf.EmbeddedDatabaseConfig) TemplateService(com.sequenceiq.cloudbreak.service.template.TemplateService) Optional(java.util.Optional) LegacyRestRequestThreadLocalService(com.sequenceiq.cloudbreak.structuredevent.LegacyRestRequestThreadLocalService) NetworkConstants(com.sequenceiq.cloudbreak.common.network.NetworkConstants) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup) PlacementSettingsV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.environment.placement.PlacementSettingsV4Request) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) FailurePolicy(com.sequenceiq.cloudbreak.domain.FailurePolicy) SecurityGroupService(com.sequenceiq.cloudbreak.service.securitygroup.SecurityGroupService) Network(com.sequenceiq.cloudbreak.domain.Network) CdpResourceTypeProvider(com.sequenceiq.cloudbreak.service.CdpResourceTypeProvider) BooleanUtils(org.apache.commons.lang3.BooleanUtils) Orchestrator(com.sequenceiq.cloudbreak.cloud.model.Orchestrator) CloudPlatform(com.sequenceiq.cloudbreak.common.mappable.CloudPlatform) InstanceGroupNetworkService(com.sequenceiq.cloudbreak.service.network.instancegroup.InstanceGroupNetworkService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Service(org.springframework.stereotype.Service) Nonnull(javax.annotation.Nonnull) InstanceGroupNetwork(com.sequenceiq.cloudbreak.domain.stack.instance.network.InstanceGroupNetwork) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) CloudSubnet(com.sequenceiq.cloudbreak.cloud.model.CloudSubnet) InstanceGroupParameterRequest(com.sequenceiq.cloudbreak.cloud.model.InstanceGroupParameterRequest) Logger(org.slf4j.Logger) Benchmark.measure(com.sequenceiq.cloudbreak.util.Benchmark.measure) CloudParameterService(com.sequenceiq.cloudbreak.cloud.service.CloudParameterService) AzureResourceGroup(com.sequenceiq.environment.api.v1.environment.model.request.azure.AzureResourceGroup) EnvironmentClientService(com.sequenceiq.cloudbreak.service.environment.EnvironmentClientService) Workspace(com.sequenceiq.cloudbreak.workspace.model.Workspace) Template(com.sequenceiq.cloudbreak.domain.Template) InstanceGroupType(com.sequenceiq.common.api.type.InstanceGroupType) SecurityGroup(com.sequenceiq.cloudbreak.domain.SecurityGroup) Credential(com.sequenceiq.cloudbreak.dto.credential.Credential) User(com.sequenceiq.cloudbreak.workspace.model.User) StackV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request) InstanceGroupParameterResponse(com.sequenceiq.cloudbreak.cloud.model.InstanceGroupParameterResponse) CloudParameterCache(com.sequenceiq.cloudbreak.service.stack.CloudParameterCache) CdpResourceType(com.sequenceiq.common.api.type.CdpResourceType) PlacementSettingsV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.environment.placement.PlacementSettingsV4Request) InstanceGroupParameterResponse(com.sequenceiq.cloudbreak.cloud.model.InstanceGroupParameterResponse) CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) SecurityGroup(com.sequenceiq.cloudbreak.domain.SecurityGroup) InstanceGroupNetwork(com.sequenceiq.cloudbreak.domain.stack.instance.network.InstanceGroupNetwork) VolumeTemplate(com.sequenceiq.cloudbreak.domain.VolumeTemplate) Template(com.sequenceiq.cloudbreak.domain.Template)

Example 18 with InstanceGroup

use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup in project cloudbreak by hortonworks.

the class MetadataSetupService method cleanupRequestedInstancesIfNotInList.

public void cleanupRequestedInstancesIfNotInList(Long stackId, Set<String> instanceGroups, Set<Long> privateIds) {
    LOGGER.info("Cleanup the requested instances if private id not in {}", privateIds);
    for (String instanceGroupName : instanceGroups) {
        Optional<InstanceGroup> instanceGroup = instanceGroupService.findOneByStackIdAndGroupName(stackId, instanceGroupName);
        instanceGroup.ifPresent(ig -> {
            List<InstanceMetaData> requestedInstanceMetaDatas = instanceMetaDataService.findAllByInstanceGroupAndInstanceStatus(ig, InstanceStatus.REQUESTED);
            LOGGER.info("Instances in requested state: {}", requestedInstanceMetaDatas);
            List<InstanceMetaData> removableInstanceMetaDatas = requestedInstanceMetaDatas.stream().filter(instanceMetaData -> !privateIds.contains(instanceMetaData.getPrivateId())).collect(Collectors.toList());
            LOGGER.info("Cleanup the following instances: {}", requestedInstanceMetaDatas);
            for (InstanceMetaData removableInstanceMetaData : removableInstanceMetaDatas) {
                removableInstanceMetaData.setTerminationDate(clock.getCurrentTimeMillis());
                removableInstanceMetaData.setInstanceStatus(InstanceStatus.TERMINATED);
            }
            instanceMetaDataService.saveAll(removableInstanceMetaDatas);
        });
    }
}
Also used : CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) StackStatus(com.sequenceiq.cloudbreak.domain.stack.StackStatus) LoggerFactory(org.slf4j.LoggerFactory) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) TransactionRuntimeExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionRuntimeExecutionException) InstanceGroupService(com.sequenceiq.cloudbreak.service.stack.InstanceGroupService) ImageService(com.sequenceiq.cloudbreak.service.image.ImageService) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) CloudbreakEventService(com.sequenceiq.cloudbreak.structuredevent.event.CloudbreakEventService) LoadBalancer(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer) Map(java.util.Map) TargetGroupPersistenceService(com.sequenceiq.cloudbreak.service.stack.TargetGroupPersistenceService) UPDATE_FAILED(com.sequenceiq.cloudbreak.api.endpoint.v4.common.Status.UPDATE_FAILED) LoadBalancerPersistenceService(com.sequenceiq.cloudbreak.service.stack.LoadBalancerPersistenceService) InstanceMetadataType(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.InstanceMetadataType) Collection(java.util.Collection) InstanceStatus(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.InstanceStatus) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Json(com.sequenceiq.cloudbreak.common.json.Json) List(java.util.List) STARTED(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus.STARTED) TransactionExecutionException(com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException) Optional(java.util.Optional) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup) StackService(com.sequenceiq.cloudbreak.service.stack.StackService) InstanceLifeCycle(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.InstanceLifeCycle) Clock(com.sequenceiq.cloudbreak.common.service.Clock) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) LoadBalancerConfigService(com.sequenceiq.cloudbreak.service.LoadBalancerConfigService) TargetGroup(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.TargetGroup) Image(com.sequenceiq.cloudbreak.cloud.model.Image) Function(java.util.function.Function) StackStatusService(com.sequenceiq.cloudbreak.service.stackstatus.StackStatusService) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) Inject(javax.inject.Inject) Service(org.springframework.stereotype.Service) LoadBalancerConfigConverter(com.sequenceiq.cloudbreak.service.LoadBalancerConfigConverter) InstanceMetaDataService(com.sequenceiq.cloudbreak.service.stack.InstanceMetaDataService) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) TERMINATED(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus.TERMINATED) Logger(org.slf4j.Logger) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) STACK_INSTANCE_METADATA_RESTORED(com.sequenceiq.cloudbreak.event.ResourceEvent.STACK_INSTANCE_METADATA_RESTORED) CloudbreakImageNotFoundException(com.sequenceiq.cloudbreak.core.CloudbreakImageNotFoundException) InstanceGroupType(com.sequenceiq.common.api.type.InstanceGroupType) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) StackType(com.sequenceiq.cloudbreak.api.endpoint.v4.common.StackType) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) StackIdView(com.sequenceiq.cloudbreak.domain.projection.StackIdView) CREATED(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus.CREATED) StringUtils(org.springframework.util.StringUtils) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup)

Example 19 with InstanceGroup

use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup in project cloudbreak by hortonworks.

the class GatewaySecurityGroupDecorator method extendGatewaysSecurityGroupsWithRules.

private void extendGatewaysSecurityGroupsWithRules(Stack stack, Set<InstanceGroup> gateways, Set<String> defaultGatewayCidrs) {
    for (InstanceGroup gateway : gateways) {
        SecurityGroup securityGroup = createNewSecurityGroupIfNotPresent(gateway);
        LOGGER.debug("InstanceGroupName: [{}] SecurityGoupIds: [{}]", gateway.getGroupName(), securityGroup.getSecurityGroupIds());
        if (CollectionUtils.isEmpty(securityGroup.getSecurityGroupIds())) {
            addSecurityRuleToSecurityGroup(stack, defaultGatewayCidrs, gateway);
        }
    }
}
Also used : SecurityGroup(com.sequenceiq.cloudbreak.domain.SecurityGroup) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup)

Example 20 with InstanceGroup

use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup in project cloudbreak by hortonworks.

the class InstanceMetaDataService method saveInstanceAndGetUpdatedStack.

public Stack saveInstanceAndGetUpdatedStack(Stack stack, Map<String, Integer> hostGroupsWithInstanceCountToCreate, Map<String, Set<String>> hostGroupWithHostnames, boolean save, boolean repair, NetworkScaleDetails networkScaleDetails) {
    LOGGER.info("Get updated stack with instance count ({}) and hostnames: {} and save: ({})", hostGroupsWithInstanceCountToCreate, hostGroupWithHostnames, save);
    DetailedEnvironmentResponse environment = getDetailedEnvironmentResponse(stack.getEnvironmentCrn());
    Map<String, String> subnetAzPairs = multiAzCalculatorService.prepareSubnetAzMap(environment);
    String stackSubnetId = getStackSubnetIdIfExists(stack);
    String stackAz = stackSubnetId == null ? null : subnetAzPairs.get(stackSubnetId);
    long privateId = getFirstValidPrivateId(stack.getInstanceGroupsAsList());
    for (Map.Entry<String, Integer> hostGroupWithInstanceCount : hostGroupsWithInstanceCountToCreate.entrySet()) {
        String hostGroup = hostGroupWithInstanceCount.getKey();
        Integer instanceToCreate = hostGroupWithInstanceCount.getValue();
        Iterator<String> hostNameIterator = getHostNameIterator(hostGroupWithHostnames.get(hostGroup));
        for (int i = 0; i < instanceToCreate; i++) {
            InstanceGroup instanceGroup = getInstanceGroup(stack.getInstanceGroups(), hostGroup);
            if (instanceGroup != null) {
                InstanceMetaData instanceMetaData = new InstanceMetaData();
                instanceMetaData.setPrivateId(privateId++);
                instanceMetaData.setInstanceStatus(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.InstanceStatus.REQUESTED);
                instanceMetaData.setInstanceGroup(instanceGroup);
                instanceMetaData.setVariant(stack.getPlatformVariant());
                if (hostNameIterator.hasNext()) {
                    String hostName = hostNameIterator.next();
                    repository.findHostInStack(stack.getId(), hostName).ifPresent(existingHost -> {
                        throw new CloudbreakServiceException("There is an existing host with the same FQDN. It can happen if you retried a failed repair. " + "Please start the repairing process again instead of retry.");
                    });
                    LOGGER.info("We have hostname to be allocated: {}, set it to this instanceMetadata: {}", hostName, instanceMetaData);
                    instanceMetaData.setDiscoveryFQDN(hostName);
                    subnetAzPairs = getSubnetAzPairsFilteredByHostNameIfRepair(environment, stack, repair, instanceGroup.getGroupName(), hostName);
                }
                Map<String, String> filteredSubnetsByLeastUsedAz = networkScaleDetails == null ? multiAzCalculatorService.filterSubnetByLeastUsedAz(instanceGroup, subnetAzPairs) : subnetAzPairs;
                prepareInstanceMetaDataSubnetAndAvailabilityZoneAndRackId(instanceGroup, instanceMetaData, filteredSubnetsByLeastUsedAz, stackSubnetId, stackAz, networkScaleDetails);
                if (save) {
                    repository.save(instanceMetaData);
                }
                instanceGroup.getInstanceMetaDataSet().add(instanceMetaData);
            }
        }
    }
    return stack;
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) Map(java.util.Map) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup)

Aggregations

InstanceGroup (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup)288 InstanceMetaData (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)132 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)93 Test (org.junit.jupiter.api.Test)91 HashSet (java.util.HashSet)68 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)57 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)47 Template (com.sequenceiq.cloudbreak.domain.Template)45 Test (org.junit.Test)44 Json (com.sequenceiq.cloudbreak.common.json.Json)38 Set (java.util.Set)37 Cluster (com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster)34 ArrayList (java.util.ArrayList)31 LinkedHashSet (java.util.LinkedHashSet)31 Map (java.util.Map)28 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)27 DetailedEnvironmentResponse (com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse)27 List (java.util.List)26 HostGroup (com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup)25 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)22