Search in sources :

Example 1 with MinaSshdService

use of com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.MinaSshdService in project cloudbreak by hortonworks.

the class MinaSshdManagementClient method listMinaSshdServices.

/**
 * Wraps calls to listMinaSshdServices with an account ID.
 *
 * @param requestId  the request ID for the request
 * @param accountId  the account ID
 * @param serviceIds the minasshd services to list. if null or empty then all minasshd services will be listed
 * @return the list of minasshd services
 */
public List<MinaSshdService> listMinaSshdServices(String requestId, String accountId, List<String> serviceIds) throws CcmException {
    checkNotNull(requestId, "requestId should not be null.");
    checkNotNull(accountId, "accountId should not be null.");
    List<MinaSshdService> groups = new ArrayList<>();
    MinaSshdManagementBlockingStub minaSshdManagementBlockingStub = newStub(requestId);
    ListMinaSshdServicesRequest.Builder requestBuilder = ListMinaSshdServicesRequest.newBuilder().setAccountId(accountId).setPageSize(minaSshdManagementClientConfig.getListMinaSshdServicesPageSize());
    if (serviceIds != null && !serviceIds.isEmpty()) {
        requestBuilder.addAllId(serviceIds);
    }
    ListMinaSshdServicesResponse response;
    do {
        try {
            LOGGER.debug("Calling listMinaSshdServices with requestId: {}, accountId: {}, serviceIds: [{}]", requestId, accountId, serviceIds);
            response = minaSshdManagementBlockingStub.listMinaSshdServices(requestBuilder.build());
            if (response == null) {
                throw new CcmException("Got null response from MinaSshdManagementService listMinaSshdServices gRPC call", false);
            } else {
                List<MinaSshdService> minaSshdServices = response.getMinaSshdServiceList();
                if (minaSshdServices == null) {
                    throw new CcmException("Got null minasshd services in MinaSshdManagementService listMinaSshdServices gRPC response", false);
                } else {
                    groups.addAll(minaSshdServices);
                }
            }
        } catch (StatusRuntimeException e) {
            String message = "MinaSshdManagementService listMinaSshdServices gRPC call failed: " + e.getMessage();
            Status status = e.getStatus();
            Status.Code code = status.getCode();
            boolean retryable = GrpcUtil.isRetryable(code);
            LOGGER.debug("Got status code: {}, retryable: {}", code, retryable);
            throw new CcmException(message, e, retryable);
        }
        requestBuilder.setPageToken(response.getNextPageToken());
    } while (response.hasNextPageToken());
    return groups;
}
Also used : ListMinaSshdServicesResponse(com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.ListMinaSshdServicesResponse) Status(io.grpc.Status) CcmException(com.sequenceiq.cloudbreak.ccm.exception.CcmException) ArrayList(java.util.ArrayList) ListMinaSshdServicesRequest(com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.ListMinaSshdServicesRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) MinaSshdManagementBlockingStub(com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementGrpc.MinaSshdManagementBlockingStub) MinaSshdService(com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.MinaSshdService)

Example 2 with MinaSshdService

use of com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.MinaSshdService in project cloudbreak by hortonworks.

the class DefaultCcmParameterSupplier method getBaseCcmParameters.

@Override
public Optional<CcmParameters> getBaseCcmParameters(@Nonnull String actorCrn, @Nonnull String accountId, @Nonnull String keyId) {
    if (grpcMinaSshdManagementClient == null) {
        return Optional.empty();
    }
    String requestId = MDCBuilder.getOrGenerateRequestId();
    try {
        MinaSshdService minaSshdService = grpcMinaSshdManagementClient.acquireMinaSshdServiceAndWaitUntilReady(requestId, Objects.requireNonNull(actorCrn, "actorCrn is null"), Objects.requireNonNull(accountId, "accountId is null"));
        GenerateAndRegisterSshTunnelingKeyPairResponse keyPairResponse = grpcMinaSshdManagementClient.generateAndRegisterSshTunnelingKeyPair(requestId, actorCrn, accountId, minaSshdService.getMinaSshdServiceId(), keyId);
        return Optional.of(new DefaultCcmParameters(createServerParameters(minaSshdService), createInstanceParameters(keyId, keyPairResponse.getEncipheredPrivateKey()), Collections.emptyList()));
    } catch (Exception e) {
        Throwables.throwIfUnchecked(e);
        throw new RuntimeException(e);
    }
}
Also used : DefaultCcmParameters(com.sequenceiq.cloudbreak.ccm.cloudinit.DefaultCcmParameters) GenerateAndRegisterSshTunnelingKeyPairResponse(com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.GenerateAndRegisterSshTunnelingKeyPairResponse) MinaSshdService(com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.MinaSshdService)

