Search in sources :

Example 1 with Executable

use of com.synopsys.integration.executable.Executable in project synopsys-detect by blackducksoftware.

the class DockerExtractor method loadDockerImage.

private void loadDockerImage(File directory, Map<String, String> environmentVariables, ExecutableTarget dockerExe, File imageToImport) throws IOException, ExecutableRunnerException, IntegrationException {
    List<String> dockerImportArguments = Arrays.asList("load", "-i", imageToImport.getCanonicalPath());
    Executable dockerImportImageExecutable = ExecutableUtils.createFromTarget(directory, environmentVariables, dockerExe, dockerImportArguments);
    ExecutableOutput exeOut = executableRunner.execute(dockerImportImageExecutable);
    if (exeOut.getReturnCode() != 0) {
        throw new IntegrationException(String.format("Command %s %s returned %d: %s", dockerExe.toCommand(), dockerImportArguments, exeOut.getReturnCode(), exeOut.getErrorOutput()));
    }
}
Also used : ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) IntegrationException(com.synopsys.integration.exception.IntegrationException) Executable(com.synopsys.integration.executable.Executable)

Example 2 with Executable

use of com.synopsys.integration.executable.Executable in project synopsys-detect by blackducksoftware.

the class DockerExtractor method executeDocker.

private Extraction executeDocker(File outputDirectory, ImageIdentifierType imageIdentifierType, String imageArgument, String suppliedImagePiece, String dockerTarFilePath, File directory, ExecutableTarget javaExe, ExecutableTarget dockerExe, DockerInspectorInfo dockerInspectorInfo, DockerProperties dockerProperties) throws IOException, ExecutableRunnerException {
    File dockerPropertiesFile = new File(outputDirectory, "application.properties");
    dockerProperties.populatePropertiesFile(dockerPropertiesFile, outputDirectory);
    Map<String, String> environmentVariables = new HashMap<>(0);
    List<String> dockerArguments = new ArrayList<>();
    dockerArguments.add("-jar");
    dockerArguments.add(dockerInspectorInfo.getDockerInspectorJar().getAbsolutePath());
    dockerArguments.add("--spring.config.location=file:" + dockerPropertiesFile.getCanonicalPath());
    dockerArguments.add(imageArgument);
    if (dockerInspectorInfo.hasAirGapImageFiles()) {
        importTars(dockerInspectorInfo.getAirGapInspectorImageTarFiles(), outputDirectory, environmentVariables, dockerExe);
    }
    Executable dockerExecutable = ExecutableUtils.createFromTarget(outputDirectory, environmentVariables, javaExe, dockerArguments);
    executableRunner.execute(dockerExecutable);
    Optional<DockerInspectorResults> dockerResults = Optional.empty();
    File producedResultFile = fileFinder.findFile(outputDirectory, RESULTS_FILENAME_PATTERN);
    if (producedResultFile != null) {
        String resultsFileContents = FileUtils.readFileToString(producedResultFile, StandardCharsets.UTF_8);
        dockerResults = dockerInspectorResultsFileParser.parse(resultsFileContents);
    }
    File producedSquashedImageFile = fileFinder.findFile(outputDirectory, SQUASHED_IMAGE_FILENAME_PATTERN);
    if (producedSquashedImageFile != null) {
        logger.debug("Returning squashed image: {}", producedSquashedImageFile.getAbsolutePath());
    }
    File producedContainerFileSystemFile = fileFinder.findFile(outputDirectory, CONTAINER_FILESYSTEM_FILENAME_PATTERN);
    if (producedContainerFileSystemFile != null) {
        logger.debug("Returning container filesystem: {}", producedContainerFileSystemFile.getAbsolutePath());
    }
    Extraction.Builder extractionBuilder = findCodeLocations(outputDirectory, directory, dockerResults.map(DockerInspectorResults::getMessage).orElse(null));
    String imageIdentifier = imageIdentifierGenerator.generate(imageIdentifierType, suppliedImagePiece, dockerResults.orElse(null));
    // The value of DOCKER_IMAGE_NAME_META_DATA is built into the codelocation name, so changing how its value is derived is likely to
    // change how codelocation names are generated. Currently either an image repo, repo:tag, or tarfile path gets written there.
    // It's tempting to always store the image repo:tag in that field, but that would change code location naming with consequences for users.
    extractionBuilder.metaData(SQUASHED_IMAGE_META_DATA, producedSquashedImageFile).metaData(CONTAINER_FILESYSTEM_META_DATA, producedContainerFileSystemFile).metaData(DOCKER_IMAGE_NAME_META_DATA, imageIdentifier);
    if (StringUtils.isNotBlank(dockerTarFilePath)) {
        File givenDockerTarfile = new File(dockerTarFilePath);
        logger.debug("Returning given docker tarfile: {}", givenDockerTarfile.getAbsolutePath());
        extractionBuilder.metaData(DOCKER_TAR_META_DATA, givenDockerTarfile);
    }
    return extractionBuilder.build();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) File(java.io.File) DockerInspectorResults(com.synopsys.integration.detectable.detectables.docker.model.DockerInspectorResults)

Example 3 with Executable

