Skip to content

Commit

Permalink
Internal/az.installer (#12860)
Browse files Browse the repository at this point in the history
* 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

* add uninstall-AzModule
  • Loading branch information
VeryEarly authored Sep 4, 2020
1 parent ddcec55 commit cc9d652
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 45 deletions.
2 changes: 1 addition & 1 deletion tools/Az.Tools.Installer/Az.Tools.Installer.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
FunctionsToExport = '*'

# 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.
CmdletsToExport = @('Install-AzModule')
CmdletsToExport = @('Install-AzModule', 'Uninstall-AzModule')

# Variables to export from this module
# VariablesToExport = @()
Expand Down
86 changes: 47 additions & 39 deletions tools/Az.Tools.Installer/exports/Install-AzModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@
# ----------------------------------------------------------------------------------

function Install-AzModule{
[OutputType([System.Collections.Hashtable])]
[CmdletBinding(DefaultParameterSetName = 'All', PositionalBinding = $false, SupportsShouldProcess)]
[OutputType()]
[CmdletBinding(DefaultParameterSetName = 'WithoutPreview',
PositionalBinding = $false,
SupportsShouldProcess)]
param(
[Parameter(ParameterSetName = 'All',HelpMessage = 'Maximum Az Version.')]
[Parameter(ParameterSetName = 'ByName',HelpMessage = 'Maximum Az Version.')]
[Parameter(ParameterSetName = 'WithoutPreview',HelpMessage = 'Maximum Az Version.')]
[ValidateNotNullOrEmpty()]
[String]
${MaximumVersion},

[Parameter(ParameterSetName = 'All',HelpMessage = 'Minimum Az Version.')]
[Parameter(ParameterSetName = 'ByName',HelpMessage = 'Minimum Az Version.')]
[Parameter(ParameterSetName = 'WithoutPreview',HelpMessage = 'Minimum Az Version.')]
[ValidateNotNullOrEmpty()]
[String]
${MinimumVersion},

[Parameter(ParameterSetName = 'All',HelpMessage = 'Required Az Version.')]
[Parameter(ParameterSetName = 'ByName',HelpMessage = 'Required Az Version.')]
[Parameter(ParameterSetName = 'WithoutPreview',HelpMessage = 'Required Az Version.')]
[ValidateNotNullOrEmpty()]
[String]
${RequiredVersion},
Expand All @@ -44,7 +43,7 @@ function Install-AzModule{
[Switch]
${RemovePrevious},

[Parameter(HelpMessage = 'Remove corresponding AzureRm modules.')]
[Parameter(HelpMessage = 'Remove all AzureRm modules.')]
[ValidateNotNullOrEmpty()]
[Switch]
${RemoveAzureRm},
Expand All @@ -54,17 +53,20 @@ function Install-AzModule{
[Switch]
${Force},

[Parameter(ParameterSetName = 'AllAndPreview', Mandatory, HelpMessage = 'Allow preview modules to be installed.')]
[Parameter(ParameterSetName = 'ByNameAndPreview', Mandatory, HelpMessage = 'Allow preview modules to be installed.')]
[Parameter(ParameterSetName = 'WithPreview', Mandatory, HelpMessage = 'Allow preview modules to be installed.')]
[ValidateNotNullOrEmpty()]
[Switch]
${AllowPrerelease},

[Parameter(ParameterSetName = 'ByName', Mandatory, HelpMessage = 'Az modules to install.', ValueFromPipelineByPropertyName = $true)]
[Parameter(ParameterSetName = 'ByNameAndPreview', Mandatory, HelpMessage = 'Az modules to install.', ValueFromPipelineByPropertyName = $true)]
[Parameter(HelpMessage = 'Az modules to install.', ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[String[]]
${Name}
${Name},

[Parameter(HelpMessage = 'Skip publisher check.')]
[ValidateNotNullOrEmpty()]
[Switch]
${SkipPublisherCheck}
)

process {
Expand All @@ -74,9 +76,8 @@ function Install-AzModule{
$author = 'Microsoft Corporation'
$company_name = 'azure-sdk'

if (!$PSBoundParameters.ContainsKey('Force')) {
$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`")"

If ((Get-PSRepository -Name $Repository).InstallationPolicy -ne 'Trusted' -and !$PSBoundParameters.ContainsKey('Force')) {
$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"$Repository"?`n[Y] Yes [N] No (default is `"N`")"
switch ($confirmation) {
'Y' {
$PSBoundParameters.Add('Force', $true)
Expand All @@ -92,7 +93,8 @@ function Install-AzModule{
$version = @{}
$module = @()
$latest = ''
$prerelease = $false
$skip_publisher_check = $false
$allow_prerelease = $false

if ($PSBoundParameters.ContainsKey('Name')) {
$PSBoundParameters['Name'] | Foreach-Object {
Expand All @@ -114,7 +116,19 @@ function Install-AzModule{
$version.Add('RequiredVersion', $PSBoundParameters['RequiredVersion'])
}

if (($PSCmdlet.ParameterSetName -eq 'ByName') -or ($PSCmdlet.ParameterSetName -eq 'All')) {
if ($PSBoundParameters.ContainsKey('SkipPublisherCheck')) {
$skip_publisher_check = $SkipPublisherCheck
}

if ($PSBoundParameters.ContainsKey('AllowPrerelease')) {
$allow_prerelease = $AllowPrerelease
}

if ($PSBoundParameters.ContainsKey('Name')) {
$Name = FullAzName -Name $Name
}

if ($PSCmdlet.ParameterSetName -eq 'WithoutPreview') {

#Without preview
$parameter = @{}
Expand All @@ -132,15 +146,14 @@ function Install-AzModule{
}
}
} catch {
Write-Error $_
Write-Error "No related Az modules were found in $Repository"
break
}


if ($PSCmdlet.ParameterSetName -eq 'All') {
if (!$PSBoundParameters.ContainsKey('Name')) {
#Install Az package
$index.Keys | Foreach-Object {$module += ([PSCustomObject] @{'Name'=$_; 'Version'=$index[$_]})}
} elseif ($PSCmdlet.ParameterSetName -eq 'ByName') {
} else {
#Install Az modules by name
$module_name | Foreach-Object {
if (!$index.ContainsKey($_)) {
Expand All @@ -156,10 +169,9 @@ function Install-AzModule{
#With preview
Write-Warning "this cmdlet will not install preview version for Az.Accounts."

$latest = ' latest'
$prerelease = $true
$latest = ' latest version of'

if ($PSCmdlet.ParameterSetName -eq 'AllAndPreview') {
if (!$PSBoundParameters.ContainsKey('Name')) {

# all latest modules
try {
Expand All @@ -180,31 +192,27 @@ function Install-AzModule{
$module_name | Foreach-Object {$module += ([PSCustomObject] @{'Name'=$_})}
}

if ($PSBoundParameters.ContainsKey('RemoveAzureRm') -and ($Force -or $PSCmdlet.ShouldProcess('Remove AzureRm modules', 'AzureRm modules', 'Remove'))) {
remove_installed_module -Name 'AzureRm*'
remove_installed_module -Name 'Azure.*'
if ($PSBoundParameters.ContainsKey('RemoveAzureRm') -and ($PSCmdlet.ShouldProcess('Remove AzureRm modules', 'AzureRm modules', 'Remove'))) {
remove_installed_module -Name 'AzureRm*' -AllVersion
remove_installed_module -Name 'Azure.*' -AllVersion
}

if ($PSBoundParameters.ContainsKey('RemovePrevious')) {
$module | Foreach-Object {
$name = $_.Name
if ($Force -or $PSCmdlet.ShouldProcess("Remove all previous versions of $name", "All previous $name", "Remove")) {
$_ | remove_installed_module
if ($PSCmdlet.ShouldProcess("Remove all previous versions of $name", "All previous $name", "Remove")) {
$_ | remove_installed_module -AllVersion
}
} #| remove_installed_module
}
}

$module | Foreach-Object {
$name = $_.Name
$version = $_.version
if ($Force -or $PSCmdlet.ShouldProcess("Install$latest $name $version", "$latest $name $version", "Install")) {
if ($PSCmdlet.ShouldProcess("Install$latest $name $version", "$latest $name $version", "Install")) {
Write-Debug "Install$latest $name $version"
if ($prerelease) {
$_ | Install-Module -Repository $Repository -AllowClobber -Force -AllowPrerelease
} else {
$_ | Install-Module -Repository $Repository -AllowClobber -Force
}
$_ | Install-Module -Repository $Repository -AllowClobber -Force -AllowPrerelease:$allow_prerelease -SkipPublisherCheck:$skip_publisher_check
}
} #| Install-Module -Repository $Repository -AllowClobber -Force -AllowPrerelease
}
}
}
183 changes: 183 additions & 0 deletions tools/Az.Tools.Installer/exports/Uninstall-AzModule.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# ----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------

function Uninstall-AzModule {
[OutputType()]
[CmdletBinding(DefaultParameterSetName = 'WithoutPreview', PositionalBinding = $false, SupportsShouldProcess = $true)]
param(
[Parameter(ParameterSetName = 'WithoutPreview',HelpMessage = 'Maximum Az Version.')]
[ValidateNotNullOrEmpty()]
[String]
${MaximumVersion},

[Parameter(ParameterSetName = 'WithoutPreview',HelpMessage = 'Minimum Az Version.')]
[ValidateNotNullOrEmpty()]
[String]
${MinimumVersion},

[Parameter(ParameterSetName = 'WithoutPreview',HelpMessage = 'Required Az Version.')]
[ValidateNotNullOrEmpty()]
[String]
${RequiredVersion},

[Parameter(HelpMessage = 'Remove all AzureRm modules.')]
[ValidateNotNullOrEmpty()]
[Switch]
${RemoveAzureRm},

[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.')]
[ValidateNotNullOrEmpty()]
[Switch]
${Force},

[Parameter(ParameterSetName = 'WithPreview', Mandatory, HelpMessage = 'Allow preview modules to be installed.')]
[Parameter(ParameterSetName = 'WithPreviewAndAllVersion', Mandatory, HelpMessage = 'Allow preview modules to be installed.')]
[ValidateNotNullOrEmpty()]
[Switch]
${AllowPrerelease},

[Parameter(HelpMessage = 'Az modules to install.', ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[String[]]
${Name},

[Parameter(ParameterSetName = 'WithPreviewAndAllVersion', Mandatory, HelpMessage = 'Remove all versions')]
[Parameter(ParameterSetName = 'WithoutPreviewAndAllVersion', Mandatory, HelpMessage = 'Remove all versions')]
[Switch]
${AllVersion},

[Parameter(ParameterSetName = 'WithoutPreview', Mandatory, HelpMessage = 'The Registered Repostory.')]
[Parameter(ParameterSetName = 'WithoutPreviewAndAllVersion', Mandatory, HelpMessage = 'The Registered Repostory.')]
[ValidateNotNullOrEmpty()]
[String]
${Repository}
)

process {
Import-Module "$PSScriptRoot\..\internal\utils.psm1"

$author = 'Microsoft Corporation'
$company_name = 'azure-sdk'

[System.Collections.ArrayList]$module_name = @()
$version = @{}
$module = @{}
$latest = ''

if ($PSBoundParameters.ContainsKey('Name')) {
$PSBoundParameters['Name'] | Foreach-Object {
$module_name += $_
}
}

if ($PSBoundParameters.ContainsKey('MaximumVersion')) {
$version.Add('MaximumVersion', $PSBoundParameters['MaximumVersion'])
}

if ($PSBoundParameters.ContainsKey('MinimumVersion')) {
$version.Add('MinimumVersion', $PSBoundParameters['MinimumVersion'])
}

if ($PSBoundParameters.ContainsKey('RequiredVersion')) {
$version.Add('RequiredVersion', $PSBoundParameters['RequiredVersion'])
}

if ($PSBoundParameters.ContainsKey('Name')) {
$Name = FullAzName -Name $Name
}

if (!$PSBoundParameters.ContainsKey("AllowPrerelease")) {

#Without preview
$parameter = @{}
$parameter.Add('Repository', $Repository)
$parameter.Add('Name', 'Az')
$parameter.Add('ErrorAction', 'Stop')
$index = @{}
$version.Keys | Foreach-Object {$parameter.Add($_, $version[$_])}
$cmd = Get-CommandAsString -Base 'Find-Module' -BoundParameter $parameter

try {
(Invoke-Expression -Command $cmd).Dependencies | Foreach-Object {
if ($_.Name -ne 'Az.Accounts') {
$index.Add($_.Name, $_.RequiredVersion)
} else {
$index.Add($_.Name, $_.MinimumVersion)
}
}
} catch {
Write-Error "No related Az modules were found in $Repository"
break
}

if (!$PSBoundParameters.ContainsKey('Name')) {
#Uninstall Az package
$index.Keys | Foreach-Object {$module.Add($_, $index[$_])}
} else {
#Uninstall Az modules by name
$module_name | Foreach-Object {
if (!$index.ContainsKey($_)) {
Write-Warning "module $_ will not be uninstalled since it is not a GAed Az module, please try adding -AllowPrerelease option."
} else {
$module.Add($_, $index[$_])
}
}
}

} else {

#With preview
$latest = ' latest version of'

if (!$PSBoundParameters.ContainsKey('Name')) {

# all latest modules
try {
Get-InstalledModule -Name 'Az.*' | ForEach-Object {
if (($_.Author -eq $author) -and ($_.CompanyName -eq $company_name)) {
$module_name += $_.Name
}
}
} catch {
Write-Error $_
break
}
}

$remove = @()
$module_name | Where-Object {$_.StartsWith('Az.Tools')} | Foreach-Object {$remove += $_}
$remove | Foreach-Object {$module_name.Remove($_)}
$module_name | Foreach-Object {$module.Add($_, '')}
}

if ($PSBoundParameters.ContainsKey('RemoveAzureRm') -and ($PSCmdlet.ShouldProcess('Remove AzureRm modules', 'AzureRm modules', 'Remove'))) {
remove_installed_module -Name 'AzureRm*' -AllVersion
remove_installed_module -Name 'Azure.*' -AllVersion
}

$module.Keys | Foreach-Object {
$version = $module[$_]
if ($PSCmdlet.ShouldProcess("Uninstall$latest $_ $version", "$latest $_ $version", "Uninstall")) {
Write-Debug "Uninstall$latest $_ $version"
if ($PSBoundParameters.ContainsKey('AllVersion')) {
remove_installed_module -Name $_ -AllVersion
} elseif ($PSBoundParameters.ContainsKey('AllowPrerelease')) {
remove_installed_module -Name $_
} else {
remove_installed_module -Name $_ -RequiredVersion $version
}
}
}
}
}
Loading

0 comments on commit cc9d652

Please sign in to comment.