Skip to content

Commit

Permalink
STONEBLD-1518: Allow disabling plugins from YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck committed Nov 15, 2023
1 parent fb4b3f6 commit a3c37f8
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class BuildRecipeInfo {

List<AdditionalDownload> additionalDownloads = new ArrayList<>();

List<String> disabledPlugins = new ArrayList<>();

boolean runTests;

/**
Expand Down Expand Up @@ -174,6 +176,15 @@ public BuildRecipeInfo setAllowedDifferences(List<String> allowedDifferences) {
return this;
}

public List<String> getDisabledPlugins() {
return disabledPlugins;
}

public BuildRecipeInfo setDisabledPlugins(List<String> disabledPlugins) {
this.disabledPlugins = disabledPlugins;
return this;
}

@Override
public String toString() {
return "BuildRecipeInfo{" +
Expand All @@ -190,6 +201,7 @@ public String toString() {
", additionalDownloads=" + additionalDownloads +
", additionalBuilds=" + additionalBuilds +
", allowedDifferences=" + allowedDifferences +
", disabledPlugins=" + disabledPlugins +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public interface RecipeDirectory {

List<Path> getAllRepositoryPaths();

Optional<Path> getDisabledPluginInfo(String name);

default <T> void writeArtifactData(AddRecipeRequest<T> data) {
throw new IllegalStateException("Not implemented");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ public class RecipeLayoutManager implements RecipeDirectory {
private final Path buildInfoDirectory;
private final Path repositoryInfoDirectory;
private final Path buildToolInfoDirectory;
private final Path pluginInfoDirectory;

public RecipeLayoutManager(Path baseDirectory) {
this.scmInfoDirectory = baseDirectory.resolve(RecipeRepositoryManager.SCM_INFO);
this.buildInfoDirectory = baseDirectory.resolve(RecipeRepositoryManager.BUILD_INFO);
this.repositoryInfoDirectory = baseDirectory.resolve(RecipeRepositoryManager.REPOSITORY_INFO);
this.buildToolInfoDirectory = baseDirectory.resolve(RecipeRepositoryManager.BUILD_TOOL_INFO);
this.pluginInfoDirectory = baseDirectory.resolve(RecipeRepositoryManager.PLUGIN_INFO);
}

/**
Expand Down Expand Up @@ -115,6 +117,15 @@ public List<Path> getAllRepositoryPaths() {
}
}

@Override
public Optional<Path> getDisabledPluginInfo(String name) {
Path target = pluginInfoDirectory.resolve(name + ".yaml");
if (Files.exists(target)) {
return Optional.of(target);
}
return Optional.empty();
}

private Optional<Path> resolveVersion(Path target, String version) {
Path versions = target.resolve(VERSION);
if (!Files.exists(versions)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class RecipeRepositoryManager implements RecipeDirectory {
public static final String BUILD_INFO = "build-info";
public static final String BUILD_TOOL_INFO = "build-tool-info";
public static final String REPOSITORY_INFO = "repository-info";
public static final String PLUGIN_INFO = "plugin-info";
private final Git git;
private final String remote;
private final Path local;
Expand Down Expand Up @@ -121,6 +122,12 @@ public Optional<Path> getBuildToolInfo(String name) {
return recipeLayoutManager.getBuildToolInfo(name);
}

@Override
public Optional<Path> getDisabledPluginInfo(String name) {
doUpdate();
return recipeLayoutManager.getDisabledPluginInfo(name);
}

@Override
public void update() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class BuildInfo {

List<String> gavs = new ArrayList<>();

List<String> disabledPlugins = new ArrayList<>();

String digest;

String image;
Expand Down Expand Up @@ -159,6 +161,15 @@ public BuildInfo setImage(String image) {
return this;
}

public List<String> getDisabledPlugins() {
return disabledPlugins;
}

public BuildInfo setDisabledPlugins(List<String> disabledPlugins) {
this.disabledPlugins = disabledPlugins;
return this;
}

@Override
public String toString() {
return "BuildInfo{" +
Expand All @@ -173,6 +184,7 @@ public String toString() {
", additionalMemory=" + additionalMemory +
", allowedDifferences=" + allowedDifferences +
", image=" + image +
", disabledPlugins=" + disabledPlugins +
", digest=" + digest +
", gavs=" + gavs +
'}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public interface CacheBuildInfoLocator {
@Produces(MediaType.APPLICATION_JSON)
@Path("build-tool-info")
public List<BuildToolInfo> lookupBuildToolInfo(@QueryParam("name") String name);

//@GET
//@Produces(MediaType.APPLICATION_JSON)
//@Path("plugin-info")
//List<String> lookupPluginInfo(@QueryParam("name") String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public abstract class AbstractPreprocessor implements Runnable {
@CommandLine.Parameters(description = "The directory to process")
protected Path buildRoot;

@CommandLine.Option(names = "-dp", required = false, description = "The comma-separated list of plugins to disable", defaultValue = "")
protected String disabledPlugins;

@CommandLine.Option(names = "-r", required = false, description = "The repository URL")
protected String repositoryUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ private void setupInitScripts() throws IOException {
var init = initDir.resolve(initScript);
try (var in = getClass().getClassLoader().getResourceAsStream("gradle/" + initScript)) {
Files.copy(in, init);

if ("disable-plugins.gradle".equals(init.getFileName().toString())) {
Files.writeString(init, Files.readString(init).replace("@DISABLED_PLUGINS@", disabledPlugins));
}

Log.infof("Wrote init script to %s", init.toAbsolutePath());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.redhat.hacbs.container.build.preprocessor.maven;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static com.redhat.hacbs.recipies.location.RecipeRepositoryManager.PLUGIN_INFO;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.codehaus.plexus.util.xml.Xpp3Dom;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.redhat.hacbs.container.build.preprocessor.AbstractPreprocessor;
import com.redhat.hacbs.recipies.BuildRecipe;
import com.redhat.hacbs.recipies.build.BuildRecipeInfo;

import io.quarkus.logging.Log;
import picocli.CommandLine;
Expand All @@ -30,20 +37,15 @@
@CommandLine.Command(name = "maven-prepare")
public class MavenPrepareCommand extends AbstractPreprocessor {

static final Set<PluginInfo> TO_REMOVE = Set.of(
new PluginInfo("org.glassfish.copyright", "glassfish-copyright-maven-plugin"),
new PluginInfo("org.sonatype.plugins", "nexus-staging-maven-plugin"),
new PluginInfo("com.mycila", "license-maven-plugin"),
new PluginInfo("org.codehaus.mojo", "findbugs-maven-plugin"), //older version of this will break the build on our version of maven
new PluginInfo("de.jjohannes", "gradle-module-metadata-maven-plugin"));
static final ObjectMapper MAPPER = new YAMLMapper().configure(FAIL_ON_UNKNOWN_PROPERTIES, false);

@Override
public void run() {
try {
Files.walkFileTree(buildRoot, new SimpleFileVisitor<>() {

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
String fileName = file.getFileName().toString();
if (fileName.equals("pom.xml")) {
try {
Expand All @@ -62,7 +64,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO

}

private void handleBuild(Path file, boolean topLevel) throws IOException {
private void handleBuild(Path file, boolean topLevel) {
try (BufferedReader pomReader = Files.newBufferedReader(file)) {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(pomReader);
Expand All @@ -87,12 +89,26 @@ private void handleBuild(Path file, boolean topLevel) throws IOException {
}
}

private boolean handlePlugins(List<Plugin> plugins, boolean pluginManagement, boolean topLevel) {
private boolean handlePlugins(List<Plugin> plugins, boolean pluginManagement, boolean topLevel) throws IOException {
boolean modified = false;
BuildRecipeInfo info = BuildRecipe.BUILD.getHandler().parse(Path.of(PLUGIN_INFO + "/maven.yaml"));
List<String> disabledPlugins = info.getDisabledPlugins();
List<PluginInfo> toRemove = new ArrayList<>();

for (String s : disabledPlugins) {
String[] ga = s.split(":");

if (ga.length != 2) {
throw new IOException("Error parsing groupId/artifactId in " + s);
}

toRemove.add(new PluginInfo(ga[0], ga[1]));
}

for (Iterator<Plugin> iterator = plugins.iterator(); iterator.hasNext();) {
Plugin i = iterator.next();
PluginInfo p = new PluginInfo(i.getGroupId(), i.getArtifactId());
if (TO_REMOVE.contains(p)) {
if (toRemove.contains(p)) {
iterator.remove();
modified = true;
} else if (i.getArtifactId().equals("maven-deploy-plugin")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: DisablePluginsPlugin

class DisablePluginsPlugin implements Plugin<Gradle> {

def PLUGINS = ["kotlin.gradle.targets.js", "org.jetbrains.dokka"]
def PLUGINS = "@DISABLED_PLUGINS@".split(",")

void apply(Gradle gradle) {
gradle.allprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,17 @@ public Response lookupBuildToolInfo(@QueryParam("name") String name)
return Response.status(500).entity(e.getMessage()).build();
}
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("plugin-info")
public Response lookupDisabledPluginInfo(@QueryParam("name") String name)
throws IOException {
try {
return Response.ok(recipeManager.getDisabledPluginInfo(name)).build();
} catch (Exception e) {
Log.errorf(e, "Failed to lookup disabled plugin info for %s", name);
return Response.status(500).entity(e.getMessage()).build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,17 @@ public BuildRecipeInfo resolveBuildInfo(String scmUrl, String version) throws IO
}
return BuildRecipe.BUILD.getHandler().parse(path);
}

public List<String> getDisabledPluginInfo(String name) {
List<String> results = new ArrayList<>();

for (var i : recipeDirs) {
var path = i.getDisabledPluginInfo(name);

if (path.isPresent()) {
// TODO
}
}
return results;
}
}
1 change: 1 addition & 0 deletions pkg/apis/jvmbuildservice/v1alpha1/dependencybuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type BuildRecipe struct {
AdditionalMemory int `json:"additionalMemory,omitempty"`
Repositories []string `json:"repositories,omitempty"`
AllowedDifferences []string `json:"allowedDifferences,omitempty"`
DisabledPlugins string `json:"disabledPlugins,omitempty"`
}
type Contaminant struct {
GAV string `json:"gav,omitempty"`
Expand Down
14 changes: 13 additions & 1 deletion pkg/reconciler/dependencybuild/buildrecipeyaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func createPipelineSpec(tool string, commitTime int64, jbsConfig *v1alpha12.JBSC
"$(params.CACHE_URL)",
"$(workspaces." + WorkspaceSource + ".path)/workspace",
}
if recipe.DisabledPlugins != nil && recipe.DisabledPlugins != "" {

Check failure on line 75 in pkg/reconciler/dependencybuild/buildrecipeyaml.go

View workflow job for this annotation

GitHub Actions / Lint

invalid operation: recipe.DisabledPlugins != nil (mismatched types string and untyped nil)

Check failure on line 75 in pkg/reconciler/dependencybuild/buildrecipeyaml.go

View workflow job for this annotation

GitHub Actions / Lint

invalid operation: recipe.DisabledPlugins != nil (mismatched types string and untyped nil)

Check failure on line 75 in pkg/reconciler/dependencybuild/buildrecipeyaml.go

View workflow job for this annotation

GitHub Actions / Golang Unit tests

invalid operation: recipe.DisabledPlugins != nil (mismatched types string and untyped nil)
preprocessorArgs = append(preprocessorArgs, "-dp "+recipe.DisabledPlugins)
}

toolEnv := []v1.EnvVar{}
if recipe.ToolVersions["maven"] != "" {
toolEnv = append(toolEnv, v1.EnvVar{Name: "MAVEN_HOME", Value: "/opt/maven/" + recipe.ToolVersions["maven"]})
Expand All @@ -97,7 +101,15 @@ func createPipelineSpec(tool string, commitTime int64, jbsConfig *v1alpha12.JBSC
buildToolSection = mavenSettings + "\n" + mavenBuild
} else if tool == "gradle" {
buildToolSection = gradleBuild
preprocessorArgs[0] = "gradle-prepare"
preprocessorArgs = []string{
"gradle-prepare",
"-r",
"$(params.CACHE_URL)",
"$(workspaces." + WorkspaceSource + ".path)/workspace",
}
if recipe.DisabledPlugins != nil && recipe.DisabledPlugins != "" {

Check failure on line 110 in pkg/reconciler/dependencybuild/buildrecipeyaml.go

View workflow job for this annotation

GitHub Actions / Lint

invalid operation: recipe.DisabledPlugins != nil (mismatched types string and untyped nil)

Check failure on line 110 in pkg/reconciler/dependencybuild/buildrecipeyaml.go

View workflow job for this annotation

GitHub Actions / Lint

invalid operation: recipe.DisabledPlugins != nil (mismatched types string and untyped nil)

Check failure on line 110 in pkg/reconciler/dependencybuild/buildrecipeyaml.go

View workflow job for this annotation

GitHub Actions / Golang Unit tests

invalid operation: recipe.DisabledPlugins != nil (mismatched types string and untyped nil)
preprocessorArgs = append(preprocessorArgs, "-dp "+recipe.DisabledPlugins)
}
} else if tool == "sbt" {
buildToolSection = sbtBuild
preprocessorArgs[0] = "sbt-prepare"
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/dependencybuild/dependencybuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (r *ReconcileDependencyBuild) handleAnalyzeBuildPipelineRunReceived(ctx con
}
}
if imageOk {
buildRecipes = append(buildRecipes, &v1alpha1.BuildRecipe{Image: image.Image, CommandLine: command.Commands, EnforceVersion: unmarshalled.EnforceVersion, ToolVersion: command.ToolVersion[command.Tool], ToolVersions: command.ToolVersion, JavaVersion: command.ToolVersion["jdk"], Tool: command.Tool, PreBuildScript: unmarshalled.PreBuildScript, PostBuildScript: unmarshalled.PostBuildScript, AdditionalDownloads: unmarshalled.AdditionalDownloads, DisableSubmodules: unmarshalled.DisableSubmodules, AdditionalMemory: unmarshalled.AdditionalMemory, Repositories: unmarshalled.Repositories, AllowedDifferences: unmarshalled.AllowedDifferences})
buildRecipes = append(buildRecipes, &v1alpha1.BuildRecipe{Image: image.Image, CommandLine: command.Commands, EnforceVersion: unmarshalled.EnforceVersion, ToolVersion: command.ToolVersion[command.Tool], ToolVersions: command.ToolVersion, JavaVersion: command.ToolVersion["jdk"], Tool: command.Tool, PreBuildScript: unmarshalled.PreBuildScript, PostBuildScript: unmarshalled.PostBuildScript, AdditionalDownloads: unmarshalled.AdditionalDownloads, DisableSubmodules: unmarshalled.DisableSubmodules, AdditionalMemory: unmarshalled.AdditionalMemory, Repositories: unmarshalled.Repositories, AllowedDifferences: unmarshalled.AllowedDifferences, disabledPlugins: unmarshalled.disabledPlugins})
break
}
}
Expand Down

0 comments on commit a3c37f8

Please sign in to comment.