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
Changes from 3 commits
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 @@ -78,77 +78,179 @@ function New-AzDataMigrationLoginsMigration
#Defining Default Output Path
$DefaultOutputFolder = Get-DefaultLoginMigrationsOutputFolder

#Defining Base and Exe paths
$BaseFolder = Join-Path -Path $DefaultOutputFolder -ChildPath Downloads;
$ExePath = Join-Path -Path $BaseFolder -ChildPath Logins.Console.csproj\Logins.Console.exe;
#Defining Downloads folder
$DownloadsFolder = Join-Path -Path $DefaultOutputFolder -ChildPath Downloads;

#Checking if BaseFolder Path is valid or not
if(-Not (Test-Path $BaseFolder))
#Checking if DownloadsFolder Path is valid or not
if(-Not (Test-Path $DownloadsFolder))
{
$null = New-Item -Path $BaseFolder -ItemType "directory"
$null = New-Item -Path $DownloadsFolder -ItemType "directory"
}
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;
Bhavikshah123 marked this conversation as resolved.
Show resolved Hide resolved
}

# 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;
}
}

#Testing Whether Console App is downloaded or not
$TestExePath = Test-Path -Path $ExePath;
#Determine latest version of Login console app
$PackageId = "Microsoft.SqlServer.Migration.LoginsConsoleApp"

#Console app download address
$ZipSource = "https://sqlassess.blob.core.windows.net/app/LoginsMigration.zip";
$ZipDestination = Join-Path -Path $BaseFolder -ChildPath "LoginsMigration.zip";
$AvailablePackagesOnNugetOrg = ""

#Downloading and extracting LoginsMigration Zip file
if(-Not $TestExePath)
try {
$AvailablePackagesOnNugetOrg = Find-Package -Source "https://api.nuget.org/v3/index.json" -Name $PackageId -AllowPrereleaseVersions -AllVersions
Bhavikshah123 marked this conversation as resolved.
Show resolved Hide resolved
Bhavikshah123 marked this conversation as resolved.
Show resolved Hide resolved
$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";

$ConsoleAppFolders = Get-ChildItem -Path $DownloadsFolder -Filter "$PackageId.*"
$LatestLocalNameAndVersion = ""
if ($ConsoleAppFolders.Length -gt 0)
{
#Downloading and extracting LoginMigration Zip file
Write-Host "Downloading and extracting latest LoginMigration Zip file..."
Invoke-RestMethod -Uri $ZipSource -OutFile $ZipDestination;
Expand-Archive -Path $ZipDestination -DestinationPath $BaseFolder -Force;
$ConsoleAppFolders = $ConsoleAppFolders | Sort-Object -Property Name -Descending
$LatestLocalNameAndVersion = $ConsoleAppFolders[0].Name
Write-Host "Installed Login migration console app nupkg version: $LatestLocalNameAndVersion"

if ($AvailablePackagesOnNugetOrg -eq "")
{
$LatestNugetOrgNameAndVersion = $LatestLocalNameAndVersion
}
}
else
{
# Get local exe version
Write-Host "Checking installed Login.Console.exe version...";
$installedVersion = (Get-Item $ExePath).VersionInfo.FileVersion;
Write-Host "Installed version: $installedVersion";

# Get latest console app version
Write-Host "Checking whether there is newer version...";
$VersionFileSource = "https://sqlassess.blob.core.windows.net/app/loginconsoleappversion.json";
$VersionFileDestination = Join-Path -Path $BaseFolder -ChildPath "loginconsoleappversion.json";
Invoke-RestMethod -Uri $VersionFileSource -OutFile $VersionFileDestination;
$jsonObj = Get-Content $VersionFileDestination | Out-String | ConvertFrom-Json;
$latestVersion = $jsonObj.version;

# Compare the latest exe version with the local exe version
if([System.Version]$installedVersion -lt [System.Version]$latestVersion)
#No local console app
if ($AvailablePackagesOnNugetOrg -eq "")
{
Write-Host "Found newer version of Logins.Console.exe '$latestVersion'";
#No version available to download
Write-Host "Connection to NuGet.org required. Please check connection and try again."
return;
}
}

Write-Host "Removing old Logins.Console.exe..."
# Remove old zip file
Remove-Item -Path $ZipDestination;
Write-Host "Latest Login migration console app nupkg version on Nuget.org: $LatestNugetOrgNameAndVersion";
$LatestNugetFolder = Join-Path -Path $DownloadsFolder -ChildPath $LatestNugetOrgNameAndVersion;
$ExePath = "tools\Microsoft.SqlServer.Migration.Logins.ConsoleApp.exe";

# Remove existing folder and contents
$ConsoleAppDestination = Join-Path -Path $BaseFolder -ChildPath "Logins.Console.csproj";
Remove-Item -Path $ConsoleAppDestination -Recurse;
#User consent for Login migration console app update. By default it is set to yes.
$userUpdateConsent = "yes";

# Remove version file
Remove-Item -Path $VersionFileDestination;
# 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";

#Downloading and extracting LoginMigration Zip file
Write-Host "Downloading and extracting latest LoginMigration Zip file..."
Invoke-RestMethod -Uri $ZipSource -OutFile $ZipDestination;
Expand-Archive -Path $ZipDestination -DestinationPath $BaseFolder -Force;
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
{
Write-Host "Installed Logins.Console.exe is the latest one...";
if($NugetVersions.Length -gt 0)
{
$LatestNugetFolder = $NugetVersions[0].Name;
}
}
}

if(-Not (Test-Path -Path "$LatestNugetFolder\$ExePath"))
{
Write-Host "Failed to locate executable."
return
}

#Collecting data
if(('CommandLine') -contains $PSCmdlet.ParameterSetName)
{
# The array list $splat contains all the parameters that will be passed to '.\Logins.Console.exe LoginsMigration'
# The array list $splat contains all the parameters that will be passed to '.\Microsoft.SqlServer.Migration.Logins.ConsoleApp.exe LoginsMigration'

$LoginsListArray = $($ListOfLogin -split " ")
[System.Collections.ArrayList] $splat = @(
Expand All @@ -173,7 +275,10 @@ function New-AzDataMigrationLoginsMigration

}
}
# Running LoginsMigration

$ExePath = Join-Path -Path $LatestNugetFolder -ChildPath $ExePath;
# Running LoginsMigration
Write-Host "Starting Execution..."
& $ExePath LoginsMigration @splat
}
else
Expand Down
Loading