Search in sources :

Example 56 with InstanceMetaData

use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData 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)

Example 57 with InstanceMetaData

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

the class InstanceMetaDataService method saveInstanceRequests.

public void saveInstanceRequests(Stack stack, List<Group> groups) {
    Set<InstanceGroup> instanceGroups = stack.getInstanceGroups();
    for (Group group : groups) {
        InstanceGroup instanceGroup = getInstanceGroup(instanceGroups, group.getName());
        List<InstanceMetaData> existingInGroup = repository.findAllByInstanceGroupAndInstanceStatus(instanceGroup, com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.InstanceStatus.REQUESTED);
        for (CloudInstance cloudInstance : group.getInstances()) {
            InstanceTemplate instanceTemplate = cloudInstance.getTemplate();
            boolean exists = existingInGroup.stream().anyMatch(i -> i.getPrivateId().equals(instanceTemplate.getPrivateId()));
            if (com.sequenceiq.cloudbreak.cloud.model.InstanceStatus.CREATE_REQUESTED == instanceTemplate.getStatus() && !exists) {
                InstanceMetaData instanceMetaData = new InstanceMetaData();
                instanceMetaData.setPrivateId(instanceTemplate.getPrivateId());
                instanceMetaData.setInstanceStatus(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.InstanceStatus.REQUESTED);
                instanceMetaData.setInstanceGroup(instanceGroup);
                instanceMetaData.setVariant(stack.getPlatformVariant());
                repository.save(instanceMetaData);
            }
        }
    }
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 58 with InstanceMetaData

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

the class TlsSetupService method setupTls.

public void setupTls(Stack stack, InstanceMetaData gwInstance) throws CloudbreakException {
    try {
        SavingX509TrustManager x509TrustManager = new SavingX509TrustManager();
        TrustManager[] trustManagers = { x509TrustManager };
        SSLContext sslContext = SslConfigurator.newInstance().createSSLContext();
        sslContext.init(null, trustManagers, new SecureRandom());
        Client client = RestClientUtil.createClient(sslContext, false);
        Integer gatewayPort = stack.getGatewayPort();
        String ip = gatewayConfigService.getGatewayIp(stack, gwInstance);
        LOGGER.debug("Trying to fetch the server's certificate: {}:{}", ip, gatewayPort);
        nginxPollerService.pollWithAbsoluteTimeout(nginxCertListenerTask, new NginxPollerObject(client, ip, gatewayPort, x509TrustManager), POLLING_INTERVAL, TEN_MIN, MAX_FAILURE);
        WebTarget nginxTarget = client.target(String.format("https://%s:%d", ip, gatewayPort));
        nginxTarget.path("/").request().get().close();
        X509Certificate[] chain = x509TrustManager.getChain();
        String serverCert = PkiUtil.convert(chain[0]);
        InstanceMetaData metaData = getInstanceMetaData(gwInstance);
        metaData.setServerCert(BaseEncoding.base64().encode(serverCert.getBytes()));
        instanceMetaDataService.save(metaData);
    } catch (Exception e) {
        throw new CloudbreakException("Failed to retrieve the server's certificate from Nginx." + " Please check your security group is open enough and the Management Console can access your VPC and subnet." + " Please also Make sure your Subnets can route to the internet and you have public DNS and IP options enabled." + " Refer to Cloudera documentation at" + " https://docs.cloudera.com/management-console/cloud/proxy/topics/mc-outbound-internet-access-and-proxy.html", e);
    }
}
Also used : SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) TrustManager(javax.net.ssl.TrustManager) SavingX509TrustManager(com.sequenceiq.cloudbreak.client.CertificateTrustManager.SavingX509TrustManager) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) SavingX509TrustManager(com.sequenceiq.cloudbreak.client.CertificateTrustManager.SavingX509TrustManager) NginxPollerObject(com.sequenceiq.cloudbreak.polling.nginx.NginxPollerObject) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Example 59 with InstanceMetaData

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

the class UnhealthyInstancesFinalizer method finalizeUnhealthyInstances.

