Skip to content

Commit

Permalink
JENKINS-19873 Batch or a shell script files to execute doesn't run wi…
Browse files Browse the repository at this point in the history
…th arguments
  • Loading branch information
Daniel Heid committed Oct 28, 2017
1 parent a6e82ab commit 71e3782
Showing 1 changed file with 42 additions and 43 deletions.
Expand Up @@ -19,13 +19,16 @@
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author Gregory Boissinot
*/
public class ScriptExecutor implements Serializable {

protected PostBuildScriptLog log;
private PostBuildScriptLog log;

private BuildListener listener;

Expand All @@ -34,18 +37,17 @@ public ScriptExecutor(PostBuildScriptLog log, BuildListener listener) {
this.listener = listener;
}

public int executeScriptPathAndGetExitCode(FilePath workspace, String scriptFilePath, Launcher launcher) throws PostBuildScriptException {
public int executeScriptPathAndGetExitCode(FilePath workspace, String command, Launcher launcher) throws PostBuildScriptException {

if (scriptFilePath == null) {
throw new NullPointerException("The scriptFilePath object must be set.");
FilePath filePath = resolveScriptPath(workspace, command);
String[] splittedCommand = command.split("\\s+");
String[] parameters;
parameters = new String[splittedCommand.length - 1];
if (splittedCommand.length > 1) {
System.arraycopy(splittedCommand, 1, parameters, 0, parameters.length);
}
return executeScript(workspace, filePath, launcher, parameters);

FilePath filePath = getFilePath(workspace, scriptFilePath);
if (filePath == null) {
throw new PostBuildScriptException(String.format("The script file path '%s' doesn't exist.", scriptFilePath));
}

return executeScript(workspace, filePath, launcher);
}

private String getResolvedContentWithEnvVars(FilePath filePath) throws PostBuildScriptException {
Expand All @@ -55,26 +57,24 @@ private String getResolvedContentWithEnvVars(FilePath filePath) throws PostBuild
scriptContentResolved =
filePath.act(new SlaveToMasterFileCallable<String>() {
@Override
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
String scriptContent = Util.loadFile(f);
return Util.replaceMacro(scriptContent, EnvVars.masterEnvVars);
}
});
} catch (IOException ioe) {
} catch (IOException | InterruptedException ioe) {
throw new PostBuildScriptException("Error to resolve environment variables", ioe);
} catch (InterruptedException ie) {
throw new PostBuildScriptException("Error to resolve environment variables", ie);
}
return scriptContentResolved;
}

private int executeScript(FilePath workspace, FilePath script, final Launcher launcher) throws PostBuildScriptException {
private int executeScript(FilePath workspace, FilePath script, final Launcher launcher, String[] parameters) throws PostBuildScriptException {

assert script != null;
assert launcher != null;

String scriptContent = getResolvedContentWithEnvVars(script);
log.info(String.format("Evaluating the script: \n %s", scriptContent));
log.info(String.format("Executing the script %s with parameters %s", script, Arrays.toString(parameters)));
FilePath tmpFile;
try {
final CommandInterpreter batchRunner;
Expand All @@ -84,11 +84,11 @@ private int executeScript(FilePath workspace, FilePath script, final Launcher la
batchRunner = new BatchFile(scriptContent);
}
tmpFile = batchRunner.createScriptFile(workspace);
return launcher.launch().cmds(batchRunner.buildCommandLine(tmpFile)).stdout(listener).pwd(workspace).join();
} catch (InterruptedException ie) {
List<String> args = new ArrayList<>(Arrays.asList(batchRunner.buildCommandLine(tmpFile)));
args.addAll(Arrays.asList(parameters));
return launcher.launch().cmds(args).stdout(listener).pwd(workspace).join();
} catch (InterruptedException | IOException ie) {
throw new PostBuildScriptException("Error to execute script", ie);
} catch (IOException ioe) {
throw new PostBuildScriptException("Error to execute script", ioe);
}
}

Expand All @@ -98,7 +98,7 @@ private FilePath getFilePath(final FilePath workspace, final String givenPath) t
try {
return workspace.act(new SlaveToMasterFileCallable<FilePath>() {
@Override
public FilePath invoke(File ws, VirtualChannel channel) throws IOException, InterruptedException {
public FilePath invoke(File ws, VirtualChannel channel) throws IOException, InterruptedException {
File givenFile = new File(givenPath);
if (givenFile.exists()) {
return new FilePath(channel, givenFile.getPath());
Expand All @@ -111,10 +111,8 @@ public FilePath invoke(File ws, VirtualChannel channel) throws IOException, Inte
return null;
}
});
} catch (IOException ioe) {
} catch (IOException | InterruptedException ioe) {
throw new PostBuildScriptException("Error to resolve script path", ioe);
} catch (InterruptedException ie) {
throw new PostBuildScriptException("Error to resolve script path", ie);
}
}

Expand All @@ -127,26 +125,20 @@ public boolean performGroovyScript(final FilePath workspace, final String script
try {
return workspace.act(new SlaveToMasterCallable<Boolean, Throwable>() {
@Override
public Boolean call() throws Throwable {
public Boolean call() throws Throwable {
final String groovyExpressionResolved = Util.replaceMacro(scriptContent, EnvVars.masterEnvVars);
log.info(String.format("Evaluating the groovy script: \n %s", scriptContent));
Binding binding = new Binding();
binding.setVariable("workspace", new File(workspace.getRemote()));
binding.setVariable("log", log);
binding.setVariable("out", log.getListener().getLogger());
ClassLoader classLoader = getClass().getClassLoader();
SecureGroovyScript script = new SecureGroovyScript(groovyExpressionResolved, false, null);
script.configuringWithNonKeyItem();
script.evaluate(classLoader, binding);
binding.setVariable("workspace", new File(workspace.getRemote()));
binding.setVariable("log", log);
binding.setVariable("out", log.getListener().getLogger());
ClassLoader classLoader = getClass().getClassLoader();
SecureGroovyScript script = new SecureGroovyScript(groovyExpressionResolved, false, null);
script.configuringWithNonKeyItem();
script.evaluate(classLoader, binding);
return true;
}
});
} catch (IOException ioe) {
listener.getLogger().print("Problems occurs: " + ioe.getMessage());
return false;
} catch (InterruptedException ie) {
listener.getLogger().print("Problems occurs: " + ie.getMessage());
return false;
} catch (Throwable e) {
listener.getLogger().print("Problems occurs: " + e.getMessage());
return false;
Expand All @@ -155,16 +147,23 @@ public Boolean call() throws Throwable {


public boolean performGroovyScriptFile(FilePath workspace, final String scriptFilePath) throws PostBuildScriptException {
if (scriptFilePath == null) {
throw new NullPointerException("The scriptFilePath object must be set.");
FilePath filePath = resolveScriptPath(workspace, scriptFilePath);

String scriptContent = getResolvedContentWithEnvVars(filePath);
return performGroovyScript(workspace, scriptContent);
}

private FilePath resolveScriptPath(FilePath workspace, String commandString) throws PostBuildScriptException {
if (commandString == null) {
throw new NullPointerException("The commandString object must be set.");
}

String scriptFilePath = commandString.split("\\s+")[0];

FilePath filePath = getFilePath(workspace, scriptFilePath);
if (filePath == null) {
throw new PostBuildScriptException(String.format("The script file path '%s' doesn't exist.", scriptFilePath));
}

String scriptContent = getResolvedContentWithEnvVars(filePath);
return performGroovyScript(workspace, scriptContent);
return filePath;
}
}

0 comments on commit 71e3782

Please sign in to comment.