Skip to content

Commit

Permalink
JENKINS-13209 Load files from system node for system script
Browse files Browse the repository at this point in the history
  • Loading branch information
edalquist committed Mar 23, 2012
1 parent 0e8ba3d commit afe69ee
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.scripttrigger.groovy;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Util;
Expand Down Expand Up @@ -141,24 +142,25 @@ protected boolean checkIfModified(Node pollingNode, XTriggerLog log) throws Scri
try {

GroovyScriptTriggerExecutor executor = getGroovyScriptTriggerExecutor(log);
final AbstractProject proj = (AbstractProject) job;

EnvVarsResolver envVarsResolver = new EnvVarsResolver();
Map<String, String> envVars;
try {
envVars = envVarsResolver.getPollingEnvVars((AbstractProject) job, pollingNode);
envVars = envVarsResolver.getPollingEnvVars(proj, pollingNode);
} catch (EnvInjectException e) {
throw new ScriptTriggerException(e);
}

if (groovyExpression != null) {
boolean evaluationSucceed = executor.evaluateGroovyScript(pollingNode, job, getGroovyExpression(), envVars, groovySystemScript);
boolean evaluationSucceed = executor.evaluateGroovyScript(pollingNode, proj, getGroovyExpression(), envVars, groovySystemScript);
if (evaluationSucceed) {
return true;
}
}

if (groovyFilePath != null) {
boolean evaluationSucceed = executor.evaluateGroovyScriptFilePath(pollingNode, job, groovyFilePath, envVars, groovySystemScript);
boolean evaluationSucceed = executor.evaluateGroovyScriptFilePath(pollingNode, proj, groovyFilePath, envVars, groovySystemScript);
if (evaluationSucceed) {
return true;
}
Expand Down
Expand Up @@ -3,11 +3,14 @@
import groovy.lang.GroovyShell;
import hudson.Util;
import hudson.PluginManager;
import hudson.model.Item;
import hudson.model.AbstractProject;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.remoting.Callable;
import hudson.util.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

Expand All @@ -24,15 +27,15 @@ public GroovyScriptTriggerExecutor(XTriggerLog log) {
super(log);
}

public boolean evaluateGroovyScript(Node executingNode, final Item job, final String scriptContent, final Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {
public boolean evaluateGroovyScript(Node executingNode, final AbstractProject proj, final String scriptContent, final Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {

if (scriptContent == null) {
throw new NullPointerException("The script content object must be set.");
}
try {
if (groovySystemScript) {
log.info("Running as system script");
return evaluateGroovyScript(job, scriptContent, envVars);
return evaluateGroovyScript(proj, scriptContent, envVars);
}

return executingNode.getRootPath().act(new Callable<Boolean, ScriptTriggerException>() {
Expand All @@ -56,24 +59,29 @@ public Boolean call() throws ScriptTriggerException {
}
}

private boolean evaluateGroovyScript(final Item job, final String scriptContent, final Map<String, String> envVars) {
private boolean evaluateGroovyScript(final AbstractProject proj, final String scriptContent, final Map<String, String> envVars) {
final StringBuilder envDebug = new StringBuilder("Replacing script vars using:");
for (final Map.Entry<String, String> envEntry : envVars.entrySet()) {
envDebug.append("\n\t").append(envEntry.getKey()).append("=").append(envEntry.getValue());
}
log.info(envDebug.toString());

final String groovyExpressionResolved = Util.replaceMacro(scriptContent, envVars);
log.info(String.format("Evaluating the groovy script: \n----------------------------------------\n%s\n----------------------------------------\n\n", groovyExpressionResolved));
log.info("Evaluating the groovy script:");
log.info("---------- Base Script -----------------");
log.info(scriptContent);
log.info("---------- Resolved Script -------------");
log.info(groovyExpressionResolved);
log.info("----------------------------------------\n");

final ClassLoader cl = getClassLoader();

GroovyShell shell = new GroovyShell(cl);

shell.setVariable("log", log);
shell.setVariable("out", log.getListener().getLogger());
if (job != null) {
shell.setVariable("job", job);
if (proj != null) {
shell.setVariable("project", proj);
}

//Evaluate the new script content
Expand Down Expand Up @@ -105,17 +113,50 @@ protected ClassLoader getClassLoader() {
return cl;
}

public boolean evaluateGroovyScriptFilePath(Node executingNode, Item job, String scriptFilePath, Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {
public boolean evaluateGroovyScriptFilePath(Node executingNode, AbstractProject proj, String scriptFilePath, Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {
if (scriptFilePath == null) {
throw new NullPointerException("The scriptFilePath object must be set.");
}

if (!existsScript(executingNode, scriptFilePath)) {
return false;
final String scriptContent;
if (groovySystemScript) {
String expandedScriptFile = Util.replaceMacro(scriptFilePath, envVars);

final File file = new File(expandedScriptFile);
final String scriptPath = file.getAbsolutePath();

if (!file.exists()) {
log.info(String.format("Can't load the file '%s'. It doesn't exist.", scriptPath));
return false;
}

log.info("Reading script from: " + file.getAbsolutePath());
try {
final FileInputStream fis = new FileInputStream(file);
try {
scriptContent = IOUtils.toString(fis);
}
finally {
fis.close();
}
log.info("Read " + scriptContent.length() + " character long script from: " + scriptPath);
}
catch (IOException e) {
final String msg = "Failed to read system groovy script file '" + scriptFilePath + "' from '" + scriptPath + "'";
log.info(msg);
e.printStackTrace(log.getListener().getLogger());
throw new RuntimeException(msg, e);
}
}
else {
if (!existsScript(executingNode, scriptFilePath)) {
return false;
}

scriptContent = getStringContent(executingNode, scriptFilePath);
}

String scriptContent = getStringContent(executingNode, scriptFilePath);
return evaluateGroovyScript(executingNode, job, scriptContent, envVars, groovySystemScript);
return evaluateGroovyScript(executingNode, proj, scriptContent, envVars, groovySystemScript);
}

}
@@ -1,7 +1,7 @@
<div>
<p>
If checked run the groovy script as a system script, the script will have access to the same
variables as the Groovy Console including the hudson and job models.
variables as the Groovy Console. The AbstractProject is also bound to the project variable.
<br/>
If not checked run the groovy script on the executor node, the script will not have access
to the hudson or job model.
Expand Down

0 comments on commit afe69ee

Please sign in to comment.