Search in sources :

Example 1 with RebootInstanceEvent

use of com.sequenceiq.freeipa.flow.instance.reboot.RebootInstanceEvent in project cloudbreak by hortonworks.

the class RepairInstancesService method rebootInstances.

/**
 * If no instance passed in request, reboot all bad instances
 * If instances passed in request, reboot all valid passed bad instances
 * If force and instances passed in request, reboot all valid passed instances
 * If force and no instances passed in request, reboot all instances
 *
 * @param accountId - The account id for the instance to reboot.
 * @param request   - A RebootInstanceRequest containing request parameters.
 */
public OperationStatus rebootInstances(String accountId, RebootInstancesRequest request) {
    Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithListsAndMdcContext(request.getEnvironmentCrn(), accountId);
    Map<String, InstanceMetaData> allInstancesByInstanceId = getAllInstancesFromStack(stack);
    Map<String, InstanceStatus> healthMap = request.isForceReboot() ? Collections.emptyMap() : getInstanceHealthMap(accountId, request.getEnvironmentCrn());
    Map<String, InstanceMetaData> instancesToReboot = getInstancesToRepair(healthMap, allInstancesByInstanceId, request.getInstanceIds(), request.isForceReboot(), true);
    if (instancesToReboot.keySet().isEmpty()) {
        throw new NotFoundException("No unhealthy instances to reboot.  Maybe use the force option.");
    }
    Operation operation = operationService.startOperation(accountId, OperationType.REBOOT, Set.of(stack.getEnvironmentCrn()), Collections.emptySet());
    if (operation.getStatus() == OperationState.RUNNING) {
        stackUpdater.updateStackStatus(stack.getId(), DetailedStackStatus.REPAIR_REQUESTED, "Reboot requested");
        flowManager.notify(REBOOT_EVENT.event(), new RebootInstanceEvent(REBOOT_EVENT.event(), stack.getId(), instancesToReboot.keySet().stream().collect(Collectors.toList()), operation.getOperationId()));
    }
    return operationToOperationStatusConverter.convert(operation);
}
Also used : InstanceMetaData(com.sequenceiq.freeipa.entity.InstanceMetaData) RebootInstanceEvent(com.sequenceiq.freeipa.flow.instance.reboot.RebootInstanceEvent) InstanceStatus(com.sequenceiq.freeipa.api.v1.freeipa.stack.model.common.instance.InstanceStatus) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) Operation(com.sequenceiq.freeipa.entity.Operation) Stack(com.sequenceiq.freeipa.entity.Stack)

Example 2 with RebootInstanceEvent

use of com.sequenceiq.freeipa.flow.instance.reboot.RebootInstanceEvent in project cloudbreak by hortonworks.

the class RebootActions method rebootAction.

