Skip to content

Commit ddcec55

Browse files
authored
Internal/az.installer (#12817)
* add util functions * add install-azmodule * add log to cmdlets * move verbose messages to debug * move under tools\ * polish warning message * move install-azmodule to exports * export install-azmodule * rename to az.tools.installer
1 parent 64a6838 commit ddcec55

File tree

5 files changed

+322
-3
lines changed

5 files changed

+322
-3
lines changed

src/Installer/Az.Installer.psd1 renamed to tools/Az.Tools.Installer/Az.Tools.Installer.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Module manifest for module 'Az.Installer'
2+
# Module manifest for module 'Az.Tools.Installer'
33
#
44
# Generated by: Microsoft Corporation
55
#
@@ -9,7 +9,7 @@
99
@{
1010

1111
# Script module or binary module file associated with this manifest.
12-
RootModule = 'Az.Installer.psm1'
12+
RootModule = 'Az.Tools.Installer.psm1'
1313

1414
# Version number of this module.
1515
ModuleVersion = '0.1.0'
@@ -72,7 +72,7 @@
7272
FunctionsToExport = '*'
7373

7474
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
75-
CmdletsToExport = @()
75+
CmdletsToExport = @('Install-AzModule')
7676

7777
# Variables to export from this module
7878
# VariablesToExport = @()
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
function Install-AzModule{
16+
[OutputType([System.Collections.Hashtable])]
17+
[CmdletBinding(DefaultParameterSetName = 'All', PositionalBinding = $false, SupportsShouldProcess)]
18+
param(
19+
[Parameter(ParameterSetName = 'All',HelpMessage = 'Maximum Az Version.')]
20+
[Parameter(ParameterSetName = 'ByName',HelpMessage = 'Maximum Az Version.')]
21+
[ValidateNotNullOrEmpty()]
22+
[String]
23+
${MaximumVersion},
24+
25+
[Parameter(ParameterSetName = 'All',HelpMessage = 'Minimum Az Version.')]
26+
[Parameter(ParameterSetName = 'ByName',HelpMessage = 'Minimum Az Version.')]
27+
[ValidateNotNullOrEmpty()]
28+
[String]
29+
${MinimumVersion},
30+
31+
[Parameter(ParameterSetName = 'All',HelpMessage = 'Required Az Version.')]
32+
[Parameter(ParameterSetName = 'ByName',HelpMessage = 'Required Az Version.')]
33+
[ValidateNotNullOrEmpty()]
34+
[String]
35+
${RequiredVersion},
36+
37+
[Parameter(Mandatory, HelpMessage = 'The Registered Repostory.')]
38+
[ValidateNotNullOrEmpty()]
39+
[String]
40+
${Repository},
41+
42+
[Parameter(HelpMessage = 'Remove given module installed previously.')]
43+
[ValidateNotNullOrEmpty()]
44+
[Switch]
45+
${RemovePrevious},
46+
47+
[Parameter(HelpMessage = 'Remove corresponding AzureRm modules.')]
48+
[ValidateNotNullOrEmpty()]
49+
[Switch]
50+
${RemoveAzureRm},
51+
52+
[Parameter(HelpMessage = 'Installs modules and overrides warning messages about module installation conflicts. If a module with the same name already exists on the computer, Force allows for multiple versions to be installed. If there is an existing module with the same name and version, Force overwrites that version.')]
53+
[ValidateNotNullOrEmpty()]
54+
[Switch]
55+
${Force},
56+
57+
[Parameter(ParameterSetName = 'AllAndPreview', Mandatory, HelpMessage = 'Allow preview modules to be installed.')]
58+
[Parameter(ParameterSetName = 'ByNameAndPreview', Mandatory, HelpMessage = 'Allow preview modules to be installed.')]
59+
[ValidateNotNullOrEmpty()]
60+
[Switch]
61+
${AllowPrerelease},
62+
63+
[Parameter(ParameterSetName = 'ByName', Mandatory, HelpMessage = 'Az modules to install.', ValueFromPipelineByPropertyName = $true)]
64+
[Parameter(ParameterSetName = 'ByNameAndPreview', Mandatory, HelpMessage = 'Az modules to install.', ValueFromPipelineByPropertyName = $true)]
65+
[ValidateNotNullOrEmpty()]
66+
[String[]]
67+
${Name}
68+
)
69+
70+
process {
71+
72+
Import-Module "$PSScriptRoot\..\internal\utils.psm1"
73+
74+
$author = 'Microsoft Corporation'
75+
$company_name = 'azure-sdk'
76+
77+
if (!$PSBoundParameters.ContainsKey('Force')) {
78+
$confirmation = Read-Host "You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the `nSet-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?`n[Y] Yes [N] No (default is `"N`")"
79+
80+
switch ($confirmation) {
81+
'Y' {
82+
$PSBoundParameters.Add('Force', $true)
83+
}
84+
85+
'N' {
86+
Return
87+
}
88+
}
89+
}
90+
91+
[System.Collections.ArrayList]$module_name = @()
92+
$version = @{}
93+
$module = @()
94+
$latest = ''
95+
$prerelease = $false
96+
97+
if ($PSBoundParameters.ContainsKey('Name')) {
98+
$PSBoundParameters['Name'] | Foreach-Object {
99+
if ($_ -ne 'Az.Accounts') {
100+
$module_name += $_
101+
}
102+
}
103+
}
104+
105+
if ($PSBoundParameters.ContainsKey('MaximumVersion')) {
106+
$version.Add('MaximumVersion', $PSBoundParameters['MaximumVersion'])
107+
}
108+
109+
if ($PSBoundParameters.ContainsKey('MinimumVersion')) {
110+
$version.Add('MinimumVersion', $PSBoundParameters['MinimumVersion'])
111+
}
112+
113+
if ($PSBoundParameters.ContainsKey('RequiredVersion')) {
114+
$version.Add('RequiredVersion', $PSBoundParameters['RequiredVersion'])
115+
}
116+
117+
if (($PSCmdlet.ParameterSetName -eq 'ByName') -or ($PSCmdlet.ParameterSetName -eq 'All')) {
118+
119+
#Without preview
120+
$parameter = @{}
121+
$parameter.Add('Repository', $Repository)
122+
$parameter.Add('Name', 'Az')
123+
$parameter.Add('ErrorAction', 'Stop')
124+
$index = @{}
125+
$version.Keys | Foreach-Object {$parameter.Add($_, $version[$_])}
126+
$cmd = Get-CommandAsString -Base 'Find-Module' -BoundParameter $parameter
127+
128+
try {
129+
(Invoke-Expression -Command $cmd).Dependencies | Foreach-Object {
130+
if ($_.Name -ne 'Az.Accounts') {
131+
$index.Add($_.Name, $_.RequiredVersion)
132+
}
133+
}
134+
} catch {
135+
Write-Error $_
136+
break
137+
}
138+
139+
140+
if ($PSCmdlet.ParameterSetName -eq 'All') {
141+
#Install Az package
142+
$index.Keys | Foreach-Object {$module += ([PSCustomObject] @{'Name'=$_; 'Version'=$index[$_]})}
143+
} elseif ($PSCmdlet.ParameterSetName -eq 'ByName') {
144+
#Install Az modules by name
145+
$module_name | Foreach-Object {
146+
if (!$index.ContainsKey($_)) {
147+
Write-Warning "module $_ will not be installed since it is not a GAed Az module, please try add -AllowPrerelease option."
148+
} else {
149+
$module += ([PSCustomObject] @{'Name'=$_; 'Version'=$index[$_]})
150+
}
151+
}
152+
}
153+
154+
} else {
155+
156+
#With preview
157+
Write-Warning "this cmdlet will not install preview version for Az.Accounts."
158+
159+
$latest = ' latest'
160+
$prerelease = $true
161+
162+
if ($PSCmdlet.ParameterSetName -eq 'AllAndPreview') {
163+
164+
# all latest modules
165+
try {
166+
Find-Module -Name 'Az.*' -Repository $Repository | ForEach-Object {
167+
if (($_.Author -eq $author) -and ($_.CompanyName -eq $company_name) -and ($_.Name -ne 'Az.Accounts')) {
168+
$module_name += $_.Name
169+
}
170+
}
171+
} catch {
172+
Write-Error $_
173+
break
174+
}
175+
}
176+
177+
$remove = @()
178+
$module_name | Where-Object {$_.StartsWith('Az.Tools')} | Foreach-Object {$remove += $_}
179+
$remove | Foreach-Object {$module_name.Remove($_)}
180+
$module_name | Foreach-Object {$module += ([PSCustomObject] @{'Name'=$_})}
181+
}
182+
183+
if ($PSBoundParameters.ContainsKey('RemoveAzureRm') -and ($Force -or $PSCmdlet.ShouldProcess('Remove AzureRm modules', 'AzureRm modules', 'Remove'))) {
184+
remove_installed_module -Name 'AzureRm*'
185+
remove_installed_module -Name 'Azure.*'
186+
}
187+
188+
if ($PSBoundParameters.ContainsKey('RemovePrevious')) {
189+
$module | Foreach-Object {
190+
$name = $_.Name
191+
if ($Force -or $PSCmdlet.ShouldProcess("Remove all previous versions of $name", "All previous $name", "Remove")) {
192+
$_ | remove_installed_module
193+
}
194+
} #| remove_installed_module
195+
}
196+
197+
$module | Foreach-Object {
198+
$name = $_.Name
199+
$version = $_.version
200+
if ($Force -or $PSCmdlet.ShouldProcess("Install$latest $name $version", "$latest $name $version", "Install")) {
201+
Write-Debug "Install$latest $name $version"
202+
if ($prerelease) {
203+
$_ | Install-Module -Repository $Repository -AllowClobber -Force -AllowPrerelease
204+
} else {
205+
$_ | Install-Module -Repository $Repository -AllowClobber -Force
206+
}
207+
}
208+
} #| Install-Module -Repository $Repository -AllowClobber -Force -AllowPrerelease
209+
}
210+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
function Get-CommandAsString{
16+
[OutputType([String[]])]
17+
[CmdletBinding()]
18+
param(
19+
[Parameter(Mandatory)]
20+
[ValidateNotNullOrEmpty()]
21+
[String]
22+
${Base},
23+
24+
[ValidateNotNullOrEmpty()]
25+
[HashTable]
26+
${BoundParameter},
27+
28+
[Parameter()]
29+
[HashTable]
30+
${Index}
31+
)
32+
33+
$cmdlist = @()
34+
$cmd = $Base
35+
36+
if ($PSBoundParameters.ContainsKey('BoundParameter')) {
37+
$BoundParameter.Keys | Foreach-Object {
38+
$parameter = ''
39+
if (($BoundParameter[$_].GetType().Name -eq 'Boolean') -or ($BoundParameter[$_].GetType().Name -eq 'SwitchParameter')) {
40+
if ($BoundParameter[$_] -eq $true) {
41+
$parameter = Get-ParameterAsString -Key $_
42+
}
43+
} else {
44+
$parameter = Get-ParameterAsString -Key $_ -Val $BoundParameter[$_]
45+
}
46+
47+
$cmd += $parameter
48+
}
49+
}
50+
51+
if ($PSBoundParameters.ContainsKey('Index')) {
52+
$Index.Keys | Foreach-Object {
53+
$parameter = ''
54+
$parameter += Get-ParameterAsString -Key 'Name' -Val $_
55+
if ($Index[$_] -ne $null) {
56+
$parameter += Get-ParameterAsString -Key 'RequiredVersion' -Val $Index[$_]
57+
}
58+
$cmdlist += $cmd + $parameter
59+
}
60+
} else {
61+
$cmdlist += $cmd
62+
}
63+
64+
return $cmdlist
65+
}
66+
67+
function Get-ParameterAsString{
68+
[OutputType([String])]
69+
[CmdletBinding()]
70+
param(
71+
[String]
72+
${Key},
73+
74+
[String]
75+
${Val}
76+
)
77+
78+
$param = ''
79+
80+
if (($PSBoundParameters.ContainsKey('Key')) -and ($Key.Length -gt 0)) {
81+
$param += ' -' + $Key
82+
}
83+
84+
if (($PSBoundParameters.ContainsKey('Val')) -and ($Val.Length -gt 0)) {
85+
$param += ' ' + $Val
86+
}
87+
88+
return $param
89+
}
90+
91+
function remove_installed_module {
92+
[CmdletBinding()]
93+
param(
94+
[Parameter(Mandatory = $true,
95+
ValueFromPipelineByPropertyName = $true,
96+
ValueFromPipeline = $true)]
97+
[ValidateNotNullOrEmpty()]
98+
[String]
99+
${Name}
100+
)
101+
102+
process {
103+
Get-InstalledModule -Name $Name -ErrorAction SilentlyContinue | Foreach-Object {
104+
$name = $_.Name
105+
Write-Debug "Remove all previous versions of Module: $name"
106+
$_ | Uninstall-Module -AllVersions -AllowPrerelease
107+
} #| Uninstall-Module -AllVersions -AllowPrerelease
108+
}
109+
}

0 commit comments

Comments
 (0)