Search in sources :

Example 1 with RunResult

use of com.blackducksoftware.integration.hub.detect.lifecycle.run.RunResult in project hub-detect by blackducksoftware.

the class ShutdownManager method shutdown.

public void shutdown(Optional<RunResult> runResultOptional) {
    if (connectivityManager.getPhoneHomeManager().isPresent()) {
        try {
            logger.debug("Ending phone home.");
            connectivityManager.getPhoneHomeManager().get().endPhoneHome();
        } catch (final Exception e) {
            logger.debug(String.format("Error trying to end the phone home task: %s", e.getMessage()));
        }
    }
    try {
        if (diagnosticManager.getDiagnosticSystem().isPresent()) {
            logger.debug("Ending diagnostics.");
            diagnosticManager.getDiagnosticSystem().get().finish();
        }
    } catch (final Exception e) {
        logger.debug(String.format("Error trying to finish diagnostics: %s", e.getMessage()));
    }
    try {
        if (detectConfiguration.getBooleanProperty(DetectProperty.DETECT_CLEANUP, PropertyAuthority.None)) {
            logger.info("Detect will cleanup.");
            boolean dryRun = detectConfiguration.getBooleanProperty(DetectProperty.DETECT_BLACKDUCK_SIGNATURE_SCANNER_DRY_RUN, PropertyAuthority.None);
            boolean offline = !connectivityManager.isDetectOnline();
            List<File> cleanupToSkip = new ArrayList<>();
            if (dryRun || offline) {
                logger.debug("Will not cleanup scan folder.");
                cleanupToSkip.add(directoryManager.getScanOutputDirectory());
            }
            if (offline) {
                logger.debug("Will not cleanup bdio folder.");
                cleanupToSkip.add(directoryManager.getBdioOutputDirectory());
            }
            logger.debug("Cleaning up directory: " + directoryManager.getRunHomeDirectory().getAbsolutePath());
            cleanup(directoryManager.getRunHomeDirectory(), cleanupToSkip);
        } else {
            logger.info("Skipping cleanup, it is disabled.");
        }
    } catch (final Exception e) {
        logger.debug(String.format("Error trying cleanup: %s", e.getMessage()));
    }
    Set<DetectorType> detectorTypes = new HashSet<>();
    if (runResultOptional.isPresent()) {
        detectorTypes.addAll(runResultOptional.get().getApplicableDetectors());
    }
    // Check required detector types
    String requiredDetectors = detectConfiguration.getProperty(DetectProperty.DETECT_REQUIRED_DETECTOR_TYPES, PropertyAuthority.None);
    RequiredDetectorChecker requiredDetectorChecker = new RequiredDetectorChecker();
    RequiredDetectorChecker.RequiredDetectorResult requiredDetectorResult = requiredDetectorChecker.checkForMissingDetectors(requiredDetectors, detectorTypes);
    if (requiredDetectorResult.wereDetectorsMissing()) {
        String missingDetectors = requiredDetectorResult.getMissingDetectors().stream().map(it -> it.toString()).collect(Collectors.joining(","));
        logger.error("One or more required detector types were not found: " + missingDetectors);
        exitCodeManager.requestExitCode(ExitCodeType.FAILURE_DETECTOR_REQUIRED);
    }
}
Also used : DetectorType(com.blackducksoftware.integration.hub.detect.detector.DetectorType) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) ConnectivityManager(com.blackducksoftware.integration.hub.detect.workflow.ConnectivityManager) File(java.io.File) DetectConfiguration(com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DiagnosticManager(com.blackducksoftware.integration.hub.detect.workflow.diagnostic.DiagnosticManager) List(java.util.List) ExitCodeType(com.blackducksoftware.integration.hub.detect.exitcode.ExitCodeType) PropertyAuthority(com.blackducksoftware.integration.hub.detect.configuration.PropertyAuthority) DirectoryManager(com.blackducksoftware.integration.hub.detect.workflow.file.DirectoryManager) Optional(java.util.Optional) RunResult(com.blackducksoftware.integration.hub.detect.lifecycle.run.RunResult) DetectStatusManager(com.blackducksoftware.integration.hub.detect.workflow.status.DetectStatusManager) RequiredDetectorChecker(com.blackducksoftware.integration.hub.detect.workflow.detector.RequiredDetectorChecker) ReportManager(com.blackducksoftware.integration.hub.detect.workflow.report.ReportManager) DetectProperty(com.blackducksoftware.integration.hub.detect.configuration.DetectProperty) DetectorType(com.blackducksoftware.integration.hub.detect.detector.DetectorType) ArrayList(java.util.ArrayList) RequiredDetectorChecker(com.blackducksoftware.integration.hub.detect.workflow.detector.RequiredDetectorChecker) File(java.io.File) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 2 with RunResult

use of com.blackducksoftware.integration.hub.detect.lifecycle.run.RunResult in project hub-detect by blackducksoftware.

the class Application method run.

@Override
public void run(final ApplicationArguments applicationArguments) throws Exception {
    final long startTime = System.currentTimeMillis();
    // Events, Status and Exit Codes are required even if boot fails.
    EventSystem eventSystem = new EventSystem();
    DetectStatusManager statusManager = new DetectStatusManager(eventSystem);
    ExitCodeUtility exitCodeUtility = new ExitCodeUtility();
    ExitCodeManager exitCodeManager = new ExitCodeManager(eventSystem, exitCodeUtility);
    ReportManager reportManager = ReportManager.createDefault(eventSystem);
    // Before boot even begins, we create a new Spring context for Detect to work within.
    logger.info("Preparing detect.");
    DetectRun detectRun = DetectRun.createDefault();
    DetectContext detectContext = new DetectContext(detectRun);
    BootResult bootResult = null;
    Optional<RunResult> runResult = Optional.empty();
    try {
        logger.info("Detect boot begin.");
        BootManager bootManager = new BootManager(new BootFactory());
        bootResult = bootManager.boot(detectRun, applicationArguments.getSourceArgs(), environment, eventSystem, detectContext);
        logger.info("Detect boot completed.");
    } catch (final Exception e) {
        logger.error("Detect boot failed.");
        exitCodeManager.requestExitCode(e);
    }
    if (bootResult != null && bootResult.bootType == BootResult.BootType.CONTINUE) {
        logger.info("Detect will attempt to run.");
        RunManager runManager = new RunManager(detectContext);
        try {
            logger.info("Detect run begin: " + detectRun.getRunId());
            runResult = Optional.ofNullable(runManager.run());
            logger.info("Detect run completed.");
        } catch (final Exception e) {
            if (e.getMessage() != null) {
                logger.error("Detect run failed: " + e.getMessage());
            } else {
                logger.error("Detect run failed: " + e.getClass().getSimpleName());
            }
            logger.debug("An exception was thrown during the detect run.", e);
            exitCodeManager.requestExitCode(e);
        }
        try {
            logger.info("Detect will attempt to shutdown.");
            DiagnosticManager diagnosticManager = detectContext.getBean(DiagnosticManager.class);
            DirectoryManager directoryManager = detectContext.getBean(DirectoryManager.class);
            DetectConfiguration detectConfiguration = detectContext.getBean(DetectConfiguration.class);
            ConnectivityManager connectivityManager = detectContext.getBean(ConnectivityManager.class);
            ShutdownManager shutdownManager = new ShutdownManager(connectivityManager, statusManager, exitCodeManager, directoryManager, detectConfiguration, reportManager, diagnosticManager);
            logger.info("Detect shutdown begin.");
            shutdownManager.shutdown(runResult);
            logger.info("Detect shutdown completed.");
        } catch (final Exception e) {
            logger.error("Detect shutdown failed.");
            exitCodeManager.requestExitCode(e);
        }
    } else {
        logger.debug("Detect will NOT attempt to run.");
    }
    logger.info("All detect actions completed.");
    // Determine how detect should actually exit
    boolean printOutput = true;
    boolean shouldForceSuccess = false;
    if (bootResult != null && bootResult.detectConfiguration != null) {
        printOutput = !bootResult.detectConfiguration.getBooleanProperty(DetectProperty.DETECT_SUPPRESS_RESULTS_OUTPUT, PropertyAuthority.None);
        shouldForceSuccess = bootResult.detectConfiguration.getBooleanProperty(DetectProperty.DETECT_FORCE_SUCCESS, PropertyAuthority.None);
    }
    // Generally, when requesting a failure status, an exit code is also requested, but if it is not, we default to an unknown error.
    if (statusManager.hasAnyFailure()) {
        eventSystem.publishEvent(Event.ExitCode, new ExitCodeRequest(ExitCodeType.FAILURE_UNKNOWN_ERROR, "A failure status was requested by one or more of Detect's tools."));
    }
    // Find the final (as requested) exit code
    ExitCodeType finalExitCode = exitCodeManager.getWinningExitCode();
    // Print detect's status
    if (printOutput) {
        reportManager.printDetectorIssues();
        statusManager.logDetectResults(new Slf4jIntLogger(logger), finalExitCode);
    }
    // Print duration of run
    final long endTime = System.currentTimeMillis();
    logger.info(String.format("Detect duration: %s", DurationFormatUtils.formatPeriod(startTime, endTime, "HH'h' mm'm' ss's' SSS'ms'")));
    // Exit with formal exit code
    if (finalExitCode != ExitCodeType.SUCCESS && shouldForceSuccess) {
        logger.warn(String.format("Forcing success: Exiting with exit code 0. Ignored exit code was %s.", finalExitCode.getExitCode()));
        System.exit(0);
    } else if (finalExitCode != ExitCodeType.SUCCESS) {
        logger.error(String.format("Exiting with code %s - %s", finalExitCode.getExitCode(), finalExitCode.toString()));
    }
    System.exit(finalExitCode.getExitCode());
}
Also used : DetectContext(com.blackducksoftware.integration.hub.detect.lifecycle.DetectContext) ConnectivityManager(com.blackducksoftware.integration.hub.detect.workflow.ConnectivityManager) ExitCodeRequest(com.blackducksoftware.integration.hub.detect.lifecycle.shutdown.ExitCodeRequest) DirectoryManager(com.blackducksoftware.integration.hub.detect.workflow.file.DirectoryManager) ShutdownManager(com.blackducksoftware.integration.hub.detect.lifecycle.shutdown.ShutdownManager) BootResult(com.blackducksoftware.integration.hub.detect.lifecycle.boot.BootResult) RunManager(com.blackducksoftware.integration.hub.detect.lifecycle.run.RunManager) ReportManager(com.blackducksoftware.integration.hub.detect.workflow.report.ReportManager) BlackDuckApiException(com.synopsys.integration.blackduck.exception.BlackDuckApiException) ExitCodeType(com.blackducksoftware.integration.hub.detect.exitcode.ExitCodeType) DetectRun(com.blackducksoftware.integration.hub.detect.workflow.DetectRun) DetectConfiguration(com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) DiagnosticManager(com.blackducksoftware.integration.hub.detect.workflow.diagnostic.DiagnosticManager) ExitCodeUtility(com.blackducksoftware.integration.hub.detect.lifecycle.shutdown.ExitCodeUtility) EventSystem(com.blackducksoftware.integration.hub.detect.workflow.event.EventSystem) RunResult(com.blackducksoftware.integration.hub.detect.lifecycle.run.RunResult) BootFactory(com.blackducksoftware.integration.hub.detect.lifecycle.boot.BootFactory) DetectStatusManager(com.blackducksoftware.integration.hub.detect.workflow.status.DetectStatusManager) ExitCodeManager(com.blackducksoftware.integration.hub.detect.lifecycle.shutdown.ExitCodeManager) BootManager(com.blackducksoftware.integration.hub.detect.lifecycle.boot.BootManager)

Aggregations

DetectConfiguration (com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration)2 ExitCodeType (com.blackducksoftware.integration.hub.detect.exitcode.ExitCodeType)2 RunResult (com.blackducksoftware.integration.hub.detect.lifecycle.run.RunResult)2 ConnectivityManager (com.blackducksoftware.integration.hub.detect.workflow.ConnectivityManager)2 DiagnosticManager (com.blackducksoftware.integration.hub.detect.workflow.diagnostic.DiagnosticManager)2 DirectoryManager (com.blackducksoftware.integration.hub.detect.workflow.file.DirectoryManager)2 ReportManager (com.blackducksoftware.integration.hub.detect.workflow.report.ReportManager)2 DetectStatusManager (com.blackducksoftware.integration.hub.detect.workflow.status.DetectStatusManager)2 DetectProperty (com.blackducksoftware.integration.hub.detect.configuration.DetectProperty)1 PropertyAuthority (com.blackducksoftware.integration.hub.detect.configuration.PropertyAuthority)1 DetectorType (com.blackducksoftware.integration.hub.detect.detector.DetectorType)1 DetectContext (com.blackducksoftware.integration.hub.detect.lifecycle.DetectContext)1 BootFactory (com.blackducksoftware.integration.hub.detect.lifecycle.boot.BootFactory)1 BootManager (com.blackducksoftware.integration.hub.detect.lifecycle.boot.BootManager)1 BootResult (com.blackducksoftware.integration.hub.detect.lifecycle.boot.BootResult)1 RunManager (com.blackducksoftware.integration.hub.detect.lifecycle.run.RunManager)1 ExitCodeManager (com.blackducksoftware.integration.hub.detect.lifecycle.shutdown.ExitCodeManager)1 ExitCodeRequest (com.blackducksoftware.integration.hub.detect.lifecycle.shutdown.ExitCodeRequest)1 ExitCodeUtility (com.blackducksoftware.integration.hub.detect.lifecycle.shutdown.ExitCodeUtility)1 ShutdownManager (com.blackducksoftware.integration.hub.detect.lifecycle.shutdown.ShutdownManager)1