Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Login Migration Console App source to NuGet.org and Versioning support. #24330

Merged
merged 5 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,47 +88,15 @@ function New-AzDataMigrationLoginsMigration
}
else
Bhavikshah123 marked this conversation as resolved.
Show resolved Hide resolved
{
#Remove old Login console app files
#Old console app download address
$ZipDestination = Join-Path -Path $DownloadsFolder -ChildPath "LoginsMigration.zip";

# Remove old zip file
if(Test-Path $ZipDestination)
{
Remove-Item -Path $ZipDestination;
}

# Remove existing folder and contents
$ConsoleAppDestination = Join-Path -Path $DownloadsFolder -ChildPath "Logins.Console.csproj";
if(Test-Path $ConsoleAppDestination)
{
Remove-Item -Path $ConsoleAppDestination -Recurse;
}

# Remove version file
$VersionFileDestination = Join-Path -Path $DownloadsFolder -ChildPath "loginconsoleappversion.json";
if(Test-Path $VersionFileDestination)
{
Remove-Item -Path $VersionFileDestination;
}
#Delete old Login console app files
Delete-OldLoginConsoleApp $DownloadsFolder;
}

#Determine latest version of Login console app
$PackageId = "Microsoft.SqlServer.Migration.LoginsConsoleApp"
$LatestNugetOrgDetails = Get-LatestConsoleAppVersionFromNugetOrg $PackageId

$AvailablePackagesOnNugetOrg = ""

try {
$AvailablePackagesOnNugetOrg = Find-Package -Source "https://api.nuget.org/v3/index.json" -Name $PackageId -AllowPrereleaseVersions -AllVersions
$AvailablePackagesOnNugetOrg = $AvailablePackagesOnNugetOrg | Sort-Object -Property Version -Descending
} catch {
Write-Host "Unable to connect to NuGet.org to check for updates."
}

$LatestNugetOrgName = $AvailablePackagesOnNugetOrg[0].Name
$LatestNugetOrgVersion = $AvailablePackagesOnNugetOrg[0].Version
$LatestNugetOrgNameAndVersion = "$LatestNugetOrgName.$LatestNugetOrgVersion";

#Determine local version of Login console app
$ConsoleAppFolders = Get-ChildItem -Path $DownloadsFolder -Filter "$PackageId.*"
$LatestLocalNameAndVersion = ""
if ($ConsoleAppFolders.Length -gt 0)
Expand All @@ -139,7 +107,7 @@ function New-AzDataMigrationLoginsMigration

if ($AvailablePackagesOnNugetOrg -eq "")
{
$LatestNugetOrgNameAndVersion = $LatestLocalNameAndVersion
$LatestNugetOrgDetails.NameAndVersion = $LatestLocalNameAndVersion
}
}
else
Expand All @@ -153,93 +121,12 @@ function New-AzDataMigrationLoginsMigration
}
}

Write-Host "Latest Login migration console app nupkg version on Nuget.org: $LatestNugetOrgNameAndVersion";
$LatestNugetFolder = Join-Path -Path $DownloadsFolder -ChildPath $LatestNugetOrgNameAndVersion;
Write-Host "Latest Login migration console app nupkg version on Nuget.org: $($LatestNugetOrgDetails.NameAndVersion)";
$LatestNugetFolder = Join-Path -Path $DownloadsFolder -ChildPath $LatestNugetOrgDetails.NameAndVersion;
$ExePath = "tools\Microsoft.SqlServer.Migration.Logins.ConsoleApp.exe";

#User consent for Login migration console app update. By default it is set to yes.
$userUpdateConsent = "yes";

# Prompt for user consent on Login migration console app update
if($LatestLocalNameAndVersion -ne "" -and $LatestNugetOrgNameAndVersion -gt $LatestLocalNameAndVersion)
{
Write-Host "Newer Login migration console app nupkg version is available in Nuget.org...";
while($true) {
$userUpdateConsent = Read-Host -Prompt "Do you want to upgrade to the latest version? (yes/no)"

if ($userUpdateConsent -eq "yes")
{
Write-Host "You chose to upgrade. Proceeding..."
break;
}
elseif ($userUpdateConsent -eq "no")
{
Write-Host "You chose not to upgrade."
$LatestNugetFolder = Join-Path -Path $DownloadsFolder -ChildPath $LatestLocalNameAndVersion;
break;
}
else
{
Write-Host "Invalid input. Please enter 'yes' or 'no'."
}
}
}

