Skip to content

Commit 28b28ff

Browse files
committed
Switch to built-in node instead of controller
The comment about running Scriptler scripts on the controller is somewhat incorrect and we should be talking about running on the built- in node instead. Make this change, as well as in the configuration data.
1 parent 84f45f8 commit 28b28ff

File tree

14 files changed

+91
-54
lines changed

14 files changed

+91
-54
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.jenkinsci.plugins.scriptler;
2+
3+
import edu.umd.cs.findbugs.annotations.NonNull;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public final class NodeNames {
9+
public static final String BUILT_IN = "(built-in)";
10+
public static final String ALL = "(all)";
11+
public static final String ALL_AGENTS = "(all agents)";
12+
private static final Map<String, String> DEPRECATED_ALIASES;
13+
14+
static {
15+
Map<String, List<String>> deprecatedNames =
16+
Map.of(BUILT_IN, List.of("(master)", "(controller)"), ALL_AGENTS, List.of("(all slaves)"));
17+
18+
Map<String, String> aliases = new HashMap<>();
19+
deprecatedNames.forEach(
20+
(newName, oldNames) -> oldNames.forEach(oldName -> aliases.put(normalizeName(oldName), newName)));
21+
22+
DEPRECATED_ALIASES = Map.copyOf(aliases);
23+
}
24+
25+
@NonNull
26+
private static String normalizeName(@NonNull String name) {
27+
return name.toLowerCase();
28+
}
29+
30+
@NonNull
31+
public static String normalizeNodeName(@NonNull String nodeName) {
32+
return DEPRECATED_ALIASES.getOrDefault(normalizeName(nodeName), nodeName);
33+
}
34+
35+
private NodeNames() {}
36+
}

