From 3bce7abee41893ea58f4b97df454bb0afa0e0426 Mon Sep 17 00:00:00 2001 From: Nick Cross Date: Thu, 12 Oct 2023 12:05:12 +0100 Subject: [PATCH] Remove unused code --- .../analyser/deploy/DeployCommand.java | 262 +++++++++--------- 1 file changed, 127 insertions(+), 135 deletions(-) diff --git a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/DeployCommand.java b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/DeployCommand.java index 2302afc7c4..1cd5afe10b 100644 --- a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/DeployCommand.java +++ b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/DeployCommand.java @@ -103,154 +103,146 @@ public DeployCommand(BeanManager beanManager, public void run() { try { - java.nio.file.Path modified = Files.createTempFile("deployment", ".tar.gz"); - try { - - Set gavs = new HashSet<>(); - - Map> contaminatedPaths = new HashMap<>(); - Map> contaminatedGavs = new HashMap<>(); - // Represents directories that should not be deployed i.e. if a single artifact (barring test jars) is - // contaminated then none of the artifacts will be deployed. - Set toRemove = new HashSet<>(); - Map jarFiles = new HashMap<>(); - Files.walkFileTree(deploymentPath, new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - String name = deploymentPath.relativize(file).toString(); - Optional gav = getGav(name); - gav.ifPresent( - gav1 -> gavs.add(gav1.getGroupId() + ":" + gav1.getArtifactId() + ":" + gav1.getVersion())); - Log.debugf("Checking %s for contaminants with GAV %s", name, gav); - //we check every file as we also want to catch .tar.gz etc - var info = ClassFileTracker.readTrackingDataFromFile(Files.newInputStream(file), name); - for (var i : info) { - if (!allowedSources.contains(i.source)) { - Log.errorf("%s was contaminated by %s from %s", name, i.gav, i.source); - if (ALLOWED_CONTAMINANTS.stream().noneMatch(a -> file.getFileName().toString().endsWith(a))) { - gav.ifPresent(g -> contaminatedGavs.computeIfAbsent(i.gav, s -> new HashSet<>()) - .add(g.getGroupId() + ":" + g.getArtifactId() + ":" + g.getVersion())); - int index = name.lastIndexOf("/"); - if (index != -1) { - contaminatedPaths.computeIfAbsent(name.substring(0, index), - s -> new HashSet<>()).add(i.gav); - } else { - contaminatedPaths.computeIfAbsent("", s -> new HashSet<>()).add(i.gav); - } - toRemove.add(file.getParent()); + Set gavs = new HashSet<>(); + + Map> contaminatedPaths = new HashMap<>(); + Map> contaminatedGavs = new HashMap<>(); + // Represents directories that should not be deployed i.e. if a single artifact (barring test jars) is + // contaminated then none of the artifacts will be deployed. + Set toRemove = new HashSet<>(); + Map jarFiles = new HashMap<>(); + Files.walkFileTree(deploymentPath, new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + String name = deploymentPath.relativize(file).toString(); + Optional gav = getGav(name); + gav.ifPresent( + gav1 -> gavs.add(gav1.getGroupId() + ":" + gav1.getArtifactId() + ":" + gav1.getVersion())); + Log.debugf("Checking %s for contaminants with GAV %s", name, gav); + //we check every file as we also want to catch .tar.gz etc + var info = ClassFileTracker.readTrackingDataFromFile(Files.newInputStream(file), name); + for (var i : info) { + if (!allowedSources.contains(i.source)) { + Log.errorf("%s was contaminated by %s from %s", name, i.gav, i.source); + if (ALLOWED_CONTAMINANTS.stream().noneMatch(a -> file.getFileName().toString().endsWith(a))) { + gav.ifPresent(g -> contaminatedGavs.computeIfAbsent(i.gav, s -> new HashSet<>()) + .add(g.getGroupId() + ":" + g.getArtifactId() + ":" + g.getVersion())); + int index = name.lastIndexOf("/"); + if (index != -1) { + contaminatedPaths.computeIfAbsent(name.substring(0, index), + s -> new HashSet<>()).add(i.gav); } else { - Log.debugf("Ignoring contaminant for %s", file.getFileName()); + contaminatedPaths.computeIfAbsent("", s -> new HashSet<>()).add(i.gav); } + toRemove.add(file.getParent()); + } else { + Log.debugf("Ignoring contaminant for %s", file.getFileName()); } } - if (gav.isPresent()) { - //now add our own tracking data - if (name.endsWith(".jar") && !name.endsWith("-sources.jar") && !name.endsWith("-javadoc.jar")) { - jarFiles.put(file, gav.get()); - } - } - return FileVisitResult.CONTINUE; } - }); - for (var e : jarFiles.entrySet()) { - Path file = e.getKey(); - Gav gav = e.getValue(); - try { - String fileName = file.getFileName().toString(); - Path temp = file.getParent().resolve(fileName + ".temp"); - ClassFileTracker.addTrackingDataToJar(Files.newInputStream(file), - new TrackingData( - gav.getGroupId() + ":" + gav.getArtifactId() + ":" - + gav.getVersion(), - "rebuilt", - Map.of("scm-uri", scmUri, "scm-commit", commit, "hermetic", - Boolean.toString(hermetic))), - Files.newOutputStream(temp), false); - Files.delete(file); - Files.move(temp, file); - try (Stream pathStream = Files.list(file.getParent())) { - pathStream.filter(s -> s.getFileName().toString().startsWith(fileName + ".")) - .forEach(f -> { - try { - Files.delete(f); - } catch (IOException ex) { - throw new RuntimeException(ex); - } - }); + if (gav.isPresent()) { + //now add our own tracking data + if (name.endsWith(".jar") && !name.endsWith("-sources.jar") && !name.endsWith("-javadoc.jar")) { + jarFiles.put(file, gav.get()); } - - Files.writeString(file.getParent().resolve(fileName + ".md5"), - HashUtil.md5(Files.newInputStream(file))); - Files.writeString(file.getParent().resolve(fileName + ".sha1"), - HashUtil.sha1(Files.newInputStream(file))); - } catch (Exception ex) { - Log.errorf(ex, "Failed to instrument %s", file); } + return FileVisitResult.CONTINUE; } - for (var i : toRemove) { - Log.errorf("Removing %s as it is contaminated", i); - FileUtil.deleteRecursive(i); - } - - //update the DB with contaminant information - Log.infof("Contaminants: %s", contaminatedPaths); - Log.infof("Contaminated GAVS: %s", contaminatedGavs); - Log.infof("GAVs to deploy: %s", gavs); - if (gavs.isEmpty()) { - Log.errorf("No content to deploy found in deploy directory"); - - Files.walkFileTree(deploymentPath, new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Log.errorf("Contents: %s", file); - return FileVisitResult.CONTINUE; - } - }); - throw new RuntimeException("deploy failed"); - } - //we still deploy, but without the contaminates - java.nio.file.Path deployFile = deploymentPath; - // This means the build failed to produce any deployable output. - // If everything is contaminated we still need the task to succeed so we can resolve the contamination. - for (var i : contaminatedGavs.entrySet()) { - gavs.removeAll(i.getValue()); - } - generateBuildSbom(); - - if (!gavs.isEmpty()) { - try { - cleanBrokenSymlinks(sourcePath); - doDeployment(deployFile, sourcePath, logsPath, gavs); - } catch (Throwable t) { - Log.error("Deployment failed", t); - flushLogs(); - throw t; + }); + for (var e : jarFiles.entrySet()) { + Path file = e.getKey(); + Gav gav = e.getValue(); + try { + String fileName = file.getFileName().toString(); + Path temp = file.getParent().resolve(fileName + ".temp"); + ClassFileTracker.addTrackingDataToJar(Files.newInputStream(file), + new TrackingData( + gav.getGroupId() + ":" + gav.getArtifactId() + ":" + + gav.getVersion(), + "rebuilt", + Map.of("scm-uri", scmUri, "scm-commit", commit, "hermetic", + Boolean.toString(hermetic))), + Files.newOutputStream(temp), false); + Files.delete(file); + Files.move(temp, file); + try (Stream pathStream = Files.list(file.getParent())) { + pathStream.filter(s -> s.getFileName().toString().startsWith(fileName + ".")) + .forEach(f -> { + try { + Files.delete(f); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + }); } - } else { - Log.errorf("Skipped deploying from task run %s as all artifacts were contaminated", taskRun); + + Files.writeString(file.getParent().resolve(fileName + ".md5"), + HashUtil.md5(Files.newInputStream(file))); + Files.writeString(file.getParent().resolve(fileName + ".sha1"), + HashUtil.sha1(Files.newInputStream(file))); + } catch (Exception ex) { + Log.errorf(ex, "Failed to instrument %s", file); } - if (taskRun != null) { - - List newContaminates = new ArrayList<>(); - for (var i : contaminatedGavs.entrySet()) { - Contaminates contaminates = new Contaminates(); - contaminates.setContaminatedArtifacts(new ArrayList<>(i.getValue())); - contaminates.setGav(i.getKey()); - newContaminates.add(contaminates); + } + for (var i : toRemove) { + Log.errorf("Removing %s as it is contaminated", i); + FileUtil.deleteRecursive(i); + } + + //update the DB with contaminant information + Log.infof("Contaminants: %s", contaminatedPaths); + Log.infof("Contaminated GAVS: %s", contaminatedGavs); + Log.infof("GAVs to deploy: %s", gavs); + if (gavs.isEmpty()) { + Log.errorf("No content to deploy found in deploy directory"); + + Files.walkFileTree(deploymentPath, new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Log.errorf("Contents: %s", file); + return FileVisitResult.CONTINUE; } - String serialisedContaminants = ResultsUpdater.MAPPER.writeValueAsString(newContaminates); - Log.infof("Updating results %s with contaminants %s and deployed resources %s", - taskRun, serialisedContaminants, gavs); - resultsUpdater.updateResults(taskRun, Map.of( - "CONTAMINANTS", serialisedContaminants, - "DEPLOYED_RESOURCES", String.join(",", gavs), - "IMAGE_URL", imageName == null ? "" : imageName, - "IMAGE_DIGEST", imageDigest == null ? "" : "sha256:" + imageDigest)); + }); + throw new RuntimeException("deploy failed"); + } + //we still deploy, but without the contaminates + java.nio.file.Path deployFile = deploymentPath; + // This means the build failed to produce any deployable output. + // If everything is contaminated we still need the task to succeed so we can resolve the contamination. + for (var i : contaminatedGavs.entrySet()) { + gavs.removeAll(i.getValue()); + } + generateBuildSbom(); + + if (!gavs.isEmpty()) { + try { + cleanBrokenSymlinks(sourcePath); + doDeployment(deployFile, sourcePath, logsPath, gavs); + } catch (Throwable t) { + Log.error("Deployment failed", t); + flushLogs(); + throw t; } - } finally { - if (Files.exists(modified)) { - Files.delete(modified); + } else { + Log.errorf("Skipped deploying from task run %s as all artifacts were contaminated", taskRun); + } + if (taskRun != null) { + + List newContaminates = new ArrayList<>(); + for (var i : contaminatedGavs.entrySet()) { + Contaminates contaminates = new Contaminates(); + contaminates.setContaminatedArtifacts(new ArrayList<>(i.getValue())); + contaminates.setGav(i.getKey()); + newContaminates.add(contaminates); } + String serialisedContaminants = ResultsUpdater.MAPPER.writeValueAsString(newContaminates); + Log.infof("Updating results %s with contaminants %s and deployed resources %s", + taskRun, serialisedContaminants, gavs); + resultsUpdater.updateResults(taskRun, Map.of( + "CONTAMINANTS", serialisedContaminants, + "DEPLOYED_RESOURCES", String.join(",", gavs), + "IMAGE_URL", imageName == null ? "" : imageName, + "IMAGE_DIGEST", imageDigest == null ? "" : "sha256:" + imageDigest)); } } catch (Exception e) { Log.error("Deployment failed", e);