Search in sources :

Example 1 with StackPatchType

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

the class ExistingStackPatcherJob method applyStackPatch.

private boolean applyStackPatch(ExistingStackPatchService existingStackPatchService, StackPatch stackPatch) throws JobExecutionException {
    Stack stack = stackPatch.getStack();
    StackPatchType stackPatchType = existingStackPatchService.getStackPatchType();
    if (!StackPatchStatus.FIXED.equals(stackPatch.getStatus())) {
        try {
            if (existingStackPatchService.isAffected(stack)) {
                LOGGER.debug("Stack {} needs patch for {}", stack.getResourceCrn(), stackPatchType);
                stackPatchService.updateStatusAndReportUsage(stackPatch, StackPatchStatus.AFFECTED);
                boolean success = existingStackPatchService.apply(stack);
                if (success) {
                    stackPatchService.updateStatusAndReportUsage(stackPatch, StackPatchStatus.FIXED);
                } else {
                    stackPatchService.updateStatus(stackPatch, StackPatchStatus.SKIPPED);
                }
                return success;
            } else {
                LOGGER.debug("Stack {} is not affected by {}", stack.getResourceCrn(), stackPatchType);
                stackPatchService.updateStatus(stackPatch, StackPatchStatus.NOT_AFFECTED);
                return true;
            }
        } catch (ExistingStackPatchApplyException e) {
            String message = String.format("Failed to patch stack %s for %s", stack.getResourceCrn(), stackPatchType);
            LOGGER.error(message, e);
            stackPatchService.updateStatusAndReportUsage(stackPatch, StackPatchStatus.FAILED, e.getMessage());
            throw new JobExecutionException(message, e);
        }
    } else {
        LOGGER.debug("Stack {} was already patched for {}", stack.getResourceCrn(), stackPatchType);
        return true;
    }
}
Also used : JobExecutionException(org.quartz.JobExecutionException) ExistingStackPatchApplyException(com.sequenceiq.cloudbreak.service.stackpatch.ExistingStackPatchApplyException) StackPatchType(com.sequenceiq.cloudbreak.domain.stack.StackPatchType) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack)

Example 2 with StackPatchType

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

the class ExistingStackPatcherJob method executeTracedJob.

@Override
protected void executeTracedJob(JobExecutionContext context) throws JobExecutionException {
    Stack stack = stackService.getByIdWithListsInTransaction(getStackId());
    Status stackStatus = stack.getStatus();
    String stackPatchTypeName = context.getJobDetail().getJobDataMap().getString(STACK_PATCH_TYPE_NAME);
    try {
        ExistingStackPatchService existingStackPatchService = existingStackPatcherServiceProvider.provide(stackPatchTypeName);
        StackPatchType stackPatchType = existingStackPatchService.getStackPatchType();
        StackPatch stackPatch = stackPatchService.getOrCreate(stack, stackPatchType);
        if (!Status.getUnschedulableStatuses().contains(stackStatus)) {
            boolean success = applyStackPatch(existingStackPatchService, stackPatch);
            if (success) {
                unscheduleJob(context, stackPatch);
            }
        } else {
            LOGGER.debug("Existing stack patching will be unscheduled, because stack {} status is {}", stack.getResourceCrn(), stackStatus);
            stackPatchService.updateStatus(stackPatch, StackPatchStatus.UNSCHEDULED);
            unscheduleJob(context, stackPatch);
        }
    } catch (UnknownStackPatchTypeException e) {
        String message = "Unknown stack patch type: " + stackPatchTypeName;
        unscheduleAndFailJob(message, context, new StackPatch(stack, StackPatchType.UNKNOWN));
    } catch (Exception e) {
        LOGGER.error("Failed", e);
        throw e;
    }
}
Also used : Status(com.sequenceiq.cloudbreak.api.endpoint.v4.common.Status) StackPatchStatus(com.sequenceiq.cloudbreak.domain.stack.StackPatchStatus) ExistingStackPatchService(com.sequenceiq.cloudbreak.service.stackpatch.ExistingStackPatchService) StackPatch(com.sequenceiq.cloudbreak.domain.stack.StackPatch) StackPatchType(com.sequenceiq.cloudbreak.domain.stack.StackPatchType) ExistingStackPatchApplyException(com.sequenceiq.cloudbreak.service.stackpatch.ExistingStackPatchApplyException) JobExecutionException(org.quartz.JobExecutionException) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack)

Example 3 with StackPatchType

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

the class ExistingStackPatcherServiceProviderTest method shouldFailWithTypeWithoutService.

@Test
void shouldFailWithTypeWithoutService() {
    StackPatchType stackPatchType = StackPatchType.LOGGING_AGENT_AUTO_RESTART;
    assertThatThrownBy(() -> underTest.provide(stackPatchType)).isInstanceOf(UnknownStackPatchTypeException.class).hasMessage("No stack patcher implementation found for type " + stackPatchType);
}
Also used : StackPatchType(com.sequenceiq.cloudbreak.domain.stack.StackPatchType) Test(org.junit.jupiter.api.Test)

Example 4 with StackPatchType

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

the class ExistingStackPatcherJobInitializer method initJobs.

