/
DetectBoot.java
310 lines (257 loc) · 18.1 KB
/
DetectBoot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
* synopsys-detect
*
* Copyright (c) 2019 Synopsys, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.detect.lifecycle.boot;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.ConfigurableEnvironment;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.synopsys.integration.detect.DetectInfo;
import com.synopsys.integration.detect.DetectInfoUtility;
import com.synopsys.integration.detect.DetectableBeanConfiguration;
import com.synopsys.integration.detect.RunBeanConfiguration;
import com.synopsys.integration.detect.configuration.ConnectionManager;
import com.synopsys.integration.detect.configuration.DetectConfiguration;
import com.synopsys.integration.detect.configuration.DetectConfigurationFactory;
import com.synopsys.integration.detect.configuration.DetectConfigurationManager;
import com.synopsys.integration.detect.configuration.DetectProperty;
import com.synopsys.integration.detect.configuration.DetectPropertyMap;
import com.synopsys.integration.detect.configuration.DetectPropertySource;
import com.synopsys.integration.detect.configuration.PropertyAuthority;
import com.synopsys.integration.detect.exception.DetectUserFriendlyException;
import com.synopsys.integration.detect.exitcode.ExitCodeType;
import com.synopsys.integration.detect.help.DetectArgumentState;
import com.synopsys.integration.detect.help.DetectArgumentStateParser;
import com.synopsys.integration.detect.help.DetectOption;
import com.synopsys.integration.detect.help.DetectOptionManager;
import com.synopsys.integration.detect.help.html.HelpHtmlWriter;
import com.synopsys.integration.detect.help.json.HelpJsonWriter;
import com.synopsys.integration.detect.help.print.DetectInfoPrinter;
import com.synopsys.integration.detect.help.print.HelpPrinter;
import com.synopsys.integration.detect.interactive.InteractiveManager;
import com.synopsys.integration.detect.interactive.mode.DefaultInteractiveMode;
import com.synopsys.integration.detect.lifecycle.DetectContext;
import com.synopsys.integration.detect.lifecycle.boot.decision.ProductDecider;
import com.synopsys.integration.detect.lifecycle.boot.decision.ProductDecision;
import com.synopsys.integration.detect.lifecycle.boot.product.BlackDuckConnectivityChecker;
import com.synopsys.integration.detect.lifecycle.boot.product.PolarisConnectivityChecker;
import com.synopsys.integration.detect.lifecycle.boot.product.ProductBoot;
import com.synopsys.integration.detect.lifecycle.boot.product.ProductBootFactory;
import com.synopsys.integration.detect.lifecycle.run.RunOptions;
import com.synopsys.integration.detect.lifecycle.run.data.ProductRunData;
import com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest;
import com.synopsys.integration.detect.property.SpringPropertySource;
import com.synopsys.integration.detect.tool.detector.impl.DetectExecutableResolver;
import com.synopsys.integration.detect.tool.detector.inspectors.DockerInspectorInstaller;
import com.synopsys.integration.detect.tool.detector.inspectors.GradleInspectorInstaller;
import com.synopsys.integration.detect.tool.detector.inspectors.nuget.NugetInspectorInstaller;
import com.synopsys.integration.detect.util.TildeInPathResolver;
import com.synopsys.integration.detect.util.filter.DetectToolFilter;
import com.synopsys.integration.detect.workflow.ArtifactResolver;
import com.synopsys.integration.detect.workflow.DetectRun;
import com.synopsys.integration.detect.workflow.airgap.AirGapCreator;
import com.synopsys.integration.detect.workflow.airgap.AirGapPathFinder;
import com.synopsys.integration.detect.workflow.airgap.DockerAirGapCreator;
import com.synopsys.integration.detect.workflow.airgap.GradleAirGapCreator;
import com.synopsys.integration.detect.workflow.airgap.NugetAirGapCreator;
import com.synopsys.integration.detect.workflow.diagnostic.DiagnosticManager;
import com.synopsys.integration.detect.workflow.diagnostic.DiagnosticSystem;
import com.synopsys.integration.detect.workflow.diagnostic.RelevantFileTracker;
import com.synopsys.integration.detect.workflow.event.Event;
import com.synopsys.integration.detect.workflow.event.EventSystem;
import com.synopsys.integration.detect.workflow.file.DirectoryManager;
import com.synopsys.integration.detect.workflow.profiling.DetectorProfiler;
import com.synopsys.integration.detect.workflow.report.DetectConfigurationReporter;
import com.synopsys.integration.detect.workflow.report.writer.InfoLogReportWriter;
import com.synopsys.integration.detectable.detectable.executable.impl.CachedExecutableResolverOptions;
import com.synopsys.integration.detectable.detectable.executable.impl.SimpleExecutableFinder;
import com.synopsys.integration.detectable.detectable.executable.impl.SimpleExecutableResolver;
import com.synopsys.integration.detectable.detectable.executable.impl.SimpleExecutableRunner;
import com.synopsys.integration.detectable.detectable.executable.impl.SimpleLocalExecutableFinder;
import com.synopsys.integration.detectable.detectable.executable.impl.SimpleSystemExecutableFinder;
import com.synopsys.integration.detectable.detectable.file.impl.SimpleFileFinder;
import com.synopsys.integration.exception.IntegrationException;
import freemarker.template.Configuration;
public class DetectBoot {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private DetectBootFactory detectBootFactory;
public DetectBoot(DetectBootFactory detectBootFactory) {
this.detectBootFactory = detectBootFactory;
}
public DetectBootResult boot(DetectRun detectRun, final String[] sourceArgs, ConfigurableEnvironment environment, EventSystem eventSystem, DetectContext detectContext) throws DetectUserFriendlyException, IntegrationException {
Gson gson = detectBootFactory.createGson();
ObjectMapper objectMapper = detectBootFactory.createObjectMapper();
DocumentBuilder xml = detectBootFactory.createXmlDocumentBuilder();
Configuration configuration = detectBootFactory.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 DetectBootResult.exit(detectConfiguration);
}
if (detectArgumentState.isHelpHtmlDocument()) {
printHelpHtmlDocument(options, detectInfo, configuration);
return DetectBootResult.exit(detectConfiguration);
}
if (detectArgumentState.isHelpJsonDocument()) {
printHelpJsonDocument(options, detectInfo, configuration, gson);
return DetectBootResult.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 DetectBootResult.exit(detectConfiguration);
}
logger.info("Main boot completed. Deciding what Detect should do.");
if (detectArgumentState.isGenerateAirGapZip()) {
createAirGapZip(detectConfiguration, gson, eventSystem, configuration);
return DetectBootResult.exit(detectConfiguration);
}
final RunOptions runOptions = factory.createRunOptions();
final DetectToolFilter detectToolFilter = runOptions.getDetectToolFilter();
ProductDecider productDecider = new ProductDecider();
ProductDecision productDecision = productDecider.decide(detectConfiguration, directoryManager.getUserHome(), detectToolFilter);
logger.info("Decided what products will be run. Starting product boot.");
ProductBootFactory productBootFactory = new ProductBootFactory(detectConfiguration, detectInfo, eventSystem, detectOptionManager);
ProductBoot productBoot = new ProductBoot();
ProductRunData productRunData = productBoot.boot(productDecision, detectConfiguration, new BlackDuckConnectivityChecker(), new PolarisConnectivityChecker(), productBootFactory);
if (productRunData == null) {
logger.info("No products to run, Detect is complete.");
return DetectBootResult.exit(detectConfiguration);
}
//TODO: Only need this if in diagnostic or online (for phone home):
DetectorProfiler profiler = new DetectorProfiler(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(gson);
detectContext.registerBean(objectMapper);
detectContext.registerBean(xml);
detectContext.registerBean(configuration);
detectContext.registerConfiguration(RunBeanConfiguration.class);
detectContext.registerConfiguration(DetectableBeanConfiguration.class);
detectContext.lock(); //can only refresh once, this locks and triggers refresh.
return DetectBootResult.run(detectConfiguration, productRunData);
}
private void printAppropriateHelp(List<DetectOption> detectOptions, DetectArgumentState detectArgumentState) {
HelpPrinter helpPrinter = new HelpPrinter();
helpPrinter.printAppropriateHelpMessage(System.out, detectOptions, detectArgumentState);
}
private void printHelpHtmlDocument(List<DetectOption> detectOptions, DetectInfo detectInfo, Configuration configuration) {
HelpHtmlWriter helpHtmlWriter = new HelpHtmlWriter(configuration);
helpHtmlWriter.writeHtmlDocument(String.format("synopsys-detect-%s-help.html", detectInfo.getDetectVersion()), detectOptions);
}
private void printHelpJsonDocument(List<DetectOption> detectOptions, DetectInfo detectInfo, Configuration configuration, Gson gson) {
HelpJsonWriter helpJsonWriter = new HelpJsonWriter(configuration, gson);
helpJsonWriter.writeGsonDocument(String.format("synopsys-detect-%s-help.json", detectInfo.getDetectVersion()), detectOptions);
}
private void printDetectInfo(DetectInfo detectInfo) {
DetectInfoPrinter detectInfoPrinter = new DetectInfoPrinter();
detectInfoPrinter.printInfo(System.out, detectInfo);
}
private void printConfiguration(boolean fullConfiguration, List<DetectOption> detectOptions) {
DetectConfigurationReporter detectConfigurationReporter = new DetectConfigurationReporter();
InfoLogReportWriter infoLogReportWriter = new InfoLogReportWriter();
if (!fullConfiguration) {
detectConfigurationReporter.print(infoLogReportWriter, detectOptions);
}
detectConfigurationReporter.printWarnings(infoLogReportWriter, detectOptions);
}
private void startInteractiveMode(DetectOptionManager detectOptionManager, DetectConfiguration detectConfiguration, Gson gson, ObjectMapper objectMapper) {
InteractiveManager interactiveManager = new InteractiveManager(detectOptionManager);
DefaultInteractiveMode defaultInteractiveMode = new DefaultInteractiveMode(detectOptionManager);
interactiveManager.configureInInteractiveMode(defaultInteractiveMode);
}
private DetectArgumentState parseDetectArgumentState(String[] sourceArgs) {
DetectArgumentStateParser detectArgumentStateParser = new DetectArgumentStateParser();
final DetectArgumentState detectArgumentState = detectArgumentStateParser.parseArgs(sourceArgs);
return detectArgumentState;
}
private void processDetectConfiguration(DetectInfo detectInfo, DetectRun detectRun, DetectConfiguration detectConfiguration, List<DetectOption> detectOptions) throws DetectUserFriendlyException {
TildeInPathResolver tildeInPathResolver = new TildeInPathResolver(DetectConfigurationManager.USER_HOME, detectInfo.getCurrentOs());
DetectConfigurationManager detectConfigurationManager = new DetectConfigurationManager(tildeInPathResolver, detectConfiguration);
detectConfigurationManager.process(detectOptions, detectRun.getRunId());
}
private void checkForInvalidOptions(DetectOptionManager detectOptionManager) throws DetectUserFriendlyException {
final List<DetectOption.OptionValidationResult> invalidDetectOptionResults = detectOptionManager.getAllInvalidOptionResults();
if (!invalidDetectOptionResults.isEmpty()) {
throw new DetectUserFriendlyException(invalidDetectOptionResults.get(0).getValidationMessage(), ExitCodeType.FAILURE_GENERAL_ERROR);
}
}
private DiagnosticManager createDiagnostics(List<DetectOption> detectOptions, DetectRun detectRun, DetectInfo detectInfo, DetectArgumentState detectArgumentState, EventSystem eventSystem, DirectoryManager directoryManager) {
if (detectArgumentState.isDiagnostic() || detectArgumentState.isDiagnosticExtended()) {
boolean extendedMode = detectArgumentState.isDiagnosticExtended();
RelevantFileTracker relevantFileTracker = new RelevantFileTracker(detectArgumentState.isDiagnostic(), detectArgumentState.isDiagnosticExtended(), directoryManager);
DiagnosticSystem diagnosticSystem = new DiagnosticSystem(extendedMode, detectOptions, detectRun, detectInfo, relevantFileTracker, directoryManager, eventSystem);
return DiagnosticManager.createWithDiagnostics(diagnosticSystem);
} else {
return DiagnosticManager.createWithoutDiagnostics();
}
}
private void createAirGapZip(DetectConfiguration detectConfiguration, Gson gson, EventSystem eventSystem, Configuration configuration) throws DetectUserFriendlyException {
ConnectionManager connectionManager = new ConnectionManager(detectConfiguration);
ArtifactResolver artifactResolver = new ArtifactResolver(connectionManager, gson);
//TODO: This is awful, why is making this so convoluted.
SimpleFileFinder fileFinder = new SimpleFileFinder();
SimpleExecutableFinder simpleExecutableFinder = SimpleExecutableFinder.forCurrentOperatingSystem(fileFinder);
SimpleLocalExecutableFinder localExecutableFinder = new SimpleLocalExecutableFinder(simpleExecutableFinder);
SimpleSystemExecutableFinder simpleSystemExecutableFinder = new SimpleSystemExecutableFinder(simpleExecutableFinder);
SimpleExecutableResolver executableResolver = new SimpleExecutableResolver(new CachedExecutableResolverOptions(false), localExecutableFinder, simpleSystemExecutableFinder);
DetectExecutableResolver detectExecutableResolver = new DetectExecutableResolver(executableResolver, detectConfiguration);
GradleInspectorInstaller gradleInspectorInstaller = new GradleInspectorInstaller(artifactResolver);
SimpleExecutableRunner simpleExecutableRunner = new SimpleExecutableRunner();
GradleAirGapCreator gradleAirGapCreator = new GradleAirGapCreator(artifactResolver, detectExecutableResolver, gradleInspectorInstaller, simpleExecutableRunner, configuration);
NugetAirGapCreator nugetAirGapCreator = new NugetAirGapCreator(new NugetInspectorInstaller(artifactResolver));
DockerAirGapCreator dockerAirGapCreator = new DockerAirGapCreator(new DockerInspectorInstaller(artifactResolver));
AirGapCreator airGapCreator = new AirGapCreator(new AirGapPathFinder(), eventSystem, gradleAirGapCreator, nugetAirGapCreator, dockerAirGapCreator);
airGapCreator.createAirGapZip();
}
}