Search in sources :

Example 1 with DetectConfiguration

use of com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration 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)

Example 2 with DetectConfiguration

use of com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration in project hub-detect by blackducksoftware.

the class DoctorApplication method run.

@Override
public void run(final ApplicationArguments applicationArguments) throws Exception {
    PropertyMap<DoctorProperty> doctorPropertyPropertyMap = new PropertyMap<>();
    SpringPropertySource springPropertySource = new SpringPropertySource(configurableEnvironment);
    DoctorConfiguration doctorConfiguration = new DoctorConfiguration(springPropertySource, doctorPropertyPropertyMap);
    DoctorArgumentStateParser argumentStateParser = new DoctorArgumentStateParser();
    DoctorArgumentState state = argumentStateParser.parseArgs(applicationArguments.getSourceArgs());
    doctorConfiguration.init();
    DoctorRun doctorRun = DoctorRun.createDefault();
    logger.info("Doctor begin: " + doctorRun.getRunId());
    DoctorDirectoryManager doctorDirectoryManager = new DoctorDirectoryManager(doctorRun);
    logger.info("Doctor ready.");
    Optional<DetectRunInfo> detectRunInfo = Optional.empty();
    File diagnosticZip = new File(doctorConfiguration.getProperty(DoctorProperty.DETECT_DIAGNOSTIC_FILE));
    if (diagnosticZip.exists()) {
        logger.info("A diagnostic zip was found: " + diagnosticZip.getAbsolutePath());
        DiagnosticParser diagnosticParser = new DiagnosticParser();
        detectRunInfo = Optional.of(diagnosticParser.processDiagnosticZip(doctorDirectoryManager, diagnosticZip));
    } else {
        logger.info("No diagnostic zip provided, looking for the pieces.");
        File log = new File(doctorConfiguration.getProperty(DoctorProperty.DETECT_LOG_FILE));
        logger.info("Looking for log file at: " + log.getAbsolutePath());
        if (log.exists()) {
            logger.info("Found log file.");
        // detectRunInfo = Optional.of(new DetectRunInfo(log));
        } else {
            logger.info("No log file found.");
        }
    }
    if (detectRunInfo.isPresent()) {
        DetectLogParser logParser = new DetectLogParser();
        logger.info("Doctor can proceed, necessary pieces located.");
        File log = detectRunInfo.get().getLogFile();
        DetectLogParseResult result = logParser.parse(log);
        logger.info("Detect log parsed.");
        String extractionId = doctorConfiguration.getProperty(DoctorProperty.DETECT_EXTRACTION_ID);
        Set<String> extractions = new HashSet<>();
        LoggedDetectExtraction extraction = null;
        for (LoggedDetectExtraction possibleExtraction : result.loggedConfiguration.extractions) {
            extractions.add(possibleExtraction.extractionIdentifier);
            if (possibleExtraction.extractionIdentifier.equals(extractionId)) {
                extraction = possibleExtraction;
            }
        }
        if (StringUtils.isBlank(extractionId)) {
            quit("Doctor needs an extraction to work with, options are: " + extractions.stream().collect(Collectors.joining(",")));
        }
        if (extraction == null) {
            quit("No extraction found for given id: " + extractionId);
        }
        logger.info("Found extraction with id: " + extractionId);
        logger.info("Doctor will attempt to diagnose!");
        logger.info("We begin by rebuilding the configuration.");
        Map<String, String> propertyMap = new HashMap<>();
        result.loggedConfiguration.loggedPropertyList.stream().forEach(it -> propertyMap.put(it.key, it.value));
        RehydratedPropertySource rehydratedPropertySource = new RehydratedPropertySource(propertyMap);
        DetectConfiguration detectConfiguration = new DetectConfiguration(new DetectPropertySource(rehydratedPropertySource), new DetectPropertyMap());
        ExtractionHandler extractionHandler = new ExtractionHandler();
        extractionHandler.processExtraction(extraction, detectRunInfo.get(), detectConfiguration);
    } else {
        quit("Neccessary pieces not found for doctor to proceed.");
    }
}
Also used : DetectRunInfo(com.synopsys.detect.doctor.diagnosticparser.DetectRunInfo) HashMap(java.util.HashMap) SpringPropertySource(com.blackducksoftware.integration.hub.detect.property.SpringPropertySource) DoctorDirectoryManager(com.synopsys.detect.doctor.run.DoctorDirectoryManager) DoctorRun(com.synopsys.detect.doctor.run.DoctorRun) DetectLogParseResult(com.synopsys.detect.doctor.logparser.DetectLogParseResult) DetectPropertyMap(com.blackducksoftware.integration.hub.detect.configuration.DetectPropertyMap) DetectPropertySource(com.blackducksoftware.integration.hub.detect.configuration.DetectPropertySource) DetectLogParser(com.synopsys.detect.doctor.logparser.DetectLogParser) DoctorArgumentState(com.synopsys.detect.doctor.configuration.DoctorArgumentState) HashSet(java.util.HashSet) ExtractionHandler(com.synopsys.detect.doctor.extraction.ExtractionHandler) LoggedDetectExtraction(com.synopsys.detect.doctor.logparser.LoggedDetectExtraction) DoctorConfiguration(com.synopsys.detect.doctor.configuration.DoctorConfiguration) DetectConfiguration(com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration) DoctorProperty(com.synopsys.detect.doctor.configuration.DoctorProperty) PropertyMap(com.blackducksoftware.integration.hub.detect.property.PropertyMap) DetectPropertyMap(com.blackducksoftware.integration.hub.detect.configuration.DetectPropertyMap) DiagnosticParser(com.synopsys.detect.doctor.diagnosticparser.DiagnosticParser) File(java.io.File) DoctorArgumentStateParser(com.synopsys.detect.doctor.configuration.DoctorArgumentStateParser)

