Skip to content

Commit

Permalink
Update Update-TemurinJDKs.ps1
Browse files Browse the repository at this point in the history
fix all issues
  • Loading branch information
rajendarreddyj authored Oct 16, 2024
1 parent 633bb77 commit 056519a
Showing 1 changed file with 202 additions and 32 deletions.
234 changes: 202 additions & 32 deletions powershell/Update-TemurinJDKs.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Set the directory where the script is located
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition

# Set the old JDK installation paths (update these as per your environment)
$OldJdkPaths = @{
# Set the JDK installation paths (update these as per your environment)
$JdkPaths = @{
"8" = "C:\apps\java\jdk-8"
"11" = "C:\apps\java\jdk-11"
"17" = "C:\apps\java\jdk-17"
Expand All @@ -25,7 +25,7 @@ if (-not (Test-Path $DownloadDir)) {
}

# Backup cacerts directory
$BackupDir = Join-Path -Path $ScriptDir -ChildPath "cacerts_backup"
$BackupDir = Join-Path -Path $ScriptDir -ChildPath "backup"
if (-not (Test-Path $BackupDir)) {
New-Item -Path $BackupDir -ItemType Directory | Out-Null
}
Expand All @@ -41,12 +41,11 @@ foreach ($version in $OldJdkPaths.Keys) {
}
}

# Function to download, extract, and update JDKs
function Download-Extract-UpdateJDK {
# Function to get the download URL for the latest JDK version
function Get-JDKDownloadUrl {
param (
[string]$Version
)

# Set the API URL to get the latest JDK version
$ApiUrl = "https://api.adoptium.net/v3/assets/latest/$Version/hotspot?release_type=ga&jvm_impl=hotspot&image_type=jdk&os=windows&architecture=x64"

Expand All @@ -58,50 +57,221 @@ function Download-Extract-UpdateJDK {
# Output the entire response for debugging
#Write-Host "API Response: $($response | ConvertTo-Json -Depth 10)"

# Parse the response to find the ZIP package
$downloadUrl = ($response.binary | Where-Object { $_.image_type -eq "jdk" -and $_.package.name -like "*.zip" }).package.link
# Check for a valid download URL for the ZIP package
$jdkPackage = $response | Where-Object { $_.binary.package.name -like "*.zip" }

if (-not $downloadUrl) {
Write-Host "Failed to retrieve download URL for JDK $Version"
return
if ($jdkPackage) {
$downloadUrl = $jdkPackage.binary.package.link
$releaseVersion = $jdkPackage.version.openjdk_version

# Return both the download URL and the release version
return @{
DownloadUrl = $downloadUrl;
# Strip everything after the first '-'
ReleaseVersion = $releaseVersion -replace '[-+].*', ''
}
} else {
Write-Host "No valid ZIP package found for JDK $Version"
return $null
}
}

# Function to check if JDK is already up-to-date
function Is-JDKUpToDate {
param (
[string]$Version,
[string]$CurrentJdkPath,
[string]$LatestVersion
)

if (Test-Path $CurrentJdkPath) {
$installedVersion = & "$CurrentJdkPath" -version 2>&1 | Select-String -Pattern 'version "(.+?)"' | ForEach-Object { $_.Matches[0].Groups[1].Value }
if ($installedVersion -eq $LatestVersion) {
Write-Host "JDK $Version is up to date (Installed: $installedVersion, Latest: $LatestVersion). Skipping download."
return $true
} else {
Write-Host "JDK $Version is outdated (Installed: $installedVersion, Latest: $LatestVersion)."
}
} else {
Write-Host "JDK $Version is not installed."
}

Write-Host ("Download URL for JDK {0}: {1}" -f $Version, $downloadUrl)
return $false
}

# Download the ZIP file
$zipFile = Join-Path -Path $DownloadDir -ChildPath "temurin-$Version.zip"
Write-Host "Downloading JDK $Version to $zipFile..."
Invoke-WebRequest -Uri $downloadUrl -OutFile $zipFile -UseBasicParsing
Write-Host "Downloaded JDK $Version"
# Function to back up the existing JDK to the "oldjdk" directory
function Backup-ExistingJDK {
param (
[string]$Version,
[string]$OldJdkDir,
[string]$BackupDir
)

# Extract the ZIP file
$ExtractedJdkDir = Join-Path -Path $ScriptDir -ChildPath "jdk-$Version"
Write-Host "Extracting JDK $Version..."
Expand-Archive -Path $zipFile -DestinationPath $ExtractedJdkDir -Force
Write-Host "Extracted JDK $Version"
$OldJdkBackupDir = Join-Path -Path $BackupDir -ChildPath "oldjdk-$Version"

# Copy old cacerts to new JDK
$BackupCacerts = Join-Path -Path $BackupDir -ChildPath "cacerts_jdk$Version"
if (Test-Path $BackupCacerts) {
Copy-Item -Path $BackupCacerts -Destination (Join-Path -Path $ExtractedJdkDir -ChildPath "lib\security\cacerts") -Force
Write-Host "Copied cacerts for JDK $Version"
if (Test-Path $OldJdkDir) {
Write-Host "Backing up existing JDK $Version to $OldJdkBackupDir..."

if (Test-Path $OldJdkBackupDir) {
Remove-Item -Path $OldJdkBackupDir -Recurse -Force
}

Copy-Item -Path $OldJdkDir -Destination $OldJdkBackupDir -Recurse -Force
Write-Host "Backup of JDK $Version completed."
} else {
Write-Host "No cacerts backup found for JDK $Version"
Write-Host "No existing JDK $Version found for backup."
}
}

# Function to download the JDK zip file
function Download-JDK {
param (
[string]$DownloadUrl,
[string]$ZipFilePath
)

Write-Host "Downloading JDK to $ZipFilePath..."

Start-BitsTransfer -Source $DownloadUrl -Destination $ZipFilePath -ErrorAction Stop
Write-Host "Downloaded JDK to $ZipFilePath"
}

# Function to extract the JDK using 7-Zip
function Extract-JDKWith7Zip {
param (
[string]$ZipFilePath,
[string]$ExtractDir,
[string]$SevenZipPath
)
$TempExtractDir = Join-Path -Path $ExtractDir -ChildPath "temp"
Write-Host "Extracting JDK from $ZipFilePath to temporary directory $TempExtractDir using 7-Zip..."

# Clean up any existing extraction directory
if (Test-Path $TempExtractDir) {
Remove-Item -Path $TempExtractDir -Recurse -Force
}

if (Test-Path $ExtractDir) {
Remove-Item -Path $ExtractDir -Recurse -Force
}

# Extract to temporary directory
& "$SevenZipPath" x "$ZipFilePath" -o"$TempExtractDir" -y | Out-Null

# Find the root directory inside the extracted contents
$innerDir = Get-ChildItem -Path $TempExtractDir | Where-Object { $_.PSIsContainer } | Select-Object -First 1

if ($innerDir) {
Write-Host "Moving extracted contents from $innerDir to $ExtractDir..."

# Move the contents to the final directory
Move-Item -Path "$($innerDir.FullName)\*" -Destination $ExtractDir -Force

# Remove the temporary directory
Remove-Item -Path $TempExtractDir -Recurse -Force

Write-Host "Extraction completed to $ExtractDir."
} else {
Write-Host "No root directory found inside the extracted archive."
}
}

# Function to copy the old cacerts file to the new JDK
function Copy-Cacerts {
param (
[string]$BackupCacerts,
[string]$ExtractedJdkDir
)

# Add root certificate to the new cacerts
$KeytoolPath = Join-Path -Path $ExtractedJdkDir -ChildPath "bin\keytool.exe"
$CacertsPath = Join-Path -Path $ExtractedJdkDir -ChildPath "lib\security\cacerts"
if (Test-Path $BackupCacerts) {
Copy-Item -Path $BackupCacerts -Destination $CacertsPath -Force
Write-Host "Copied cacerts for the JDK."
} else {
Write-Host "No cacerts backup found."
}
}

# Function to add root certificate to cacerts
function Add-RootCert {
param (
[string]$KeytoolPath,
[string]$CacertsPath,
[string]$RootCert
)

Write-Host "Adding root certificate to cacerts..."

& "$KeytoolPath" -importcert -noprompt -trustcacerts -keystore "$CacertsPath" -storepass "changeit" -alias "rootcert" -file "$RootCert"
Write-Host "Added root certificate to JDK $Version cacerts"
Write-Host "Added root certificate to cacerts."
}

# Main function to update the JDK
function Update-JDK {
param (
[string]$Version,
[string]$OldJdkDir,
[string]$BackupDir,
[string]$DownloadDir,
[string]$ExtractDir,
[string]$SevenZipPath,
[string]$RootCert
)

# Get download URL
$jdkInfo = Get-JDKDownloadUrl -Version $Version
$DownloadUrl = $jdkInfo["DownloadUrl"]
$releaseVersion = $jdkInfo["ReleaseVersion"]
if (-not $DownloadUrl) { return }

# Set file paths
$ZipFilePath = Join-Path -Path $DownloadDir -ChildPath "temurin-$Version.zip"

# Check if JDK is up-to-date
$CurrentJdkPath = Join-Path -Path $OldJdkDir -ChildPath "bin\java.exe"
if (Is-JDKUpToDate -Version $Version -CurrentJdkPath $CurrentJdkPath -LatestVersion $releaseVersion) {
return
}

# Backup old JDK
Backup-ExistingJDK -Version $Version -OldJdkDir $OldJdkDir -BackupDir $BackupDir

# Download JDK
Download-JDK -DownloadUrl $DownloadUrl -ZipFilePath $ZipFilePath

# Extract JDK using 7-Zip
Extract-JDKWith7Zip -ZipFilePath $ZipFilePath -ExtractDir $ExtractDir -SevenZipPath $SevenZipPath

# Copy old cacerts
$BackupCacerts = Join-Path -Path $BackupDir -ChildPath "cacerts_jdk$Version"
Copy-Cacerts -BackupCacerts $BackupCacerts -ExtractedJdkDir $ExtractDir

# Add root certificate
$KeytoolPath = Join-Path -Path $ExtractDir -ChildPath "bin\keytool.exe"
Add-RootCert -KeytoolPath $KeytoolPath -CacertsPath (Join-Path -Path $ExtractDir -ChildPath "lib\security\cacerts") -RootCert $RootCert
}

# Example usage:
#$DownloadDir = "C:\tmp\jdk_download"
#$BackupDir = "C:\tmp\backup"
$SevenZipPath = "C:\Program Files\7-Zip\7z.exe"
#$RootCert = "C:\path\to\root.cer"
#$JdkPaths = @{
# "8" = "C:\Program Files\Java\jdk1.8.0_xxx"
# "11" = "C:\Program Files\Java\jdk-11.x.x"
# "17" = "C:\Program Files\Java\jdk-17.x.x"
# "21" = "C:\Program Files\Java\jdk-21.x.x"
#}

# Call the function for the desired JDK version (e.g., 8)
#Update-JDK -Version "8" -OldJdkDir $OldJdkPaths["8"] -BackupDir $BackupDir -DownloadDir $DownloadDir -SevenZipPath $SevenZipPath -RootCert $RootCert

# JDK versions to download and update
$JdkVersions = @("8", "11", "17", "21")

# Process each JDK version
foreach ($version in $JdkVersions) {
Download-Extract-UpdateJDK -Version $version
Update-JDK -Version $version -OldJdkDir $JdkPaths[$version] -BackupDir $BackupDir -DownloadDir $DownloadDir -ExtractDir $JdkPaths[$version] -SevenZipPath $SevenZipPath -RootCert $RootCert
}

# Clean up the downloaded files
Expand Down

0 comments on commit 056519a

Please sign in to comment.