Skip to content

Commit

Permalink
[PATCH] Merge pull request jenkinsci/docker-inbound-agent#227 from Vl…
Browse files Browse the repository at this point in the history
…atombe/windows_javaopts

Allows to pass JAVA_OPTS to the JVM on windows
  • Loading branch information
Vlatombe authored and lemeurherve committed Dec 10, 2022
1 parent dccea8d commit c4318f3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
23 changes: 19 additions & 4 deletions jenkins-agent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

[CmdletBinding()]
Param(
$Cmd = '', # this is only used when docker run has one arg positional arg
$Cmd = '', # this must be specified explicitly
$Url = $( if([System.String]::IsNullOrWhiteSpace($Cmd) -and [System.String]::IsNullOrWhiteSpace($env:JENKINS_URL)) { throw ("Url is required") } else { '' } ),
[Parameter(Position=0)]$Secret = $( if([System.String]::IsNullOrWhiteSpace($Cmd) -and [System.String]::IsNullOrWhiteSpace($env:JENKINS_SECRET)) { throw ("Secret is required") } else { '' } ),
[Parameter(Position=1)]$Name = $( if([System.String]::IsNullOrWhiteSpace($Cmd) -and [System.String]::IsNullOrWhiteSpace($env:JENKINS_AGENT_NAME)) { throw ("Name is required") } else { '' } ),
Expand All @@ -33,12 +33,14 @@ Param(
$InstanceIdentity = '',
$Protocols = '',
$JenkinsJavaBin = '',
$JavaHome = $env:JAVA_HOME
$JavaHome = $env:JAVA_HOME,
$JenkinsJavaOpts = ''
)

# Usage jenkins-agent.ps1 [options] -Url http://jenkins -Secret [SECRET] -Name [AGENT_NAME]
# Optional environment variables :
# * JENKINS_JAVA_BIN : Java executable to use instead of the default in PATH or obtained from JAVA_HOME
# * JENKINS_JAVA_OPTS : Java Options to use for the remoting process, otherwise obtained from JAVA_OPTS
# * JENKINS_TUNNEL : HOST:PORT for a tunnel to route TCP traffic to jenkins host, when jenkins can't be directly accessed over network
# * JENKINS_URL : alternate jenkins URL
# * JENKINS_SECRET : agent secret, if not set as an argument
Expand All @@ -54,11 +56,11 @@ Param(
if(![System.String]::IsNullOrWhiteSpace($Cmd)) {
Invoke-Expression "$Cmd"
} else {
$AgentArguments = @("-cp", "C:/ProgramData/Jenkins/agent.jar", "hudson.remoting.jnlp.Main", "-headless")

# this maps the variable name from the CmdletBinding to environment variables
$ParamMap = @{
'JenkinsJavaBin' = 'JENKINS_JAVA_BIN';
'JenkinsJavaOpts' = 'JENKINS_JAVA_OPTS';
'Tunnel' = 'JENKINS_TUNNEL';
'Url' = 'JENKINS_URL';
'Secret' = 'JENKINS_SECRET';
Expand Down Expand Up @@ -91,6 +93,19 @@ if(![System.String]::IsNullOrWhiteSpace($Cmd)) {
}
}

$AgentArguments = @()

if(![System.String]::IsNullOrWhiteSpace($JenkinsJavaOpts)) {
# this magic will basically process the $JenkinsJavaOpts like a command line
# and split into an array, the command line processing follows the PowerShell
# commnd line processing, which means for things like -Dsomething.something=something,
# you need to quote the string like this: "-Dsomething.something=something" or else it
# will get parsed incorrectly.
$AgentArguments += Invoke-Expression "echo $JenkinsJavaOpts"
}

$AgentArguments += @("-cp", "C:/ProgramData/Jenkins/agent.jar", "hudson.remoting.jnlp.Main", "-headless")

if(![System.String]::IsNullOrWhiteSpace($Tunnel)) {
$AgentArguments += @("-tunnel", "`"$Tunnel`"")
}
Expand All @@ -116,7 +131,7 @@ if(![System.String]::IsNullOrWhiteSpace($Cmd)) {
if(![System.String]::IsNullOrWhiteSpace($InstanceIdentity)) {
$AgentArguments += @('-instanceIdentity', $InstanceIdentity)
}

if(![System.String]::IsNullOrWhiteSpace($Protocols)) {
$AgentArguments += @('-protocols', $Protocols)
}
Expand Down
35 changes: 34 additions & 1 deletion tests/inboundAgent.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ Describe "[$global:JDK $global:FLAVOR] build image" {

It 'builds image' {
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "build --build-arg VERSION=$global:VERSION -t $global:AGENT_IMAGE $global:FOLDER"
# This failure was added on purpose to verify that the build will fail if a test fails
$exitCode | Should -Be 0
}

Expand Down Expand Up @@ -172,3 +171,37 @@ Describe "[$global:JDK $global:FLAVOR] build args" {
Pop-Location -StackName 'agent'
}
}


Describe "[$global:JDK $global:FLAVOR] passing JVM options (slow test)" {
It "shows the java version with --show-version" {
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "network create --driver nat jnlp-network"
# Launch the netcat utility, listening at port 5000 for 30 sec
# bats will capture the output from netcat and compare the first line
# of the header of the first HTTP request with the expected one
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -dit --name nmap --network=jnlp-network nmap:latest ncat.exe -w 30 -l 5000"
$exitCode | Should -Be 0
Is-ContainerRunning "nmap" | Should -BeTrue

# get the ip address of the nmap container
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "inspect -f `"{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}`" nmap"
$exitCode | Should -Be 0
$nmap_ip = $stdout.Trim()

# run Jenkins agent which tries to connect to the nmap container at port 5000
$secret = "aaa"
$name = "bbb"
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "run -dit --network=jnlp-network --name $global:AGENT_CONTAINER $global:AGENT_IMAGE -Url http://${nmap_ip}:5000 -JenkinsJavaOpts `"--show-version`" $secret $name"
$exitCode | Should -Be 0
Is-ContainerRunning $global:AGENT_CONTAINER | Should -BeTrue
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "logs $global:AGENT_CONTAINER"
$exitCode | Should -Be 0
$stdout | Should -Match "OpenJDK Runtime Environment Temurin-${global:JDK}"
}

AfterAll {
Cleanup($global:AGENT_CONTAINER)
Cleanup("nmap")
CleanupNetwork("jnlp-network")
}
}

0 comments on commit c4318f3

Please sign in to comment.