From 8ce91e3c37480f548fba720cc2b4b9f76249b550 Mon Sep 17 00:00:00 2001 From: Maksim Petrov <47208721+vmapetr@users.noreply.github.com> Date: Wed, 1 Jul 2020 15:16:01 +0300 Subject: [PATCH] [Windows] Install AZ 4.3.0 (#1137) * Install and validate AZ 4.3.0 for Windows images * Move azureModules to toolset * Fix validation * Fix modules root location * Small fix in syntax * Fix in syntax * Fixes for default tool versions --- images/win/Windows2016-Azure.json | 9 +++ images/win/Windows2019-Azure.json | 9 +++ .../Installers/Install-AzureModules.ps1 | 72 ++++++++----------- .../Installers/Validate-AzureModules.ps1 | 55 +++++++------- images/win/toolsets/toolset-2016.json | 38 ++++++++++ images/win/toolsets/toolset-2019.json | 38 ++++++++++ 6 files changed, 151 insertions(+), 70 deletions(-) diff --git a/images/win/Windows2016-Azure.json b/images/win/Windows2016-Azure.json index c65be16ec..05333241a 100644 --- a/images/win/Windows2016-Azure.json +++ b/images/win/Windows2016-Azure.json @@ -24,6 +24,7 @@ "imagedata_file": "C:\\imagedata.json", "metadata_file": "C:\\image\\metadata.txt", "helper_script_folder": "C:\\Program Files\\WindowsPowerShell\\Modules\\", + "psmodules_root_folder": "C:\\Modules", "commit_url": "LATEST", "install_user": "installer", "install_password": null, @@ -550,6 +551,10 @@ }, { "type": "powershell", + "environment_vars":[ + "TOOLSET_JSON_PATH={{user `toolset_json_path`}}", + "PSMODULES_ROOT_FOLDER={{user `psmodules_root_folder`}}" + ], "scripts":[ "{{ template_dir }}/scripts/Installers/Install-AzureModules.ps1" ] @@ -676,6 +681,10 @@ }, { "type": "powershell", + "environment_vars":[ + "TOOLSET_JSON_PATH={{user `toolset_json_path`}}", + "PSMODULES_ROOT_FOLDER={{user `psmodules_root_folder`}}" + ], "scripts":[ "{{ template_dir }}/scripts/Installers/Validate-AzureModules.ps1" ] diff --git a/images/win/Windows2019-Azure.json b/images/win/Windows2019-Azure.json index 8b17eae41..cf10a9033 100644 --- a/images/win/Windows2019-Azure.json +++ b/images/win/Windows2019-Azure.json @@ -24,6 +24,7 @@ "imagedata_file": "C:\\imagedata.json", "metadata_file": "C:\\image\\metadata.txt", "helper_script_folder": "C:\\Program Files\\WindowsPowerShell\\Modules\\", + "psmodules_root_folder": "C:\\Modules", "commit_id": "LATEST", "install_user": "installer", "install_password": null, @@ -511,6 +512,10 @@ }, { "type": "powershell", + "environment_vars":[ + "TOOLSET_JSON_PATH={{user `toolset_json_path`}}", + "PSMODULES_ROOT_FOLDER={{user `psmodules_root_folder`}}" + ], "scripts":[ "{{ template_dir }}/scripts/Installers/Install-AzureModules.ps1" ] @@ -661,6 +666,10 @@ }, { "type": "powershell", + "environment_vars":[ + "TOOLSET_JSON_PATH={{user `toolset_json_path`}}", + "PSMODULES_ROOT_FOLDER={{user `psmodules_root_folder`}}" + ], "scripts":[ "{{ template_dir }}/scripts/Installers/Validate-AzureModules.ps1" ] diff --git a/images/win/scripts/Installers/Install-AzureModules.ps1 b/images/win/scripts/Installers/Install-AzureModules.ps1 index 3f41a7743..fa33710fa 100644 --- a/images/win/scripts/Installers/Install-AzureModules.ps1 +++ b/images/win/scripts/Installers/Install-AzureModules.ps1 @@ -3,58 +3,35 @@ ## Desc: Install Azure PowerShell modules ################################################################################ +$ErrorActionPreference = "Stop" + Set-PSRepository -InstallationPolicy Trusted -Name PSGallery -#### NOW The correct Modules need to be saved in C:\Modules -$installPSModulePath = 'C:\Modules' -if(-not (Test-Path -LiteralPath $installPSModulePath)) +# The correct Modules need to be saved in C:\Modules +$installPSModulePath = $env:PSMODULES_ROOT_FOLDER +if (-not (Test-Path -LiteralPath $installPSModulePath)) { - Write-Host "Creating '$installPSModulePath' folder to store PowerShell Azure modules" + Write-Host "Creating ${installPSModulePath} folder to store PowerShell Azure modules..." $null = New-Item -Path $installPSModulePath -ItemType Directory } -# Powershell Azure modules to install -$psAzureModulesToInstall = @{ - "azurerm" = @( - "2.1.0" - "3.8.0" - "4.2.1" - "5.1.1" - "6.7.0" - "6.13.1" - ) +# Get modules content from toolset +$modules = (Get-ToolsetContent).azureModules - "azure" = @( - "2.1.0" - "3.8.0" - "4.2.1" - "5.1.1" - "5.3.0" - ) +$psModuleMachinePath = "" - "az" = @( - "1.0.0" - "1.6.0" - "2.3.2" - "2.6.0" - "3.1.0" - "3.5.0" - "3.8.0" - ) -} - -# Download Azure PowerShell modules -foreach($psmoduleName in $psAzureModulesToInstall.Keys) +foreach ($module in $modules) { - Write-Host "Installing '$psmoduleName' to the '$installPSModulePath' path:" - $psmoduleVersions = $psAzureModulesToInstall[$psmoduleName] - foreach($psmoduleVersion in $psmoduleVersions) + $moduleName = $module.name + + Write-Host "Installing ${moduleName} to the ${installPSModulePath} path..." + foreach ($version in $module.versions) { - $psmodulePath = Join-Path $installPSModulePath "${psmoduleName}_${psmoduleVersion}" - Write-Host " - $psmoduleVersion [$psmodulePath]" + $modulePath = Join-Path -Path $installPSModulePath -ChildPath "${moduleName}_${version}" + Write-Host " - $version [$modulePath]" try { - Save-Module -Path $psmodulePath -Name $psmoduleName -RequiredVersion $psmoduleVersion -Force -ErrorAction Stop + Save-Module -Path $modulePath -Name $moduleName -RequiredVersion $version -Force -ErrorAction Stop } catch { @@ -62,8 +39,17 @@ foreach($psmoduleName in $psAzureModulesToInstall.Keys) exit 1 } } + + # Append default tool version to machine path + if ($null -ne $module.default) + { + $defaultVersion = $module.default + + Write-Host "Use ${moduleName} ${defaultVersion} as default version..." + $psModuleMachinePath += "${installPSModulePath}\${moduleName}_${defaultVersion};" + } } -# Add AzureRM and Azure modules to the PSModulePath -$finalModulePath = '{0};{1};{2}' -f "${installPSModulePath}\azurerm_2.1.0", "${installPSModulePath}\azure_2.1.0", $env:PSModulePath -[Environment]::SetEnvironmentVariable("PSModulePath", $finalModulePath, "Machine") +# Add modules to the PSModulePath +$psModuleMachinePath += $env:PSModulePath +[Environment]::SetEnvironmentVariable("PSModulePath", $psModuleMachinePath, "Machine") \ No newline at end of file diff --git a/images/win/scripts/Installers/Validate-AzureModules.ps1 b/images/win/scripts/Installers/Validate-AzureModules.ps1 index 10190e0fd..39f1a2ea4 100644 --- a/images/win/scripts/Installers/Validate-AzureModules.ps1 +++ b/images/win/scripts/Installers/Validate-AzureModules.ps1 @@ -3,41 +3,42 @@ ## Desc: Validate Azure PowerShell modules ################################################################################ -function Validate-AzureModule -{ - param([String]$ModuleName, [String[]]$ModuleVersions) +$ErrorActionPreference = "Stop" - foreach($moduleVersion in $moduleVersions) +$modulesRootPath = $env:PSMODULES_ROOT_FOLDER + +# Get modules content from toolset +$modules = (Get-ToolsetContent).azureModules + +foreach ($module in $modules) +{ + foreach ($version in $module.versions) { - $modulePath = "${installPSModulePath}\${moduleName}_${moduleVersion}" - # Import each module in PowerShell session - $job = Start-Job -ScriptBlock { - param($modulePath, $moduleName) + $moduleName = $module.name + $modulePath = Join-Path -Path $modulesRootPath -ChildPath "$($module.name)_${version}" + + Write-Host "Trying to import ${moduleName}_${version}..." + $testJob = Start-Job -ScriptBlock { + param ( + $modulePath, + $moduleName + ) $env:PsModulePath = "$modulePath;$env:PsModulePath" Import-Module -Name $moduleName Get-Module -Name $moduleName + } -ArgumentList $modulePath, $moduleName - $isError = $job | Wait-Job | Foreach-Object ChildJobs | Where-Object {$_.Error} - if($isError) + + $isError = $testJob | Wait-Job | Foreach-Object ChildJobs | Where-Object { $_.Error } + if ($isError) { - Write-Host "Required '$moduleName' module '$moduleVersion' version is not present" + Write-Host "Required ${moduleName} module ${version} version is not present" + exit 1 } - $job | Receive-Job | Select-Object Name,Version,Path - Remove-Job $job + + $testJob | Receive-Job | Select-Object Name,Version,Path + Remove-Job $testJob } -} - -# Modules path -$installPSModulePath = 'C:\Modules' - -# Validate Azure modules and versions -$azurermVersions = "2.1.0", "3.8.0", "4.2.1", "5.1.1", "6.7.0", "6.13.1" -Validate-AzureModule -ModuleName AzureRM -ModuleVersions $azurermVersions - -$azureVersions = "2.1.0", "3.8.0", "4.2.1", "5.1.1", "5.3.0" -Validate-AzureModule -ModuleName Azure -ModuleVersions $azureVersions - -$azVersions = "1.0.0", "1.6.0", "2.3.2", "2.6.0", "3.1.0", "3.5.0", "3.8.0" -Validate-AzureModule -ModuleName Az -ModuleVersions $azVersions +} \ No newline at end of file diff --git a/images/win/toolsets/toolset-2016.json b/images/win/toolsets/toolset-2016.json index 9dac4edc2..c21042201 100644 --- a/images/win/toolsets/toolset-2016.json +++ b/images/win/toolsets/toolset-2016.json @@ -74,5 +74,43 @@ "1.72.0" ] } + ], + "azureModules": [ + { + "name": "azurerm", + "versions": [ + "2.1.0", + "3.8.0", + "4.2.1", + "5.1.1", + "6.7.0", + "6.13.1" + ], + "default": "2.1.0" + }, + { + "name": "azure", + "versions": [ + "2.1.0", + "3.8.0", + "4.2.1", + "5.1.1", + "5.3.0" + ], + "default": "2.1.0" + }, + { + "name": "az", + "versions": [ + "1.0.0", + "1.6.0", + "2.3.2", + "2.6.0", + "3.1.0", + "3.5.0", + "3.8.0", + "4.3.0" + ] + } ] } \ No newline at end of file diff --git a/images/win/toolsets/toolset-2019.json b/images/win/toolsets/toolset-2019.json index 83e8535ef..f1b605cad 100644 --- a/images/win/toolsets/toolset-2019.json +++ b/images/win/toolsets/toolset-2019.json @@ -83,5 +83,43 @@ "1.72.0" ] } + ], + "azureModules": [ + { + "name": "azurerm", + "versions": [ + "2.1.0", + "3.8.0", + "4.2.1", + "5.1.1", + "6.7.0", + "6.13.1" + ], + "default": "2.1.0" + }, + { + "name": "azure", + "versions": [ + "2.1.0", + "3.8.0", + "4.2.1", + "5.1.1", + "5.3.0" + ], + "default": "2.1.0" + }, + { + "name": "az", + "versions": [ + "1.0.0", + "1.6.0", + "2.3.2", + "2.6.0", + "3.1.0", + "3.5.0", + "3.8.0", + "4.3.0" + ] + } ] } \ No newline at end of file