From 98a92b615079372a5a47eff101eec7c9ca5ecbd8 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Fri, 17 Jul 2020 13:37:29 +0200 Subject: [PATCH] Initial Plugin Installation Manager Bundling --- bootstrap/pom.xml | 4 ++ .../runner/bootstrap/Bootstrap.java | 39 +++++++------------ .../jenkinsfile/runner/bootstrap/Util.java | 28 +++++++++++++ packaging/docker/unix/debian-jdk8/Dockerfile | 4 +- pom.xml | 8 ++++ 5 files changed, 57 insertions(+), 26 deletions(-) diff --git a/bootstrap/pom.xml b/bootstrap/pom.xml index e2cd51da..87aa0c53 100644 --- a/bootstrap/pom.xml +++ b/bootstrap/pom.xml @@ -22,5 +22,9 @@ org.jenkins-ci version-number + + io.jenkins.plugin-management + plugin-management-library + diff --git a/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/Bootstrap.java b/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/Bootstrap.java index aaf94255..e4b87532 100644 --- a/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/Bootstrap.java +++ b/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/Bootstrap.java @@ -1,6 +1,10 @@ package io.jenkins.jenkinsfile.runner.bootstrap; import hudson.util.VersionNumber; +import io.jenkins.tools.pluginmanager.impl.Plugin; +import io.jenkins.tools.pluginmanager.impl.PluginManager; +import io.jenkins.tools.pluginmanager.util.PluginListParser; + import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.kohsuke.args4j.CmdLineException; @@ -20,6 +24,7 @@ import java.nio.file.Files; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Properties; @@ -189,18 +194,18 @@ public void postConstruct(CmdLineParser parser) throws IOException { File plugins_txt = this.pluginsDir; // This is a plugin list file this.pluginsDir = Files.createTempDirectory("plugins").toFile(); - for (String line : FileUtils.readLines(plugins_txt, UTF_8)) { - String shortname = line; - String version = "latest"; - - int i = line.indexOf(':'); - if (i != -1) { - shortname = line.substring(0,i); - version = line.substring(i+1); - } - installPlugin(shortname, version); + List plugins = new PluginListParser().parsePluginTxtFile(plugins_txt); + if (plugins == null || plugins.isEmpty()) { + // TODO: proper error propagation? + System.err.println("Failed to read plugins from " + plugins_txt); + System.exit(-1); } + + PluginManager pluginManager = Util.initPluginManager( + pluginsDir, + configBuilder -> configBuilder.withPlugins(plugins)); + pluginManager.installedPlugins(); } if (this.runWorkspace != null){ @@ -279,20 +284,6 @@ private File getJenkinsWar() throws IOException { return war; } - private void installPlugin(String shortname, String version) throws IOException { - - final File install = new File(pluginsDir, shortname + ".jpi"); - File plugin = new File(cache, String.format("plugins/%s/%s-%s.hpi", shortname, shortname, version)); - if (!plugin.exists() || ("latest".equals(version) && plugin.lastModified() < CACHE_EXPIRE) ) { - plugin.getParentFile().mkdirs(); - final URL url = new URL(getMirrorURL(String.format("http://updates.jenkins.io/download/plugins/%s/%s/%s.hpi", shortname, version, shortname))); - System.out.printf("Downloading jenkins plugin %s (%s)...\n", shortname, version); - FileUtils.copyURLToFile(url, plugin); - } - - Files.createSymbolicLink(install.toPath(), plugin.toPath()); - } - private String getMirrorURL(String url) { if (this.mirror == null || "".equals(this.mirror.trim())) { return url; diff --git a/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/Util.java b/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/Util.java index 8fffca44..41171ec7 100644 --- a/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/Util.java +++ b/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/Util.java @@ -11,6 +11,10 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; +import edu.umd.cs.findbugs.annotations.NonNull; +import io.jenkins.tools.pluginmanager.config.Config; +import io.jenkins.tools.pluginmanager.impl.PluginManager; + public class Util { @@ -46,4 +50,28 @@ public static File explodeWar(String jarPath) throws IOException { } return destDir; } + + public interface PluginManagerConfigurator { + void configure(Config.Builder configBuilder); + } + + @NonNull + public static PluginManager initPluginManager(File pluginsDir, PluginManagerConfigurator configurator) { + Config.Builder configBuilder = Config.builder() + //.withJenkinsWar(jenkinsWar.getAbsolutePath()) + .withPluginDir(pluginsDir) + .withShowAvailableUpdates(true) + .withIsVerbose(true) + .withDoDownload(false); + configurator.configure(configBuilder); + Config config = configBuilder.build(); + + PluginManager pluginManager = new PluginManager(config); + //pluginManager.setCm(new CacheManager(cacheDir.toPath(), true)); + //pluginManager.setJenkinsVersion(new VersionNumber("2.222.1")); + //pluginManager.setLatestUcJson(latestUcJson); + //pluginManager.setLatestUcPlugins(latestUcJson.getJSONObject("plugins")); + //pluginManager.setPluginInfoJson(pluginManager.getJson(pluginVersionsFile.toURI().toURL(), "plugin-versions")); + return pluginManager; + } } diff --git a/packaging/docker/unix/debian-jdk8/Dockerfile b/packaging/docker/unix/debian-jdk8/Dockerfile index 6b6fc429..3feea455 100644 --- a/packaging/docker/unix/debian-jdk8/Dockerfile +++ b/packaging/docker/unix/debian-jdk8/Dockerfile @@ -11,8 +11,8 @@ RUN mkdir /app && unzip /jenkinsfile-runner/vanilla-package/target/war/jenkins.w FROM openjdk:8-jdk ENV JENKINS_UC https://updates.jenkins.io ENV CASC_JENKINS_CONFIG /usr/share/jenkins/ref/casc -ENV JENKINS_PM_VERSION 0.1-alpha-10 -ENV JENKINS_PM_URL https://github.com/jenkinsci/plugin-installation-manager-tool/releases/download/plugin-management-parent-pom-$JENKINS_PM_VERSION/jenkins-plugin-manager-$JENKINS_PM_VERSION.jar +ENV JENKINS_PM_VERSION 1.1.2 +ENV JENKINS_PM_URL https://repo.jenkins-ci.org/releases/io/jenkins/plugin-management/plugin-management-cli/${PLUGIN_MANAGER_TOOL_VERSION}/plugin-management-cli-${JENKINS_PM_VERSION}.jar -O tmp/jenkins-plugin-manager-${JENKINS_PM_VERSION}.jar USER root RUN mkdir -p /app /usr/share/jenkins/ref/plugins /usr/share/jenkins/ref/casc /app/bin \ && echo "jenkins: {}" >/usr/share/jenkins/ref/casc/jenkins.yaml \ diff --git a/pom.xml b/pom.xml index 9c64581b..5260e439 100644 --- a/pom.xml +++ b/pom.xml @@ -64,6 +64,8 @@ THE SOFTWARE. 9.4.30.v20200611 false + + 1.1.2 @@ -89,6 +91,12 @@ THE SOFTWARE. support-log-formatter 1.0 + + io.jenkins.plugin-management + plugin-management-library + 1.1.2 + + commons-collections