This commit is contained in:
@@ -373,6 +373,7 @@ cp "${SCRIPT_DIR}/deploy/maintenance-status.sh" "${TARGET_DIR}/scripts/maintenan
|
||||
cp "${SCRIPT_DIR}/deploy/jenkins-inbound-agent-start.sh" "${TARGET_DIR}/scripts/jenkins-inbound-agent-start.sh"
|
||||
cp "${SCRIPT_DIR}/deploy/install-jenkins-inbound-agent.sh" "${TARGET_DIR}/scripts/install-jenkins-inbound-agent.sh"
|
||||
cp "${SCRIPT_DIR}/deploy/jenkins-agent-reverse-tunnel.ps1" "${TARGET_DIR}/scripts/jenkins-agent-reverse-tunnel.ps1"
|
||||
cp "${SCRIPT_DIR}/deploy/jenkins-local-controller-watchdog.ps1" "${TARGET_DIR}/scripts/jenkins-local-controller-watchdog.ps1"
|
||||
chmod +x \
|
||||
"${TARGET_DIR}/scripts/maintenance-on.sh" \
|
||||
"${TARGET_DIR}/scripts/maintenance-off.sh" \
|
||||
|
||||
95
scripts/deploy/jenkins-local-controller-watchdog.ps1
Normal file
95
scripts/deploy/jenkins-local-controller-watchdog.ps1
Normal file
@@ -0,0 +1,95 @@
|
||||
param(
|
||||
[string]$JavaPath = "$env:USERPROFILE\jenkins-local\jdk-21\jdk-21.0.11+10\bin\java.exe",
|
||||
[string]$JenkinsWar = "$env:USERPROFILE\jenkins-local\jenkins.war",
|
||||
[string]$JenkinsHome = "$env:USERPROFILE\.jenkins",
|
||||
[int]$HttpPort = 8080,
|
||||
[int]$AgentPort = 50000,
|
||||
[int]$RestartDelaySeconds = 10
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
function Write-Log {
|
||||
param([string]$Message)
|
||||
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
Write-Output "[$timestamp] $Message"
|
||||
}
|
||||
|
||||
function Get-ListeningProcessId {
|
||||
param([int]$Port)
|
||||
$line = netstat -ano | Select-String -Pattern "LISTENING\s+(\d+)$" | Where-Object {
|
||||
$_.Line -match "[:.]$Port\s+"
|
||||
} | Select-Object -First 1
|
||||
|
||||
if (-not $line) {
|
||||
return $null
|
||||
}
|
||||
|
||||
if ($line.Line -match "LISTENING\s+(\d+)$") {
|
||||
return [int]$Matches[1]
|
||||
}
|
||||
|
||||
return $null
|
||||
}
|
||||
|
||||
function Test-JenkinsProcess {
|
||||
param([int]$ProcessId)
|
||||
if (-not $ProcessId) {
|
||||
return $false
|
||||
}
|
||||
|
||||
$process = Get-CimInstance Win32_Process -Filter "ProcessId = $ProcessId" -ErrorAction SilentlyContinue
|
||||
if (-not $process) {
|
||||
return $false
|
||||
}
|
||||
|
||||
return ($process.CommandLine -like "*jenkins.war*")
|
||||
}
|
||||
|
||||
if (-not (Test-Path -LiteralPath $JavaPath)) {
|
||||
throw "Java path not found: $JavaPath"
|
||||
}
|
||||
|
||||
if (-not (Test-Path -LiteralPath $JenkinsWar)) {
|
||||
throw "Jenkins war not found: $JenkinsWar"
|
||||
}
|
||||
|
||||
New-Item -ItemType Directory -Force -Path $JenkinsHome | Out-Null
|
||||
|
||||
while ($true) {
|
||||
$listeningPid = Get-ListeningProcessId -Port $HttpPort
|
||||
|
||||
if ($listeningPid -and (Test-JenkinsProcess -ProcessId $listeningPid)) {
|
||||
Write-Log "Jenkins is already listening on port $HttpPort with pid $listeningPid; monitoring it."
|
||||
try {
|
||||
Wait-Process -Id $listeningPid
|
||||
} catch {
|
||||
Write-Log "Existing Jenkins process wait failed: $($_.Exception.Message)"
|
||||
}
|
||||
} elseif ($listeningPid) {
|
||||
Write-Log "Port $HttpPort is occupied by pid $listeningPid, but it is not Jenkins. Retrying in ${RestartDelaySeconds}s."
|
||||
Start-Sleep -Seconds $RestartDelaySeconds
|
||||
continue
|
||||
} else {
|
||||
$arguments = @(
|
||||
"-Djenkins.install.runSetupWizard=false",
|
||||
"-jar", $JenkinsWar,
|
||||
"--httpPort=$HttpPort",
|
||||
"--agentPort=$AgentPort"
|
||||
)
|
||||
|
||||
Write-Log "Starting local Jenkins controller on port $HttpPort."
|
||||
$previousJenkinsHome = $env:JENKINS_HOME
|
||||
$env:JENKINS_HOME = $JenkinsHome
|
||||
$process = Start-Process -FilePath $JavaPath -ArgumentList $arguments -WorkingDirectory (Split-Path -Parent $JenkinsWar) -NoNewWindow -PassThru
|
||||
$env:JENKINS_HOME = $previousJenkinsHome
|
||||
try {
|
||||
Wait-Process -Id $process.Id
|
||||
} catch {
|
||||
Write-Log "Started Jenkins process wait failed: $($_.Exception.Message)"
|
||||
}
|
||||
}
|
||||
|
||||
Write-Log "Jenkins controller stopped; retrying in ${RestartDelaySeconds}s."
|
||||
Start-Sleep -Seconds $RestartDelaySeconds
|
||||
}
|
||||
Reference in New Issue
Block a user