|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright 2009-2014 Sun Microsystems and contributors |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package hudson.tools; |
| 25 | + |
| 26 | +import hudson.FilePath; |
| 27 | +import hudson.model.Node; |
| 28 | +import hudson.model.TaskListener; |
| 29 | +import hudson.util.FormValidation; |
| 30 | +import java.io.IOException; |
| 31 | +import org.kohsuke.stapler.QueryParameter; |
| 32 | + |
| 33 | +/** |
| 34 | + * A generic script-based installer. |
| 35 | + * @since TODO: define a version |
| 36 | + * @see BatchCommandInstaller |
| 37 | + * @see CommandInstaller |
| 38 | + * @author Oleg Nenashev |
| 39 | + * |
| 40 | + */ |
| 41 | +public abstract class AbstractCommandInstaller extends ToolInstaller { |
| 42 | + |
| 43 | + /** |
| 44 | + * Command to execute, similar to {@link CommandInterpreter#command}. |
| 45 | + */ |
| 46 | + private final String command; |
| 47 | + private final String toolHome; |
| 48 | + |
| 49 | + public AbstractCommandInstaller(String label, String command, String toolHome) { |
| 50 | + super(label); |
| 51 | + this.command = fixCrLf(command); |
| 52 | + this.toolHome = toolHome; |
| 53 | + } |
| 54 | + |
| 55 | + public String getCommand() { |
| 56 | + return command; |
| 57 | + } |
| 58 | + |
| 59 | + public String getToolHome() { |
| 60 | + return toolHome; |
| 61 | + } |
| 62 | + |
| 63 | + public abstract String getCommandFileExtension(); |
| 64 | + |
| 65 | + /** |
| 66 | + * Retrieves a call for remote script caller. |
| 67 | + */ |
| 68 | + public abstract String[] getCommandCall(FilePath script); |
| 69 | + |
| 70 | + @Override |
| 71 | + public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException { |
| 72 | + FilePath dir = preferredLocation(tool, node); |
| 73 | + // XXX support Windows batch scripts, Unix scripts with interpreter line, etc. (see CommandInterpreter subclasses) |
| 74 | + FilePath script = dir.createTextTempFile("hudson", getCommandFileExtension(), command); |
| 75 | + try { |
| 76 | + String cmd[] = getCommandCall(script); |
| 77 | + int r = node.createLauncher(log).launch().cmds(cmd).stdout(log).pwd(dir).join(); |
| 78 | + if (r != 0) { |
| 79 | + throw new IOException("Command returned status " + r); |
| 80 | + } |
| 81 | + } finally { |
| 82 | + script.delete(); |
| 83 | + } |
| 84 | + return dir.child(getToolHome()); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Fix CR/LF and always make it Unix style. |
| 89 | + */ |
| 90 | + //TODO: replace by a Windows style |
| 91 | + private static String fixCrLf(String s) { |
| 92 | + // eliminate CR |
| 93 | + int idx; |
| 94 | + while ((idx = s.indexOf("\r\n")) != -1) { |
| 95 | + s = s.substring(0, idx) + s.substring(idx + 1); |
| 96 | + } |
| 97 | + return s; |
| 98 | + } |
| 99 | + |
| 100 | + public static abstract class Descriptor<TInstallerClass extends AbstractCommandInstaller> |
| 101 | + extends ToolInstallerDescriptor<TInstallerClass> { |
| 102 | + |
| 103 | + public FormValidation doCheckCommand(@QueryParameter String value) { |
| 104 | + if (value.length() > 0) { |
| 105 | + return FormValidation.ok(); |
| 106 | + } else { |
| 107 | + return FormValidation.error(Messages.CommandInstaller_no_command()); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public FormValidation doCheckToolHome(@QueryParameter String value) { |
| 112 | + if (value.length() > 0) { |
| 113 | + return FormValidation.ok(); |
| 114 | + } else { |
| 115 | + return FormValidation.error(Messages.CommandInstaller_no_command()); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments