Search in sources :

Example 1 with SpringPropertySource

use of com.blackducksoftware.integration.hub.detect.property.SpringPropertySource 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 2 with SpringPropertySource

use of com.blackducksoftware.integration.hub.detect.property.SpringPropertySource 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 3 with SpringPropertySource

use of com.blackducksoftware.integration.hub.detect.property.SpringPropertySource in project hub-detect by blackducksoftware.

the class BootManager method boot.

public BootResult boot(DetectRun detectRun, final String[] sourceArgs, ConfigurableEnvironment environment, EventSystem eventSystem, DetectContext detectContext) throws DetectUserFriendlyException, IntegrationException {
    Gson gson = bootFactory.createGson();
    ObjectMapper objectMapper = bootFactory.createObjectMapper();
    DocumentBuilder xml = bootFactory.createXmlDocumentBuilder();
    Configuration configuration = bootFactory.createConfiguration();
    DetectInfo detectInfo = DetectInfoUtility.createDefaultDetectInfo();
    SpringPropertySource springPropertySource = new SpringPropertySource(environment);
    DetectPropertySource propertySource = new DetectPropertySource(springPropertySource);
    DetectPropertyMap propertyMap = new DetectPropertyMap();
    DetectConfiguration detectConfiguration = new DetectConfiguration(propertySource, propertyMap);
    DetectOptionManager detectOptionManager = new DetectOptionManager(detectConfiguration, detectInfo);
    final List<DetectOption> options = detectOptionManager.getDetectOptions();
    DetectArgumentState detectArgumentState = parseDetectArgumentState(sourceArgs);
    if (detectArgumentState.isHelp() || detectArgumentState.isDeprecatedHelp() || detectArgumentState.isVerboseHelp()) {
        printAppropriateHelp(options, detectArgumentState);
        return BootResult.exit(detectConfiguration);
    }
    if (detectArgumentState.isHelpHtmlDocument()) {
        printHelpHtmlDocument(options, detectInfo, configuration);
        return BootResult.exit(detectConfiguration);
    }
    if (detectArgumentState.isHelpJsonDocument()) {
        printHelpJsonDocument(options, detectInfo, configuration, gson);
        return BootResult.exit(detectConfiguration);
    }
    printDetectInfo(detectInfo);
    if (detectArgumentState.isInteractive()) {
        startInteractiveMode(detectOptionManager, detectConfiguration, gson, objectMapper);
    }
    processDetectConfiguration(detectInfo, detectRun, detectConfiguration, options);
    detectOptionManager.postConfigurationProcessedInit();
    logger.info("Configuration processed completely.");
    printConfiguration(detectConfiguration.getBooleanProperty(DetectProperty.DETECT_SUPPRESS_CONFIGURATION_OUTPUT, PropertyAuthority.None), options);
    logger.info("Initializing detect.");
    DetectConfigurationFactory factory = new DetectConfigurationFactory(detectConfiguration);
    DirectoryManager directoryManager = new DirectoryManager(factory.createDirectoryOptions(), detectRun);
    DiagnosticManager diagnosticManager = createDiagnostics(detectOptionManager.getDetectOptions(), detectRun, detectInfo, detectArgumentState, eventSystem, directoryManager);
    checkForInvalidOptions(detectOptionManager);
    if (detectOptionManager.checkForAnyFailureProperties()) {
        eventSystem.publishEvent(Event.ExitCode, new ExitCodeRequest(ExitCodeType.FAILURE_CONFIGURATION));
        return BootResult.exit(detectConfiguration);
    }
    ConnectivityManager connectivityManager;
    boolean offline = detectConfiguration.getBooleanProperty(DetectProperty.BLACKDUCK_OFFLINE_MODE, PropertyAuthority.None);
    if (offline) {
        logger.info("Detect is in offline mode.");
        connectivityManager = ConnectivityManager.offline();
    } else {
        logger.info("Detect is in online mode.");
        // check my connectivity
        ConnectivityChecker connectivityChecker = new ConnectivityChecker();
        ConnectivityResult connectivityResult = connectivityChecker.determineConnectivity(detectConfiguration, detectOptionManager, detectInfo, gson, objectMapper, eventSystem);
        if (connectivityResult.isSuccessfullyConnected()) {
            logger.info("Detect is capable of communicating with server.");
            connectivityManager = ConnectivityManager.online(connectivityResult.getBlackDuckServicesFactory(), connectivityResult.getPhoneHomeManager(), connectivityResult.getBlackDuckServerConfig());
        } else {
            logger.info("Detect is NOT capable of communicating with server.");
            logger.info("Please double check the Detect documentation: https://synopsys.atlassian.net/wiki/spaces/INTDOCS/pages/622633/Hub+Detect");
            if (detectConfiguration.getBooleanProperty(DetectProperty.DETECT_DISABLE_WITHOUT_BLACKDUCK, PropertyAuthority.None)) {
                logger.info(connectivityResult.getFailureReason());
                logger.info(String.format("%s is set to 'true' so Detect will simply exit.", DetectProperty.DETECT_DISABLE_WITHOUT_BLACKDUCK.getPropertyName()));
                return BootResult.exit(detectConfiguration);
            } else {
                throw new DetectUserFriendlyException("Could not communicate with Black Duck: " + connectivityResult.getFailureReason(), ExitCodeType.FAILURE_HUB_CONNECTIVITY);
            }
        }
    }
    if (detectConfiguration.getBooleanProperty(DetectProperty.DETECT_TEST_CONNECTION, PropertyAuthority.None)) {
        logger.info(String.format("%s is set to 'true' so Detect will not run.", DetectProperty.DETECT_TEST_CONNECTION.getPropertyName()));
        return BootResult.exit(detectConfiguration);
    }
    // TODO: Only need this if in diagnostic or online (for phone home):
    BomToolProfiler profiler = new BomToolProfiler(eventSystem);
    // lock the configuration, boot has completed.
    logger.debug("Configuration is now complete. No changes should occur to configuration.");
    detectConfiguration.lock();
    // Finished, populate the detect context
    detectContext.registerBean(detectRun);
    detectContext.registerBean(eventSystem);
    detectContext.registerBean(profiler);
    detectContext.registerBean(detectConfiguration);
    detectContext.registerBean(detectInfo);
    detectContext.registerBean(directoryManager);
    detectContext.registerBean(diagnosticManager);
    detectContext.registerBean(connectivityManager);
    detectContext.registerBean(gson);
    detectContext.registerBean(objectMapper);
    detectContext.registerBean(xml);
    detectContext.registerBean(configuration);
    detectContext.registerConfiguration(RunBeanConfiguration.class);
    detectContext.registerConfiguration(DetectorBeanConfiguration.class);
    // can only refresh once, this locks and triggers refresh.
    detectContext.lock();
    BootResult result = new BootResult();
    result.bootType = BootResult.BootType.CONTINUE;
    result.detectConfiguration = detectConfiguration;
    return result;
}
Also used : RunBeanConfiguration(com.blackducksoftware.integration.hub.detect.RunBeanConfiguration) DetectorBeanConfiguration(com.blackducksoftware.integration.hub.detect.DetectorBeanConfiguration) DetectConfiguration(com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration) Configuration(freemarker.template.Configuration) DetectInfo(com.blackducksoftware.integration.hub.detect.DetectInfo) ConnectivityManager(com.blackducksoftware.integration.hub.detect.workflow.ConnectivityManager) SpringPropertySource(com.blackducksoftware.integration.hub.detect.property.SpringPropertySource) ExitCodeRequest(com.blackducksoftware.integration.hub.detect.lifecycle.shutdown.ExitCodeRequest) DirectoryManager(com.blackducksoftware.integration.hub.detect.workflow.file.DirectoryManager) Gson(com.google.gson.Gson) BomToolProfiler(com.blackducksoftware.integration.hub.detect.workflow.profiling.BomToolProfiler) DetectOptionManager(com.blackducksoftware.integration.hub.detect.help.DetectOptionManager) DetectOption(com.blackducksoftware.integration.hub.detect.help.DetectOption) DetectConfiguration(com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration) DetectPropertyMap(com.blackducksoftware.integration.hub.detect.configuration.DetectPropertyMap) DetectUserFriendlyException(com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException) DetectArgumentState(com.blackducksoftware.integration.hub.detect.help.DetectArgumentState) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DetectConfigurationFactory(com.blackducksoftware.integration.hub.detect.configuration.DetectConfigurationFactory) DiagnosticManager(com.blackducksoftware.integration.hub.detect.workflow.diagnostic.DiagnosticManager) DetectPropertySource(com.blackducksoftware.integration.hub.detect.configuration.DetectPropertySource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

DetectConfiguration (com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration)3 DetectPropertyMap (com.blackducksoftware.integration.hub.detect.configuration.DetectPropertyMap)3 DetectPropertySource (com.blackducksoftware.integration.hub.detect.configuration.DetectPropertySource)3 SpringPropertySource (com.blackducksoftware.integration.hub.detect.property.SpringPropertySource)3 Gson (com.google.gson.Gson)2 DetectInfo (com.blackducksoftware.integration.hub.detect.DetectInfo)1 DetectorBeanConfiguration (com.blackducksoftware.integration.hub.detect.DetectorBeanConfiguration)1 RunBeanConfiguration (com.blackducksoftware.integration.hub.detect.RunBeanConfiguration)1 DetectConfigurationFactory (com.blackducksoftware.integration.hub.detect.configuration.DetectConfigurationFactory)1 DetectUserFriendlyException (com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException)1 DetectArgumentState (com.blackducksoftware.integration.hub.detect.help.DetectArgumentState)1 DetectOption (com.blackducksoftware.integration.hub.detect.help.DetectOption)1 DetectOptionManager (com.blackducksoftware.integration.hub.detect.help.DetectOptionManager)1 ExitCodeRequest (com.blackducksoftware.integration.hub.detect.lifecycle.shutdown.ExitCodeRequest)1 PropertyMap (com.blackducksoftware.integration.hub.detect.property.PropertyMap)1 ConnectivityManager (com.blackducksoftware.integration.hub.detect.workflow.ConnectivityManager)1 DiagnosticManager (com.blackducksoftware.integration.hub.detect.workflow.diagnostic.DiagnosticManager)1 DirectoryManager (com.blackducksoftware.integration.hub.detect.workflow.file.DirectoryManager)1 BomToolProfiler (com.blackducksoftware.integration.hub.detect.workflow.profiling.BomToolProfiler)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1