From edd08259e7f6b38678e198e5beb2dccddc2c6f48 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Mon, 7 Jun 2021 16:51:37 +0200 Subject: [PATCH] Allow exposing HTTP and Agent connection ports via `--httpPort` and `--agentPort` options --- README.adoc | 5 +++++ .../bootstrap/commands/JenkinsLauncherOptions.java | 10 ++++++++++ .../jenkins/jenkinsfile/runner/JenkinsLauncher.java | 12 ++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/README.adoc b/README.adoc index a5e6c1f3..88e2a191 100644 --- a/README.adoc +++ b/README.adoc @@ -161,6 +161,11 @@ Advanced arguments: Also, plugin https://javadoc.jenkins.io/hudson/init/Terminator.html[@Terminator] extensions will not be invoked. It may lead to undefined behavior in the system, including potential data loss. This option is considered safe for the Vanilla package with the default plugin set. +* `--httpPort` - Port for exposing the web server and Jenkins Web UI from Jenkinsfile Runner. + Disabled by default. +* `--agentPort` - Port for connecting inbound Jenkins agents (over JNLP or WebSockets). + Disabled by default. + ==== Running Jenkinsfiles (`run` command) diff --git a/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/commands/JenkinsLauncherOptions.java b/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/commands/JenkinsLauncherOptions.java index cd1a4389..bd526089 100644 --- a/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/commands/JenkinsLauncherOptions.java +++ b/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/commands/JenkinsLauncherOptions.java @@ -1,6 +1,7 @@ package io.jenkins.jenkinsfile.runner.bootstrap.commands; import edu.umd.cs.findbugs.annotations.CheckForNull; +import edu.umd.cs.findbugs.annotations.NonNull; import picocli.CommandLine; import java.io.File; @@ -62,7 +63,16 @@ public class JenkinsLauncherOptions { description = "When a slim packaging is used, points to the library directory which contains payload.jar and setup.jar files") public File libPath; + @CheckForNull + @CommandLine.Option(names = "--httpPort", + description = "Port for exposing the web server and Jenkins Web UI from Jenkinsfile Runner. Disabled by default") + public Integer httpPort; + @CheckForNull + @CommandLine.Option(names = "--agentPort", + description = "Port for connecting inbound Jenkins agents (over JNLP or WebSockets). Disabled by default") + public Integer agentPort; + public String getMirrorURL(String url) { if (this.mirror == null || "".equals(this.mirror.trim())) { return url; diff --git a/setup/src/main/java/io/jenkins/jenkinsfile/runner/JenkinsLauncher.java b/setup/src/main/java/io/jenkins/jenkinsfile/runner/JenkinsLauncher.java index dfa0c6cd..052fd755 100644 --- a/setup/src/main/java/io/jenkins/jenkinsfile/runner/JenkinsLauncher.java +++ b/setup/src/main/java/io/jenkins/jenkinsfile/runner/JenkinsLauncher.java @@ -56,6 +56,12 @@ protected Jenkins newJenkins() throws Exception { final Jenkins j = super.newJenkins(); // Notify the bootstrap about the plugin classloader to be used in its logic // command.setPluginClassloader(j.getPluginManager().uberClassLoader); + + // Configure the agent endpoint + if (command.launcherOptions.agentPort != null) { + j.setSlaveAgentPort(command.launcherOptions.agentPort); + } + return j; } @@ -67,7 +73,9 @@ protected ServletContext createWebServer() throws Exception { final JenkinsLauncherOptions launcherOptions = command.getLauncherOptions(); QueuedThreadPool queuedThreadPool = new QueuedThreadPool(10); - server = new Server(queuedThreadPool); + server = launcherOptions.httpPort != null + ? new Server(launcherOptions.httpPort) + : new Server(queuedThreadPool); WebAppContext context = new WebAppContext(launcherOptions.warDir.getPath(), contextPath); context.setClassLoader(getClass().getClassLoader()); @@ -84,7 +92,7 @@ protected ServletContext createWebServer() throws Exception { server.start(); - localPort = -1; + localPort = launcherOptions.httpPort != null ? launcherOptions.httpPort : -1; String pluginManagerClass = SystemProperties.getString(PluginManager.CUSTOM_PLUGIN_MANAGER); if (pluginManagerClass == null) {