diff --git a/automatic/nexus-repository/tools/chocolateyinstall.ps1 b/automatic/nexus-repository/tools/chocolateyinstall.ps1 index 3a73c4a0d3f..59790963ce9 100644 --- a/automatic/nexus-repository/tools/chocolateyinstall.ps1 +++ b/automatic/nexus-repository/tools/chocolateyinstall.ps1 @@ -55,31 +55,18 @@ if ($pp.ContainsKey("BackupSslConfig") -or $CurrentConfig.'application-port-ssl' } } -if ($CurrentlyInstalledVersion -and [version]($CurrentlyInstalledVersion -replace '-\d+$') -lt "3.70") { - Write-Error (@( - "Please upgrade nexus-repository to version 3.70.1-02 before upgrading further." - "You can do this by running 'choco upgrade nexus-repository --version 3.70.1.2 --confirm'" - "For more details, please see: https://help.sonatype.com/en/orient-pre-3-70-java-8-or-11.html" - ) -join "`n") - throw "Package cannot upgrade from '$($CurrentlyInstalledVersion)' to '$($env:ChocolateyPackageVersion)'" -} elseif ($CurrentlyInstalledVersion -and [version]($CurrentlyInstalledVersion -replace '-\d+$') -eq "3.70.1" -and (Test-NexusDatabaseType -Type "OrientDb" -DataDir $TargetDataFolder -ProgramDir $TargetFolder)) { +if (Test-NexusMigratorRequired -DataDir $TargetDataFolder -ProgramDir $TargetFolder) { Write-Host "Preparing for database migration..." $MigrationFiles = Join-Path $ExtractFolder "dbmigration" - if ((Get-PSDrive $MigrationFiles[0]).Free -lt ($RequiredSpace = [Math]::Max((3 * (Get-ChildItem -Path $TargetDataFolder\nexus3\db -Recurse | Measure-Object Length -Sum).Sum), 10GB))) { - Write-Error -Message (@( - "The Sonatype Nexus Database Migrator requires at least $([Math]::Round(($RequiredSpace / 1GB), 2))GB free space." - "There is only $([Math]::Floor((Get-PSDrive $MigrationFiles[0]).Free / 1GB))GB free. For more details, please see: " - "https://help.sonatype.com/en/orient-3-70-java-8-or-11.html#considerations-before-migrating-252166" - ) -join "`n") + + if (Test-NexusMigratorFreeSpaceProblem -Drive $MigrationFiles.Split(':')[0] -DatabaseFolder $TargetDataFolder\nexus3\db) { throw "Cannot migrate database with available disk space" } - if (($Memory = Get-CimInstance Win32_PhysicalMemory).Capacity -lt 16GB) { - Write-Warning -Message (@( - "The Sonatype Nexus Database Migrator requires at least 16GB free space." - "There is only $($Memory.Capacity/1GB)GB available. For more details, please see: " - "https://help.sonatype.com/en/orient-3-70-java-8-or-11.html#considerations-before-migrating-252166" - ) -join "`n") + + if (Test-NexusMigratorFreeSpaceProblem) { + throw "Cannot migrate database with available memory" } + New-NexusOrientDbBackup -DataDir $TargetDataFolder -ServiceName $ServiceName -DestinationPath $MigrationFiles # See: https://help.sonatype.com/en/orientdb-downloads.html diff --git a/automatic/nexus-repository/tools/helpers.ps1 b/automatic/nexus-repository/tools/helpers.ps1 index 57066ec0292..ee86703d535 100644 --- a/automatic/nexus-repository/tools/helpers.ps1 +++ b/automatic/nexus-repository/tools/helpers.ps1 @@ -133,7 +133,7 @@ function Get-NexusVersion { [string]$ProgramDir = (Join-Path $env:ProgramData 'nexus') ) # This isn't a very appropriate file to target. - ([xml](Get-Content (Join-Path $ProgramDir "\.install4j\i4jparams.conf"))).config.general.applicationVersion + ([xml](Get-Content (Join-Path $ProgramDir "\.install4j\i4jparams.conf") -ErrorAction SilentlyContinue)).config.general.applicationVersion } function Test-NexusDatabaseType { @@ -169,8 +169,11 @@ function Test-NexusDatabaseType { "PostgreSQL" } } else { - # No external database found - probably... - "OrientDb" + if (Test-Path $DataDir\nexus3\db\nexus.mv.db) { + "H2" + } else { + "OrientDb" + } } $Type -eq $FoundType @@ -282,4 +285,130 @@ function Compress-ArchiveCompat { end { $Archive.Dispose() } +} + +function Test-NexusMigratorRequired { + <# + .Synopsis + Tests if we need to migrate this system's database + + .Example + Test-NexusMigratorRequired -DataDir $TargetDataFolder -ProgramDir $TargetFolder + # Returns if a migration is required, based on the existing version and database. + #> + [OutputType([Nullable[bool]])] + [CmdletBinding()] + param( + # The installed version of Nexus + $CurrentVersion = (Get-NexusVersion -ProgramDir $ProgramDir), + + # The only version of Nexus we should be upgrading from + $RequiredVersion = "3.70.1", + + # Nexus' working directory + $DataDir = (Join-Path $env:ProgramData 'sonatype-work'), + + # Nexus' program directory + $ProgramDir = (Join-Path $env:ProgramData 'nexus') + ) + if ($CurrentVersion) { + [version]$CurrentDeNexusVersion = $CurrentVersion -replace '-\d+$' + + if ($CurrentDeNexusVersion -lt $RequiredVersion) { + Write-Error (@( + "Please upgrade nexus-repository to version 3.70.1-02 before upgrading further." + "You can do this by running 'choco upgrade nexus-repository --version 3.70.1.2 --confirm'" + "For more details, please see: https://help.sonatype.com/en/orient-pre-3-70-java-8-or-11.html" + ) -join "`n") + throw "Package cannot upgrade from '$($CurrentVersion)' to '$($env:ChocolateyPackageVersion)'" + } elseif ($CurrentDeNexusVersion -eq $RequiredVersion) { + # We will upgrade if we are on OrientDb, otherwise leave it alone. + Test-NexusDatabaseType -Type "OrientDb" -DataDir $DataDir -ProgramDir $ProgramDir + } + } +} + +function Test-NexusMigratorFreeSpaceProblem { + <# + .Synopsis + Tests to see if we don't have more free space than we think we require. + + .Example + Test-NexusMigratorFreeSpaceProblem -FreeSpace 1GB -RequiredSpace 5GB + # Should be a problem, because 1GB is less than 5GB + + .Example + Test-NexusMigratorFreeSpaceProblem -Drive C -DatabaseFolder $env:ProgramData\sonatype-work\nexus3\db + # Will test for the values found. + #> + [OutputType([bool])] + [CmdletBinding()] + param( + # The name of the drive we care about. + [Parameter(Mandatory, ParameterSetName="Location")] + [string]$Drive, + + # The folder the database lives in + [Parameter(Mandatory, ParameterSetName="Location")] + [string]$DatabaseFolder, + + # This is the free space on the drive that we'll be manipulating the migration files. + [Parameter(Mandatory, ParameterSetName="Values")] + $FreeSpace = (Get-PSDrive $Drive).Free, + + # The migrator requires 3 times the size of the database files, or 10GB - whichever is larger. + [Parameter(Mandatory, ParameterSetName="Values")] + $RequiredSpace = $( + [Math]::Max( + (3 * (Get-ChildItem -Path $DatabaseFolder -Recurse | Measure-Object Length -Sum).Sum), + 10GB + ) + ) + ) + $Result = $FreeSpace -lt $RequiredSpace + + if ($Result) { + Write-Error -Message (@( + "The Sonatype Nexus Database Migrator requires at least $([Math]::Round(($RequiredSpace / 1GB), 2))GB free space." + "There is only $([Math]::Floor($FreeSpace / 1GB))GB free. For more details, please see: " + "https://help.sonatype.com/en/orient-3-70-java-8-or-11.html#considerations-before-migrating-252166" + ) -join "`n") + } + + return $Result +} + +function Test-NexusMigratorMemoryProblem { + <# + .Synopsis + Tests to see if we don't have enough memory for the migration. + + .Example + Test-NexusMigratorMemoryProblem -Memory 10GB -Requirement 5GB + # Should be fine, no problem. + + .Example + Test-NexusMigratorMemoryProblem + # Tests the expected values against the existing memory on this system. + #> + [OutputType([bool])] + [CmdletBinding()] + param( + # The current amount of memory, in bytes. + $Memory = (Get-CimInstance Win32_PhysicalMemory).Capacity, + + # The migrator requires 16GB of memory. + $Requirement = 16GB + ) + $Result = $Memory -lt $Requirement + + if ($Result) { + Write-Error -Message (@( + "The Sonatype Nexus Database Migrator requires at least 16GB free space." + "There is only $($Memory/1GB)GB available. For more details, please see: " + "https://help.sonatype.com/en/orient-3-70-java-8-or-11.html#considerations-before-migrating-252166" + ) -join "`n") + } + + return $Result } \ No newline at end of file