Search in sources :

Example 46 with DBStack

use of com.sequenceiq.redbeams.domain.stack.DBStack in project cloudbreak by hortonworks.

the class RedbeamsTerminationActions method failedAction.

@Bean(name = "REDBEAMS_TERMINATION_FAILED_STATE")
public Action<?, ?> failedAction() {
    return new AbstractRedbeamsTerminationAction<>(RedbeamsFailureEvent.class, false) {

        // A lot here - some of this could go into some sort of failure handler class
        // compare to core StackCreationService::handleStackCreationFailure
        @Override
        protected void doExecute(RedbeamsContext context, RedbeamsFailureEvent payload, Map<Object, Object> variables) {
            Exception failureException = payload.getException();
            LOGGER.info("Error during database stack termination flow:", failureException);
            if (failureException instanceof CancellationException || ExceptionUtils.getRootCause(failureException) instanceof CancellationException) {
                LOGGER.debug("The flow has been cancelled");
            } else {
                // StackCreationActions / StackCreationService only update status if stack isn't mid-deletion
                String errorReason = failureException == null ? "Unknown error" : failureException.getMessage();
                Optional<DBStack> dbStack = dbStackStatusUpdater.updateStatus(payload.getResourceId(), DetailedDBStackStatus.DELETE_FAILED, errorReason);
                metricService.incrementMetricCounter(MetricType.DB_TERMINATION_FAILED, dbStack);
            }
            sendEvent(context, REDBEAMS_TERMINATION_FAILURE_HANDLED_EVENT.event(), payload);
        }

        @Override
        protected RedbeamsContext createFlowContext(FlowParameters flowParameters, StateContext<RedbeamsTerminationState, RedbeamsTerminationEvent> stateContext, RedbeamsFailureEvent payload) {
            Flow flow = getFlow(flowParameters.getFlowId());
            flow.setFlowFailed(payload.getException());
            return super.createFlowContext(flowParameters, stateContext, payload);
        }

        @Override
        protected Object getFailurePayload(RedbeamsFailureEvent payload, Optional<RedbeamsContext> flowContext, Exception ex) {
            return payload;
        }
    };
}
Also used : Optional(java.util.Optional) DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) StateContext(org.springframework.statemachine.StateContext) CancellationException(com.sequenceiq.cloudbreak.cloud.scheduler.CancellationException) Flow(com.sequenceiq.flow.core.Flow) AbstractRedbeamsTerminationAction(com.sequenceiq.redbeams.flow.redbeams.termination.AbstractRedbeamsTerminationAction) RedbeamsFailureEvent(com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsFailureEvent) FlowParameters(com.sequenceiq.flow.core.FlowParameters) CancellationException(com.sequenceiq.cloudbreak.cloud.scheduler.CancellationException) RedbeamsContext(com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext) Map(java.util.Map) Bean(org.springframework.context.annotation.Bean)

Example 47 with DBStack

use of com.sequenceiq.redbeams.domain.stack.DBStack in project cloudbreak by hortonworks.

the class RedbeamsTerminationService method terminate.

private DatabaseServerConfig terminate(DatabaseServerConfig server, boolean force) {
    String crn = server.getResourceCrn().toString();
    if (server.getResourceStatus() != ResourceStatus.SERVICE_MANAGED) {
        return databaseServerConfigService.deleteByCrn(crn);
    }
    DBStack dbStack = dbStackService.getByCrn(crn);
    MDCBuilder.addEnvironmentCrn(dbStack.getEnvironmentId());
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Terminate called for: {} with force: {}", dbStack, force);
    }
    if (dbStack.getStatus().isDeleteInProgressOrCompleted() && !deletetedOnProviderSide(server, dbStack)) {
        LOGGER.debug("DatabaseServer with crn {} is already being deleted", dbStack.getResourceCrn());
        return server;
    }
    dbStackStatusUpdater.updateStatus(dbStack.getId(), DetailedDBStackStatus.DELETE_REQUESTED);
    // re-fetch to see new status
    server = databaseServerConfigService.getByCrn(crn);
    cancelService.cancelRunningFlows(dbStack.getId());
    flowManager.notify(RedbeamsTerminationEvent.REDBEAMS_TERMINATION_EVENT.selector(), new RedbeamsEvent(RedbeamsTerminationEvent.REDBEAMS_TERMINATION_EVENT.selector(), dbStack.getId(), force));
    return server;
}
Also used : DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) RedbeamsEvent(com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsEvent)

Example 48 with DBStack

use of com.sequenceiq.redbeams.domain.stack.DBStack in project cloudbreak by hortonworks.

the class DeregisterDatabaseServerHandler method doAccept.

@Override
protected Selectable doAccept(HandlerEvent<DeregisterDatabaseServerRequest> event) {
    DeregisterDatabaseServerRequest request = event.getData();
    DBStack dbStack = dbStackService.getById(request.getResourceId());
    try {
        databaseServerConfigService.getByCrn(Crn.safeFromString(dbStack.getResourceCrn())).ifPresent(dsc -> databaseServerConfigService.delete(dsc));
        return new DeregisterDatabaseServerSuccess(request.getResourceId());
    } catch (Exception e) {
        LOGGER.warn("Error deregistering database:", e);
        return new DeregisterDatabaseServerFailed(request.getResourceId(), e);
    }
}
Also used : DeregisterDatabaseServerFailed(com.sequenceiq.redbeams.flow.redbeams.termination.event.deregister.DeregisterDatabaseServerFailed) DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) DeregisterDatabaseServerSuccess(com.sequenceiq.redbeams.flow.redbeams.termination.event.deregister.DeregisterDatabaseServerSuccess) DeregisterDatabaseServerRequest(com.sequenceiq.redbeams.flow.redbeams.termination.event.deregister.DeregisterDatabaseServerRequest)

