From fd3234798e20844792da159977c90ea2c1008af0 Mon Sep 17 00:00:00 2001 From: Sverre Boschman Date: Thu, 19 Feb 2015 13:02:49 +0100 Subject: [PATCH] Support for a global pool --- .../hudson/plugins/port_allocator/Pool.java | 23 +++++------ .../plugins/port_allocator/PortAllocator.java | 39 ++++++++++++++----- .../port_allocator/PortAllocator/global.jelly | 3 ++ .../webapp/help-pool-definition-global.html | 5 +++ 4 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 src/main/webapp/help-pool-definition-global.html 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 bc12a32..2ef8f1a 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 @@ -2,20 +2,32 @@ import hudson.Extension; 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 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.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; -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 @@ -39,7 +51,8 @@ private PortAllocator(PortType[] ports){ @Override public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException { PrintStream logger = listener.getLogger(); - + + final Computer master = Jenkins.getInstance().toComputer(); final Computer cur = Executor.currentExecutor().getOwner(); Map prefPortMap = new HashMap(); if (build.getPreviousBuild() != null) { @@ -49,14 +62,22 @@ public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener 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(build, pam, prefPort, launcher, listener); + Port p = pt.allocate(build, global ? mpam : pam, prefPort, launcher, listener); 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 2c4e71c..3a67e46 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 @@ -8,6 +8,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