Example 3 with DetectConfiguration

use of com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration in project hub-detect by blackducksoftware.

the class DetectorFactoryTest method createSpringContext.

@Before
public void createSpringContext() {
    ConfigurableEnvironment environment = new StandardEnvironment();
    DetectConfiguration mockConfig = new DetectConfiguration(new DetectPropertySource(new SpringPropertySource(environment)), new DetectPropertyMap());
    runContext = new AnnotationConfigApplicationContext();
    runContext.setDisplayName("Detect Run Test");
    runContext.register(DetectorBeanConfiguration.class);
    runContext.getBeanFactory().registerSingleton(Gson.class.getSimpleName(), new Gson());
    runContext.getBeanFactory().registerSingleton(JsonParser.class.getSimpleName(), new JsonParser());
    registerMock(runContext, Configuration.class);
    registerMock(runContext, DocumentBuilder.class);
    registerMock(runContext, ExecutableRunner.class);
    registerMock(runContext, AirGapManager.class);
    registerMock(runContext, ExecutableFinder.class);
    registerMock(runContext, ExternalIdFactory.class);
    registerMock(runContext, DetectFileFinder.class);
    registerMock(runContext, DirectoryManager.class);
    registerMock(runContext, DetectConfiguration.class);
    registerMock(runContext, ConnectionManager.class);
    registerMock(runContext, CacheableExecutableFinder.class);
    registerMock(runContext, ArtifactResolver.class);
    registerMock(runContext, DetectInfo.class);
    runContext.refresh();
}
Also used : DetectConfiguration(com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) DetectPropertyMap(com.blackducksoftware.integration.hub.detect.configuration.DetectPropertyMap) SpringPropertySource(com.blackducksoftware.integration.hub.detect.property.SpringPropertySource) Gson(com.google.gson.Gson) DetectPropertySource(com.blackducksoftware.integration.hub.detect.configuration.DetectPropertySource) StandardEnvironment(org.springframework.core.env.StandardEnvironment) JsonParser(com.google.gson.JsonParser) Before(org.junit.Before)