if ($LatestNugetOrgNameAndVersion -gt $LatestLocalNameAndVersion -and $userUpdateConsent -eq "yes")
{
#Update is available
$DownloadUrl = "https://www.nuget.org/api/v2/package/$PackageId/$LatestNugetOrgVersion"

#Checking if LatestNugetFolder Path is valid or not
if(-Not (Test-Path $LatestNugetFolder))
{
$null = New-Item -Path $LatestNugetFolder -ItemType "directory";
}

Write-Host "Downloading the latest Login migration console app nupkg: $LatestNugetOrgNameAndVersion ..."
Invoke-WebRequest $DownloadUrl -OutFile "$LatestNugetFolder\\$LatestNugetOrgName.$LatestNugetOrgVersion.nupkg"

$ToolsPathExists = Test-Path -Path (Join-Path -Path $LatestNugetFolder -ChildPath "tools");

if ($ToolsPathExists -eq $False)
{
$Nugets = Get-ChildItem -Path $LatestNugetFolder -Filter "$PackageId.*.nupkg";

if ($Nugets.Length -gt 0)
{
Write-Host "Extracting the latest Login migration console app nupkg: $LatestNugetOrgNameAndVersion ..."
$Nugets = $Nugets | Sort-Object -Property Name -Descending;
$LatestNugetPath = $Nugets[0].FullName;
Expand-Archive -Path $LatestNugetPath -DestinationPath $LatestNugetFolder;
}
}

#Check if update was successful
$TestPathResult = Test-Path -Path "$LatestNugetFolder\$ExePath"

$NugetVersions = Get-ChildItem -Path $DownloadsFolder -Filter "$PackageId.*";
$NugetVersions = $NugetVersions | Sort-Object -Property Name -Descending

if($TestPathResult)
{
Write-Host "Removing all older Login migration console apps..."
#Remove all other NuGet versions except for the version just downloaded
for ($NugetIndex = 0; $NugetIndex -lt $NugetVersions.Length; $NugetIndex = $NugetIndex + 1)
{
if($NugetVersions[$NugetIndex].Name -ne $LatestNugetOrgNameAndVersion)
{
Remove-Item -Path $NugetVersions[$NugetIndex].FullName -Recurse -Force
}
}
}
else
{
if($NugetVersions.Length -gt 0)
{
$LatestNugetFolder = $NugetVersions[0].Name;
}
}
}
# Check for the latest console app version and download it if needed.
CheckAndDownloadConsoleAppFromNugetOrg $LatestLocalNameAndVersion $LatestNugetOrgDetails $ExePath ([ref]$LatestNugetFolder)

