Skip to content

Internal/az.installer #12817

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
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Module manifest for module 'Az.Installer'
# Module manifest for module 'Az.Tools.Installer'
#
# Generated by: Microsoft Corporation
#
Expand All @@ -9,7 +9,7 @@
@{

# Script module or binary module file associated with this manifest.
RootModule = 'Az.Installer.psm1'
RootModule = 'Az.Tools.Installer.psm1'

# Version number of this module.
ModuleVersion = '0.1.0'
Expand Down 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 = @()
CmdletsToExport = @('Install-AzModule')

# Variables to export from this module
# VariablesToExport = @()
Expand Down
210 changes: 210 additions & 0 deletions tools/Az.Tools.Installer/exports/Install-AzModule.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
# ----------------------------------------------------------------------------------
#
# 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 Install-AzModule{
[OutputType([System.Collections.Hashtable])]
[CmdletBinding(DefaultParameterSetName = 'All', PositionalBinding = $false, SupportsShouldProcess)]
param(
[Parameter(ParameterSetName = 'All',HelpMessage = 'Maximum Az Version.')]
[Parameter(ParameterSetName = 'ByName',HelpMessage = 'Maximum Az Version.')]
[ValidateNotNullOrEmpty()]
[String]
${MaximumVersion},

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

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

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

[Parameter(HelpMessage = 'Remove given module installed previously.')]
[ValidateNotNullOrEmpty()]
[Switch]
${RemovePrevious},

[Parameter(HelpMessage = 'Remove corresponding 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 = 'AllAndPreview', Mandatory, HelpMessage = 'Allow preview modules to be installed.')]
[Parameter(ParameterSetName = 'ByNameAndPreview', 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)]
[ValidateNotNullOrEmpty()]
[String[]]
${Name}
)

process {

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

$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`")"

switch ($confirmation) {
'Y' {
$PSBoundParameters.Add('Force', $true)
}

'N' {
Return
}
}
}

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

if ($PSBoundParameters.ContainsKey('Name')) {
$PSBoundParameters['Name'] | Foreach-Object {
if ($_ -ne 'Az.Accounts') {
$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 (($PSCmdlet.ParameterSetName -eq 'ByName') -or ($PSCmdlet.ParameterSetName -eq 'All')) {

#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)
}
}
} catch {
Write-Error $_
break
}


if ($PSCmdlet.ParameterSetName -eq 'All') {
#Install Az package
$index.Keys | Foreach-Object {$module += ([PSCustomObject] @{'Name'=$_; 'Version'=$index[$_]})}
} elseif ($PSCmdlet.ParameterSetName -eq 'ByName') {
#Install Az modules by name
$module_name | Foreach-Object {
if (!$index.ContainsKey($_)) {
Write-Warning "module $_ will not be installed since it is not a GAed Az module, please try add -AllowPrerelease option."
} else {
$module += ([PSCustomObject] @{'Name'=$_; 'Version'=$index[$_]})
}
}
}

} else {

#With preview
Write-Warning "this cmdlet will not install preview version for Az.Accounts."

$latest = ' latest'
$prerelease = $true

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

# all latest modules
try {
Find-Module -Name 'Az.*' -Repository $Repository | ForEach-Object {
if (($_.Author -eq $author) -and ($_.CompanyName -eq $company_name) -and ($_.Name -ne 'Az.Accounts')) {
$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 += ([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('RemovePrevious')) {
$module | Foreach-Object {
$name = $_.Name
if ($Force -or $PSCmdlet.ShouldProcess("Remove all previous versions of $name", "All previous $name", "Remove")) {
$_ | remove_installed_module
}
} #| remove_installed_module
}

$module | Foreach-Object {
$name = $_.Name
$version = $_.version
if ($Force -or $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
}
}
109 changes: 109 additions & 0 deletions tools/Az.Tools.Installer/internal/utils.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# ----------------------------------------------------------------------------------
#
# 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 Get-CommandAsString{
[OutputType([String[]])]
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]
${Base},

[ValidateNotNullOrEmpty()]
[HashTable]
${BoundParameter},

[Parameter()]
[HashTable]
${Index}
)

$cmdlist = @()
$cmd = $Base

if ($PSBoundParameters.ContainsKey('BoundParameter')) {
$BoundParameter.Keys | Foreach-Object {
$parameter = ''
if (($BoundParameter[$_].GetType().Name -eq 'Boolean') -or ($BoundParameter[$_].GetType().Name -eq 'SwitchParameter')) {
if ($BoundParameter[$_] -eq $true) {
$parameter = Get-ParameterAsString -Key $_
}
} else {
$parameter = Get-ParameterAsString -Key $_ -Val $BoundParameter[$_]
}

$cmd += $parameter
}
}

if ($PSBoundParameters.ContainsKey('Index')) {
$Index.Keys | Foreach-Object {
$parameter = ''
$parameter += Get-ParameterAsString -Key 'Name' -Val $_
if ($Index[$_] -ne $null) {
$parameter += Get-ParameterAsString -Key 'RequiredVersion' -Val $Index[$_]
}
$cmdlist += $cmd + $parameter
}
} else {
$cmdlist += $cmd
}

return $cmdlist
}

function Get-ParameterAsString{
[OutputType([String])]
[CmdletBinding()]
param(
[String]
${Key},

[String]
${Val}
)

$param = ''

if (($PSBoundParameters.ContainsKey('Key')) -and ($Key.Length -gt 0)) {
$param += ' -' + $Key
}

if (($PSBoundParameters.ContainsKey('Val')) -and ($Val.Length -gt 0)) {
$param += ' ' + $Val
}

return $param
}

function remove_installed_module {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[String]
${Name}
)

process {
Get-InstalledModule -Name $Name -ErrorAction SilentlyContinue | Foreach-Object {
$name = $_.Name
Write-Debug "Remove all previous versions of Module: $name"
$_ | Uninstall-Module -AllVersions -AllowPrerelease
} #| Uninstall-Module -AllVersions -AllowPrerelease
}
}