diff --git a/README.md b/README.md index 9c3d4c0..33886a2 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ package { 'xPSDesiredStateConfiguration': ensure => latest, provider => 'windowspowershell', source => 'PSGallery', + install_options => [ '-AllowClobber' ] } package { 'Pester': @@ -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 diff --git a/lib/puppet/provider/package/powershellcore.rb b/lib/puppet/provider/package/powershellcore.rb index 837220a..e9bf471 100644 --- a/lib/puppet/provider/package/powershellcore.rb +++ b/lib/puppet/provider/package/powershellcore.rb @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/lib/puppet/provider/package/windowspowershell.rb b/lib/puppet/provider/package/windowspowershell.rb index 859c6d8..c2f8c8f 100644 --- a/lib/puppet/provider/package/windowspowershell.rb +++ b/lib/puppet/provider/package/windowspowershell.rb @@ -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)