if(-Not (Test-Path -Path "$LatestNugetFolder\$ExePath"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,196 @@ function Get-DefaultLoginMigrationsOutputFolder {
return $DefualtPath

}

}

function Delete-OldLoginConsoleApp {
[Microsoft.Azure.PowerShell.Cmdlets.DataMigration.DoNotExportAttribute()]
param(
[Parameter(Mandatory=$true, Position=0)]
[System.String]
$DownloadsFolder
)

process {
try
{
#Remove old Login console app files
#Old console app download address
$ZipDestination = Join-Path -Path $DownloadsFolder -ChildPath "LoginsMigration.zip";

# Remove old zip file
if(Test-Path $ZipDestination)
{
Remove-Item -Path $ZipDestination;
}

# Remove existing folder and contents
$ConsoleAppDestination = Join-Path -Path $DownloadsFolder -ChildPath "Logins.Console.csproj";
if(Test-Path $ConsoleAppDestination)
{
Remove-Item -Path $ConsoleAppDestination -Recurse;
}

# Remove version file
$VersionFileDestination = Join-Path -Path $DownloadsFolder -ChildPath "loginconsoleappversion.json";
if(Test-Path $VersionFileDestination)
{
Remove-Item -Path $VersionFileDestination;
}
}
catch
{
throw $_
}
}
}


function Get-LatestConsoleAppVersionFromNugetOrg {
[Microsoft.Azure.PowerShell.Cmdlets.DataMigration.DoNotExportAttribute()]
param(
[Parameter(Mandatory=$true, Position=0)]
[System.String]
$PackageId
)

process {
try
{
$AvailablePackagesOnNugetOrg = ""

try {
$AvailablePackagesOnNugetOrg = Find-Package -Source "https://api.nuget.org/v3/index.json" -Name $PackageId -AllowPrereleaseVersions -AllVersions
$AvailablePackagesOnNugetOrg = $AvailablePackagesOnNugetOrg | Sort-Object -Property Version -Descending
} catch {
Write-Host "Unable to connect to NuGet.org to check for updates."
}

$LatestNugetOrgName = $AvailablePackagesOnNugetOrg[0].Name
$LatestNugetOrgVersion = $AvailablePackagesOnNugetOrg[0].Version
$LatestNugetOrgNameAndVersion = "$LatestNugetOrgName.$LatestNugetOrgVersion";

return @{Name=$LatestNugetOrgName; Version=$LatestNugetOrgVersion; NameAndVersion=$LatestNugetOrgNameAndVersion}
}
catch
{
throw $_
}
}
}

function CheckAndDownloadConsoleAppFromNugetOrg {
[Microsoft.Azure.PowerShell.Cmdlets.DataMigration.DoNotExportAttribute()]
param(
[Parameter(Mandatory=$true, Position=0)]
[AllowEmptyString()]
[System.String]
$LatestLocalNameAndVersion,

[Parameter(Mandatory=$true, Position=1)]
[HashTable]
$LatestNugetOrgDetails,

[Parameter(Mandatory=$true, Position=2)]
[System.String]
$ExePath,

[Parameter(Mandatory=$true, Position=3)]
[ref]
[System.String]
$LatestNugetFolder
)

process {
try
{
#User consent for Login migration console app update. By default it is set to yes.
$userUpdateConsent = "yes";

# Prompt for user consent on Login migration console app update
if($LatestLocalNameAndVersion -ne "" -and $LatestNugetOrgDetails.NameAndVersion -gt $LatestLocalNameAndVersion)
{
Write-Host "Newer Login migration console app nupkg version is available in Nuget.org...";
while($true) {
$userUpdateConsent = Read-Host -Prompt "Do you want to upgrade to the latest version? (yes/no)"

if ($userUpdateConsent -eq "yes")
{
Write-Host "You chose to upgrade. Proceeding..."
break;
}
elseif ($userUpdateConsent -eq "no")
{
Write-Host "You chose not to upgrade."
$LatestNugetFolder.Value = Join-Path -Path $DownloadsFolder -ChildPath $LatestLocalNameAndVersion;
break;
}
else
{
Write-Host "Invalid input. Please enter 'yes' or 'no'."
}
}
}

if ($LatestNugetOrgDetails.NameAndVersion -gt $LatestLocalNameAndVersion -and $userUpdateConsent -eq "yes")
{
#Update is available
$DownloadUrl = "https://www.nuget.org/api/v2/package/$PackageId/$($LatestNugetOrgDetails.Version)"

#Checking if LatestNugetFolder Path is valid or not
if(-Not (Test-Path $LatestNugetFolder.Value))
{
$null = New-Item -Path $LatestNugetFolder.Value -ItemType "directory";
}

Write-Host "Downloading the latest Login migration console app nupkg: $($LatestNugetOrgDetails.NameAndVersion) ..."
Invoke-WebRequest $DownloadUrl -OutFile "$($LatestNugetFolder.Value)\\$($LatestNugetOrgDetails.NameAndVersion).nupkg"

$ToolsPathExists = Test-Path -Path (Join-Path -Path $LatestNugetFolder.Value -ChildPath "tools");

if ($ToolsPathExists -eq $False)
{
$Nugets = Get-ChildItem -Path $LatestNugetFolder.Value -Filter "$PackageId.*.nupkg";

if ($Nugets.Length -gt 0)
{
Write-Host "Extracting the latest Login migration console app nupkg: $($LatestNugetOrgDetails.NameAndVersion) ..."
$Nugets = $Nugets | Sort-Object -Property Name -Descending;
$LatestNugetPath = $Nugets[0].FullName;
Expand-Archive -Path $LatestNugetPath -DestinationPath $LatestNugetFolder.Value;
}
}

#Check if update was successful
$TestPathResult = Test-Path -Path "$($LatestNugetFolder.Value)\$ExePath"

$NugetVersions = Get-ChildItem -Path $DownloadsFolder -Filter "$PackageId.*";
$NugetVersions = $NugetVersions | Sort-Object -Property Name -Descending

if($TestPathResult)
{
Write-Host "Removing all older Login migration console apps..."
#Remove all other NuGet versions except for the version just downloaded
for ($NugetIndex = 0; $NugetIndex -lt $NugetVersions.Length; $NugetIndex = $NugetIndex + 1)
{
if($NugetVersions[$NugetIndex].Name -ne $LatestNugetOrgDetails.NameAndVersion)
{
Remove-Item -Path $NugetVersions[$NugetIndex].FullName -Recurse -Force
}
}
}
else
{
if($NugetVersions.Length -gt 0)
{
$LatestNugetFolder.Value = $NugetVersions[0].Name;
}
}
}
}
catch
Bhavikshah123 marked this conversation as resolved.
Show resolved Hide resolved
{
throw $_
}
}
}
Loading