Added automatic tool installation for Ant.

- Most of the installer logic is made reusable in DownloadFromUrlInstaller.
- ToolDescriptor can now contribute global.jelly to the system configuration page.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@18260 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2009-05-21 18:22:02 +00:00
1 parent 46b540e5eb
commit 49ace47432
20 files changed
+424 -91

No files matched your search

@@ -5,6 +5,7 @@ import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.util.QuotedStringTokenizer;
import hudson.util.TextFile;
import hudson.util.TimeUnit2;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.Stapler;
@@ -39,7 +40,7 @@ public class DownloadService extends PageDecorator {
long now = System.currentTimeMillis();
for (Downloadable d : Downloadable.all()) {
if(d.getDue()<now) {
buf.append("<script>downloadService.download(")
buf.append("<script>downloadService.download(")
.append(QuotedStringTokenizer.quote(d.getId()))
.append(',')
.append(QuotedStringTokenizer.quote(d.getUrl()))
@@ -75,7 +76,7 @@ public class DownloadService extends PageDecorator {
*
* @since 1.305
*/
public static abstract class Downloadable implements ExtensionPoint {
public static class Downloadable implements ExtensionPoint {
private final String id;
private final String url;
private final long interval;
@@ -91,12 +92,27 @@ public class DownloadService extends PageDecorator {
* For security and privacy reasons, we don't allow the retrieval
* from random locations.
*/
protected Downloadable(String id, String url, long interval) {
public Downloadable(String id, String url, long interval) {
this.id = id;
this.url = url;
this.interval = interval;
}
/**
* Uses the class name as an ID.
*/
public Downloadable(Class id) {
this(id.getName().replace('$','.'));
}
public Downloadable(String id) {
this(id,id+".json");
}
public Downloadable(String id, String url) {
this(id,url,TimeUnit2.DAYS.toMillis(1));
}
public String getId() {
return id;
}
@@ -105,7 +121,7 @@ public class DownloadService extends PageDecorator {
* URL to download.
*/
public String getUrl() {
return Hudson.getInstance().getUpdateCenter().getUrl()+url;
return Hudson.getInstance().getUpdateCenter().getUrl()+"updates/"+url;
}
/**
@@ -167,6 +183,17 @@ public class DownloadService extends PageDecorator {
return Hudson.getInstance().getExtensionList(Downloadable.class);
}
/**
* Returns the {@link Downloadable} that has the given ID.
*/
public static Downloadable get(String id) {
for (Downloadable d : all()) {
if(d.id.equals(id))
return d;
}
return null;
}
private static final Logger LOGGER = Logger.getLogger(Downloadable.class.getName());
}
}
@@ -45,6 +45,8 @@ import hudson.ExtensionPoint;
import hudson.DescriptorExtensionList;
import hudson.ExtensionListView;
import hudson.Extension;
import hudson.tools.ToolInstallation;
import hudson.tools.ToolDescriptor;
import hudson.cli.CliEntryPoint;
import hudson.cli.CLICommand;
import hudson.cli.HelpCommand;
@@ -2209,6 +2211,9 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
for( PageDecorator d : PageDecorator.all() )
result &= configureDescriptor(req,json,d);
for( ToolDescriptor d : ToolInstallation.all() )
result &= configureDescriptor(req,json,d);
for( JSONObject o : StructuredForm.toList(json,"plugin"))
pluginManager.getPlugin(o.getString("name")).getPlugin().configure(req, o);
@@ -229,5 +229,5 @@ public class UsageStatistics extends PageDecorator {
private static final long DAY = DAYS.toMillis(1);
public static boolean DISABLED = Boolean.getBoolean(UsageStatistics.class.getName()+".disable");
public static boolean DISABLED = Boolean.getBoolean(UsageStatistics.class.getName()+".disabled");
}
+89 -43
View File
@@ -41,6 +41,9 @@ import hudson.remoting.Callable;
import hudson.slaves.NodeSpecific;
import hudson.tools.ToolDescriptor;
import hudson.tools.ToolInstallation;
import hudson.tools.DownloadFromUrlInstaller;
import hudson.tools.ToolInstaller;
import hudson.tools.ToolProperty;
import hudson.util.ArgumentListBuilder;
import hudson.util.VariableResolver;
import hudson.util.FormValidation;
@@ -53,6 +56,8 @@ import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.List;
import java.util.Collections;
/**
* Ant launcher.
@@ -185,7 +190,7 @@ public class Ant extends Builder {
args.addTokenized(targets.replaceAll("[\t\r\n]+"," "));
if(ai!=null)
env.put("ANT_HOME",ai.getAntHome());
env.put("ANT_HOME",ai.getHome());
if(antOpts!=null)
env.put("ANT_OPTS",env.expand(antOpts));
@@ -253,14 +258,21 @@ public class Ant extends Builder {
load();
}
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
protected DescriptorImpl(Class<? extends Ant> clazz) {
super(clazz);
}
/**
* Obtains the {@link AntInstallation.DescriptorImpl} instance.
*/
public AntInstallation.DescriptorImpl getToolDescriptor() {
return ToolInstallation.all().get(AntInstallation.DescriptorImpl.class);
}
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
protected void convert(Map<String,Object> oldPropertyBag) {
if(oldPropertyBag.containsKey("installations"))
installations = (AntInstallation[]) oldPropertyBag.get("installations");
@@ -278,56 +290,36 @@ public class Ant extends Builder {
return installations;
}
@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
installations = req.bindJSONToList(
AntInstallation.class, json.get("ant")).toArray(new AntInstallation[0]);
save();
return true;
}
public Ant newInstance(StaplerRequest req, JSONObject formData) throws FormException {
return (Ant)req.bindJSON(clazz,formData);
}
//
// web methods
//
/**
* Checks if the ANT_HOME is valid.
*/
public FormValidation doCheckAntHome(@QueryParameter File value) {
// this can be used to check the existence of a file on the server, so needs to be protected
if(!Hudson.getInstance().hasPermission(Hudson.ADMINISTER))
return FormValidation.ok();
if(!value.isDirectory())
return FormValidation.error(Messages.Ant_NotADirectory(value));
File antJar = new File(value,"lib/ant.jar");
if(!antJar.exists())
return FormValidation.error(Messages.Ant_NotAntDirectory(value));
return FormValidation.ok();
}
public void setInstallations(AntInstallation... antInstallations) {
this.installations = antInstallations;
save();
}
}
/**
* Represents the Ant installation on the system.
*/
public static final class AntInstallation extends ToolInstallation implements
EnvironmentSpecific<AntInstallation>, NodeSpecific<AntInstallation> {
// to remain backward compatible with earlier Hudson that stored this field here.
private final String antHome;
@DataBoundConstructor
public AntInstallation(String name, String home, List<? extends ToolProperty<?>> properties) {
super(name, launderHome(home), properties);
this.antHome = super.getHome();
}
/**
* @deprecated as of 1.308
* Use {@link #AntInstallation(String, String, List)}
*/
public AntInstallation(String name, String home) {
super(name, launderHome(home));
if(home.endsWith("/") || home.endsWith("\\"))
// see https://issues.apache.org/bugzilla/show_bug.cgi?id=26947
// Ant doesn't like the trailing slash, especially on Windows
home = home.substring(0,home.length()-1);
this.antHome = home;
this(name,home,Collections.<ToolProperty<?>>emptyList());
}
private static String launderHome(String home) {
@@ -342,6 +334,8 @@ public class Ant extends Builder {
/**
* install directory.
*
* @deprecated as of 1.307. Use {@link #getHome()}.
*/
public String getAntHome() {
return getHome();
@@ -388,11 +382,11 @@ public class Ant extends Builder {
private static final long serialVersionUID = 1L;
public AntInstallation forEnvironment(EnvVars environment) {
return new AntInstallation(getName(), environment.expand(antHome));
return new AntInstallation(getName(), environment.expand(antHome), getProperties().toList());
}
public AntInstallation forNode(Node node, TaskListener log) throws IOException, InterruptedException {
return new AntInstallation(getName(), translateFor(node, log));
return new AntInstallation(getName(), translateFor(node, log), getProperties().toList());
}
@Extension
@@ -403,6 +397,7 @@ public class Ant extends Builder {
return "Ant";
}
// for compatibility reasons, the persistence is done by Ant.DescriptorImpl
@Override
public AntInstallation[] getInstallations() {
return Hudson.getInstance().getDescriptorByType(Ant.DescriptorImpl.class).getInstallations();
@@ -412,7 +407,58 @@ public class Ant extends Builder {
public void setInstallations(AntInstallation... installations) {
Hudson.getInstance().getDescriptorByType(Ant.DescriptorImpl.class).setInstallations(installations);
}
@Override
public List<? extends ToolInstaller> getDefaultInstallers() {
return Collections.singletonList(new AntInstaller(null));
}
@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
setInstallations(req.bindJSONToList(
AntInstallation.class, json.get("ant")).toArray(new AntInstallation[0]));
return true;
}
/**
* Checks if the ANT_HOME is valid.
*/
public FormValidation doCheckHome(@QueryParameter File value) {
// this can be used to check the existence of a file on the server, so needs to be protected
if(!Hudson.getInstance().hasPermission(Hudson.ADMINISTER))
return FormValidation.ok();
if(!value.isDirectory())
return FormValidation.error(Messages.Ant_NotADirectory(value));
File antJar = new File(value,"lib/ant.jar");
if(!antJar.exists())
return FormValidation.error(Messages.Ant_NotAntDirectory(value));
return FormValidation.ok();
}
}
}
/**
* Automatic Ant installer from apache.org.
*/
public static class AntInstaller extends DownloadFromUrlInstaller {
@DataBoundConstructor
public AntInstaller(String id) {
super(id);
}
}
@Extension
public static final class DescriptorImpl extends DownloadFromUrlInstaller.DescriptorImpl<AntInstaller> {
public String getDisplayName() {
return "Install from Apache";
}
@Override
public boolean isApplicable(Class<? extends ToolInstallation> toolType) {
return toolType==AntInstallation.class;
}
}
}
}
@@ -0,0 +1,172 @@
package hudson.tools;
import hudson.FilePath;
import hudson.model.DownloadService.Downloadable;
import hudson.model.Node;
import hudson.model.TaskListener;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.net.URL;
/**
* Partial convenience implementation of {@link ToolInstaller} that just downloads
* an archive from the URL and extracts it.
*
* @author Kohsuke Kawaguchi
* @since 1.308
*/
public abstract class DownloadFromUrlInstaller extends ToolInstaller {
public final String id;
@DataBoundConstructor
protected DownloadFromUrlInstaller(String id) {
// this installer implementation is designed for platform independent binary,
// and as such we don't provide the label support
super(null);
this.id = id;
}
/**
* Checks if the specified expected location already contains the installed version of the tool.
*
* This check needs to run fairly efficiently. The current implementation uses the souce URL of {@link Installable},
* based on the assumption that released bits do not change its content.
*/
protected boolean isUpToDate(FilePath expectedLocation, Installable i, ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException {
FilePath marker = expectedLocation.child(".installedFrom");
return marker.exists() && marker.readToString().equals(i.url);
}
/**
* Gets the {@link Installable} identified by {@link #id}.
*
* @return null if no such ID is found.
*/
public Installable getInstallable() throws IOException {
for (Installable i : ((DescriptorImpl<?>)getDescriptor()).getInstallables())
if(id.equals(i.id))
return i;
return null;
}
public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException {
FilePath expected = node.createPath(tool.getHome());
Installable inst = getInstallable();
if(inst==null) {
log.getLogger().println("Invalid tool ID "+id);
return expected;
}
if(isUpToDate(expected,inst,tool,node,log))
return expected;
if(expected.installIfNecessaryFrom(new URL(inst.url), log, "Unpacking " + inst.url + " to " + expected + " on " + node.getDisplayName())) {
expected.child(".timestamp").delete(); // we don't use the timestamp
FilePath base = findPullUpDirectory(expected);
if(base!=null && base!=expected)
base.moveAllChildrenTo(expected);
// leave a record for the next up-to-date check
expected.child(".installedFrom").write(inst.url,"UTF-8");
}
return expected;
}
/**
* Often an archive contains an extra top-level directory that's unnecessary when extracted on the disk
* into the expected location. If your installation sources provide that kind of archives, override
* this method to find the real root location.
*
* <p>
* The caller will "pull up" the discovered real root by throw away the intermediate directory,
* so that the user-configured "tool home" directory contains the right files.
*
* <p>
* The default implementation applies some heuristics to auto-determine if the pull up is necessary.
* This should work for typical archive files.
*
* @param root
* The directory that contains the extracted archive. This directory contains nothing but the
* extracted archive. For example, if the user installed
* http://archive.apache.org/dist/ant/binaries/jakarta-ant-1.1.zip , this directory would contain
* a single directory "jakarta-ant".
*
* @return
* Return the real top directory inside {@code root} that contains the meat. In the above example,
* <tt>root.child("jakarta-ant")</tt> should be returned. If there's no directory to pull up,
* return null.
*/
protected FilePath findPullUpDirectory(FilePath root) throws IOException, InterruptedException {
// if the directory just contains one directory and that alone, assume that's the pull up subject
// otherwise leave it as is.
List<FilePath> children = root.list();
if(children.size()!=1) return null;
if(children.get(0).isDirectory())
return children.get(0);
return null;
}
public static abstract class DescriptorImpl<T extends DownloadFromUrlInstaller> extends ToolInstallerDescriptor<T> {
protected DescriptorImpl() {
Downloadable.all().add(createDownloadable());
}
protected Downloadable createDownloadable() {
return new Downloadable(getId());
}
/**
* This ID needs to be unique, and needs to match the ID token in the JSON update file.
* <p>
* By default we use the fully-qualified class name of the {@link DownloadFromUrlInstaller} subtype.
*/
protected String getId() {
return clazz.getName().replace('$','.');
}
/**
* List of installable tools.
*
* <p>
* The UI uses this information to populate the drop-down. Subtypes can override this method
* if it wants to change the way the list is filled.
*
* @return never null.
*/
public List<? extends Installable> getInstallables() throws IOException {
JSONObject d = Downloadable.get(getId()).getData();
if(d==null) return Collections.emptyList();
return Arrays.asList(((InstallableList)JSONObject.toBean(d,InstallableList.class)).list);
}
}
/**
* Used for JSON databinding to parse the obtained list.
*/
public static class InstallableList {
// initialize with an empty array just in case JSON doesn't have the list field (which shouldn't happen.)
public Installable[] list = new Installable[0];
}
public static class Installable {
/**
* Used internally to uniquely identify the name.
*/
public String id;
/**
* This is the human readable name.
*/
public String name;
/**
* URL.
*/
public String url;
}
}
@@ -34,15 +34,12 @@ import hudson.Extension;
import hudson.FilePath;
import hudson.Util;
import hudson.util.FormValidation;
import hudson.util.TimeUnit2;
import hudson.util.ArgumentListBuilder;
import hudson.FilePath.FileCallable;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.model.DownloadService.Downloadable;
import static hudson.tools.JDKInstaller.Preference.*;
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.apache.commons.io.IOUtils;
@@ -128,11 +125,8 @@ public class JDKInstaller extends ToolInstaller {
if(paths.size()!=1)
throw new AbortException("Failed to find the extracted JDKs: "+paths);
paths.get(0).act(PULLUP_DIRECTORY);
// clean up
paths.get(0).delete();
// remove the intermediate directory
paths.get(0).moveAllChildrenTo(expectedLocation);
break;
case WINDOWS:
/*
@@ -218,21 +212,6 @@ public class JDKInstaller extends ToolInstaller {
}
};
/**
* Moves all the contents of this directory into ".."
*/
private static final FileCallable<Void> PULLUP_DIRECTORY = new FileCallable<Void>() {
public Void invoke(File f, VirtualChannel channel) throws IOException {
File p = f.getParentFile();
for(File child : f.listFiles()) {
File target = new File(p, child.getName());
if(!child.renameTo(target))
throw new IOException("Failed to rename "+child+" to "+target);
}
return null;
}
};
/**
* Performs a license click through and obtains the one-time URL for downloading bits.
*
@@ -446,7 +425,7 @@ public class JDKInstaller extends ToolInstaller {
@Extension
public static final class JDKList extends Downloadable {
public JDKList() {
super(JDKInstaller.class.getName(), "jdk.json", TimeUnit2.DAYS.toMillis(1));
super(JDKInstaller.class);
}
public JDKFamilyList toList() throws IOException {