Skip to content

Commit

Permalink
Merge pull request #20 from palintir/allowclobber
Browse files Browse the repository at this point in the history
Added support for install_options argument.
  • Loading branch information
hbuckle authored Jan 28, 2019
2 parents b6524cf + 998e819 commit 22f738c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ package { 'xPSDesiredStateConfiguration':
ensure => latest,
provider => 'windowspowershell',
source => 'PSGallery',
install_options => [ '-AllowClobber' ]
}
package { 'Pester':
Expand Down Expand Up @@ -187,6 +188,19 @@ file{"C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Micr

`puppet-powershellmodule` implements a [package type](http://docs.puppet.com/references/latest/type.html#package) with a resource provider, which is built into Puppet.

The implementation supports the [install_options](https://puppet.com/docs/puppet/6.2/type.html#package-attribute-install_options) attribute which can be used to pass additional options to the PowerShell Install-Modules command, e.g.:

```
package { 'xPSDesiredStateConfiguration':
ensure => latest,
provider => 'windowspowershell',
source => 'PSGallery',
install_options => [ '-AllowClobber',
{ '-proxy' => 'http://proxy.local.domain' } ]
}
```

### pspackageprovider

#### Properties/Parameters
Expand Down
30 changes: 29 additions & 1 deletion lib/puppet/provider/package/powershellcore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Puppet::Type.type(:package).provide :powershellcore, parent: Puppet::Provider::Package do
initvars
has_feature :installable, :uninstallable, :upgradeable, :versionable
has_feature :installable, :uninstallable, :upgradeable, :versionable, :install_options
commands pwsh: 'pwsh'

def self.invoke_ps_command(command)
Expand Down Expand Up @@ -44,6 +44,32 @@ def update
self.class.invoke_ps_command update_command
end

# Turns a array of install_options into flags to be passed to a command.
# The options can be passed as a string or hash. Note that passing a hash
# should only be used in case "-foo bar" must be passed,
# Regular flags like '-foobar' must be passed as a string.
# which can be accomplished with:
# install_options => [ '-foobar',{ '-foo' => 'bar' } ]
# This will result in the following being passed as arguments to the command:
# -foobar -foo bar
# @param options [Array]
# @return Concatenated list of options
# @api private
def install_options(options)
return unless options

options.collect do |val|
case val
when Hash
val.keys.sort.collect do |k|
"#{k} #{val[k]}"
end
else
val
end
end.flatten.join(" ")
end

def self.instances_command
# Get-Package is way faster than Get-InstalledModule
<<-COMMAND
Expand All @@ -62,6 +88,7 @@ def install_command
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force"
command << " -RequiredVersion #{@resource[:ensure]}" unless [:present, :latest].include? @resource[:ensure]
command << " -Repository #{@resource[:source]}" if @resource[:source]
command << " #{install_options(@resource[:install_options])}" if @resource[:install_options]
command
end

Expand All @@ -76,6 +103,7 @@ def latest_command
def update_command
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force"
command << " -Repository #{@resource[:source]}" if @resource[:source]
command << " #{install_options(@resource[:install_options])}" if @resource[:install_options]
command
end
end
2 changes: 1 addition & 1 deletion lib/puppet/provider/package/windowspowershell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
initvars
confine operatingsystem: :windows
confine feature: :powershellgetwindows
has_feature :installable, :uninstallable, :upgradeable, :versionable
has_feature :installable, :uninstallable, :upgradeable, :versionable, :install_options
commands powershell: 'powershell'

def self.invoke_ps_command(command)
Expand Down

0 comments on commit 22f738c

Please sign in to comment.