1+ package com.plugin.${javaPluginClass.toLowerCase()};
2+
3+ import com.dtolabs.rundeck.core.common.INodeEntry;
4+ import com.dtolabs.rundeck.core.execution.workflow.StepExecutionContext;
5+ import com.dtolabs.rundeck.core.execution.workflow.steps.node.NodeStepResult;
6+ import com.dtolabs.rundeck.plugins.orchestrator.Orchestrator;
7+
8+ import java.util.ArrayList;
9+ import java.util.Collection;
10+ import java.util.List;
11+ import java.util.Random;
12+
13+ /**
14+ * Selects a random subset of the nodes
15+ */
16+ public class ${javaPluginClass}Orchestrator implements Orchestrator {
17+
18+ Random random;
19+ final int count;
20+ List<INodeEntry> nodes;
21+
22+ public ${javaPluginClass}Orchestrator(
23+ int count,
24+ StepExecutionContext context,
25+ Collection<INodeEntry> nodes
26+ )
27+ {
28+ this.random = new Random();
29+ this.count = count;
30+ this.nodes = select(count, nodes);
31+ }
32+
33+ /**
34+ * Select count random items from the input nodes, or if nodes is smaller than count, reorders them
35+ * @param count number of nodes
36+ * @param nodes input nodes
37+ * @return list of count nodes
38+ */
39+ private List<INodeEntry> select(final int count, final Collection<INodeEntry> nodes) {
40+ List<INodeEntry> source = new ArrayList<>(nodes);
41+ List<INodeEntry> selected = new ArrayList<>();
42+ int total = Math.min(count, nodes.size());
43+ for (int i = 0; i < total; i++) {
44+ selected.add(source.remove(random.nextInt(source.size())));
45+ }
46+ return selected;
47+ }
48+
49+
50+ @Override
51+ public INodeEntry nextNode() {
52+ if (nodes.size() > 0) {
53+ return nodes.remove(0);
54+ } else {
55+ return null;
56+ }
57+ }
58+
59+ @Override
60+ public void returnNode( final INodeEntry node, final boolean success, final NodeStepResult result)
61+ {
62+
63+ }
64+
65+ @Override
66+ public boolean isComplete() {
67+ return nodes.size() == 0;
68+ }
69+ }
0 commit comments