Skip to content

Commit

Permalink
[JENKINS-7113, JENKINS-10920] Auto-installer
Browse files Browse the repository at this point in the history
  • Loading branch information
vjuranek committed Sep 8, 2011
1 parent 03d32cc commit 2a0e746
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 52 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -2,9 +2,9 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.391</version>
<version>1.409</version>
</parent>

<artifactId>groovy</artifactId>
Expand Down
61 changes: 32 additions & 29 deletions src/main/java/hudson/plugins/groovy/Groovy.java
Expand Up @@ -5,32 +5,21 @@
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Launcher.LocalLauncher;
import hudson.Util;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.ParametersAction;
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
import hudson.tasks.Builder;
import hudson.util.NullStream;
import hudson.util.StreamTaskListener;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;

import net.sf.json.JSONObject;

Expand All @@ -40,7 +29,7 @@
/**
* A Builder for Groovy scripts.
*
* @author dvrzalik
* @author dvrzalik, vjuranek
*/
public class Groovy extends AbstractGroovy {

Expand All @@ -53,6 +42,7 @@ public class Groovy extends AbstractGroovy {

private String classPath; //for user convenience when added more item into class path not have to deal with path separator

@DataBoundConstructor
public Groovy(ScriptSource scriptSource, String groovyName, String parameters,
String scriptParameters, String properties, String javaOpts, String classPath) {
super(scriptSource);
Expand Down Expand Up @@ -83,7 +73,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
return false;
}
try {
String[] cmd = buildCommandLine(build,script,launcher.isUnix());
String[] cmd = buildCommandLine(build,listener,script,launcher.isUnix());

int result;
try {
Expand Down Expand Up @@ -178,6 +168,7 @@ public GroovyInstallation[] getInstallations() {
return installations;
}

/*
@Override
public boolean configure(StaplerRequest req, JSONObject formData) {
try {
Expand All @@ -188,20 +179,23 @@ public boolean configure(StaplerRequest req, JSONObject formData) {
} catch (ServletException ex) {
Logger.getLogger(Groovy.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
}

*/

@Override
public Builder newInstance(StaplerRequest req, JSONObject data) throws FormException {

ScriptSource source = getScriptSource(req, data);
String instName = data.getString("groovyName");
String params = data.getString("parameters");
String classPath = data.getString("classPath").trim();
String scriptParams = data.getString("scriptParameters");
String props = data.getString("properties");
String javaOpts = data.getString("javaOpts");

return new Groovy(source, instName, params, scriptParams, props, javaOpts, classPath);

//return (Groovy) req.bindJSON(clazz, data);
}

public static GroovyInstallation getGroovy(String groovyName) {
Expand All @@ -211,8 +205,14 @@ public static GroovyInstallation getGroovy(String groovyName) {
}
return null;
}

public void setInstallations(GroovyInstallation... installations) {
this.installations = installations;
save();
}

}

/*
public static final class GroovyInstallation implements Serializable {
private final String name;
Expand All @@ -224,23 +224,23 @@ public GroovyInstallation(String name, String home) {
this.home = home;
}
/**
*//**
* install directory.
*/
*//*
public String getHome() {
return home;
}
/**
*//**
* Human readable display name.
*/
*//*
public String getName() {
return name;
}
/**
*//**
* Gets the executable path of this groovy installation on the given target system.
*/
*//*
public String getExecutable(VirtualChannel channel) throws IOException, InterruptedException {
return channel.call(new Callable<String, IOException>() {
Expand Down Expand Up @@ -268,9 +268,9 @@ private File getExeFile(String execName) {
return new File(binDir, execName);
}
/**
*//**
* Returns true if the executable exists.
*/
*//*
public boolean exists() {
try {
return getExecutable(new LocalLauncher(new StreamTaskListener(new NullStream())).getChannel()) != null;
Expand All @@ -285,23 +285,26 @@ public boolean exists() {
}

*/
protected GroovyInstallation getGroovy() {
return DescriptorImpl.getGroovy(groovyName);
}

//backward compatibility, default is Unix
protected String[] buildCommandLine(AbstractBuild build,FilePath script) throws IOException, InterruptedException {
return buildCommandLine(build,script, true);
return buildCommandLine(build, null, script, true);
}

protected String[] buildCommandLine(AbstractBuild build,FilePath script, boolean isOnUnix) throws IOException, InterruptedException {
protected String[] buildCommandLine(AbstractBuild build, BuildListener listener, FilePath script, boolean isOnUnix) throws IOException, InterruptedException {
ArrayList<String> list = new ArrayList<String>();

String cmd = "groovy";//last hope in case of missing or not selected installation

GroovyInstallation installation = getGroovy();
if(installation != null) {
EnvVars env = build.getEnvironment(listener);
installation = installation.forNode(Computer.currentComputer().getNode(), listener);
installation = installation.forEnvironment(env);
cmd = installation.getExecutable(script.getChannel());
}
list.add(cmd);
Expand Down
148 changes: 148 additions & 0 deletions src/main/java/hudson/plugins/groovy/GroovyInstallation.java
@@ -0,0 +1,148 @@
package hudson.plugins.groovy;

import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher.LocalLauncher;
import hudson.Util;
import hudson.model.EnvironmentSpecific;
import hudson.model.TaskListener;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
import hudson.slaves.NodeSpecific;
import hudson.tools.ToolDescriptor;
import hudson.tools.ToolInstaller;
import hudson.tools.ToolProperty;
import hudson.tools.ToolInstallation;
import hudson.util.NullStream;
import hudson.util.StreamTaskListener;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import org.kohsuke.stapler.DataBoundConstructor;

public class GroovyInstallation extends ToolInstallation implements EnvironmentSpecific<GroovyInstallation>, NodeSpecific<GroovyInstallation> {

private final String name;
private final String home;

/*
public GroovyInstallation(String name, String home) {
super(name,home,new LinkedList());
this.name = name;
this.home = home;
}
*/
@DataBoundConstructor
public GroovyInstallation(String name, String home, List<? extends ToolProperty<?>> properties){
super(name,home,properties);
this.name = name;
this.home = home;

}

/**
* install directory.
*/
public String getHome() {
return home;
}

/**
* Human readable display name.
*/
public String getName() {
return name;
}

/**
* Gets the executable path of this groovy installation on the given target system.
*/
public String getExecutable(VirtualChannel channel) throws IOException, InterruptedException {
return channel.call(new Callable<String, IOException>() {

public String call() throws IOException {
File exe = getExeFile("groovy");
if (exe.exists()) {
return exe.getPath();
}
return null;
}
});
}

private File getExeFile(String execName) {
String groovyHome = Util.replaceMacro(getHome(),EnvVars.masterEnvVars);
File binDir = new File(groovyHome, "bin/");
if (File.separatorChar == '\\') {
if(new File(binDir, execName + ".exe").exists()) {
execName += ".exe";
} else {
execName += ".bat";
}
}
return new File(binDir, execName);
}

/**
* Returns true if the executable exists.
*/
/*
public boolean exists() {
try {
return getExecutable(new LocalLauncher(new StreamTaskListener(new NullStream())).getChannel()) != null;
} catch (IOException e) {
return false;
} catch (InterruptedException e) {
return false;
}
}
*/

public GroovyInstallation forEnvironment(EnvVars environment) {
return new GroovyInstallation(getName(), environment.expand(getHome()), getProperties().toList());
}

public GroovyInstallation forNode(Node node, TaskListener log) throws IOException, InterruptedException {
return new GroovyInstallation(getName(), translateFor(node, log), getProperties().toList());
}

@Extension
public static class DescriptorImpl extends ToolDescriptor<GroovyInstallation> {

public DescriptorImpl() {
}

@Override
public String getDisplayName() {
return "Groovy";//Messages.installer_displayName();
}

@Override
public List<? extends ToolInstaller> getDefaultInstallers() {
return Collections.singletonList(new GroovyInstaller(null));
}

// for compatibility reasons, the persistence is done by GradleBuilder.DescriptorImpl

@Override
public GroovyInstallation[] getInstallations() {
return Hudson.getInstance().getDescriptorByType(Groovy.DescriptorImpl.class).getInstallations();
}

@Override
public void setInstallations(GroovyInstallation... installations) {
Hudson.getInstance().getDescriptorByType(Groovy.DescriptorImpl.class).setInstallations(installations);
}

}


private static final long serialVersionUID = 1L;

}
26 changes: 26 additions & 0 deletions src/main/java/hudson/plugins/groovy/GroovyInstaller.java
@@ -0,0 +1,26 @@
package hudson.plugins.groovy;

import hudson.Extension;
import hudson.tools.DownloadFromUrlInstaller;
import hudson.tools.ToolInstallation;

import org.kohsuke.stapler.DataBoundConstructor;

public class GroovyInstaller extends DownloadFromUrlInstaller {
@DataBoundConstructor
public GroovyInstaller(String id) {
super(id);
}

@Extension
public static final class DescriptorImpl extends DownloadFromUrlInstaller.DescriptorImpl<GroovyInstaller> {
public String getDisplayName() {
return "Install from http://groovy.codehaus.org";
}

@Override
public boolean isApplicable(Class<? extends ToolInstallation> toolType) {
return toolType == GroovyInstallation.class;
}
}
}
21 changes: 0 additions & 21 deletions src/main/resources/hudson/plugins/groovy/Groovy/global.jelly
@@ -1,26 +1,5 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:section title="${%Groovy}">
<f:entry title="${%Groovy installation}"
description="${%List of Groovy installations on this system.}">
<f:repeatable var="groovy" items="${descriptor.installations}">
<table width="100%">
<f:entry title="${%name}">
<input class="setting-input" name="groovy.name"
type="text" value="${groovy.name}"/>
</f:entry>

<f:entry title="GROOVY_HOME">
<input class="setting-input" name="groovy.home"
type="text" value="${groovy.home}" />
</f:entry>
<f:entry title="">
<div align="right">
<f:repeatableDeleteButton />
</div>
</f:entry>
</table>
</f:repeatable>
</f:entry>
<f:entry title="${%Allow token macro processing}" description="${%Possible security hole, read help!}"
help="/plugin/groovy/help/global/allowMacro.html">
<f:checkbox name="allowMacro" checked="${descriptor.allowMacro}" />
Expand Down

0 comments on commit 2a0e746

Please sign in to comment.