Skip to content
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

Add force option to win psrepository copy #599

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
@@ -0,0 +1,3 @@
---
minor_changes:
- win_psrepository_copy - Add Force option that deletes repositories that are not present in the source
29 changes: 19 additions & 10 deletions plugins/modules/win_psrepository_copy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ $spec = @{
'NetworkService'
)
}
force = @{
type = 'bool'
default = $false
}
}
}

Expand Down Expand Up @@ -206,17 +210,22 @@ foreach ($user in $profiles) {
$cur_repos = @{}
}

$new_repos = $cur_repos.Clone()
$updated = $false

$src_repos.Keys |
Select-Wildcard -Include $module.Params.name -Exclude $module.Params.exclude |
ForEach-Object -Process {
# explicit scope used inside ForEach-Object to satisfy lint (PSUseDeclaredVarsMoreThanAssignment)
# see https://github.com/PowerShell/PSScriptAnalyzer/issues/827
$Script:updated = $true
$Script:new_repos[$_] = $Script:src_repos[$_]
}
if ($module.Params.force) {
$new_repos = $src_repos.Clone()
$updated = $true
}
else {
$new_repos = $cur_repos.Clone()
$src_repos.Keys |
Select-Wildcard -Include $module.Params.name -Exclude $module.Params.exclude |
ForEach-Object -Process {
# explicit scope used inside ForEach-Object to satisfy lint (PSUseDeclaredVarsMoreThanAssignment)
# see https://github.com/PowerShell/PSScriptAnalyzer/issues/827
$Script:updated = $true
$Script:new_repos[$_] = $Script:src_repos[$_]
}
}

$module.Diff.before[$username] = $cur_repos
$module.Diff.after[$username] = $new_repos
Expand Down
6 changes: 6 additions & 0 deletions plugins/modules/win_psrepository_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
- systemprofile
- LocalService
- NetworkService
force:
description:
- If C(True), this deletes repositories that are not present in the source.
type: bool
default: False
tgoetheyn marked this conversation as resolved.
Show resolved Hide resolved
version_added: 2.4.0
notes:
- Does not require the C(PowerShellGet) module or any other external dependencies.
- User profiles are loaded from the registry. If a given path does not exist (like if the profile directory was deleted), it is silently skipped.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Collections.Hashtable</T>
<T>System.Object</T>
</TN>
<DCT>
<En>
<S N="Key">CustomUserRepo</S>
<Obj N="Value" RefId="1">
<TN RefId="1">
<T>Microsoft.PowerShell.Commands.PSRepository</T>
<T>System.Management.Automation.PSCustomObject</T>
<T>System.Object</T>
</TN>
<MS>
<S N="Name">CustomUserRepo</S>
<S N="SourceLocation">\\domain\share\CustomUserRepo\</S>
<S N="PublishLocation">\\domain\share\CustomUserRepo\</S>
<S N="ScriptSourceLocation">\\domain\share\CustomUserRepo\</S>
<S N="ScriptPublishLocation">\\domain\share\CustomUserRepo\</S>
<B N="Trusted">false</B>
<B N="Registered">true</B>
<S N="InstallationPolicy">Untrusted</S>
<S N="PackageManagementProvider">NuGet</S>
<Obj N="ProviderOptions" RefId="2">
<TNRef RefId="0" />
<DCT />
</Obj>
</MS>
</Obj>
</En>
<En>
<S N="Key">PSGallery</S>
<Obj N="Value" RefId="3">
<TN RefId="2">
<T>Deserialized.Microsoft.PowerShell.Commands.PSRepository</T>
<T>Deserialized.System.Management.Automation.PSCustomObject</T>
<T>Deserialized.System.Object</T>
</TN>
<MS>
<S N="Name">PSGallery</S>
<S N="SourceLocation">https://www.powershellgallery.com/api/v2</S>
<S N="PublishLocation">https://www.powershellgallery.com/api/v2/package/</S>
<S N="ScriptSourceLocation">https://www.powershellgallery.com/api/v2/items/psscript</S>
<S N="ScriptPublishLocation">https://www.powershellgallery.com/api/v2/package/</S>
<Obj N="Trusted" RefId="4">
<TN RefId="3">
<T>System.Management.Automation.SwitchParameter</T>
<T>System.ValueType</T>
<T>System.Object</T>
</TN>
<ToString>False</ToString>
<Props>
<B N="IsPresent">false</B>
</Props>
</Obj>
<B N="Registered">true</B>
<S N="InstallationPolicy">Untrusted</S>
<S N="PackageManagementProvider">NuGet</S>
<Obj N="ProviderOptions" RefId="5">
<TN RefId="4">
<T>Deserialized.System.Collections.Hashtable</T>
<T>Deserialized.System.Object</T>
</TN>
<DCT />
</Obj>
</MS>
</Obj>
</En>
<En>
<S N="Key">FakeGet</S>
<Obj N="Value" RefId="6">
<TNRef RefId="2" />
<MS>
<S N="Name">FakeGet</S>
<S N="SourceLocation">https://www.example.org/api/nuget/v2</S>
<S N="PublishLocation">https://www.example.org/api/nuget/v2/package/</S>
<S N="ScriptSourceLocation">https://www.exampe.org/api/nuget/v2/items/psscript</S>
<S N="ScriptPublishLocation">https://www.example.org/api/nuget/v2/package/</S>
<B N="Trusted">true</B>
<B N="Registered">true</B>
<S N="InstallationPolicy">Trusted</S>
<S N="PackageManagementProvider">NuGet</S>
<Obj N="ProviderOptions" RefId="7">
<TNRef RefId="4" />
<DCT />
</Obj>
</MS>
</Obj>
</En>
</DCT>
</Obj>
</Objs>
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@

- import_tasks: test_include_repo.yml

- import_tasks: test_force_profiles.yml

always:
- name: Reset
import_tasks: reset.yml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This file is part of Ansible

# Copyright: (c) 2025, Tony Goetheyn <@tgoetheyn>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
---
#####################################
## Begin force copy repo (inclusive) tests

- name: Reset
import_tasks: reset.yml

- name: Put User Custom repofile in place
ansible.windows.win_copy:
src: UserCustomRepositories.xml
dest: "{{ remote_tmp_dir }}"
force: true

- name: Set UserCustomRepositories for users
community.windows.win_psrepository_copy:
source: '{{ remote_tmp_dir }}\UserCustomRepositories.xml'

- name: Force copy repos - check
community.windows.win_psrepository_copy:
force: true
register: status
check_mode: true

- assert:
that: status is changed

- name: Force copy repos
community.windows.win_psrepository_copy:
force: true
register: status

- assert:
that: status is changed

- name: Force copy repos - again
community.windows.win_psrepository_copy:
force: true
register: status

- assert:
that: status is not changed

- name: Test if only the repos in the SampleRepositories.xml are present
include_tasks:
file: test_by_user.yml
apply:
vars:
user: "{{ item }}"
expected_repos: "{{ test_repos }}"
loop: "{{ test_users | map(attribute='name') | list }}"

- name: Remove UserCustomRepositories file
ansible.windows.win_file:
path: '{{ remote_tmp_dir }}\UserCustomRepositories.xml'
state: absent

## End force copy repo (inclusive) tests
#####################################
Loading