@Override
public void initJobs() {
    Set<StackPatchType> enabledStackPatchTypes = getEnabledStackPatchTypes();
    LOGGER.info("Existing stack patch types enabled: {}", enabledStackPatchTypes);
    List<JobResource> stacks = getAliveJobResources();
    Set<Long> stackIds = stacks.stream().map(JobResource::getLocalId).map(Long::valueOf).collect(Collectors.toSet());
    enabledStackPatchTypes.forEach(stackPatchType -> {
        LOGGER.info("Scheduling stack patcher jobs for {}", stackPatchType);
        Map<Long, List<StackPatch>> stackPatchesByStack = stackPatchService.findAllByTypeForStackIds(stackPatchType, stackIds).stream().collect(Collectors.groupingBy(StackPatch::getStackId));
        for (JobResource stack : stacks) {
            Long stackId = Long.valueOf(stack.getLocalId());
            List<StackPatch> stackPatches = stackPatchesByStack.get(stackId);
            boolean stackPatchIsFinalized = stackPatches != null && stackPatches.stream().anyMatch(stackPatch -> stackPatch.getStatus().isFinal());
            if (!stackPatchIsFinalized) {
                ExistingStackPatcherJobAdapter jobAdapter = new ExistingStackPatcherJobAdapter(stack, stackPatchType);
                stackPatchService.getOrCreate(stackId, stackPatchType);
                jobService.schedule(jobAdapter);
            }
        }
    });
}
Also used : ExistingStackPatcherConfig(com.sequenceiq.cloudbreak.job.stackpatcher.config.ExistingStackPatcherConfig) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) JobResource(com.sequenceiq.cloudbreak.quartz.model.JobResource) Collectors(java.util.stream.Collectors) StackPatch(com.sequenceiq.cloudbreak.domain.stack.StackPatch) StackPatchType(com.sequenceiq.cloudbreak.domain.stack.StackPatchType) Inject(javax.inject.Inject) StackPatchService(com.sequenceiq.cloudbreak.service.stackpatch.StackPatchService) List(java.util.List) Component(org.springframework.stereotype.Component) Map(java.util.Map) AbstractStackJobInitializer(com.sequenceiq.cloudbreak.job.AbstractStackJobInitializer) JobResource(com.sequenceiq.cloudbreak.quartz.model.JobResource) StackPatchType(com.sequenceiq.cloudbreak.domain.stack.StackPatchType) StackPatch(com.sequenceiq.cloudbreak.domain.stack.StackPatch) List(java.util.List)

Example 5 with StackPatchType

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

the class ExistingStackPatcherJobService method schedule.

public void schedule(ExistingStackPatcherJobAdapter resource) {
    JobDetail jobDetail = buildJobDetail(resource);
    JobKey jobKey = jobDetail.getKey();
    StackPatchType stackPatchType = resource.getStackPatchType();
    try {
        ExistingStackPatchService existingStackPatchService = existingStackPatcherServiceProvider.provide(stackPatchType);
        Trigger trigger = buildJobTrigger(jobDetail, existingStackPatchService);
        if (scheduler.getJobDetail(jobKey) != null) {
            LOGGER.info("Unscheduling stack patcher job for stack with key: '{}' and group: '{}'", jobKey.getName(), jobKey.getGroup());
            unschedule(jobKey);
        }
        LOGGER.info("Scheduling stack patcher {} job for stack with key: '{}' and group: '{}'", stackPatchType, jobKey.getName(), jobKey.getGroup());
        scheduler.scheduleJob(jobDetail, trigger);
    } catch (UnknownStackPatchTypeException e) {
        LOGGER.error("Failed to get stack patcher for type {}", stackPatchType, e);
    } catch (SchedulerException e) {
        LOGGER.error("Error during scheduling stack patcher job: {}", jobDetail, e);
    }
}
Also used : ExistingStackPatchService(com.sequenceiq.cloudbreak.service.stackpatch.ExistingStackPatchService) JobDetail(org.quartz.JobDetail) JobKey(org.quartz.JobKey) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException) StackPatchType(com.sequenceiq.cloudbreak.domain.stack.StackPatchType)

Aggregations

StackPatchType (com.sequenceiq.cloudbreak.domain.stack.StackPatchType)6 ExistingStackPatchApplyException (com.sequenceiq.cloudbreak.service.stackpatch.ExistingStackPatchApplyException)3 JobExecutionException (org.quartz.JobExecutionException)3 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)2 StackPatch (com.sequenceiq.cloudbreak.domain.stack.StackPatch)2 ExistingStackPatchService (com.sequenceiq.cloudbreak.service.stackpatch.ExistingStackPatchService)2 Status (com.sequenceiq.cloudbreak.api.endpoint.v4.common.Status)1 StackPatchStatus (com.sequenceiq.cloudbreak.domain.stack.StackPatchStatus)1 AbstractStackJobInitializer (com.sequenceiq.cloudbreak.job.AbstractStackJobInitializer)1 ExistingStackPatcherConfig (com.sequenceiq.cloudbreak.job.stackpatcher.config.ExistingStackPatcherConfig)1 JobResource (com.sequenceiq.cloudbreak.quartz.model.JobResource)1 StackPatchService (com.sequenceiq.cloudbreak.service.stackpatch.StackPatchService)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 Test (org.junit.jupiter.api.Test)1 JobDetail (org.quartz.JobDetail)1 JobKey (org.quartz.JobKey)1