Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement workflow support #10

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
54 changes: 51 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.424</version>
<version>2.4</version>
</parent>

<artifactId>port-allocator</artifactId>
<packaging>hpi</packaging>
<version>1.9-SNAPSHOT</version>
Expand All @@ -31,6 +30,11 @@
</developer>
</developers>

<properties>
<jenkins.version>2.7.1</jenkins.version>
<workflow.version>1.8</workflow.version>
</properties>

<scm>
<connection>scm:git:git://github.com/jenkinsci/port-allocator-plugin.git</connection>
<developerConnection>scm:git:ssh://[email protected]/jenkinsci/port-allocator-plugin.git</developerConnection>
Expand Down Expand Up @@ -59,6 +63,50 @@
</pluginRepositories>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency> <!-- StepConfigTester -->
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>${workflow.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency> <!-- JenkinsRuleExt -->
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-aggregator</artifactId>
<version>${workflow.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.modules</groupId>
<artifactId>sshd</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
Expand All @@ -67,4 +115,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.jvnet.hudson.plugins.port_allocator;

import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.TaskListener;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
Expand All @@ -22,13 +23,15 @@ public DefaultPortType(String name) {
}

@Override
public Port allocate(AbstractBuild<?, ?> build, final PortAllocationManager manager, int prefPort, Launcher launcher, BuildListener buildListener) throws IOException, InterruptedException {
public Port allocate(Run<?, ?> run, final PortAllocationManager manager, int prefPort, Launcher launcher, TaskListener taskListener)
throws IOException, InterruptedException
{
final int n;
if(isFixedPort())
n = manager.allocate(build, getFixedPort());
n = manager.allocate(run, getFixedPort());
else
n = manager.allocateRandom(build, prefPort);
n = manager.allocateRandom(run, prefPort);

return new Port(this) {
public int get() {
return n;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.jvnet.hudson.plugins.port_allocator;

import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.remoting.Callable;
import net.sf.json.JSONObject;
import org.jenkinsci.remoting.RoleChecker;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

Expand All @@ -24,12 +25,14 @@
import java.rmi.UnmarshalException;
import java.util.HashMap;
import java.util.Map;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
* GlassFish JMX port so that runaway GF instance can be terminated.
*
*
* @author Kohsuke Kawaguchi
*/
@SuppressFBWarnings(value="SE_INNER_CLASS")
public class GlassFishJmxPortType extends PortType {
/**
* GlassFish admin user name.
Expand All @@ -47,22 +50,25 @@ public GlassFishJmxPortType(String name, String userName, String password) {
this.password = password;
}


@Override
public Port allocate(AbstractBuild<?, ?> build, final PortAllocationManager manager, int prefPort, final Launcher launcher, final BuildListener buildListener) throws IOException, InterruptedException {
public Port allocate(Run<?, ?> run, final PortAllocationManager manager, int prefPort, final Launcher launcher, final TaskListener taskListener)
throws IOException, InterruptedException
{
final int n;
if(isFixedPort())
n = manager.allocate(build, getFixedPort());
n = manager.allocate(run, getFixedPort());
else
n = manager.allocateRandom(build, prefPort);
n = manager.allocateRandom(run, prefPort);

/**
* Cleans up GlassFish instance.
*/
final class GlassFishCleanUpTask implements Callable<Void,IOException>, Serializable {
private final BuildListener buildListener;
class GlassFishCleanUpTask implements Callable<Void,IOException>, Serializable {
private final TaskListener taskListener;

public GlassFishCleanUpTask(BuildListener buildListener) {
this.buildListener = buildListener;
public GlassFishCleanUpTask(TaskListener taskListener) {
this.taskListener = taskListener;
}

public Void call() throws IOException {
Expand All @@ -87,23 +93,27 @@ public Void call() throws IOException {
} catch (UnmarshalException e) {
if(e.getCause() instanceof SocketException || e.getCause() instanceof IOException) {
// to be expected, as the above would shut down the server.
buildListener.getLogger().println("GlassFish was shut down");
taskListener.getLogger().println("GlassFish was shut down");
} else {
throw e;
}
} catch (MalformedObjectNameException e) {
throw new AssertionError(e); // impossible
} catch (InstanceNotFoundException e) {
e.printStackTrace(buildListener.error("Unable to find J2EEServer mbean"));
e.printStackTrace(taskListener.error("Unable to find J2EEServer mbean"));
} catch (ReflectionException e) {
e.printStackTrace(buildListener.error("Unable to access J2EEServer mbean"));
e.printStackTrace(taskListener.error("Unable to access J2EEServer mbean"));
} catch (MBeanException e) {
e.printStackTrace(buildListener.error("Unable to call J2EEServer mbean"));
e.printStackTrace(taskListener.error("Unable to call J2EEServer mbean"));
}
return null;
}

private static final long serialVersionUID = 1L;

@Override
public void checkRoles(RoleChecker roleChecker) throws SecurityException {
}
}

return new Port(this) {
Expand All @@ -113,7 +123,7 @@ public int get() {

public void cleanUp() throws IOException, InterruptedException {
manager.free(n);
launcher.getChannel().call(new GlassFishCleanUpTask(buildListener));
launcher.getChannel().call(new GlassFishCleanUpTask(taskListener));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
/**
* @author pepov
*/
public class PoolNotDefinedException extends Throwable {
public class PoolNotDefinedException extends Exception {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.ListBoxModel;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
Expand All @@ -30,23 +31,14 @@ public PooledPortType(String name) {
* Wait for a short period if no free port is available, then try again.
*/
@Override
public Port allocate(
AbstractBuild<?, ?> build,
final PortAllocationManager manager,
int prefPort,
Launcher launcher,
BuildListener buildListener
) throws IOException, InterruptedException {

public Port allocate(Run<?, ?> run, PortAllocationManager manager, int prefPort, Launcher launcher, TaskListener taskListener) throws IOException, InterruptedException {
try {
while (true) {

Pool pool = PortAllocator.DESCRIPTOR.getPoolByName(name);

synchronized (pool) {
for (int port : pool.getPortsAsInt()) {
if (manager.isFree(port)) {
manager.allocate(build, port);
manager.allocate(run, port);
return new PooledPort(this, port, manager);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.jvnet.hudson.plugins.port_allocator;

import java.io.IOException;
import java.io.Serializable;

/**
* Represents an assigned TCP port and encapsulates how it should be cleaned up.
*
* @author Kohsuke Kawaguchi
*/
public abstract class Port {
public abstract class Port implements Serializable {
/**
* {@link PortType} that created this port.
*/
Expand Down
Loading