Skip to content
Permalink
Browse files
Fixed JENKINS-11042
  • Loading branch information
gboissinot committed Oct 9, 2011
1 parent 5d7299d commit f5f2e1bf2856a75ed8b96436e726e01de7fc0188
@@ -63,25 +63,35 @@ protected void logChanges(ScriptTriggerLog log) {

@Override
protected void logNoChanges(ScriptTriggerLog log) {
log.info("The script doesn't return the expected code.");
log.info("No changes. The script doesn't return the expected code or it can't be evaluated.");
}


@Override
protected boolean checkIfModified(ScriptTriggerLog log) throws ScriptTriggerException {

FilePath executionScriptRootPath = getExecutionNodeRootPath();
//if the node is off, the value is null, return no modification
if (executionScriptRootPath == null) {
if (isNodeOff(executionScriptRootPath)) {
log.info("The execution node is off.");
return false;
}

assert executionScriptRootPath != null;

int expectedExitCode = getExpectedExitCode();
log.info("The expected script execution code will be " + expectedExitCode);

return checkIfModifiedWithScriptsEvaluation(executionScriptRootPath, expectedExitCode, log);
}

private boolean isNodeOff(FilePath executionScriptRootPath) {
//if the node is off, the value is null, return no modification
if (executionScriptRootPath == null) {
return true;
}
return false;
}

private int getExpectedExitCode() throws ScriptTriggerException {

//not set
@@ -127,7 +137,7 @@ private ScriptTriggerExecutor getScriptTriggerExecutor(FilePath rootPathExecutio

private boolean testExpectedExitCode(int exitCode, int expectedExitCode, ScriptTriggerLog log) {
log.info(String.format("The exit code is '%s'.", exitCode));
log.info(String.format("Testing if the script execution code returns : '%s'.", expectedExitCode));
log.info(String.format("Testing if the script execution code returns '%s'.", expectedExitCode));
return expectedExitCode == exitCode;
}

@@ -3,7 +3,7 @@
/**
* @author Gregory Boissinot
*/
public class ScriptTriggerException extends Exception {
public class ScriptTriggerException extends RuntimeException {

public ScriptTriggerException(String message) {
super(message);
@@ -4,7 +4,6 @@
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.Hudson;
import hudson.model.TaskListener;
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
@@ -57,6 +56,9 @@ public int executeScriptPathAndGetExitCode(String scriptFilePath) throws ScriptT
}

private String getResolvedContentWithEnvVars(final String scriptContent) throws ScriptTriggerException {

assert executionNodeRootPath != null;

String scriptContentResolved;
try {
log.info("Resolving environment variables for script the content.");
@@ -74,13 +76,6 @@ public String invoke(File f, VirtualChannel channel) throws IOException, Interru
return scriptContentResolved;
}

private Launcher.LocalLauncher getLocalLauncher(TaskListener listener) throws ScriptTriggerException {
final Launcher launcher = Hudson.getInstance().createLauncher(listener);
if (!(launcher instanceof Launcher.LocalLauncher)) {
throw new ScriptTriggerException("The launcher object must be a local launcher");
}
return (Launcher.LocalLauncher) launcher;
}

protected String getStringContent(final String filePath) throws ScriptTriggerException {

@@ -114,20 +109,26 @@ private int executeScript(final String scriptContent) throws ScriptTriggerExcept
log.info(String.format("Evaluating the script: \n %s", scriptContent));
try {

final Launcher.LocalLauncher localLauncher = getLocalLauncher(listener);
return executionNodeRootPath.act(new Callable<Integer, ScriptTriggerException>() {
public Integer call() throws ScriptTriggerException {
FilePath tmpFile;

boolean isUnix = executionNodeRootPath.act(new Callable<Boolean, ScriptTriggerException>() {
public Boolean call() throws ScriptTriggerException {
return File.pathSeparatorChar == ':';
}
});

CommandInterpreter batchRunner;
if (isUnix) {
batchRunner = new Shell(scriptContent);
} else {
batchRunner = new BatchFile(scriptContent);
}
FilePath tmpFile = batchRunner.createScriptFile(executionNodeRootPath);
final String[] cmd = batchRunner.buildCommandLine(tmpFile);

return executionNodeRootPath.act(new FilePath.FileCallable<Integer>() {
public Integer invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
try {
boolean isUnix = File.pathSeparatorChar == ':';
final CommandInterpreter batchRunner;
if (isUnix) {
batchRunner = new Shell(scriptContent);
} else {
batchRunner = new BatchFile(scriptContent);
}
tmpFile = batchRunner.createScriptFile(executionNodeRootPath);
return localLauncher.launch().cmds(batchRunner.buildCommandLine(tmpFile)).stdout(listener).pwd(executionNodeRootPath).join();
return getLocalLauncher(listener).launch().cmds(cmd).stdout(listener).pwd(executionNodeRootPath).join();
} catch (InterruptedException ie) {
throw new ScriptTriggerException(ie);
} catch (IOException ioe) {
@@ -142,6 +143,10 @@ public Integer call() throws ScriptTriggerException {
}
}

private Launcher getLocalLauncher(TaskListener listener) throws ScriptTriggerException {
return new Launcher.LocalLauncher(listener);
}

protected boolean existsScript(final String path) throws ScriptTriggerException {

try {

0 comments on commit f5f2e1b

Please sign in to comment.