use of com.synopsys.integration.executable.Executable in project synopsys-detect by blackducksoftware.

the class NugetInspectorExtractor method executeTarget.

private NugetTargetResult executeTarget(ExecutableTarget inspector, File targetFile, File outputDirectory, NugetInspectorOptions nugetInspectorOptions) throws ExecutableRunnerException, IOException, DetectableException {
    if (!outputDirectory.exists() && !outputDirectory.mkdirs()) {
        throw new DetectableException(String.format("Executing the nuget inspector failed, could not create output directory: %s", outputDirectory));
    }
    List<String> arguments = NugetInspectorArguments.fromInspectorOptions(nugetInspectorOptions, targetFile, outputDirectory);
    Executable executable = ExecutableUtils.createFromTarget(outputDirectory, inspector, arguments);
    ExecutableOutput executableOutput = executableRunner.execute(executable);
    if (executableOutput.getReturnCode() != 0) {
        throw new DetectableException(String.format("Executing the nuget inspector failed: %s", executableOutput.getReturnCode()));
    }
    List<File> dependencyNodeFiles = fileFinder.findFiles(outputDirectory, INSPECTOR_OUTPUT_PATTERN);
    List<NugetParseResult> parseResults = new ArrayList<>();
    if (dependencyNodeFiles != null) {
        for (File dependencyNodeFile : dependencyNodeFiles) {
            String text = FileUtils.readFileToString(dependencyNodeFile, StandardCharsets.UTF_8);
            NugetParseResult result = nugetInspectorParser.createCodeLocation(text);
            parseResults.add(result);
        }
    }
    NugetTargetResult targetResult = new NugetTargetResult();
    targetResult.codeLocations = parseResults.stream().flatMap(it -> it.getCodeLocations().stream()).collect(Collectors.toList());
    targetResult.nameVersion = parseResults.stream().filter(it -> StringUtils.isNotBlank(it.getProjectName())).map(it -> new NameVersion(it.getProjectName(), it.getProjectVersion())).findFirst().orElse(null);
    return targetResult;
}
Also used : Extraction(com.synopsys.integration.detectable.extraction.Extraction) NugetParseResult(com.synopsys.integration.detectable.detectables.nuget.parse.NugetParseResult) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) FileFinder(com.synopsys.integration.common.util.finder.FileFinder) ArrayList(java.util.ArrayList) NameVersion(com.synopsys.integration.util.NameVersion) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Map(java.util.Map) BasicDependencyGraph(com.synopsys.integration.bdio.graph.BasicDependencyGraph) Logger(org.slf4j.Logger) NugetInspectorParser(com.synopsys.integration.detectable.detectables.nuget.parse.NugetInspectorParser) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Executable(com.synopsys.integration.executable.Executable) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) Optional(java.util.Optional) ExecutableUtils(com.synopsys.integration.detectable.ExecutableUtils) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) NameVersion(com.synopsys.integration.util.NameVersion) ArrayList(java.util.ArrayList) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) NugetParseResult(com.synopsys.integration.detectable.detectables.nuget.parse.NugetParseResult) Executable(com.synopsys.integration.executable.Executable) File(java.io.File) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException)

Example 4 with Executable

use of com.synopsys.integration.executable.Executable in project synopsys-detect by blackducksoftware.

the class DetectableFunctionalTest method addExecutableOutput.

public void addExecutableOutput(@NotNull Path workingDirectory, @NotNull ExecutableOutput executableOutput, @NotNull Map<String, String> environment, @NotNull String... command) {
    List<String> commandList = Arrays.asList(command);
    Executable executable = new Executable(workingDirectory.toFile(), environment, commandList);
    executableRunner.addExecutableOutput(executable, executableOutput);
}
Also used : Executable(com.synopsys.integration.executable.Executable)

Example 5 with Executable

use of com.synopsys.integration.executable.Executable in project synopsys-detect by blackducksoftware.

the class FunctionalDetectableExecutableRunner method execute.

@NotNull
@Override
public ExecutableOutput execute(@NotNull File workingDirectory, @NotNull File exeFile, @NotNull List<String> args) {
    List<String> command = new ArrayList<>();
    command.add(exeFile.getPath());
    command.addAll(args);
    return execute(new Executable(workingDirectory, new HashMap<>(), command));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Executable(com.synopsys.integration.executable.Executable) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Executable (com.synopsys.integration.executable.Executable)18 Extraction (com.synopsys.integration.detectable.extraction.Extraction)11 File (java.io.File)9 ArrayList (java.util.ArrayList)8 DetectableExecutableRunner (com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner)7 HashMap (java.util.HashMap)7 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)6 Test (org.junit.jupiter.api.Test)6 DisabledOnOs (org.junit.jupiter.api.condition.DisabledOnOs)4 ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)3 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)3 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)3 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)3 ProcessBuilderRunner (com.synopsys.integration.executable.ProcessBuilderRunner)3 GoModCliExtractor (com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor)2 Slf4jIntLogger (com.synopsys.integration.log.Slf4jIntLogger)2 IOException (java.io.IOException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 GraphParser (com.paypal.digraph.parser.GraphParser)1