Skip to content

Commit

Permalink
(nexus-repository) Improves Parameter Handling
Browse files Browse the repository at this point in the history
Sources all connection variables in Wait-NexusAvailability (other than Hostname) from the on-disk configuration.

Also automatically backs-up and restores the port and SSL configuration if they are defined in the configuration.
  • Loading branch information
JPRuskin committed Aug 14, 2024
1 parent f4ba836 commit b645a6d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
15 changes: 11 additions & 4 deletions automatic/nexus-repository/tools/chocolateyinstall.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$ErrorActionPreference = 'Stop'
$ErrorActionPreference = 'Stop'
$toolsDir = Split-Path $MyInvocation.MyCommand.Definition
. $toolsDir\helpers.ps1

Expand All @@ -16,6 +16,7 @@ $ServiceName = 'nexus'

# Handle Package Parameters
$pp = Get-PackageParameters
$CurrentConfig = Get-NexusConfiguration -Path $NexusConfigFile -ErrorAction SilentlyContinue

$Hostname = if ($pp.ContainsKey("Fqdn")) {
$pp["Fqdn"]
Expand All @@ -26,6 +27,12 @@ $Hostname = if ($pp.ContainsKey("Fqdn")) {
$NexusPort = if ($pp.ContainsKey("Port")) {
$pp["Port"]
Write-Host "/Port was used, Nexus will listen on port $($PP['Port'])."
} elseif ($CurrentConfig.'application-port-ssl' -gt 0) {
$CurrentConfig.'application-port-ssl'
Write-Host "Nexus is configured to use application-port-ssl, Nexus will listen on port $($CurrentConfig.'application-port-ssl')"
} elseif ($CurrentConfig.'application-port' -gt 0) {
$CurrentConfig.'application-port'
Write-Host "Nexus is configured to use application-port, Nexus will listen on port $($CurrentConfig.'application-port')"
} else {
"8081"
}
Expand All @@ -40,7 +47,7 @@ if ((Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
Get-Service $ServiceName | Stop-Service -Force
}

if ($pp.ContainsKey("BackupSslConfig")) {
if ($pp.ContainsKey("BackupSslConfig") -or $CurrentConfig.'application-port-ssl' -gt 0) {
if ($pp.ContainsKey("BackupLocation")) {
Backup-NexusSSL -BackupLocation $pp["BackupLocation"]
} else {
Expand Down Expand Up @@ -158,7 +165,7 @@ $processArgs = @{

$null = Start-ChocolateyProcessAsAdmin @processArgs

if ($pp.ContainsKey("BackupSslConfig")) {
if ($pp.ContainsKey("BackupSslConfig") -or $CurrentConfig.'application-port-ssl' -gt 0) {
if ($pp.ContainsKey("BackupLocation")) {
Restore-NexusSSL -BackupLocation $pp['BackupLocation']
} else {
Expand All @@ -179,7 +186,7 @@ if ($NexusPort -ne '8081') {

# Start the service, and wait for the site to become available
if ((Start-Service $ServiceName -PassThru).Status -eq 'Running') {
Wait-NexusAvailability -Hostname $Hostname -Port $NexusPort -Config $NexusConfigFile -SSL:$pp.ContainsKey("BackupSslConfig")
Wait-NexusAvailability -Hostname $Hostname -Config $NexusConfigFile
} else {
Write-Warning "The Nexus Repository service ($ServiceName) did not start."
}
Expand Down
46 changes: 19 additions & 27 deletions automatic/nexus-repository/tools/helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,45 +49,37 @@ function Wait-NexusAvailability {
[Parameter(Mandatory = $true)]
[string]$Hostname,

[Parameter(Mandatory = $true)]
[uint16]$Port,

[Parameter(Mandatory = $true)]
[Parameter(Mandatory)]
[Alias("Config")]
[string]$NexusConfigFile,

[switch]$SSL
[string]$NexusConfigFile = (Join-Path $env:ProgramData "\sonatype-work\nexus3\etc\nexus.properties")
)
# Even though windows reports service is ready - web url will not respond until Nexus is actually ready to serve content
# We need to use this method to collect the port number so we can properly test the website has returned OK.
$nexusScheme, $portConfigLine = if ($SSL) {
# This is to combat Package Internalizer's over-enthusiastic URL matching
('http' + 's'), 'application-port-ssl'
} else {
'http', 'application-port'
}

# As the service is started, this should be present momentarily
$Timer = [System.Diagnostics.Stopwatch]::StartNew()
while (-not ($ConfigPresent = Test-Path $NexusConfigFile) -and $Timer.Elapsed.TotalSeconds -le 60) {
Write-Verbose "Waiting for '$($NexusConfigFile)' to become available ($($Timer.Elapsed.TotalSeconds) seconds waited)..."
Start-Sleep -Seconds 5
}

if ($ConfigPresent) {
$nexusPort = (Get-Content $NexusConfigFile | Where-Object {
$_ -match $portConfigLine
}).Split('=')[-1]
$nexusScheme, $nexusPort, $nexusPath = if ($ConfigPresent) {
$Config = Get-NexusConfiguration -Path $NexusConfigFile

$nexusPath = (Get-Content $NexusConfigFile | Where-Object {
$_ -match "nexus-context-path"
}).Split("=")[-1]
} else {
Write-Warning "Expected Nexus Config file '$($NexusConfigFile)' is not present."
$nexusPath, $nexusPort = '/', $Port
if ($Config.'application-port-ssl' -gt 0) {
# This is to combat Package Internalizer's over-enthusiastic URL matching
('http' + 's')
$Config.'application-port-ssl'
} elseif ($Config.'application-port' -gt 0) {
'http'
$Config.'application-port'
}

$Config.'nexus-context-path'
}

$NexusUri = "$($nexusScheme)://$($hostname):$($nexusPort)$($nexusPath)"
# Set defaults if not present
if (-not $nexusScheme) {$nexusScheme = 'http'}
if (-not $nexusPort) {$nexusPort = '8081'}

$NexusUri = "$($nexusScheme)://$($Hostname):$($nexusPort)$($nexusPath)"

Write-Host "Waiting on Nexus Web UI to be available at '$($NexusUri)'"
while ($Response.StatusCode -ne '200' -and $Timer.Elapsed.TotalMinutes -lt 3) {
Expand Down

0 comments on commit b645a6d

Please sign in to comment.