Example 4 with DetectConfiguration

use of com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration in project hub-detect by blackducksoftware.

the class PearDependencyTest method init.

@Before
public void init() {
    detectConfiguration = Mockito.mock(DetectConfiguration.class);
    pearParser = new PearParser(new ExternalIdFactory(), detectConfiguration);
    testUtil = new TestUtil();
}
Also used : DetectConfiguration(com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration) DependencyGraphResourceTestUtil(com.blackducksoftware.integration.hub.detect.testutils.DependencyGraphResourceTestUtil) TestUtil(com.blackducksoftware.integration.hub.detect.testutils.TestUtil) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) Before(org.junit.Before)

Example 5 with DetectConfiguration

use of com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration in project hub-detect by blackducksoftware.

the class PackagistTest method packagistParserTest.

@Test
public void packagistParserTest() throws IOException {
    final DetectConfiguration detectConfiguration = Mockito.mock(DetectConfiguration.class);
    Mockito.when(detectConfiguration.getBooleanProperty(DetectProperty.DETECT_PACKAGIST_INCLUDE_DEV_DEPENDENCIES, PropertyAuthority.None)).thenReturn(true);
    final PackagistParser packagistParser = new PackagistParser(new ExternalIdFactory(), detectConfiguration);
    final String composerLockText = testUtil.getResourceAsUTF8String("/packagist/composer.lock");
    final String composerJsonText = testUtil.getResourceAsUTF8String("/packagist/composer.json");
    final PackagistParseResult result = packagistParser.getDependencyGraphFromProject("source", composerJsonText, composerLockText);
    Assert.assertEquals(result.projectName, "clue/graph-composer");
    Assert.assertEquals(result.projectVersion, "1.0.0");
    DependencyGraphResourceTestUtil.assertGraph("/packagist/PackagistTestDependencyNode_graph.json", result.codeLocation.getDependencyGraph());
}
Also used : DetectConfiguration(com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) Test(org.junit.Test)

Aggregations

DetectConfiguration (com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration)8 ConnectivityManager (com.blackducksoftware.integration.hub.detect.workflow.ConnectivityManager)4 DirectoryManager (com.blackducksoftware.integration.hub.detect.workflow.file.DirectoryManager)4 DetectConfigurationFactory (com.blackducksoftware.integration.hub.detect.configuration.DetectConfigurationFactory)3 DetectPropertyMap (com.blackducksoftware.integration.hub.detect.configuration.DetectPropertyMap)3 DetectPropertySource (com.blackducksoftware.integration.hub.detect.configuration.DetectPropertySource)3 ExitCodeRequest (com.blackducksoftware.integration.hub.detect.lifecycle.shutdown.ExitCodeRequest)3 SpringPropertySource (com.blackducksoftware.integration.hub.detect.property.SpringPropertySource)3 DetectInfo (com.blackducksoftware.integration.hub.detect.DetectInfo)2 ConnectionManager (com.blackducksoftware.integration.hub.detect.configuration.ConnectionManager)2 DiagnosticManager (com.blackducksoftware.integration.hub.detect.workflow.diagnostic.DiagnosticManager)2 EventSystem (com.blackducksoftware.integration.hub.detect.workflow.event.EventSystem)2 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)2 DetectorBeanConfiguration (com.blackducksoftware.integration.hub.detect.DetectorBeanConfiguration)1 RunBeanConfiguration (com.blackducksoftware.integration.hub.detect.RunBeanConfiguration)1 DetectorEnvironment (com.blackducksoftware.integration.hub.detect.detector.DetectorEnvironment)1 DetectorFactory (com.blackducksoftware.integration.hub.detect.detector.DetectorFactory)1 DetectUserFriendlyException (com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException)1 ExitCodeType (com.blackducksoftware.integration.hub.detect.exitcode.ExitCodeType)1 DetectArgumentState (com.blackducksoftware.integration.hub.detect.help.DetectArgumentState)1