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());
}
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.");
}
}
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();
}
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();
}
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());
}
Aggregations