Skip to content

Commit

Permalink
Merge pull request #3 from subos2008/sboschman-master
Browse files Browse the repository at this point in the history
Support for a global pool of port numbers
  • Loading branch information
subos2008 authored Apr 12, 2017
2 parents a96a1aa + e56cf06 commit 32f839c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
23 changes: 12 additions & 11 deletions src/main/java/org/jvnet/hudson/plugins/port_allocator/Pool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -54,7 +66,7 @@ private PortAllocator(PortType[] ports) {

@DataBoundConstructor
public PortAllocator() {
// empty
// empty
}

@DataBoundSetter
Expand Down Expand Up @@ -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<String,Integer> prefPortMap = new HashMap<String,Integer>();
Run<?, ?> prevBuild = run.getPreviousBuild();
Expand All @@ -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<String,Integer> portMap = new HashMap<String,Integer>();
final List<Port> allocated = new ArrayList<Port>();

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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<f:entry title="Pool definitions">
<f:repeatable var="pool" items="${descriptor.pools}">
<table width="100%">
<f:entry title="Global" help="/plugin/port-allocator/help-pool-definition-global.html">
<f:checkbox name="pool.global" checked="${pool.global}"/>
</f:entry>
<f:entry title="Name" help="/plugin/port-allocator/help-pool-definition-name.html">
<f:textbox name="pool.name" value="${pool.name}"
checkUrl="'descriptorByName/PortAllocator/checkName?value='+this.value" />
Expand Down
5 changes: 5 additions & 0 deletions src/main/webapp/help-pool-definition-global.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<p>
Should this pool allocate unique port numbers globally or per node.
</p>
</div>

0 comments on commit 32f839c

Please sign in to comment.