Skip to content

Migrate DataMigration from generation to main #24460

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

Merged
merged 2 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion src/DataMigration/DataMigration.Autorest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
This directory contains the PowerShell module for the DataMigration service.

---

## Info
- Modifiable: yes
- Generated: all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,77 +78,66 @@ 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
{
#Delete old Login console app files
Delete-OldLoginConsoleApp $DownloadsFolder;
}

#Testing Whether Console App is downloaded or not
$TestExePath = Test-Path -Path $ExePath;

#Console app download address
$ZipSource = "https://sqlassess.blob.core.windows.net/app/LoginsMigration.zip";
$ZipDestination = Join-Path -Path $BaseFolder -ChildPath "LoginsMigration.zip";
#Determine latest version of Login console app
$PackageId = "Microsoft.SqlServer.Migration.LoginsConsoleApp"
$LatestNugetOrgDetails = Get-LatestConsoleAppVersionFromNugetOrg $PackageId

#Downloading and extracting LoginsMigration Zip file
if(-Not $TestExePath)
#Determine local version of Login console app
$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 "")
{
$LatestNugetOrgDetails.NameAndVersion = $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'";

Write-Host "Removing old Logins.Console.exe..."
# Remove old zip file
Remove-Item -Path $ZipDestination;
#No version available to download
Write-Host "Connection to NuGet.org required. Please check connection and try again."
return;
}
}

# Remove existing folder and contents
$ConsoleAppDestination = Join-Path -Path $BaseFolder -ChildPath "Logins.Console.csproj";
Remove-Item -Path $ConsoleAppDestination -Recurse;
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";

# Remove version file
Remove-Item -Path $VersionFileDestination;
# Check for the latest console app version and download it if needed.
CheckAndDownloadConsoleAppFromNugetOrg $LatestLocalNameAndVersion $LatestNugetOrgDetails $ExePath ([ref]$LatestNugetFolder)

#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;
}
else
{
Write-Host "Installed Logins.Console.exe is the latest one...";
}
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 +162,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
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,175 @@ function Get-DefaultLoginMigrationsOutputFolder {
return $DefualtPath

}

}

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

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


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

process {
$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}
}
}

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 {
#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;
}
}
}
}
}
13 changes: 7 additions & 6 deletions src/DataMigration/DataMigration/Az.DataMigration.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Microsoft Corporation
#
# Generated on: 1/30/2024
# Generated on: 3/23/2024
#

@{
Expand Down Expand Up @@ -53,17 +53,17 @@ DotNetFrameworkVersion = '4.7.2'
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.15.1'; })
RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.16.0'; })

# Assemblies that must be loaded prior to importing this module
RequiredAssemblies = 'DataMigration.Autorest/bin/Az.DataMigration.private.dll',
'Microsoft.Azure.Management.DataMigration.dll'

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
FormatsToProcess = 'DataMigration.Autorest/Az.DataMigration.format.ps1xml'
Expand Down Expand Up @@ -147,7 +147,8 @@ PrivateData = @{
PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
Tags = 'Azure','ResourceManager','ARM','Sql','Database','Data','Migration','Service'
Tags = 'Azure', 'ResourceManager', 'ARM', 'Sql', 'Database', 'Data', 'Migration',
'Service'

# A URL to the license for this module.
LicenseUri = 'https://aka.ms/azps-license'
Expand All @@ -172,7 +173,7 @@ PrivateData = @{

} # End of PSData hashtable

} # End of PrivateData hashtable
} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''
Expand Down
1 change: 1 addition & 0 deletions src/DataMigration/DataMigration/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Changed the Login Migration Console App source to NuGet.org and added versioning support for updating the console app.

## Version 0.14.4
* Added versioning to login migration console app.
Expand Down
Loading