src/main/java/org/jenkinsci/plugins/scriptler/ScriptlerManagement.java

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ public class ScriptlerManagement extends ManagementLink implements RootAction {
7676
private static final String NOT_APPROVED_YET = "notApprovedYet";
7777
private static final String CAN_BYPASS_APPROVAL = "canByPassScriptApproval";
7878
private static final String SCRIPT = "script";
79-
private static final String MASTER = "(master)";
80-
private static final String CONTROLLER = "(controller)";
81-
private static final String ALL = "(all)";
82-
private static final String ALL_SLAVES = "(all slaves)";
83-
private static final String ALL_AGENTS = "(all agents)";
8479

8580
private static final MarkupFormatter INSTANCE = RawHtmlMarkupFormatter.INSTANCE;
8681

@@ -244,8 +239,8 @@ public HttpResponse doDownloadScript(
244239
* script code
245240
* @param nonAdministerUsing
246241
* allow usage in Scriptler build step
247-
* @param onlyController
248-
* this script is only allowed to run on the controller
242+
* @param onlyBuiltIn
243+
* this script is only allowed to run on the built-in node
249244
* @param originCatalogName
250245
* (optional) the name of the catalog the script is loaded/added from
251246
* @param originId
@@ -261,7 +256,7 @@ public HttpResponse doScriptAdd(
261256
@QueryParameter("comment") String comment,
262257
@QueryParameter(SCRIPT) String script,
263258
@QueryParameter("nonAdministerUsing") boolean nonAdministerUsing,
264-
@QueryParameter("onlyController") boolean onlyController,
259+
@QueryParameter("onlyBuiltIn") boolean onlyBuiltIn,
265260
String originCatalogName,
266261
String originId)
267262
throws IOException, ServletException {
@@ -271,7 +266,7 @@ public HttpResponse doScriptAdd(
271266
List<Parameter> parameters = UIHelper.extractParameters(req.getSubmittedForm());
272267

273268
saveScriptAndForward(
274-
id, name, comment, script, nonAdministerUsing, onlyController, originCatalogName, originId, parameters);
269+
id, name, comment, script, nonAdministerUsing, onlyBuiltIn, originCatalogName, originId, parameters);
275270
return new HttpRedirect(INDEX);
276271
}
277272

@@ -286,7 +281,7 @@ private String saveScriptAndForward(
286281
String comment,
287282
String script,
288283
boolean nonAdministerUsing,
289-
boolean onlyController,
284+
boolean onlyBuiltIn,
290285
String originCatalogName,
291286
String originId,
292287
@NonNull List<Parameter> parameters)
@@ -330,7 +325,7 @@ private String saveScriptAndForward(
330325
parameters);
331326
} else {
332327
// save (overwrite) the meta information
333-
newScript = new Script(finalFileName, displayName, comment, nonAdministerUsing, parameters, onlyController);
328+
newScript = new Script(finalFileName, displayName, comment, nonAdministerUsing, parameters, onlyBuiltIn);
334329
}
335330
ScriptlerConfiguration cfg = getConfiguration();
336331
cfg.addOrReplace(newScript);
@@ -520,7 +515,7 @@ public void doRunScript(StaplerRequest2 req, StaplerResponse2 rsp, @QueryParamet
520515

521516
req.setAttribute(SCRIPT, script);
522517
// set default selection
523-
req.setAttribute("currentNode", CONTROLLER);
518+
req.setAttribute("currentNode", NodeNames.BUILT_IN);
524519
req.getView(this, "runScript.jelly").forward(req, rsp);
525520
}
526521

@@ -603,7 +598,7 @@ public void doTriggerScript(
603598
* @param script
604599
* the script code (groovy)
605600
* @param node
606-
* the node, to execute the code on, defaults to {@value #CONTROLLER}
601+
* the node, to execute the code on, defaults to {@value NodeNames#BUILT_IN}
607602
* @param contentType
608603
* the contentType to use in the response, defaults to text/plain
609604
*/
@@ -653,7 +648,7 @@ public void doRun(
653648

654649
rsp.setContentType(contentType == null ? "text/plain" : contentType);
655650

656-
final List<String> computers = resolveComputerNames(node == null ? CONTROLLER : node);
651+
final List<String> computers = resolveComputerNames(node == null ? NodeNames.BUILT_IN : node);
657652
if (computers.size() > 1) {
658653
rsp.getOutputStream().print(ScriptHelper.runScript(computers, script, paramArray));
659654
} else {
@@ -678,17 +673,14 @@ private Collection<Parameter> prepareParameters(StaplerRequest2 req, Script temp
678673
return params.values();
679674
}
680675

681-
private List<String> resolveComputerNames(String nameAlias) {
676+
private List<String> resolveComputerNames(String rawNameAlias) {
677+
final String nameAlias = NodeNames.normalizeNodeName(rawNameAlias);
682678
final List<String> computers;
683-
if (nameAlias.equalsIgnoreCase(ALL)
684-
|| nameAlias.equalsIgnoreCase(ALL_AGENTS)
685-
|| nameAlias.equalsIgnoreCase(ALL_SLAVES)) {
679+
if (nameAlias.equalsIgnoreCase(NodeNames.ALL) || nameAlias.equalsIgnoreCase(NodeNames.ALL_AGENTS)) {
686680
computers = getComputerNames();
687-
if (nameAlias.equalsIgnoreCase(ALL)) {
688-
computers.add(CONTROLLER);
681+
if (nameAlias.equalsIgnoreCase(NodeNames.ALL)) {
682+
computers.add(NodeNames.BUILT_IN);
689683
}
690-
} else if (nameAlias.equalsIgnoreCase(MASTER)) {
691-
computers = List.of(CONTROLLER);
692684
} else {
693685
computers = List.of(nameAlias);
694686
}
@@ -765,18 +757,18 @@ public List<String> getSlaveAlias(Script script) {
765757
* @return list with all computer names
766758
*/
767759
public List<String> getComputerAliases(Script script) {
768-
if (script.onlyController) {
769-
return List.of(CONTROLLER);
760+
if (script.onlyBuiltIn) {
761+
return List.of(NodeNames.BUILT_IN);
770762
}
771763
final List<String> computerNames = getComputerNames();
772-
// add 'magic' name for the controller, so all nodes can be handled the same way
773-
computerNames.addAll(0, List.of(CONTROLLER, ALL, ALL_AGENTS));
764+
// add 'magic' name for the built-in node, so all nodes can be handled the same way
765+
computerNames.addAll(0, List.of(NodeNames.BUILT_IN, NodeNames.ALL, NodeNames.ALL_AGENTS));
774766
return computerNames;
775767
}
776768

777769
private List<String> getComputerNames() {
778770
return Arrays.stream(Jenkins.get().getComputers())
779-
// remove the controller's computer as it has an empty name
771+
// remove the built-in's computer as it has an empty name
780772
.filter(Predicate.not(Jenkins.MasterComputer.class::isInstance))
781773
.map(Computer::getName)
782774
.collect(Collectors.toCollection(ArrayList::new));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
255255
parameter.getName(), TokenMacro.expandAll(build, listener, parameter.getValue())));
256256
}
257257
final Object output;
258-
if (script.onlyController) {
259-
// When run on controller, make build, launcher, listener available to script
258+
if (script.onlyBuiltIn) {
259+
// When run on the built-in node, make build, launcher, listener available to script
260260
output = FilePath.localChannel.call(new ControllerGroovyScript(
261261
script.getScriptText(), expandedParams, true, listener, launcher, build));
262262
} else {

src/main/java/org/jenkinsci/plugins/scriptler/config/Script.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,17 @@ public class Script implements Comparable<Script>, NamedResource, Serializable {
5454
// User with Scriptler/RUN_SCRIPT permission can add/edit Scriptler step in projects
5555
public final boolean nonAdministerUsing;
5656

57-
// script is runnable only on controller
58-
public final boolean onlyController;
57+
// script is runnable only on the built-in node
58+
public final boolean onlyBuiltIn;
5959

6060
/**
61-
* @deprecated Use {@link #onlyController} instead.
61+
* @deprecated Use {@link #onlyBuiltIn} instead.
62+
*/
63+
@Deprecated(since = "386")
64+
public final Boolean onlyController;
65+
66+
/**
67+
* @deprecated Use {@link #onlyBuiltIn} instead.
6268
*/
6369
@Deprecated(since = "381")
6470
public final Boolean onlyMaster;
@@ -72,15 +78,15 @@ public Script(
7278
String comment,
7379
boolean nonAdministerUsing,
7480
@NonNull List<Parameter> parameters,
75-
boolean onlyController) {
76-
this(id, name, comment, true, null, null, null, nonAdministerUsing, parameters, onlyController);
81+
boolean onlyBuiltIn) {
82+
this(id, name, comment, true, null, null, null, nonAdministerUsing, parameters, onlyBuiltIn);
7783
}
7884

7985
/**
8086
* used during plugin start to synchronize available scripts
8187
*/
82-
public Script(String id, String comment, boolean available, boolean nonAdministerUsing, boolean onlyController) {
83-
this(id, id, comment, available, null, null, null, nonAdministerUsing, Collections.emptyList(), onlyController);
88+
public Script(String id, String comment, boolean available, boolean nonAdministerUsing, boolean onlyBuiltIn) {
89+
this(id, id, comment, available, null, null, null, nonAdministerUsing, List.of(), onlyBuiltIn);
8490
}
8591

8692
/**
@@ -112,7 +118,7 @@ public Script(
112118
String originDate,
113119
boolean nonAdministerUsing,
114120
@NonNull List<Parameter> parameters,
115-
boolean onlyController) {
121+
boolean onlyBuiltIn) {
116122
this(
117123
id,
118124
name,
@@ -123,7 +129,7 @@ public Script(
123129
originDate,
124130
nonAdministerUsing,
125131
parameters,
126-
onlyController);
132+
onlyBuiltIn);
127133
}
128134

129135
/**
@@ -139,7 +145,7 @@ public Script(
139145
String originDate,
140146
boolean nonAdministerUsing,
141147
@NonNull List<Parameter> parameters,
142-
boolean onlyController) {
148+
boolean onlyBuiltIn) {
143149
this.id = id;
144150
this.name = name;
145151
this.comment = comment;
@@ -149,8 +155,8 @@ public Script(
149155
this.originDate = originDate;
150156
this.nonAdministerUsing = nonAdministerUsing;
151157
this.parameters = new ArrayList<>(parameters);
152-
this.onlyController = onlyController;
153-
this.onlyMaster = null;
158+
this.onlyBuiltIn = onlyBuiltIn;
159+
this.onlyMaster = this.onlyController = null;
154160
}
155161

156162
public Script copy() {
@@ -164,7 +170,7 @@ public Script copy() {
164170
originDate,
165171
nonAdministerUsing,
166172
parameters,
167-
onlyController);
173+
onlyBuiltIn);
168174
}
169175

170176
/*
@@ -239,7 +245,8 @@ public int compareTo(Script o) {
239245

240246
@Serial
241247
public Object readResolve() {
242-
if (onlyMaster != null) {
248+
if (onlyMaster != null || onlyController != null) {
249+
boolean onlyBuiltIn = onlyMaster == null ? onlyController : onlyMaster;
243250
return new Script(
244251
id,
245252
name,
@@ -250,7 +257,7 @@ public Object readResolve() {
250257
originDate,
251258
nonAdministerUsing,
252259
parameters,
253-
onlyMaster);
260+
onlyBuiltIn);
254261
}
255262
return this;
256263
}

src/main/java/org/jenkinsci/plugins/scriptler/config/ScriptSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private Script merge(Script origin, Script newScript) {
8585
originDate,
8686
newScript.nonAdministerUsing,
8787
newScript.getParameters(),
88-
newScript.onlyController);
88+
newScript.onlyBuiltIn);
8989
}
9090

9191
public final Set<Script> getScripts() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, Strin
4444
String scriptText = script.getScriptText();
4545
VirtualChannel channel;
4646
GroovyScript groovyScript;
47-
if (script.onlyController || Jenkins.get().equals(context.getBuiltOn())) {
47+
if (script.onlyBuiltIn || Jenkins.get().equals(context.getBuiltOn())) {
4848
channel = FilePath.localChannel;
4949
groovyScript = new ControllerGroovyScript(scriptText, List.of(), true, listener, null, context);
5050
} else {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class ControllerGroovyScript extends GroovyScript {
1818
private final transient Launcher launcher;
1919

2020
/**
21-
* This constructor can only be used when the script is executed on the controller, because launcher and build can not be transferred to an agent and therefore the execution will fail
21+
* This constructor can only be used when the script is executed on the built-in node, because launcher and build
22+
* can not be transferred to an agent and therefore the execution will fail
2223
* @param script the script to be executed
2324
* @param parameters the parameters to be passed to the script
2425
* @param failWithException should the job fail with an exception

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import net.sf.json.JSONObject;
2222
import net.sf.json.JSONSerializer;
2323
import org.jenkinsci.plugins.scriptler.Messages;
24+
import org.jenkinsci.plugins.scriptler.NodeNames;
2425
import org.jenkinsci.plugins.scriptler.ScriptlerManagement;
2526
import org.jenkinsci.plugins.scriptler.config.Parameter;
2627
import org.jenkinsci.plugins.scriptler.config.Script;
@@ -170,7 +171,7 @@ public static String runScript(String node, String scriptTxt, @NonNull Collectio
170171
try {
171172
Computer comp = Jenkins.get().getComputer(node);
172173
TaskListener listener = new StreamTaskListener(sos, StandardCharsets.UTF_8);
173-
if (comp == null && "(controller)".equals(node)) {
174+
if (comp == null && NodeNames.BUILT_IN.equals(node)) {
174175
FilePath.localChannel.call(new GroovyScript(scriptTxt, parameters, false, listener));
175176
} else if (comp != null && comp.getChannel() != null) {
176177
comp.getChannel().call(new GroovyScript(scriptTxt, parameters, false, listener));

src/main/resources/org/jenkinsci/plugins/scriptler/Messages.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# THE SOFTWARE.
2222

2323
display_name=Scriptler
24-
description=Store/edit/run scripts on any of the agents or the controller.
24+
description=Store/edit/run scripts on any of the agents or the built-in node.
2525
scriptdirectorytitle=The scripts are stored at:
2626
not_groovy_script=this is not a groovy script: {0}
2727
script_loaded_from_directory=this script was discovered on the file system, please describe it!

src/main/resources/org/jenkinsci/plugins/scriptler/ScriptlerManagement/edit.jelly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ THE SOFTWARE.
4646
<f:checkbox name="nonAdministerUsing" checked="${script.nonAdministerUsing}" />
4747
</f:entry>
4848
<f:entry title="${%Restriction}" description="${%RestrictionDescription}">
49-
<f:checkbox name="onlyController" checked="${script.onlyController}" />
49+
<f:checkbox name="onlyBuiltIn" checked="${script.onlyBuiltIn}" />
5050
</f:entry>
5151
<f:block>
5252
<div>

0 commit comments

Comments
 (0)