mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Implemented support for Repository selection when installing module (#39909)
* Implemented support for Repository selection when installing module * Fixed AllowClobber check * Fixing missing AllowClobber argument * Fixed missing -Force switch * Test installing package from custom repo * Added custom repo variables * Fixed repo registration * Modified output check * Modified output check * Adding debug output * Modified update handling * Fixed output check * Added missing PowerShellGet 1.6.0 install * Added error handling * Modified test output check * Fixing output check * Fixing output filter * Implemented registering custom repo * Fixing registering custom repo * Fixing registering custom repo * Fixing registering custom repo * Tweaked module to shorten execution time * Removed installing PowerShellGet 1.6.0 * Re-added Install-NugetProvider
This commit is contained in:
parent
8deced3e04
commit
7d2012fdd7
4 changed files with 103 additions and 10 deletions
|
@ -108,6 +108,7 @@ Function Install-PsModule {
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory=$true)]
|
[Parameter(Mandatory=$true)]
|
||||||
[string]$Name,
|
[string]$Name,
|
||||||
|
[string]$Repository,
|
||||||
[bool]$AllowClobber,
|
[bool]$AllowClobber,
|
||||||
[bool]$CheckMode
|
[bool]$CheckMode
|
||||||
)
|
)
|
||||||
|
@ -117,15 +118,27 @@ Function Install-PsModule {
|
||||||
else {
|
else {
|
||||||
try{
|
try{
|
||||||
# Install NuGet Provider if needed
|
# Install NuGet Provider if needed
|
||||||
Install-NugetProvider -CheckMode $CheckMode
|
Install-NugetProvider -CheckMode $CheckMode;
|
||||||
|
|
||||||
# Check Powershell Version (-AllowClobber was introduced in early version only)
|
$ht = @{
|
||||||
if ($PsVersion.Minor -ge 1){
|
Name = $Name;
|
||||||
Install-Module -Name $Name -Force -ErrorAction Stop -Whatif:$CheckMode -AllowClobber:$AllowClobber | out-null
|
WhatIf = $CheckMode;
|
||||||
|
ErrorAction = "Stop";
|
||||||
|
Force = $true;
|
||||||
|
};
|
||||||
|
|
||||||
|
# If specified, use repository name to select module source
|
||||||
|
if ($Repository) {
|
||||||
|
$ht["Repository"] = "$Repository";
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
Install-Module -Name $Name -Force -ErrorAction Stop -Whatif:$CheckMode | out-null
|
# Check Powershell Version (-AllowClobber was introduced in PowerShellGet 1.6.0)
|
||||||
|
if ("AllowClobber" -in ((Get-Command PowerShellGet\Install-Module | Select -ExpandProperty Parameters).Keys)) {
|
||||||
|
$ht['AllowClobber'] = $AllowClobber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Install-Module @ht | out-null;
|
||||||
|
|
||||||
$result.output = "Module $($Name) installed"
|
$result.output = "Module $($Name) installed"
|
||||||
$result.changed = $true
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
|
@ -174,7 +187,8 @@ if ($state -eq "present") {
|
||||||
else {
|
else {
|
||||||
$ErrorMessage = "Repository Name and Url are mandatory if you want to add a new repository"
|
$ErrorMessage = "Repository Name and Url are mandatory if you want to add a new repository"
|
||||||
}
|
}
|
||||||
Install-PsModule -Name $Name -CheckMode $check_mode -AllowClobber $allow_clobber
|
|
||||||
|
Install-PsModule -Name $Name -Repository $repo -CheckMode $check_mode -AllowClobber $allow_clobber;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ($repo) {
|
if ($repo) {
|
||||||
|
|
|
@ -30,10 +30,10 @@ options:
|
||||||
default: 'no'
|
default: 'no'
|
||||||
repository:
|
repository:
|
||||||
description:
|
description:
|
||||||
- Name of the custom repository to register.
|
- Name of the custom repository to register or use.
|
||||||
url:
|
url:
|
||||||
description:
|
description:
|
||||||
- URL of the custom repository.
|
- URL of the custom repository to register.
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- If C(present) a new module is installed.
|
- If C(present) a new module is installed.
|
||||||
|
@ -61,6 +61,12 @@ EXAMPLES = '''
|
||||||
url: https://myrepo.com
|
url: https://myrepo.com
|
||||||
state: present
|
state: present
|
||||||
|
|
||||||
|
- name: Add a powershell module from a specific repository
|
||||||
|
win_psmodule:
|
||||||
|
name: PowershellModule
|
||||||
|
repository: MyRepository
|
||||||
|
state: present
|
||||||
|
|
||||||
- name: Remove a powershell module
|
- name: Remove a powershell module
|
||||||
win_psmodule:
|
win_psmodule:
|
||||||
name: PowershellModule
|
name: PowershellModule
|
||||||
|
|
|
@ -3,3 +3,5 @@
|
||||||
powershell_module: powershell-yaml
|
powershell_module: powershell-yaml
|
||||||
wrong_module: powershell_yaml
|
wrong_module: powershell_yaml
|
||||||
allow_clobber_module: PowerShellCookbook
|
allow_clobber_module: PowerShellCookbook
|
||||||
|
custom_repo_path: C:\_repo
|
||||||
|
custom_repo_name: PSRegisterRepo
|
||||||
|
|
|
@ -132,5 +132,76 @@
|
||||||
- "module_uninstall_2 is not changed"
|
- "module_uninstall_2 is not changed"
|
||||||
- "module_uninstall_3 is changed"
|
- "module_uninstall_3 is changed"
|
||||||
|
|
||||||
|
- name: Create repository path
|
||||||
|
win_file:
|
||||||
|
path: "{{custom_repo_path}}"
|
||||||
|
state: directory
|
||||||
|
|
||||||
|
- name: Make sure sample module is uninstalled
|
||||||
|
win_psmodule:
|
||||||
|
name: "{{ powershell_module }}"
|
||||||
|
state: absent
|
||||||
|
register: module_uninstall_4
|
||||||
|
|
||||||
|
- name: Copy some module to custom repo
|
||||||
|
win_shell: |
|
||||||
|
|
||||||
|
# Need PSGet 1.6.0 for publishing and named repo usage
|
||||||
|
$psg = [PSCustomObject]@{ n="PowerShellGet"; v="1.6.0"};
|
||||||
|
Remove-Module -Name $psg.n -Force -EA SilentlyContinue;
|
||||||
|
Import-PackageProvider -Name $psg.n -RequiredVersion $psg.v -EV missingProvider -Force | Out-Null;
|
||||||
|
|
||||||
|
if($missingProvider){
|
||||||
|
Install-PackageProvider -Name $psg.n -RequiredVersion $psg.v -Confirm:$false -Force | Out-Null;
|
||||||
|
|
||||||
|
# Unload previous version
|
||||||
|
Remove-Module -Name $psg.n -Force -EA SilentlyContinue;
|
||||||
|
Import-PackageProvider -Name $psg.n -RequiredVersion $psg.v -Force | Out-Null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$modName = "{{powershell_module}}";
|
||||||
|
$temp = $env:Temp;
|
||||||
|
|
||||||
|
Save-Module -Name $modName -Repository PSGallery -Path $temp | Out-Null;
|
||||||
|
|
||||||
|
$repoName = "{{custom_repo_name}}";
|
||||||
|
$repoPath = "{{custom_repo_path}}";
|
||||||
|
|
||||||
|
if(!(Test-Path $repoPath)){
|
||||||
|
New-Item -Type Directory $repoPath -Force | Out-Null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Register-PSRepository -Name $repoName -SourceLocation $repoPath -InstallationPolicy Trusted | Out-Null;
|
||||||
|
|
||||||
|
Publish-Module -Path "$temp\\$modName" -Repository $repoName -Force -Confirm:$false | Out-Null;
|
||||||
|
Get-ChildItem "$repoPath\\*" | ? Name -match "$modName.*.nupkg" | % Name;
|
||||||
|
|
||||||
|
register: saved_package
|
||||||
|
|
||||||
|
- name: Validate sample module in custom repo
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- "powershell_module in (saved_package.stdout_lines | last)"
|
||||||
|
|
||||||
|
- name: Install module from custom Powershell repository
|
||||||
|
win_psmodule:
|
||||||
|
name: "{{ powershell_module }}"
|
||||||
|
state: present
|
||||||
|
repository: "{{custom_repo_name}}"
|
||||||
|
url: "{{custom_repo_path}}"
|
||||||
|
register: module_from_custom_repo
|
||||||
|
|
||||||
|
- name: Test custom Powershell repository module install
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- "module_from_custom_repo is changed"
|
||||||
|
- "module_from_custom_repo.output == 'Module {{ powershell_module }} installed'"
|
||||||
|
|
||||||
|
- name: Verify module was installed from custom repo
|
||||||
|
win_shell: (Get-InstalledModule -Name "{{powershell_module}}").Repository
|
||||||
|
register: is_package_customrepo
|
||||||
|
|
||||||
|
- name: Validate sample module is installed from custom repo
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- "is_package_customrepo.stdout_lines[0] == custom_repo_name"
|
||||||
|
|
Loading…
Reference in a new issue