Skip to content

Commit

Permalink
Add support for cache plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Oct 13, 2018
1 parent 9ffe2b4 commit 32a3003
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/org/jvnet/hudson/update_center/HPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class HPI extends MavenArtifact {
* Which of the lineage did this come from?
*/
public final PluginHistory history;
private String pluginSite = "http://updates.jenkins-ci.org/download/plugins/";

private final Pattern developersPattern = Pattern.compile("([^:]*):([^:]*):([^,]*),?");

Expand All @@ -58,7 +59,7 @@ public HPI(MavenRepository repository, PluginHistory history, ArtifactInfo artif
* Download a plugin via more intuitive URL. This also helps us track download counts.
*/
public URL getURL() throws MalformedURLException {
return new URL("http://updates.jenkins-ci.org/download/plugins/"+artifact.artifactId+"/"+version+"/"+artifact.artifactId+".hpi");
return new URL(pluginSite+artifact.artifactId+"/"+version+"/"+artifact.artifactId+".hpi");
}

/**
Expand Down Expand Up @@ -145,6 +146,14 @@ public List<Developer> getDevelopers() throws IOException {
return r;
}

public String getPluginSite() {
return pluginSite;
}

public void setPluginSite(String pluginSite) {
this.pluginSite = pluginSite;
}

boolean isEqualsTo(String groupId, String artifactId, String version) {
return artifact.artifactId.equals(artifactId)
&& artifact.groupId.equals(groupId)
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/org/jvnet/hudson/update_center/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@
import org.kohsuke.args4j.Option;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
Expand All @@ -48,6 +52,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

/**
Expand Down Expand Up @@ -87,6 +92,15 @@ public class Main {
@Option(name="-download",usage="Build mirrors.jenkins-ci.org layout")
public File download = null;

@Option(name="-cache",usage="Cache target plugin")
public File cache = null;

@Option(name="-cacheAll",usage="Cache all version of plugin")
public boolean cacheAll;

@Option(name="-cacheServer",usage="Cache server will replace the orignal")
public String cacheServer;

/**
* This options builds update site. update-center.json(.html) that contains metadata,
* latest symlinks, and download/ directories that are referenced from metadata and
Expand Down Expand Up @@ -147,6 +161,9 @@ public class Main {
@Option(name="-skip-release-history",usage="Skip generation of release history")
public boolean skipReleaseHistory;

@Option(name="-whitelist",usage="White list for plugins")
public File whitelist = null;

private Signer signer = new Signer();

public static final String EOL = System.getProperty("line.separator");
Expand Down Expand Up @@ -302,9 +319,22 @@ protected JSONObject buildPlugins(MavenRepository repository, LatestLinkBuilder

int validCount = 0;

Properties whitelistPro = new Properties();
if(whitelist != null && whitelist.isFile()) {
try(InputStream input = new FileInputStream(whitelist)) {
whitelistPro.load(input);
}
}

JSONObject plugins = new JSONObject();
System.out.println("Gathering list of plugins and versions from the maven repo...");
for (PluginHistory hpi : repository.listHudsonPlugins()) {
if(whitelistPro.size() > 0) {
if(whitelistPro.get(hpi.artifactId) == null) {
continue;
}
}

try {
System.out.println(hpi.artifactId);

Expand All @@ -316,8 +346,26 @@ protected JSONObject buildPlugins(MavenRepository repository, LatestLinkBuilder
}
pluginToDocumentationUrl.put(plugin.artifactId, plugin.getPluginUrl());

if (cache!=null) {
if(cacheAll) {
for (HPI v : hpi.artifacts.values()) {
cachePlugin(v, new File(cache, "plugins/" + hpi.artifactId + "/" + v.version + "/" + hpi.artifactId + ".hpi"));
}
}

HPI latestHpi = plugin.latest;
cachePlugin(latestHpi, new File(cache, "plugins/" + hpi.artifactId + "/" + latestHpi.version + "/" + hpi.artifactId + ".hpi"));
}

if (cacheServer!=null) {
for (HPI v : hpi.artifacts.values()) {
v.setPluginSite(cacheServer);
}
}

JSONObject json = plugin.toJSON();
System.out.println("=> " + json);

plugins.put(plugin.artifactId, json);
latest.add(plugin.artifactId+".hpi", plugin.latest.getURL().getPath());

Expand Down Expand Up @@ -347,6 +395,35 @@ protected JSONObject buildPlugins(MavenRepository repository, LatestLinkBuilder
return plugins;
}

/**
* Cache target plugin file into local
* @param v plugin
* @param file target location
*/
private void cachePlugin(HPI v, File file) {
if(file.exists()) {
System.out.println("Plugin file " + file.getName() + " already exists, skip download.");
return;
}

File parentDir = file.getParentFile();
if(!parentDir.exists() && !parentDir.mkdirs()) {
System.err.println("Can't create directory: " + parentDir.getAbsolutePath());
return;
}

try(InputStream input = v.getURL().openStream();
OutputStream output = new FileOutputStream(file)) {
System.out.println("Prepare download file: " + file.getName());

IOUtils.copy(input, output);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* Generates symlink to the latest version.
*/
Expand Down

0 comments on commit 32a3003

Please sign in to comment.