diff --git a/src/main/java/org/jvnet/hudson/plugins/port_allocator/Pool.java b/src/main/java/org/jvnet/hudson/plugins/port_allocator/Pool.java index 4d04539..14f4a32 100644 --- a/src/main/java/org/jvnet/hudson/plugins/port_allocator/Pool.java +++ b/src/main/java/org/jvnet/hudson/plugins/port_allocator/Pool.java @@ -6,20 +6,21 @@ * @author pepov */ public class Pool { - - public String name; - public String ports; - public int[] getPortsAsInt() { + public boolean global; + public String name; + public String ports; - String[] portsItemsAsString = ports.split(","); + public int[] getPortsAsInt() { - int[] portsItems = new int[portsItemsAsString.length]; + String[] portsItemsAsString = ports.split(","); - for (int i = 0; i < portsItemsAsString.length; i++) { - portsItems[i] = Integer.parseInt(portsItemsAsString[i]); - } + int[] portsItems = new int[portsItemsAsString.length]; - return portsItems; - } + for (int i = 0; i < portsItemsAsString.length; i++) { + portsItems[i] = Integer.parseInt(portsItemsAsString[i]); + } + + return portsItems; + } } diff --git a/src/main/java/org/jvnet/hudson/plugins/port_allocator/PortAllocator.java b/src/main/java/org/jvnet/hudson/plugins/port_allocator/PortAllocator.java index ccdb062..75e05de 100644 --- a/src/main/java/org/jvnet/hudson/plugins/port_allocator/PortAllocator.java +++ b/src/main/java/org/jvnet/hudson/plugins/port_allocator/PortAllocator.java @@ -4,11 +4,28 @@ import hudson.Extension; import hudson.FilePath; import hudson.Launcher; -import hudson.model.*; +import hudson.model.BuildListener; +import hudson.model.AbstractBuild; +import hudson.model.Computer; +import hudson.model.Descriptor; +import hudson.model.Executor; import hudson.tasks.BuildWrapper; import jenkins.tasks.SimpleBuildWrapper; import hudson.util.FormValidation; + +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; + +import jenkins.model.Jenkins; +import jenkins.model.Jenkins.MasterComputer; import net.sf.json.JSONObject; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jenkinsci.Symbol; @@ -19,11 +36,6 @@ import com.google.common.collect.Lists; -import java.io.IOException; -import java.io.PrintStream; -import java.util.*; -import java.util.regex.Pattern; - /** * Allocates TCP Ports on a Computer for consumption and sets it as * environment variables, see configuration @@ -54,7 +66,7 @@ private PortAllocator(PortType[] ports) { @DataBoundConstructor public PortAllocator() { - // empty + // empty } @DataBoundSetter @@ -102,6 +114,7 @@ public String[] getPlainports() { public void setUp(Context context, Run run, FilePath workspace, Launcher launcher, TaskListener taskListener, EnvVars envVars) throws IOException, InterruptedException { PrintStream logger = taskListener.getLogger(); + Computer master = Jenkins.getInstance().toComputer(); Computer cur = workspace.toComputer(); Map prefPortMap = new HashMap(); Run prevBuild = run.getPreviousBuild(); @@ -112,14 +125,22 @@ public void setUp(Context context, Run run, FilePath workspace, Launcher l prefPortMap = prevAlloc.getPreviousAllocatedPorts(); } } + final PortAllocationManager mpam = PortAllocationManager.getManager(master); final PortAllocationManager pam = PortAllocationManager.getManager(cur); Map portMap = new HashMap(); final List allocated = new ArrayList(); for (PortType pt : ports) { - logger.println("Allocating TCP port "+pt.name); + boolean global = false; + try { + Pool pool = PortAllocator.DESCRIPTOR.getPoolByName(pt.name); + global = pool.global; + } catch (PoolNotDefinedException e) { + // ignore, global = false + } + logger.println("Allocating TCP port "+pt.name + (global ? " (global)" : "")); int prefPort = prefPortMap.get(pt.name)== null?0:prefPortMap.get(pt.name); - Port p = pt.allocate(run, pam, prefPort, launcher, taskListener); + Port p = pt.allocate(run, global ? mpam : pam, prefPort, launcher, taskListener); allocated.add(p); portMap.put(pt.name, p.get()); logger.println(" -> Assigned "+p.get()); diff --git a/src/main/resources/org/jvnet/hudson/plugins/port_allocator/PortAllocator/global.jelly b/src/main/resources/org/jvnet/hudson/plugins/port_allocator/PortAllocator/global.jelly index dd12560..d5796a1 100644 --- a/src/main/resources/org/jvnet/hudson/plugins/port_allocator/PortAllocator/global.jelly +++ b/src/main/resources/org/jvnet/hudson/plugins/port_allocator/PortAllocator/global.jelly @@ -9,6 +9,9 @@ + + + diff --git a/src/main/webapp/help-pool-definition-global.html b/src/main/webapp/help-pool-definition-global.html new file mode 100644 index 0000000..aa722aa --- /dev/null +++ b/src/main/webapp/help-pool-definition-global.html @@ -0,0 +1,5 @@ +
+

+ Should this pool allocate unique port numbers globally or per node. +

+
\ No newline at end of file