Search in sources :

Example 31 with HostsResourceApi

use of com.cloudera.api.swagger.HostsResourceApi in project cloudbreak by hortonworks.

the class ClouderaManagerSecurityService method rotateHostCertificates.

@Override
public void rotateHostCertificates(String sshUser, KeyPair sshKeyPair, String subAltName) throws CloudbreakException {
    Cluster cluster = stack.getCluster();
    String user = cluster.getCloudbreakAmbariUser();
    String password = cluster.getCloudbreakAmbariPassword();
    try {
        ApiClient client = getClient(stack.getGatewayPort(), user, password, clientConfig);
        HostsResourceApi hostsResourceApi = clouderaManagerApiFactory.getHostsResourceApi(client);
        BatchResourceApi batchResourceApi = clouderaManagerApiFactory.getBatchResourceApi(client);
        ApiHostList hostList = hostsResourceApi.readHosts(null, null, "SUMMARY");
        ApiBatchRequest batchRequest = createHostCertsBatchRequest(hostList, sshUser, sshKeyPair, subAltName);
        ApiBatchResponse apiBatchResponse = batchResourceApi.execute(batchRequest);
        processHostCertsBatchResponse(client, apiBatchResponse);
    } catch (ApiException | ClouderaManagerClientInitException e) {
        LOGGER.warn("Can't rotate the host certificates", e);
        throw new CloudbreakException("Can't rotate the host certificates due to: " + e.getMessage());
    }
}
Also used : ApiHostList(com.cloudera.api.swagger.model.ApiHostList) BatchResourceApi(com.cloudera.api.swagger.BatchResourceApi) HostsResourceApi(com.cloudera.api.swagger.HostsResourceApi) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) ClouderaManagerClientInitException(com.sequenceiq.cloudbreak.cm.client.ClouderaManagerClientInitException) ApiClient(com.cloudera.api.swagger.client.ApiClient) ApiBatchResponse(com.cloudera.api.swagger.model.ApiBatchResponse) ApiBatchRequest(com.cloudera.api.swagger.model.ApiBatchRequest) ApiException(com.cloudera.api.swagger.client.ApiException)

Example 32 with HostsResourceApi

use of com.cloudera.api.swagger.HostsResourceApi in project cloudbreak by hortonworks.

the class ClouderaManagerCommissioner method collectHostsToCommission.

public Map<String, InstanceMetaData> collectHostsToCommission(Stack stack, HostGroup hostGroup, Set<String> hostNames, ApiClient client) {
    Set<InstanceMetaData> hostsInHostGroup = hostGroup.getInstanceGroup().getNotTerminatedAndNotZombieInstanceMetaDataSet();
    Map<String, InstanceMetaData> hostsToCommission = hostsInHostGroup.stream().filter(hostMetadata -> hostNames.contains(hostMetadata.getDiscoveryFQDN())).collect(Collectors.toMap(InstanceMetaData::getDiscoveryFQDN, hostMetadata -> hostMetadata));
    if (hostsToCommission.size() != hostNames.size()) {
        List<String> missingHosts = hostNames.stream().filter(h -> !hostsToCommission.containsKey(h)).collect(Collectors.toList());
        LOGGER.debug("Not all requested hosts found in CB for host group: {}. MissingCount={}, missingHosts=[{}]. Requested hosts: [{}]", hostGroup.getName(), missingHosts.size(), missingHosts, hostNames);
    }
    HostsResourceApi hostsResourceApi = clouderaManagerApiFactory.getHostsResourceApi(client);
    try {
        ApiHostList hostRefList = hostsResourceApi.readHosts(null, null, SUMMARY_REQUEST_VIEW);
        List<String> cmHostNames = hostRefList.getItems().stream().map(ApiHost::getHostname).collect(Collectors.toList());
        List<String> matchingCmHosts = hostsToCommission.keySet().stream().filter(hostName -> cmHostNames.contains(hostName)).collect(Collectors.toList());
        Set<String> matchingCmHostSet = new HashSet<>(matchingCmHosts);
        if (matchingCmHosts.size() != hostsToCommission.size()) {
            List<String> missingHostsInCm = hostsToCommission.keySet().stream().filter(h -> !matchingCmHostSet.contains(h)).collect(Collectors.toList());
            LOGGER.debug("Not all requested hosts found in CM. MissingCount={}, missingHosts=[{}]. Requested hosts: [{}]", missingHostsInCm.size(), missingHostsInCm, hostsToCommission.keySet());
        }
        Sets.newHashSet(hostsToCommission.keySet()).stream().filter(hostName -> !matchingCmHostSet.contains(hostName)).forEach(hostsToCommission::remove);
        LOGGER.debug("Collected hosts to commission: [{}]", hostsToCommission);
        return hostsToCommission;
    } catch (ApiException e) {
        // TODO: CB-14929: This exception has to be handled properly. Situations where CM communications fails. There's no retries here right now.
        LOGGER.error("Failed to get host list for cluster: {}", stack.getName(), e);
        throw new CloudbreakServiceException(e.getMessage(), e);
    }
}
Also used : Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) ApiCommand(com.cloudera.api.swagger.model.ApiCommand) ApiClient(com.cloudera.api.swagger.client.ApiClient) LoggerFactory(org.slf4j.LoggerFactory) ApiHostList(com.cloudera.api.swagger.model.ApiHostList) ApiException(com.cloudera.api.swagger.client.ApiException) ClouderaManagerPollingServiceProvider(com.sequenceiq.cloudbreak.cm.polling.ClouderaManagerPollingServiceProvider) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ApiHost(com.cloudera.api.swagger.model.ApiHost) Map(java.util.Map) ClouderaManagerResourceApi(com.cloudera.api.swagger.ClouderaManagerResourceApi) Logger(org.slf4j.Logger) HostsResourceApi(com.cloudera.api.swagger.HostsResourceApi) Set(java.util.Set) ExtendedPollingResult(com.sequenceiq.cloudbreak.polling.ExtendedPollingResult) ApiHostNameList(com.cloudera.api.swagger.model.ApiHostNameList) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) Component(org.springframework.stereotype.Component) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) CommandsResourceApi(com.cloudera.api.swagger.CommandsResourceApi) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) CancellationException(com.sequenceiq.cloudbreak.cloud.scheduler.CancellationException) ClouderaManagerApiFactory(com.sequenceiq.cloudbreak.cm.client.retry.ClouderaManagerApiFactory) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) ApiHostList(com.cloudera.api.swagger.model.ApiHostList) HostsResourceApi(com.cloudera.api.swagger.HostsResourceApi) HashSet(java.util.HashSet) ApiException(com.cloudera.api.swagger.client.ApiException)