Example 49 with DBStack

use of com.sequenceiq.redbeams.domain.stack.DBStack in project cloudbreak by hortonworks.

the class AbstractRedbeamsProvisionAction method createFlowContext.

@Override
protected RedbeamsContext createFlowContext(FlowParameters flowParameters, StateContext<RedbeamsProvisionState, RedbeamsProvisionEvent> stateContext, P payload) {
    DBStack dbStack = dbStackService.getById(payload.getResourceId());
    MDCBuilder.buildMdcContext(dbStack);
    Location location = location(region(dbStack.getRegion()), availabilityZone(dbStack.getAvailabilityZone()));
    String accountId = dbStack.getOwnerCrn().getAccountId();
    CloudContext cloudContext = CloudContext.Builder.builder().withId(dbStack.getId()).withName(dbStack.getName()).withCrn(dbStack.getResourceCrn()).withPlatform(dbStack.getCloudPlatform()).withVariant(dbStack.getPlatformVariant()).withLocation(location).withUserName(dbStack.getUserName()).withAccountId(accountId).build();
    Credential credential = credentialService.getCredentialByEnvCrn(dbStack.getEnvironmentId());
    CloudCredential cloudCredential = credentialConverter.convert(credential);
    DatabaseStack databaseStack = databaseStackConverter.convert(dbStack);
    return new RedbeamsContext(flowParameters, cloudContext, cloudCredential, databaseStack, dbStack);
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Credential(com.sequenceiq.redbeams.dto.Credential) DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) RedbeamsContext(com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Example 50 with DBStack

use of com.sequenceiq.redbeams.domain.stack.DBStack in project cloudbreak by hortonworks.

the class AllocateDatabaseServerHandler method doAccept.

@Override
protected Selectable doAccept(HandlerEvent<AllocateDatabaseServerRequest> handlerEvent) {
    Event<AllocateDatabaseServerRequest> event = handlerEvent.getEvent();
    LOGGER.debug("Received event: {}", event);
    AllocateDatabaseServerRequest request = event.getData();
    CloudContext cloudContext = request.getCloudContext();
    Selectable response;
    try {
        CloudConnector<Object> connector = cloudPlatformConnectors.get(cloudContext.getPlatformVariant());
        CloudCredential cloudCredential = request.getCloudCredential();
        AuthenticatedContext ac = connector.authentication().authenticate(cloudContext, cloudCredential);
        DatabaseStack databaseStack = request.getDatabaseStack();
        DBStack dbStack = dbStackService.getById(request.getResourceId());
        databaseServerSslCertificatePrescriptionService.prescribeSslCertificateIfNeeded(cloudContext, cloudCredential, dbStack, databaseStack);
        List<CloudResourceStatus> resourceStatuses = connector.resources().launchDatabaseServer(ac, databaseStack, persistenceNotifier);
        List<CloudResource> resources = ResourceLists.transform(resourceStatuses);
        PollTask<ResourcesStatePollerResult> task = statusCheckFactory.newPollResourcesStateTask(ac, resources, true);
        ResourcesStatePollerResult statePollerResult = ResourcesStatePollerResults.build(cloudContext, resourceStatuses);
        if (!task.completed(statePollerResult)) {
            statePollerResult = syncPollingScheduler.schedule(task);
        }
        validateResourcesState(cloudContext, statePollerResult);
        response = new AllocateDatabaseServerSuccess(request.getResourceId());
        LOGGER.debug("Launching the database stack successfully finished for {}", cloudContext);
    } catch (Exception e) {
        response = new AllocateDatabaseServerFailed(request.getResourceId(), e);
        LOGGER.warn("Error launching the database stack:", e);
    }
    return response;
}
Also used : AllocateDatabaseServerSuccess(com.sequenceiq.redbeams.flow.redbeams.provision.event.allocate.AllocateDatabaseServerSuccess) AllocateDatabaseServerFailed(com.sequenceiq.redbeams.flow.redbeams.provision.event.allocate.AllocateDatabaseServerFailed) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) OperationException(com.sequenceiq.cloudbreak.service.OperationException) Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) AllocateDatabaseServerRequest(com.sequenceiq.redbeams.flow.redbeams.provision.event.allocate.AllocateDatabaseServerRequest) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ResourcesStatePollerResult(com.sequenceiq.cloudbreak.cloud.task.ResourcesStatePollerResult)

Aggregations

DBStack (com.sequenceiq.redbeams.domain.stack.DBStack)83 Test (org.junit.jupiter.api.Test)28 DatabaseServerConfig (com.sequenceiq.redbeams.domain.DatabaseServerConfig)17 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)17 Test (org.junit.Test)13 Selectable (com.sequenceiq.cloudbreak.common.event.Selectable)8 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)7 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)7 CloudSubnet (com.sequenceiq.cloudbreak.cloud.model.CloudSubnet)7 DatabaseStack (com.sequenceiq.cloudbreak.cloud.model.DatabaseStack)7 DatabaseServerV4Response (com.sequenceiq.redbeams.api.endpoint.v4.databaseserver.responses.DatabaseServerV4Response)7 DetailedEnvironmentResponse (com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse)6 SslConfig (com.sequenceiq.redbeams.domain.stack.SslConfig)6 Credential (com.sequenceiq.redbeams.dto.Credential)6 RedbeamsFailureEvent (com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsFailureEvent)6 Crn (com.sequenceiq.cloudbreak.auth.crn.Crn)5 CloudDatabaseServerSslCertificate (com.sequenceiq.cloudbreak.cloud.model.database.CloudDatabaseServerSslCertificate)5 RedbeamsEvent (com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsEvent)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Location (com.sequenceiq.cloudbreak.cloud.model.Location)4