public Set<String> finalizeUnhealthyInstances(Stack stack, Iterable<InstanceMetaData> candidateUnhealthyInstances) {
    Location location = location(region(stack.getRegion()), availabilityZone(stack.getAvailabilityZone()));
    CloudContext cloudContext = CloudContext.Builder.builder().withId(stack.getId()).withName(stack.getName()).withCrn(stack.getResourceCrn()).withPlatform(stack.getCloudPlatform()).withVariant(stack.getPlatformVariant()).withLocation(location).withWorkspaceId(stack.getWorkspace().getId()).withAccountId(Crn.safeFromString(stack.getResourceCrn()).getAccountId()).withTenantId(stack.getTenant().getId()).build();
    CloudCredential cloudCredential = stackUtil.getCloudCredential(stack);
    List<CloudInstance> cloudInstances = cloudInstanceConverter.convert(candidateUnhealthyInstances, stack.getEnvironmentCrn(), stack.getStackAuthentication());
    List<CloudVmInstanceStatus> cloudVmInstanceStatuses = instanceStateQuery.getCloudVmInstanceStatuses(cloudCredential, cloudContext, cloudInstances);
    Map<String, CloudVmInstanceStatus> cloudVmInstanceStatusesById = new HashMap<>();
    cloudVmInstanceStatuses.forEach(c -> cloudVmInstanceStatusesById.put(c.getCloudInstance().getInstanceId(), c));
    Set<String> unhealthyInstances = new HashSet<>();
    for (InstanceMetaData i : candidateUnhealthyInstances) {
        CloudVmInstanceStatus instanceStatus = cloudVmInstanceStatusesById.get(i.getInstanceId());
        if ((instanceStatus == null) || (instanceStatus.getStatus().equals(InstanceStatus.TERMINATED))) {
            unhealthyInstances.add(i.getInstanceId());
        }
    }
    return unhealthyInstances;
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) HashMap(java.util.HashMap) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) Location(com.sequenceiq.cloudbreak.cloud.model.Location) HashSet(java.util.HashSet)

Example 60 with InstanceMetaData

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

the class LoggingAgentAutoRestartPatchService method doApply.

@Override
boolean doApply(Stack stack) throws ExistingStackPatchApplyException {
    if (isPrimaryGatewayReachable(stack)) {
        try {
            byte[] currentSaltState = getCurrentSaltStateStack(stack);
            List<String> saltStateDefinitions = Arrays.asList("salt-common", "salt");
            List<String> loggingAgentSaltStateDef = List.of("/salt/fluent");
            byte[] fluentSaltStateConfig = compressUtil.generateCompressedOutputFromFolders(saltStateDefinitions, loggingAgentSaltStateDef);
            boolean loggingAgentContentMatches = compressUtil.compareCompressedContent(currentSaltState, fluentSaltStateConfig, loggingAgentSaltStateDef);
            if (!loggingAgentContentMatches) {
                Set<InstanceMetaData> instanceMetaDataSet = instanceMetaDataService.findNotTerminatedAndNotZombieForStack(stack.getId());
                List<GatewayConfig> gatewayConfigs = gatewayConfigService.getAllGatewayConfigs(stack);
                ClusterDeletionBasedExitCriteriaModel exitModel = ClusterDeletionBasedExitCriteriaModel.nonCancellableModel();
                Set<Node> availableNodes = getAvailableNodes(instanceMetaDataSet, gatewayConfigs, exitModel);
                if (CollectionUtils.isEmpty(availableNodes)) {
                    LOGGER.info("Not found any available nodes for patch, stack: " + stack.getName());
                    return false;
                } else {
                    getTelemetryOrchestrator().executeLoggingAgentDiagnostics(fluentSaltStateConfig, gatewayConfigs, availableNodes, exitModel);
                    byte[] newFullSaltState = compressUtil.updateCompressedOutputFolders(saltStateDefinitions, loggingAgentSaltStateDef, currentSaltState);
                    clusterBootstrapper.updateSaltComponent(stack, newFullSaltState);
                    LOGGER.debug("Logging agent partial salt refresh and diagnostics successfully finished for stack {}", stack.getName());
                    return true;
                }
            } else {
                LOGGER.debug("Logging agent partial salt refresh and diagnostics is not required for stack {}", stack.getName());
                return true;
            }
        } catch (ExistingStackPatchApplyException e) {
            throw e;
        } catch (Exception e) {
            throw new ExistingStackPatchApplyException(e.getMessage(), e);
        }
    } else {
        LOGGER.info("Salt partial update cannot run, because primary gateway is unreachable of stack: " + stack.getResourceCrn());
        return false;
    }
}
Also used : Node(com.sequenceiq.cloudbreak.common.orchestration.Node) CloudbreakRuntimeException(com.sequenceiq.cloudbreak.service.CloudbreakRuntimeException) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) ClusterDeletionBasedExitCriteriaModel(com.sequenceiq.cloudbreak.core.bootstrap.service.ClusterDeletionBasedExitCriteriaModel) GatewayConfig(com.sequenceiq.cloudbreak.orchestrator.model.GatewayConfig)

Aggregations

InstanceMetaData (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)415 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)165 InstanceGroup (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup)152 Test (org.junit.jupiter.api.Test)143 Map (java.util.Map)92 HashSet (java.util.HashSet)90 Set (java.util.Set)86 List (java.util.List)84 HostGroup (com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup)77 Cluster (com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster)73 Collectors (java.util.stream.Collectors)71 ArrayList (java.util.ArrayList)62 Test (org.junit.Test)60 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)57 Optional (java.util.Optional)52 StackService (com.sequenceiq.cloudbreak.service.stack.StackService)48 Inject (javax.inject.Inject)47 Logger (org.slf4j.Logger)47 LoggerFactory (org.slf4j.LoggerFactory)47 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)45