Example 3 with MinaSshdService

use of com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.MinaSshdService in project cloudbreak by hortonworks.

the class GrpcMinaSshdManagementClient method awaitValidMinaSshdService.

/**
 * Polls trying to find a service which is ready and has a status of {@code STARTED}.
 *
 * @param listAction               the action to list the minasshd services (must return at most 1 service)
 * @param actionDescription        the action description, for logging and exception messages
 * @param waitUntilTime            the latest time to wait to perform the action
 * @param pollingIntervalMillis    the polling interval in milliseconds
 * @param timeoutExceptionSupplier a supplier of exceptions for when all attempts fail
 * @return an optional wrapping a service that is ready and has a status of {@code STARTED}, or an empty optional if there are no pending services
 * @throws InterruptedException if the thread is interrupted
 * @throws CcmException         if the operation times out with services still pending, or if a non-transient error occurs
 */
private Optional<MinaSshdService> awaitValidMinaSshdService(Callable<List<MinaSshdService>> listAction, String actionDescription, ZonedDateTime waitUntilTime, int pollingIntervalMillis, Supplier<CcmException> timeoutExceptionSupplier) throws InterruptedException, CcmException {
    long waitUntilTimeMillis = waitUntilTime.toInstant().toEpochMilli();
    while (true) {
        // Call listMinaSshdServices, with retries in case of transient failures.
        List<MinaSshdService> minaSshdServices = RetryUtil.performWithRetries(listAction, actionDescription, waitUntilTime, pollingIntervalMillis, CcmException.class, timeoutExceptionSupplier, LOGGER);
        MinaSshdService service;
        switch(minaSshdServices.size()) {
            case 0:
                return Optional.empty();
            case 1:
                service = minaSshdServices.get(0);
                break;
            default:
                throw new IllegalStateException("listAction returned multiple services");
        }
        switch(service.getStatus()) {
            case STARTED:
                if (service.getReady()) {
                    return Optional.of(service);
                }
                break;
            case FAILED:
                return Optional.empty();
            default:
                break;
        }
        if (Thread.interrupted()) {
            throw new InterruptedException(String.format("Interrupted while trying to %s", actionDescription));
        }
        long delay = Math.min(pollingIntervalMillis, waitUntilTimeMillis - clock.millis());
        if (delay <= 0) {
            throw timeoutExceptionSupplier.get();
        }
        Thread.sleep(delay);
    }
}
Also used : MinaSshdService(com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.MinaSshdService)

Example 4 with MinaSshdService

use of com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.MinaSshdService in project cloudbreak by hortonworks.

the class MinaSshdManagementClient method acquireMinaSshdService.

/**
 * Wraps call to acquireMinaSshdService.
 *
 * @param requestId the request ID for the request
 * @param accountId the account ID
 * @return the minasshd service
 * @throws CcmException if an exception occurs
 */
public MinaSshdService acquireMinaSshdService(String requestId, String accountId) throws CcmException {
    checkNotNull(requestId, "requestId should not be null.");
    checkNotNull(accountId, "accountId should not be null.");
    MinaSshdManagementBlockingStub blockingStub = newStub(requestId);
    AcquireMinaSshdServiceRequest.Builder requestBuilder = AcquireMinaSshdServiceRequest.newBuilder().setAccountId(accountId);
    try {
        LOGGER.debug("Calling acquireMinaSshdService with requestId: {}, accountId: {}", requestId, accountId);
        AcquireMinaSshdServiceResponse response = blockingStub.acquireMinaSshdService(requestBuilder.build());
        if (response == null) {
            throw new CcmException("Got null response from MinaSshdManagementService acquireMinaSshdService gRPC call", false);
        } else {
            MinaSshdService minaSshdService = response.getMinaSshdService();
            if (minaSshdService == null) {
                throw new CcmException("Got null minasshd service in MinaSshdManagementService acquireMinaSshdService gRPC response", false);
            } else {
                return minaSshdService;
            }
        }
    } catch (StatusRuntimeException e) {
        String message = "MinaSshdManagementService acquireMinaSshdService gRPC call failed: " + e.getMessage();
        Status status = e.getStatus();
        Status.Code code = status.getCode();
        boolean retryable = GrpcUtil.isRetryable(code);
        LOGGER.debug("Got status code: {}, retryable: {}", code, retryable);
        throw new CcmException(message, e, retryable);
    }
}
Also used : Status(io.grpc.Status) CcmException(com.sequenceiq.cloudbreak.ccm.exception.CcmException) AcquireMinaSshdServiceRequest(com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.AcquireMinaSshdServiceRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) MinaSshdManagementBlockingStub(com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementGrpc.MinaSshdManagementBlockingStub) AcquireMinaSshdServiceResponse(com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.AcquireMinaSshdServiceResponse) MinaSshdService(com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.MinaSshdService)

