Skip to content

Commit 5c03398

Browse files
committed
Revert "Split GroovyScript for agent and controller"
1 parent bc93044 commit 5c03398

File tree

4 files changed

+57
-76
lines changed

4 files changed

+57
-76
lines changed

src/main/java/org/jenkinsci/plugins/scriptler/builder/ScriptlerBuilder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.jenkinsci.plugins.scriptler.config.Parameter;
5252
import org.jenkinsci.plugins.scriptler.config.Script;
5353
import org.jenkinsci.plugins.scriptler.config.ScriptlerConfiguration;
54-
import org.jenkinsci.plugins.scriptler.util.ControllerGroovyScript;
5554
import org.jenkinsci.plugins.scriptler.util.GroovyScript;
5655
import org.jenkinsci.plugins.scriptler.util.ScriptHelper;
5756
import org.jenkinsci.plugins.scriptler.util.UIHelper;
@@ -263,8 +262,8 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
263262
final Object output;
264263
if (script.onlyBuiltIn) {
265264
// When run on the built-in node, make build, launcher, listener available to script
266-
output = FilePath.localChannel.call(new ControllerGroovyScript(
267-
script.getScriptText(), expandedParams, true, listener, launcher, build));
265+
output = FilePath.localChannel.call(
266+
new GroovyScript(script.getScriptText(), expandedParams, true, listener, launcher, build));
268267
} else {
269268
VirtualChannel channel = launcher.getChannel();
270269
if (channel == null) {

src/main/java/org/jenkinsci/plugins/scriptler/tokenmacro/ScriptlerTokenMacro.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
import hudson.remoting.ChannelClosedException;
99
import hudson.remoting.VirtualChannel;
1010
import java.io.IOException;
11-
import java.util.List;
12-
import jenkins.model.Jenkins;
11+
import java.util.Collections;
1312
import org.jenkinsci.plugins.scriptler.Messages;
1413
import org.jenkinsci.plugins.scriptler.config.Script;
15-
import org.jenkinsci.plugins.scriptler.util.ControllerGroovyScript;
1614
import org.jenkinsci.plugins.scriptler.util.GroovyScript;
1715
import org.jenkinsci.plugins.scriptler.util.ScriptHelper;
1816
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
@@ -41,23 +39,21 @@ public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, Strin
4139
throw new MacroEvaluationException(Messages.tokenmacro_AdminScriptOnly(scriptId));
4240
}
4341

44-
String scriptText = script.getScriptText();
4542
VirtualChannel channel;
46-
GroovyScript groovyScript;
47-
if (script.onlyBuiltIn || Jenkins.get().equals(context.getBuiltOn())) {
43+
if (script.onlyBuiltIn) {
4844
channel = FilePath.localChannel;
49-
groovyScript = new ControllerGroovyScript(scriptText, List.of(), true, listener, null, context);
5045
} else {
5146
FilePath remoteFilePath = context.getWorkspace();
5247
if (remoteFilePath == null) {
5348
// the remote node has apparently disconnected, so we can't run our script
5449
throw new ChannelClosedException((Channel) null, null);
5550
}
5651
channel = remoteFilePath.getChannel();
57-
groovyScript = new GroovyScript(scriptText, List.of(), true, listener);
5852
}
5953

60-
Object output = channel.call(groovyScript);
54+
Object output = channel.call(
55+
new GroovyScript(script.getScriptText(), Collections.emptyList(), true, listener, null, context));
56+
6157
return output != null ? output.toString() : "";
6258
}
6359

src/main/java/org/jenkinsci/plugins/scriptler/util/ControllerGroovyScript.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/main/java/org/jenkinsci/plugins/scriptler/util/GroovyScript.java

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
import edu.umd.cs.findbugs.annotations.NonNull;
44
import groovy.lang.GroovyShell;
55
import groovy.lang.Script;
6+
import hudson.Launcher;
7+
import hudson.model.AbstractBuild;
68
import hudson.model.TaskListener;
79
import java.io.PrintStream;
810
import java.io.Serial;
911
import java.util.*;
1012
import java.util.concurrent.ConcurrentLinkedQueue;
13+
import jenkins.model.Jenkins;
1114
import jenkins.security.MasterToSlaveCallable;
15+
import jenkins.security.Roles;
1216
import org.apache.commons.collections.map.LRUMap;
1317
import org.jenkinsci.plugins.scriptler.Messages;
1418
import org.jenkinsci.plugins.scriptler.config.Parameter;
19+
import org.jenkinsci.remoting.Role;
20+
import org.jenkinsci.remoting.RoleChecker;
1521

1622
/**
1723
* Inspired by hudson.util.RemotingDiagnostics.Script, but adding parameters.
@@ -27,6 +33,9 @@ public class GroovyScript extends MasterToSlaveCallable<Object, RuntimeException
2733

2834
private final boolean failWithException;
2935
private final TaskListener listener;
36+
private final transient AbstractBuild<?, ?> build;
37+
private final transient Launcher launcher;
38+
private transient ClassLoader cl;
3039

3140
@SuppressWarnings("unchecked")
3241
private static final Map<String, ConcurrentLinkedQueue<Script>> cache = Collections.synchronizedMap(new LRUMap(10));
@@ -41,30 +50,57 @@ public class GroovyScript extends MasterToSlaveCallable<Object, RuntimeException
4150
}
4251

4352
/**
44-
* Constructor
53+
* This constructor can only be used when the script is executed on the built-in node, because launcher and build
54+
* can not be transferred to an agent and therefore the execution will fail
4555
* @param script the script to be executed
4656
* @param parameters the parameters to be passed to the script
4757
* @param failWithException should the job fail with an exception
4858
* @param listener access to logging via listener
59+
* @param launcher the launcher
60+
* @param build the current build
4961
*/
5062
public GroovyScript(
5163
String script,
5264
@NonNull Collection<Parameter> parameters,
5365
boolean failWithException,
54-
TaskListener listener) {
66+
TaskListener listener,
67+
Launcher launcher,
68+
AbstractBuild<?, ?> build) {
5569
this.script = script;
5670
this.parameters = new ArrayList<>(parameters);
5771
this.failWithException = failWithException;
5872
this.listener = listener;
73+
this.cl = getClassLoader();
74+
this.build = build;
75+
this.launcher = launcher;
76+
}
77+
78+
/**
79+
* Constructor
80+
* @param script the script to be executed
81+
* @param parameters the parameters to be passed to the script
82+
* @param failWithException should the job fail with an exception
83+
* @param listener access to logging via listener
84+
*/
85+
public GroovyScript(
86+
String script,
87+
@NonNull Collection<Parameter> parameters,
88+
boolean failWithException,
89+
TaskListener listener) {
90+
this(script, parameters, failWithException, listener, null, null);
5991
}
6092

6193
public ClassLoader getClassLoader() {
62-
return Thread.currentThread().getContextClassLoader();
94+
return Jenkins.get().getPluginManager().uberClassLoader;
6395
}
6496

6597
public Object call() {
98+
// if we run locally, cl!=null. Otherwise the delegating classloader will be available as context classloader.
99+
if (cl == null) {
100+
cl = Thread.currentThread().getContextClassLoader();
101+
}
66102
PrintStream logger = listener.getLogger();
67-
GroovyShell shell = new GroovyShell(getClassLoader());
103+
GroovyShell shell = new GroovyShell(cl);
68104

69105
for (Parameter param : parameters) {
70106
final String paramName = param.getName();
@@ -77,7 +113,9 @@ public Object call() {
77113

78114
// set default variables
79115
shell.setVariable("out", logger);
80-
setShellVariables(shell);
116+
shell.setVariable("listener", listener);
117+
if (build != null) shell.setVariable("build", build);
118+
if (launcher != null) shell.setVariable("launcher", launcher);
81119

82120
ConcurrentLinkedQueue<Script> scriptPool = cache.get(script);
83121
if (scriptPool == null) {
@@ -115,8 +153,13 @@ public Object call() {
115153
}
116154
}
117155

118-
protected void setShellVariables(@NonNull GroovyShell shell) {
119-
shell.setVariable("listener", listener);
156+
@Override
157+
public void checkRoles(RoleChecker roleChecker) throws SecurityException {
158+
if (launcher != null && build != null) {
159+
roleChecker.check(this, Roles.MASTER);
160+
} else {
161+
roleChecker.check(this, Role.UNKNOWN);
162+
}
120163
}
121164

122165
private static final class ScriptlerExecutionException extends RuntimeException {

0 commit comments

Comments
 (0)