Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand All @@ -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<String,Integer> prefPortMap = new HashMap<String,Integer>();
if (build.getPreviousBuild() != null) {
Expand All @@ -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<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(build, pam, prefPort, launcher, listener);
Port p = pt.allocate(build, global ? mpam : pam, prefPort, launcher, listener);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

My main concern with this PR is that we are "allocating" ports on slaves only checking its availability in the master. I may have misunderstood the use case, or maybe it not in the scope of this plugin.

Please open a JIRA issue and/or a mailing list thread to elaborate.

Thanks.

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 @@ -8,6 +8,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>