[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
This commit is contained in:
Maksim Petrov
2020-07-01 15:16:01 +03:00
committed by GitHub
parent cc6117e0db
commit 8ce91e3c37
6 changed files with 151 additions and 70 deletions

View File

@@ -24,6 +24,7 @@
"imagedata_file": "C:\\imagedata.json", "imagedata_file": "C:\\imagedata.json",
"metadata_file": "C:\\image\\metadata.txt", "metadata_file": "C:\\image\\metadata.txt",
"helper_script_folder": "C:\\Program Files\\WindowsPowerShell\\Modules\\", "helper_script_folder": "C:\\Program Files\\WindowsPowerShell\\Modules\\",
"psmodules_root_folder": "C:\\Modules",
"commit_url": "LATEST", "commit_url": "LATEST",
"install_user": "installer", "install_user": "installer",
"install_password": null, "install_password": null,
@@ -550,6 +551,10 @@
}, },
{ {
"type": "powershell", "type": "powershell",
"environment_vars":[
"TOOLSET_JSON_PATH={{user `toolset_json_path`}}",
"PSMODULES_ROOT_FOLDER={{user `psmodules_root_folder`}}"
],
"scripts":[ "scripts":[
"{{ template_dir }}/scripts/Installers/Install-AzureModules.ps1" "{{ template_dir }}/scripts/Installers/Install-AzureModules.ps1"
] ]
@@ -676,6 +681,10 @@
}, },
{ {
"type": "powershell", "type": "powershell",
"environment_vars":[
"TOOLSET_JSON_PATH={{user `toolset_json_path`}}",
"PSMODULES_ROOT_FOLDER={{user `psmodules_root_folder`}}"
],
"scripts":[ "scripts":[
"{{ template_dir }}/scripts/Installers/Validate-AzureModules.ps1" "{{ template_dir }}/scripts/Installers/Validate-AzureModules.ps1"
] ]

View File

@@ -24,6 +24,7 @@
"imagedata_file": "C:\\imagedata.json", "imagedata_file": "C:\\imagedata.json",
"metadata_file": "C:\\image\\metadata.txt", "metadata_file": "C:\\image\\metadata.txt",
"helper_script_folder": "C:\\Program Files\\WindowsPowerShell\\Modules\\", "helper_script_folder": "C:\\Program Files\\WindowsPowerShell\\Modules\\",
"psmodules_root_folder": "C:\\Modules",
"commit_id": "LATEST", "commit_id": "LATEST",
"install_user": "installer", "install_user": "installer",
"install_password": null, "install_password": null,
@@ -511,6 +512,10 @@
}, },
{ {
"type": "powershell", "type": "powershell",
"environment_vars":[
"TOOLSET_JSON_PATH={{user `toolset_json_path`}}",
"PSMODULES_ROOT_FOLDER={{user `psmodules_root_folder`}}"
],
"scripts":[ "scripts":[
"{{ template_dir }}/scripts/Installers/Install-AzureModules.ps1" "{{ template_dir }}/scripts/Installers/Install-AzureModules.ps1"
] ]
@@ -661,6 +666,10 @@
}, },
{ {
"type": "powershell", "type": "powershell",
"environment_vars":[
"TOOLSET_JSON_PATH={{user `toolset_json_path`}}",
"PSMODULES_ROOT_FOLDER={{user `psmodules_root_folder`}}"
],
"scripts":[ "scripts":[
"{{ template_dir }}/scripts/Installers/Validate-AzureModules.ps1" "{{ template_dir }}/scripts/Installers/Validate-AzureModules.ps1"
] ]

View File

@@ -3,58 +3,35 @@
## Desc: Install Azure PowerShell modules ## Desc: Install Azure PowerShell modules
################################################################################ ################################################################################
$ErrorActionPreference = "Stop"
Set-PSRepository -InstallationPolicy Trusted -Name PSGallery Set-PSRepository -InstallationPolicy Trusted -Name PSGallery
#### NOW The correct Modules need to be saved in C:\Modules # The correct Modules need to be saved in C:\Modules
$installPSModulePath = 'C:\Modules' $installPSModulePath = $env:PSMODULES_ROOT_FOLDER
if(-not (Test-Path -LiteralPath $installPSModulePath)) 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 $null = New-Item -Path $installPSModulePath -ItemType Directory
} }
# Powershell Azure modules to install # Get modules content from toolset
$psAzureModulesToInstall = @{ $modules = (Get-ToolsetContent).azureModules
"azurerm" = @(
"2.1.0"
"3.8.0"
"4.2.1"
"5.1.1"
"6.7.0"
"6.13.1"
)
"azure" = @( $psModuleMachinePath = ""
"2.1.0"
"3.8.0"
"4.2.1"
"5.1.1"
"5.3.0"
)
"az" = @( foreach ($module in $modules)
"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)
{ {
Write-Host "Installing '$psmoduleName' to the '$installPSModulePath' path:" $moduleName = $module.name
$psmoduleVersions = $psAzureModulesToInstall[$psmoduleName]
foreach($psmoduleVersion in $psmoduleVersions) Write-Host "Installing ${moduleName} to the ${installPSModulePath} path..."
foreach ($version in $module.versions)
{ {
$psmodulePath = Join-Path $installPSModulePath "${psmoduleName}_${psmoduleVersion}" $modulePath = Join-Path -Path $installPSModulePath -ChildPath "${moduleName}_${version}"
Write-Host " - $psmoduleVersion [$psmodulePath]" Write-Host " - $version [$modulePath]"
try try
{ {
Save-Module -Path $psmodulePath -Name $psmoduleName -RequiredVersion $psmoduleVersion -Force -ErrorAction Stop Save-Module -Path $modulePath -Name $moduleName -RequiredVersion $version -Force -ErrorAction Stop
} }
catch catch
{ {
@@ -62,8 +39,17 @@ foreach($psmoduleName in $psAzureModulesToInstall.Keys)
exit 1 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 # Add modules to the PSModulePath
$finalModulePath = '{0};{1};{2}' -f "${installPSModulePath}\azurerm_2.1.0", "${installPSModulePath}\azure_2.1.0", $env:PSModulePath $psModuleMachinePath += $env:PSModulePath
[Environment]::SetEnvironmentVariable("PSModulePath", $finalModulePath, "Machine") [Environment]::SetEnvironmentVariable("PSModulePath", $psModuleMachinePath, "Machine")

View File

@@ -3,41 +3,42 @@
## Desc: Validate Azure PowerShell modules ## Desc: Validate Azure PowerShell modules
################################################################################ ################################################################################
function Validate-AzureModule $ErrorActionPreference = "Stop"
{
param([String]$ModuleName, [String[]]$ModuleVersions)
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}" $moduleName = $module.name
# Import each module in PowerShell session $modulePath = Join-Path -Path $modulesRootPath -ChildPath "$($module.name)_${version}"
$job = Start-Job -ScriptBlock {
param($modulePath, $moduleName) Write-Host "Trying to import ${moduleName}_${version}..."
$testJob = Start-Job -ScriptBlock {
param (
$modulePath,
$moduleName
)
$env:PsModulePath = "$modulePath;$env:PsModulePath" $env:PsModulePath = "$modulePath;$env:PsModulePath"
Import-Module -Name $moduleName Import-Module -Name $moduleName
Get-Module -Name $moduleName Get-Module -Name $moduleName
} -ArgumentList $modulePath, $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 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

View File

@@ -74,5 +74,43 @@
"1.72.0" "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"
]
}
] ]
} }

View File

@@ -83,5 +83,43 @@
"1.72.0" "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"
]
}
] ]
} }