Example 33 with HostsResourceApi

use of com.cloudera.api.swagger.HostsResourceApi in project cloudbreak by hortonworks.

the class ClouderaManagerClientActions method checkCmServicesStartedSuccessfully.

public DistroXTestDto checkCmServicesStartedSuccessfully(DistroXTestDto testDto, String user, String password) {
    String serverIp = testDto.getResponse().getCluster().getServerIp();
    ApiClient apiClient = getCmApiClientWithTimeoutDisabledDirect(serverIp, testDto.getName(), V_43, user, password);
    // CHECKSTYLE:OFF
    HostsResourceApi hostsResourceApi = new HostsResourceApi(apiClient);
    // CHECKSTYLE:ON
    try {
        Set<String> masterHostnames = testDto.getResponse().getInstanceGroups().stream().filter(ig -> "master".equals(ig.getName())).flatMap(ig -> ig.getMetadata().stream()).map(InstanceMetaDataV4Response::getDiscoveryFQDN).collect(Collectors.toSet());
        List<ApiHost> apiHosts = hostsResourceApi.readHosts(null, null, "FULL_WITH_HEALTH_CHECK_EXPLANATION").getItems();
        Set<String> servicesNotStarted = apiHosts.stream().filter(apiHost -> masterHostnames.contains(apiHost.getHostname())).flatMap(apiHost -> collectNotStartedServicesOnHost(apiHost).stream()).filter(serviceName -> REQUIRED_SERVICES.contains(serviceName)).collect(Collectors.toSet());
        if (!servicesNotStarted.isEmpty()) {
            LOGGER.error("There are not started required services: {}", servicesNotStarted);
            throw new TestFailException(String.format("There are not started required services: %s", servicesNotStarted));
        }
    } catch (ApiException e) {
        LOGGER.error("Exception when calling HostsResourceApi#readHosts. Response: {}", e.getResponseBody(), e);
        String message = format("Exception when calling HostsResourceApi#readHosts at %s. Response: %s", apiClient.getBasePath(), e.getResponseBody());
        throw new TestFailException(message, e);
    } catch (Exception e) {
        LOGGER.error("Can't read host statuses at: '{}'!", apiClient.getBasePath());
        throw new TestFailException("Can't read host statuses at: " + apiClient.getBasePath(), e);
    }
    return testDto;
}
Also used : Logger(org.slf4j.Logger) HostsResourceApi(com.cloudera.api.swagger.HostsResourceApi) ApiClient(com.cloudera.api.swagger.client.ApiClient) LoggerFactory(org.slf4j.LoggerFactory) ApiConfigList(com.cloudera.api.swagger.model.ApiConfigList) Log(com.sequenceiq.it.cloudbreak.log.Log) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ApiException(com.cloudera.api.swagger.client.ApiException) DistroXTestDto(com.sequenceiq.it.cloudbreak.dto.distrox.DistroXTestDto) Value(org.springframework.beans.factory.annotation.Value) RoleConfigGroupsResourceApi(com.cloudera.api.swagger.RoleConfigGroupsResourceApi) ApiRoleState(com.cloudera.api.swagger.model.ApiRoleState) InstanceMetaDataV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.instancegroup.instancemetadata.InstanceMetaDataV4Response) SdxInternalTestDto(com.sequenceiq.it.cloudbreak.dto.sdx.SdxInternalTestDto) List(java.util.List) Component(org.springframework.stereotype.Component) ClouderaManagerClient(com.sequenceiq.it.cloudbreak.util.clouderamanager.client.ClouderaManagerClient) TestFailException(com.sequenceiq.it.cloudbreak.exception.TestFailException) ApiHost(com.cloudera.api.swagger.model.ApiHost) ListUtils.emptyIfNull(org.apache.commons.collections4.ListUtils.emptyIfNull) ApiRoleRef(com.cloudera.api.swagger.model.ApiRoleRef) HostsResourceApi(com.cloudera.api.swagger.HostsResourceApi) TestFailException(com.sequenceiq.it.cloudbreak.exception.TestFailException) ApiClient(com.cloudera.api.swagger.client.ApiClient) ApiException(com.cloudera.api.swagger.client.ApiException) TestFailException(com.sequenceiq.it.cloudbreak.exception.TestFailException) ApiHost(com.cloudera.api.swagger.model.ApiHost) ApiException(com.cloudera.api.swagger.client.ApiException)