@Bean(name = "REBOOT_STATE")
public Action<?, ?> rebootAction() {
    return new AbstractRebootAction<>(RebootInstanceEvent.class) {

        @Override
        protected void doExecute(RebootContext context, RebootInstanceEvent payload, Map<Object, Object> variables) {
            setOperationId(variables, payload.getOperationId());
            LOGGER.info("Starting reboot for {}", context.getInstanceIds());
            rebootService.startInstanceReboot(context);
            sendEvent(context);
        }

        @Override
        protected Object getFailurePayload(RebootInstanceEvent payload, Optional<RebootContext> flowContext, Exception ex) {
            return new InstanceFailureEvent(payload.getResourceId(), ex, payload.getInstanceIds());
        }

        @Override
        protected Selectable createRequest(RebootContext context) {
            List<CloudInstance> cloudInstances = context.getInstanceMetaDataList().stream().map(instanceMetaData -> instanceMetaDataToCloudInstanceConverter.convert(instanceMetaData)).collect(Collectors.toList());
            List<CloudResource> cloudResources = getCloudResources(context.getStack().getId());
            return new RebootInstancesRequest<>(context.getCloudContext(), context.getCloudCredential(), cloudResources, cloudInstances);
        }

        @Override
        protected RebootContext createFlowContext(FlowParameters flowParameters, StateContext<RebootState, RebootEvent> stateContext, RebootInstanceEvent payload) {
            Long stackId = payload.getResourceId();
            Stack stack = stackService.getStackById(stackId);
            MDCBuilder.buildMdcContext(stack);
            List<InstanceMetaData> instances = instanceMetaDataService.findNotTerminatedForStack(stackId).stream().filter(instanceMetaData -> payload.getInstanceIds().contains(instanceMetaData.getInstanceId())).collect(Collectors.toList());
            CloudContext cloudContext = getCloudContext(stack);
            Credential credential = credentialService.getCredentialByEnvCrn(stack.getEnvironmentCrn());
            CloudCredential cloudCredential = credentialConverter.convert(credential);
            return new RebootContext(flowParameters, stack, instances, cloudContext, cloudCredential);
        }
    };
}
Also used : Action(org.springframework.statemachine.action.Action) InstanceMetaDataToCloudInstanceConverter(com.sequenceiq.freeipa.converter.cloud.InstanceMetaDataToCloudInstanceConverter) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) RebootInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.RebootInstancesRequest) LoggerFactory(org.slf4j.LoggerFactory) MDCBuilder(com.sequenceiq.cloudbreak.logger.MDCBuilder) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) RebootInstanceEvent(com.sequenceiq.freeipa.flow.instance.reboot.RebootInstanceEvent) ResourceToCloudResourceConverter(com.sequenceiq.freeipa.converter.cloud.ResourceToCloudResourceConverter) InstanceEvent(com.sequenceiq.freeipa.flow.instance.InstanceEvent) Inject(javax.inject.Inject) HealthCheckRequest(com.sequenceiq.freeipa.flow.stack.HealthCheckRequest) StateContext(org.springframework.statemachine.StateContext) Credential(com.sequenceiq.freeipa.dto.Credential) ResourceService(com.sequenceiq.freeipa.service.resource.ResourceService) InstanceFailureEvent(com.sequenceiq.freeipa.flow.instance.InstanceFailureEvent) Map(java.util.Map) Resource(com.sequenceiq.freeipa.entity.Resource) StackService(com.sequenceiq.freeipa.service.stack.StackService) RebootEvent(com.sequenceiq.freeipa.flow.instance.reboot.RebootEvent) InstanceMetaData(com.sequenceiq.freeipa.entity.InstanceMetaData) Stack(com.sequenceiq.freeipa.entity.Stack) RebootInstancesResultToCleanupFailureEventConverter(com.sequenceiq.freeipa.flow.freeipa.cleanup.event.failure.RebootInstancesResultToCleanupFailureEventConverter) RebootState(com.sequenceiq.freeipa.flow.instance.reboot.RebootState) PayloadConverter(com.sequenceiq.flow.core.PayloadConverter) InstanceMetaDataService(com.sequenceiq.freeipa.service.stack.instance.InstanceMetaDataService) Logger(org.slf4j.Logger) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) FailureDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails) FlowParameters(com.sequenceiq.flow.core.FlowParameters) CredentialToCloudCredentialConverter(com.sequenceiq.freeipa.converter.cloud.CredentialToCloudCredentialConverter) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Collectors(java.util.stream.Collectors) HealthCheckSuccess(com.sequenceiq.freeipa.flow.stack.HealthCheckSuccess) OperationService(com.sequenceiq.freeipa.service.operation.OperationService) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) RebootContext(com.sequenceiq.freeipa.flow.instance.reboot.RebootContext) RebootService(com.sequenceiq.freeipa.flow.instance.reboot.RebootService) RebootInstancesResult(com.sequenceiq.cloudbreak.cloud.event.instance.RebootInstancesResult) WaitUntilAvailableFailedToInstanceFailureEventConverter(com.sequenceiq.freeipa.flow.instance.reboot.failure.WaitUntilAvailableFailedToInstanceFailureEventConverter) Optional(java.util.Optional) SuccessDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) CredentialService(com.sequenceiq.freeipa.service.CredentialService) Credential(com.sequenceiq.freeipa.dto.Credential) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) InstanceFailureEvent(com.sequenceiq.freeipa.flow.instance.InstanceFailureEvent) Optional(java.util.Optional) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) StateContext(org.springframework.statemachine.StateContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Stack(com.sequenceiq.freeipa.entity.Stack) RebootInstancesRequest(com.sequenceiq.cloudbreak.cloud.event.instance.RebootInstancesRequest) InstanceMetaData(com.sequenceiq.freeipa.entity.InstanceMetaData) FlowParameters(com.sequenceiq.flow.core.FlowParameters) RebootInstanceEvent(com.sequenceiq.freeipa.flow.instance.reboot.RebootInstanceEvent) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) RebootContext(com.sequenceiq.freeipa.flow.instance.reboot.RebootContext) Map(java.util.Map) Bean(org.springframework.context.annotation.Bean)

Aggregations

InstanceMetaData (com.sequenceiq.freeipa.entity.InstanceMetaData)2 Stack (com.sequenceiq.freeipa.entity.Stack)2 RebootInstanceEvent (com.sequenceiq.freeipa.flow.instance.reboot.RebootInstanceEvent)2 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)1 RebootInstancesRequest (com.sequenceiq.cloudbreak.cloud.event.instance.RebootInstancesRequest)1 RebootInstancesResult (com.sequenceiq.cloudbreak.cloud.event.instance.RebootInstancesResult)1 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)1 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)1 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1 Selectable (com.sequenceiq.cloudbreak.common.event.Selectable)1 NotFoundException (com.sequenceiq.cloudbreak.common.exception.NotFoundException)1 MDCBuilder (com.sequenceiq.cloudbreak.logger.MDCBuilder)1 FlowParameters (com.sequenceiq.flow.core.FlowParameters)1 PayloadConverter (com.sequenceiq.flow.core.PayloadConverter)1 InstanceStatus (com.sequenceiq.freeipa.api.v1.freeipa.stack.model.common.instance.InstanceStatus)1 FailureDetails (com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails)1 SuccessDetails (com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails)1 CredentialToCloudCredentialConverter (com.sequenceiq.freeipa.converter.cloud.CredentialToCloudCredentialConverter)1 InstanceMetaDataToCloudInstanceConverter (com.sequenceiq.freeipa.converter.cloud.InstanceMetaDataToCloudInstanceConverter)1 ResourceToCloudResourceConverter (com.sequenceiq.freeipa.converter.cloud.ResourceToCloudResourceConverter)1