Skip to content

Commit a6d991e

Browse files
authored
Merge pull request #2 from EncoreTechnologies/bugfix/CLOUD-1860_powercli_install_failures
[WIP] Various bug fixes
2 parents bf15de2 + 8046d61 commit a6d991e

File tree

3 files changed

+66
-10
lines changed

3 files changed

+66
-10
lines changed

CHANGELOG.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
## Development
2+
3+
BUG FIXES:
4+
5+
`psrepository` fixes:
6+
- Fixes the inability to register psrepoositorys when none are registered on the node prior to puppet due to bug in `instances_command` expecting a returned hashtable.
7+
- Many fixes around the default powershell gallery repo due to the `flush` command attempting to set the source_location of the repo:
8+
- Fixes the inability to change the installation policy of a pre-existing powershell gallery repo
9+
- Fixes the inability to register the powershell gallery repo
10+
11+
`package` fixes:
12+
- Fixes the inability to upgrade previously installed modules with -AllowClobber. This would previously fail with an error if a cmdlet was moved to a new module. Powershell would error stating the cmdlet exists in the system already within a module and you need to specific -AllowClobber to install the new one.
13+
14+
115
## 2.0.1 (September 6, 2018)
216

317
BUG FIXES:
@@ -17,4 +31,4 @@ IMPROVEMENTS:
1731
BUG FIXES:
1832

1933
* `package` - add :versionable to windowspowershell provider ([#12](https://github.com/hbuckle/puppet-powershellmodule/issues/12))
20-
* `psrepository` - make source_location case sensitive ([#11](https://github.com/hbuckle/puppet-powershellmodule/issues/11))
34+
* `psrepository` - make source_location case sensitive ([#11](https://github.com/hbuckle/puppet-powershellmodule/issues/11))

lib/puppet/provider/package/powershellcore.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def self.instances_command
8484
COMMAND
8585
end
8686

87+
# Command used to install powershell modules
8788
def install_command
8889
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force"
8990
command << " -RequiredVersion #{@resource[:ensure]}" unless [:present, :latest].include? @resource[:ensure]
@@ -92,16 +93,22 @@ def install_command
9293
command
9394
end
9495

96+
# Command to unisntall powershell modules
9597
def uninstall_command
9698
"Uninstall-Module #{@resource[:name]} -AllVersions -Force"
9799
end
98100

101+
# Command to fetch latest version of powershell modules
99102
def latest_command
100103
"$mod = Find-Module #{@resource[:name]} -Repository #{@resource[:source]}; $mod.Version.ToString()"
101104
end
102105

106+
# Command to update powershell modules
103107
def update_command
104-
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force"
108+
# The -AllowClobber here is needed because this command will fail with an error like:
109+
# "This cmdlet exists on the system and could cause a conflict, use -allowclobber to install"
110+
# I believe this is due to a cmdlet moving from one module to another, seen in powercli modules
111+
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force -AllowClobber"
105112
command << " -Repository #{@resource[:source]}" if @resource[:source]
106113
command << " #{install_options(@resource[:install_options])}" if @resource[:install_options]
107114
command

lib/puppet/provider/psrepository/powershellcore.rb

+43-8
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,58 @@ def destroy
4646
@property_hash.clear
4747
end
4848

49+
# The source location for existing psrepos
50+
# You cannot define this for the default psgallery repo
4951
def source_location=(value)
5052
@property_flush[:sourcelocation] = value
5153
end
5254

55+
# The installation policy for existing psrepos
5356
def installation_policy=(value)
5457
@property_flush[:installationpolicy] = value
5558
end
5659

60+
# Sets any pre-existing psrepo to have the proper attributes. Source location, install policy, etc.
5761
def flush
62+
# If any psrepos existed on the system...
5863
unless @property_flush.empty?
64+
# Base block of the command which will be used to true-up psrepos
5965
flush_command = "Set-PSRepository #{@resource[:name]}"
66+
# For each attribute on the psrepos..
6067
@property_flush.each do |key, value|
68+
# If the repo we is powershell gallery, then DROP the source_location key
69+
# If you specify source_location for the PSGallery default repo, it will fail
70+
next if @resource[:name].downcase == 'psgallery' && key == :sourcelocation
71+
72+
# Append that attribute to the true-up command
6173
flush_command << " -#{key} '#{value}'"
6274
end
75+
# launch pwsh to true-up any pre-existing repo with proper settings
6376
self.class.invoke_ps_command flush_command
6477
end
6578
@property_hash = @resource.to_hash
6679
end
6780

81+
# Expected return example when an actual repo is registered:
82+
# {"name":"PSGallery","source_location":"https://www.powershellgallery.com/api/v2","installation_policy":"trusted"}
83+
# When no ps repos are registered it returns:
84+
# WARNING: Unable to find module repositories
85+
# The try, catch here gets around the issue of having no repos
6886
def self.instances_command
6987
<<-COMMAND
70-
@(Get-PSRepository -WarningAction SilentlyContinue).foreach({
71-
[ordered]@{
72-
'name' = $_.Name
73-
'source_location' = $_.SourceLocation
74-
'installation_policy' = $_.InstallationPolicy.ToLower()
75-
} | ConvertTo-Json -Depth 99 -Compress
76-
})
88+
try{
89+
@(Get-PSRepository -ErrorAction Stop -WarningAction Stop 3>$null).foreach({
90+
[ordered]@{
91+
'name' = $_.Name
92+
'source_location' = $_.SourceLocation
93+
'installation_policy' = $_.InstallationPolicy.ToLower()
94+
} | ConvertTo-Json -Depth 99 -Compress
95+
})
96+
}
97+
# If no repos were registered
98+
catch {
99+
exit 0
100+
}
77101
COMMAND
78102
end
79103

@@ -84,7 +108,18 @@ def create_command
84108
SourceLocation = '#{@resource[:source_location]}'
85109
InstallationPolicy = '#{@resource[:installation_policy]}'
86110
}
87-
Register-PSRepository @params
111+
112+
# Detecting if this is Powershell Gallery repo or not
113+
if($params.Name -eq 'PSGallery' -or $params.SourceLocation -match 'powershellgallery'){
114+
# Trim these params or the splatting will fail
115+
$params.Remove('Name')
116+
$params.Remove('SourceLocation')
117+
Register-PSRepository -Default @params
118+
}
119+
# For all non-PSGallery repos..
120+
else{
121+
Register-PSRepository @params
122+
}
88123
COMMAND
89124
end
90125

0 commit comments

Comments
 (0)