Skip to content

Commit

Permalink
Fixed JENKINS-11907
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Dec 8, 2011
1 parent dc6ccc2 commit d45a45f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 97 deletions.
Expand Up @@ -25,7 +25,6 @@ public abstract class AbstractTrigger extends Trigger<BuildableItem> implements

private static Logger LOGGER = Logger.getLogger(AbstractTrigger.class.getName());

protected transient TaskListener listener;

public AbstractTrigger(String cronTabSpec) throws ANTLRException {
super(cronTabSpec);
Expand All @@ -40,6 +39,7 @@ public void run() {
if (!Hudson.getInstance().isQuietingDown() && ((AbstractProject) this.job).isBuildable()) {
AbstractScriptTriggerDescriptor descriptor = getDescriptor();
ExecutorService executorService = descriptor.getExecutor();
StreamTaskListener listener;
try {
try {
listener = new StreamTaskListener(getLogFile(), Charset.forName("UTF-8"));
Expand All @@ -51,7 +51,6 @@ public void run() {
AsynchronousTask asynchronousTask = new AsynchronousTask((AbstractProject) job, scriptTriggerLog, getLogFile());
executorService.execute(asynchronousTask);
}

} catch (Throwable t) {
executorService.shutdown();
LOGGER.log(Level.SEVERE, "Severe Error during the trigger execution " + t.getMessage());
Expand All @@ -60,10 +59,6 @@ public void run() {
}
}

protected TaskListener getListener() {
return listener;
}

protected abstract File getLogFile();

protected class AsynchronousTask implements Runnable, Serializable {
Expand Down Expand Up @@ -93,6 +88,7 @@ public void run() {
String cause = extractRootCause(scriptContent);
Action[] actions = getScheduledActions(scriptContent);
project.scheduleBuild(0, new ScriptTriggerCause(cause), actions);
scriptTriggerLog.closeQuietly();
} else {
logNoChanges(scriptTriggerLog);
}
Expand Down
@@ -1,18 +1,14 @@
package org.jenkinsci.plugins.scripttrigger;

import antlr.ANTLRException;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
import hudson.model.*;
import hudson.remoting.Callable;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -98,10 +94,8 @@ private int getExpectedExitCode() throws ScriptTriggerException {
private boolean checkIfModifiedWithScriptsEvaluation(Node executingNode, int expectedExitCode, ScriptTriggerLog log) throws ScriptTriggerException {

ScriptTriggerExecutor executor = getScriptTriggerExecutor(log);
//Map<String, String> envVars = prepareAndGetEnvVars(executingNode);
ScriptTriggerEnvVarsRetriever varsRetriever = new ScriptTriggerEnvVarsRetriever();
Map<String, String> envVars = varsRetriever.getEnvVars((AbstractProject)job, executingNode, log);

Map<String, String> envVars = varsRetriever.getEnvVars((AbstractProject) job, executingNode, log);

if (script != null) {
int exitCode = executor.executeScriptAndGetExitCode(executingNode, script, envVars);
Expand All @@ -122,75 +116,8 @@ private boolean checkIfModifiedWithScriptsEvaluation(Node executingNode, int exp
return false;
}

// private Map<String, String> prepareAndGetEnvVars(Node executingNode) {
// Map<String, String> overrides = new HashMap<String, String>();
// if (Hudson.getInstance().getPlugin("envinject") != null) {
// try {
// overrides = prepareEnvironmentWithEnvInjectPlugin(executingNode);
// } catch (IOException e) {
// throw new ScriptTriggerException(e);
// } catch (InterruptedException e) {
// throw new ScriptTriggerException(e);
// }
// }
//
// Map<String, String> env;
// try {
// env = getNodeEnvVars(executingNode, job);
// } catch (IOException e) {
// throw new ScriptTriggerException(e);
// } catch (InterruptedException e) {
// throw new ScriptTriggerException(e);
// }
//
// env.putAll(overrides);
// return env;
// }

private Map<String, String> getNodeEnvVars(Node executingNode, final Item job) throws ScriptTriggerException, IOException, InterruptedException {
Map<String, String> env = new HashMap<String, String>();
env.putAll(executingNode.getRootPath().act(new Callable<Map<String, String>, ScriptTriggerException>() {
public Map<String, String> call() throws ScriptTriggerException {
Map<String, String> env = new HashMap<String, String>();
env.putAll(EnvVars.masterEnvVars);
return env;
}
}));

env.put("JENKINS_SERVER_COOKIE", Util.getDigestOf("ServerID:" + Hudson.getInstance().getSecretKey()));
env.put("HUDSON_SERVER_COOKIE", Util.getDigestOf("ServerID:" + Hudson.getInstance().getSecretKey())); // Legacy compatibility
env.put("JOB_NAME", job.getFullName());
env.put("JENKINS_HOME", Hudson.getInstance().getRootDir().getPath());
env.put("HUDSON_HOME", Hudson.getInstance().getRootDir().getPath()); // legacy compatibility
env.put("NODE_NAME", executingNode.getNodeName());
env.put("NODE_LABELS", Util.join(executingNode.getAssignedLabels(), " "));

//Workspace (maybe not exist yet)
if (job instanceof TopLevelItem) {
env.put("WORKSPACE", Hudson.getInstance().getWorkspaceFor((TopLevelItem) job).getRemote());
}

return env;
}

// private Map<String, String> prepareEnvironmentWithEnvInjectPlugin(Node executingNode) throws IOException, InterruptedException {
// EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
// boolean isEnvInjectActivated = variableGetter.isEnvInjectJobPropertyActive((Project) job);
// if (isEnvInjectActivated) {
// EnvInjectJobProperty envInjectJobProperty = variableGetter.getEnvInjectJobProperty((Project) job);
// EnvInjectJobPropertyInfo info = envInjectJobProperty.getInfo();
// Map<String, String> infraEnvVarsMater = new HashMap<String, String>();
// Map<String, String> infraEnvVarsNode = new HashMap<String, String>();
// EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(new EnvInjectLogger(listener));
// Map<String, String> vars = envInjectEnvVarsService.(executingNode.getRootPath(), info, infraEnvVarsMater, infraEnvVarsNode);
//
// return vars;
// }
// return new HashMap<String, String>();
// }

private ScriptTriggerExecutor getScriptTriggerExecutor(ScriptTriggerLog log) throws ScriptTriggerException {
return new ScriptTriggerExecutor(getListener(), log);
return new ScriptTriggerExecutor(log);
}

private boolean testExpectedExitCode(int exitCode, int expectedExitCode, ScriptTriggerLog log) {
Expand Down
Expand Up @@ -18,12 +18,9 @@
*/
public class ScriptTriggerExecutor implements Serializable {

protected TaskListener listener;

protected ScriptTriggerLog log;

public ScriptTriggerExecutor(TaskListener listener, ScriptTriggerLog log) {
this.listener = listener;
public ScriptTriggerExecutor(ScriptTriggerLog log) {
this.log = log;
}

Expand Down Expand Up @@ -103,7 +100,7 @@ public Boolean call() throws ScriptTriggerException {
return rootPath.act(new FilePath.FileCallable<Integer>() {
public Integer invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
try {
return getLocalLauncher(listener).launch().cmds(cmd).envs(envVars).stdout(listener).pwd(rootPath).join();
return getLocalLauncher(log.getListener()).launch().cmds(cmd).envs(envVars).stdout(log.getListener()).pwd(rootPath).join();
} catch (InterruptedException ie) {
throw new ScriptTriggerException(ie);
} catch (IOException ioe) {
Expand Down
@@ -1,6 +1,6 @@
package org.jenkinsci.plugins.scripttrigger;

import hudson.model.TaskListener;
import hudson.util.StreamTaskListener;

import java.io.Serializable;

Expand All @@ -9,16 +9,12 @@
*/
public class ScriptTriggerLog implements Serializable {

private TaskListener listener;
private StreamTaskListener listener;

public ScriptTriggerLog(TaskListener listener) {
public ScriptTriggerLog(StreamTaskListener listener) {
this.listener = listener;
}

public TaskListener getListener() {
return listener;
}

public void info(String message) {
listener.getLogger().println(message);
}
Expand All @@ -27,4 +23,13 @@ public void error(String message) {
listener.getLogger().println("[ERROR] - " + message);
}

public StreamTaskListener getListener() {
return listener;
}

public void closeQuietly() {
if (listener != null) {
listener.closeQuietly();
}
}
}
Expand Up @@ -144,7 +144,7 @@ protected boolean checkIfModifiedByExecutingScript(Node executingNode, ScriptTri
}

private GroovyScriptTriggerExecutor getGroovyScriptTriggerExecutor(ScriptTriggerLog log) throws ScriptTriggerException {
return new GroovyScriptTriggerExecutor(getListener(), log);
return new GroovyScriptTriggerExecutor(log);
}


Expand Down
Expand Up @@ -4,7 +4,6 @@
import hudson.EnvVars;
import hudson.Util;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.remoting.Callable;
import org.jenkinsci.plugins.scripttrigger.ScriptTriggerException;
import org.jenkinsci.plugins.scripttrigger.ScriptTriggerExecutor;
Expand All @@ -17,8 +16,8 @@
*/
public class GroovyScriptTriggerExecutor extends ScriptTriggerExecutor {

public GroovyScriptTriggerExecutor(TaskListener listener, ScriptTriggerLog log) {
super(listener, log);
public GroovyScriptTriggerExecutor(ScriptTriggerLog log) {
super(log);
}

public boolean evaluateGroovyScript(Node executingNode, final String scriptContent) throws ScriptTriggerException {
Expand Down

0 comments on commit d45a45f

Please sign in to comment.