Skip to content
Permalink
Browse files
[JENKINS-21202] - Generalization of Command installers
This change adds an AbstractCommandInstaller, which can be extended for every script-based installation command.

Related to https://issues.jenkins-ci.org/browse/JENKINS-21202

Signed-off-by: Oleg Nenashev <nenashev@synopsys.com>
  • Loading branch information
oleg-nenashev committed Jan 2, 2014
1 parent 1e849c0 commit 38afe1650203378295a99cc472a8777718afa427
Showing with 130 additions and 70 deletions.
  1. +119 −0 core/src/main/java/hudson/tools/AbstractCommandInstaller.java
  2. +11 −70 core/src/main/java/hudson/tools/CommandInstaller.java
  3. 0 core/src/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/config.jelly
  4. 0 ...rc/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/config_da.properties
  5. 0 ...rc/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/config_de.properties
  6. 0 ...rc/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/config_es.properties
  7. 0 ...rc/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/config_fr.properties
  8. 0 ...rc/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/config_ja.properties
  9. 0 ...rc/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/config_nl.properties
  10. 0 ...main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/config_pt_BR.properties
  11. 0 ...rc/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/config_ru.properties
  12. 0 ...main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/config_zh_CN.properties
  13. 0 ...main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/config_zh_TW.properties
  14. 0 core/src/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help-command.html
  15. 0 ...rc/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help-command_de.html
  16. 0 ...rc/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help-command_ja.html
  17. 0 ...main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help-command_zh_CN.html
  18. 0 ...main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help-command_zh_TW.html
  19. 0 .../src/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help-toolHome.html
  20. 0 ...c/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help-toolHome_de.html
  21. 0 ...ain/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help-toolHome_zh_CN.html
  22. 0 ...ain/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help-toolHome_zh_TW.html
  23. 0 core/src/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help.html
  24. 0 core/src/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help_de.html
  25. 0 core/src/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help_ja.html
  26. 0 core/src/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help_zh_CN.html
  27. 0 core/src/main/resources/hudson/tools/{CommandInstaller → AbstractCommandInstaller}/help_zh_TW.html
@@ -0,0 +1,119 @@
/*
* The MIT License
*
* Copyright 2009-2014 Sun Microsystems and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.tools;

import hudson.FilePath;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.util.FormValidation;
import java.io.IOException;
import org.kohsuke.stapler.QueryParameter;

/**
* A generic script-based installer.
* @since TODO: define a version
* @see BatchCommandInstaller
* @see CommandInstaller
* @author Oleg Nenashev
*
*/
public abstract class AbstractCommandInstaller extends ToolInstaller {

/**
* Command to execute, similar to {@link CommandInterpreter#command}.
*/
private final String command;
private final String toolHome;

public AbstractCommandInstaller(String label, String command, String toolHome) {
super(label);
this.command = fixCrLf(command);
this.toolHome = toolHome;
}

public String getCommand() {
return command;
}

public String getToolHome() {
return toolHome;
}

public abstract String getCommandFileExtension();

/**
* Retrieves a call for remote script caller.
*/
public abstract String[] getCommandCall(FilePath script);

@Override
public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException {
FilePath dir = preferredLocation(tool, node);
// XXX support Windows batch scripts, Unix scripts with interpreter line, etc. (see CommandInterpreter subclasses)
FilePath script = dir.createTextTempFile("hudson", getCommandFileExtension(), command);
try {
String cmd[] = getCommandCall(script);
int r = node.createLauncher(log).launch().cmds(cmd).stdout(log).pwd(dir).join();
if (r != 0) {
throw new IOException("Command returned status " + r);
}
} finally {
script.delete();
}
return dir.child(getToolHome());
}

/**
* Fix CR/LF and always make it Unix style.
*/
//TODO: replace by a Windows style
private static String fixCrLf(String s) {
// eliminate CR
int idx;
while ((idx = s.indexOf("\r\n")) != -1) {
s = s.substring(0, idx) + s.substring(idx + 1);
}
return s;
}

public static abstract class Descriptor<TInstallerClass extends AbstractCommandInstaller>
extends ToolInstallerDescriptor<TInstallerClass> {

public FormValidation doCheckCommand(@QueryParameter String value) {
if (value.length() > 0) {
return FormValidation.ok();
} else {
return FormValidation.error(Messages.CommandInstaller_no_command());
}
}

public FormValidation doCheckToolHome(@QueryParameter String value) {
if (value.length() > 0) {
return FormValidation.ok();
} else {
return FormValidation.error(Messages.CommandInstaller_no_command());
}
}
}
}
@@ -26,95 +26,36 @@

import hudson.Extension;
import hudson.FilePath;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.tasks.CommandInterpreter;
import hudson.util.FormValidation;
import java.io.IOException;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

/**
* Installs a tool by running an arbitrary shell command.
* @since 1.305
*/
public class CommandInstaller extends ToolInstaller {

/**
* Command to execute, similar to {@link CommandInterpreter#command}.
*/
private final String command;

/**
* Resulting tool home directory.
*/
private final String toolHome;
public class CommandInstaller extends AbstractCommandInstaller {

@DataBoundConstructor
public CommandInstaller(String label, String command, String toolHome) {
super(label);
this.command = fixCrLf(command);
this.toolHome = toolHome;
}

/**
* Fix CR/LF and always make it Unix style.
*/
private static String fixCrLf(String s) {
// eliminate CR
int idx;
while((idx=s.indexOf("\r\n"))!=-1)
s = s.substring(0,idx)+s.substring(idx+1);
return s;
}

public String getCommand() {
return command;
super(label, command, toolHome);
}

public String getToolHome() {
return toolHome;
@Override
public String getCommandFileExtension() {
return ".sh";
}

public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException {
FilePath dir = preferredLocation(tool, node);
// TODO support Windows batch scripts, Unix scripts with interpreter line, etc. (see CommandInterpreter subclasses)
FilePath script = dir.createTextTempFile("hudson", ".sh", command);
try {
String[] cmd = {"sh", "-e", script.getRemote()};
int r = node.createLauncher(log).launch().cmds(cmd).stdout(log).pwd(dir).join();
if (r != 0) {
throw new IOException("Command returned status " + r);
}
} finally {
script.delete();
}
return dir.child(toolHome);
@Override
public String[] getCommandCall(FilePath script) {
String[] cmd = {"sh", "-e", script.getRemote()};
return cmd;
}

@Extension
public static class DescriptorImpl extends ToolInstallerDescriptor<CommandInstaller> {
public static class DescriptorImpl extends Descriptor<CommandInstaller> {

@Override
public String getDisplayName() {
return Messages.CommandInstaller_DescriptorImpl_displayName();
}

public FormValidation doCheckCommand(@QueryParameter String value) {
if (value.length() > 0) {
return FormValidation.ok();
} else {
return FormValidation.error(Messages.CommandInstaller_no_command());
}
}

public FormValidation doCheckToolHome(@QueryParameter String value) {
if (value.length() > 0) {
return FormValidation.ok();
} else {
return FormValidation.error(Messages.CommandInstaller_no_toolHome());
}
}

}

}

0 comments on commit 38afe16

Please sign in to comment.