Example 34 with HostsResourceApi

use of com.cloudera.api.swagger.HostsResourceApi in project cloudbreak by hortonworks.

the class ClouderaManagerDecomissioner method enterMaintenanceMode.

public void enterMaintenanceMode(Stack stack, Set<String> hostList, ApiClient client) {
    HostsResourceApi hostsResourceApi = clouderaManagerApiFactory.getHostsResourceApi(client);
    String currentHostId = null;
    int successCount = 0;
    List<String> availableHostsIdsFromCm = null;
    LOGGER.debug("Attempting to put {} instances into CM maintenance mode", hostList == null ? 0 : hostList.size());
    try {
        ApiHostList hostRefList = hostsResourceApi.readHosts(null, null, SUMMARY_REQUEST_VIEW);
        availableHostsIdsFromCm = hostRefList.getItems().stream().filter(apiHostRef -> hostList.contains(apiHostRef.getHostname())).parallel().map(ApiHost::getHostId).collect(Collectors.toList());
        for (String hostId : availableHostsIdsFromCm) {
            currentHostId = hostId;
            hostsResourceApi.enterMaintenanceMode(hostId);
            successCount++;
        }
        LOGGER.debug("Finished putting {} instances into CM maintenance mode. Initial request size: {}, CM availableCount: {}", successCount, hostList == null ? 0 : hostList.size(), availableHostsIdsFromCm == null ? "null" : availableHostsIdsFromCm);
    } catch (ApiException e) {
        LOGGER.error("Failed while putting a node into maintenance mode. nodeId=" + currentHostId + ", successCount=" + successCount, e);
        throw new CloudbreakServiceException(e.getMessage(), e);
    }
}
Also used : ApiHostList(com.cloudera.api.swagger.model.ApiHostList) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) HostsResourceApi(com.cloudera.api.swagger.HostsResourceApi) ApiHost(com.cloudera.api.swagger.model.ApiHost) ApiException(com.cloudera.api.swagger.client.ApiException)

Aggregations

HostsResourceApi (com.cloudera.api.swagger.HostsResourceApi)34 ApiHostList (com.cloudera.api.swagger.model.ApiHostList)29 ApiClient (com.cloudera.api.swagger.client.ApiClient)19 ApiException (com.cloudera.api.swagger.client.ApiException)19 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)15 ApiHost (com.cloudera.api.swagger.model.ApiHost)13 List (java.util.List)13 HostGroup (com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup)11 ExtendedPollingResult (com.sequenceiq.cloudbreak.polling.ExtendedPollingResult)11 CloudbreakServiceException (com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException)10 InstanceMetaData (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)10 Set (java.util.Set)10 Collectors (java.util.stream.Collectors)10 Test (org.junit.jupiter.api.Test)10 HostTemplatesResourceApi (com.cloudera.api.swagger.HostTemplatesResourceApi)9 ApiHostTemplateList (com.cloudera.api.swagger.model.ApiHostTemplateList)9 CancellationException (com.sequenceiq.cloudbreak.cloud.scheduler.CancellationException)9 ClouderaManagerApiFactory (com.sequenceiq.cloudbreak.cm.client.retry.ClouderaManagerApiFactory)9 ClouderaManagerPollingServiceProvider (com.sequenceiq.cloudbreak.cm.polling.ClouderaManagerPollingServiceProvider)9 HashSet (java.util.HashSet)9