Example 5 with MinaSshdService

use of com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.MinaSshdService in project cloudbreak by hortonworks.

the class GrpcMinaSshdManagementClient method acquireMinaSshdServiceAndWaitUntilReady.

/**
 * Attempts to acquire a minasshd service for the specified account. If it is not available immediately,
 * polls until it is acquires a ready service, is interrupted, times out, or there are no pending minasshd service instances.
 *
 * @param requestId the request ID for the request
 * @param actorCrn  the actor CRN
 * @param accountId the account ID
 * @return the minasshd service
 * @throws InterruptedException if the thread is interrupted
 * @throws CcmException         if the initial acquisition fails, if the timeout is reached, or if all minasshd service instances are in a FAILED state
 */
public MinaSshdService acquireMinaSshdServiceAndWaitUntilReady(String requestId, String actorCrn, String accountId) throws InterruptedException, CcmException {
    try (ManagedChannelWrapper channelWrapper = makeWrapper()) {
        MinaSshdManagementClient client = makeClient(channelWrapper.getChannel(), actorCrn);
        ZonedDateTime waitUntilTime = ZonedDateTime.now(clock).plus(minaSshdManagementClientConfig.getTimeoutMs(), ChronoUnit.MILLIS);
        int pollingIntervalMillis = minaSshdManagementClientConfig.getPollingIntervalMs();
        String actionDescription = "acquire MinaSSHD service for accountId " + accountId;
        Supplier<CcmException> timeoutExceptionSupplier = () -> new CcmException(String.format("Timed out while trying to %s", actionDescription), true);
        // First call acquireMinaSshdService, with retries in case of transient failures.
        MinaSshdService initialService = RetryUtil.performWithRetries(() -> client.acquireMinaSshdService(requestId, accountId), actionDescription, waitUntilTime, pollingIntervalMillis, CcmException.class, timeoutExceptionSupplier, LOGGER);
        // If the minasshd service was pre-existing, it is in the initial call result
        return getValidMinaSshdService(actionDescription, initialService).orElse(awaitValidMinaSshdService(() -> client.listMinaSshdServices(requestId, accountId, Collections.singletonList(initialService.getMinaSshdServiceId())), actionDescription, waitUntilTime, pollingIntervalMillis, timeoutExceptionSupplier).orElseThrow(() -> new CcmException(String.format("Failed while trying to %s", actionDescription), false)));
    }
}
Also used : CcmException(com.sequenceiq.cloudbreak.ccm.exception.CcmException) ZonedDateTime(java.time.ZonedDateTime) ManagedChannelWrapper(com.sequenceiq.cloudbreak.grpc.ManagedChannelWrapper) MinaSshdService(com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.MinaSshdService)

Aggregations

MinaSshdService (com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.MinaSshdService)5 CcmException (com.sequenceiq.cloudbreak.ccm.exception.CcmException)3 MinaSshdManagementBlockingStub (com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementGrpc.MinaSshdManagementBlockingStub)2 Status (io.grpc.Status)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 AcquireMinaSshdServiceRequest (com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.AcquireMinaSshdServiceRequest)1 AcquireMinaSshdServiceResponse (com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.AcquireMinaSshdServiceResponse)1 GenerateAndRegisterSshTunnelingKeyPairResponse (com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.GenerateAndRegisterSshTunnelingKeyPairResponse)1 ListMinaSshdServicesRequest (com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.ListMinaSshdServicesRequest)1 ListMinaSshdServicesResponse (com.cloudera.thunderhead.service.minasshdmanagement.MinaSshdManagementProto.ListMinaSshdServicesResponse)1 DefaultCcmParameters (com.sequenceiq.cloudbreak.ccm.cloudinit.DefaultCcmParameters)1 ManagedChannelWrapper (com.sequenceiq.cloudbreak.grpc.ManagedChannelWrapper)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1