Skip to content

Commit

Permalink
fb_powershell_module: Use PowerShell exec for improved performance
Browse files Browse the repository at this point in the history
Differential Revision: D50035571

fbshipit-source-id: 3c98750e7ce90b676b7d9102b050ff52b66ad0b2
  • Loading branch information
Gilbert Sanchez authored and facebook-github-bot committed Dec 21, 2023
1 parent 350728c commit 0f6aab9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

resource_name :fb_powershell_apply_config
provides :fb_powershell_apply_config, :os => 'windows'
unified_mode(false) if Chef::VERSION >= 18 # TODO(T144966423)
provides :fb_powershell_apply_config, :os => 'darwin'
provides :fb_powershell_apply_config, :os => 'linux'
unified_mode(false) if Chef::VERSION >= 18 # TODO(T144966423)
description 'This resource was added to fb_powershell to manage configuration in a runtime safe manner.'

default_action :manage

Expand Down
55 changes: 41 additions & 14 deletions cookbooks/fb_powershell/resources/fb_powershell_module.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# vim: syntax=ruby:expandtab:shiftwidth=2:softtabstop=2:tabstop=2
require 'chef/mixin/powershell_exec' unless defined?(Chef::Mixin::PowershellExec)
unified_mode true

resource_name :fb_powershell_module
Expand All @@ -9,36 +9,60 @@
property :module_name,
String,
:required => true,
:name_property => true
:name_property => true,
:description => <<~DOC
Specifies the exact names of the module to manage from the repository.
DOC
property :version,
[Integer, String, Array],
:coerce => proc { |m| Array(m) },
:default => '0'
:default => '0',
:description => <<~DOC
Specifies the version(s) of a single module to install. If there is no match in the repository for the specified version, an error is displayed.
If given a major version it will attempt to get the latest the matches. And so on for minor and release.
See https://learn.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershellget-2.x#-requiredversion
DOC
property :repository, # rubocop:todo Chef/RedundantCode/PropertyWithRequiredAndDefault
String,
:required => true,
:default => 'PSGallery'
:default => 'PSGallery',
:description => <<~DOC
Use the Repository parameter to specify the name of repository from which to download and install a module. Used when multiple repositories are registered.
See https://learn.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershellget-2.x#-repository
DOC
property :skip_publisher_check,
[true, false],
:default => false
:default => false,
:description => <<~DOC
Allows you to install a newer version of a module that already exists on your computer. For example, when an existing module is digitally signed by a trusted publisher but the new version isn't digitally signed by a trusted publisher.
See https://learn.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershellget-2.x#-skippublishercheck
DOC
property :allow_clobber,
[true, false],
:default => false
:default => false,
:description => <<~DOC
Overrides warning messages about installation conflicts about existing commands on a computer. Overwrites existing commands that have the same name as commands being installed by a module.
See https://learn.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershellget-2.x#-allowclobber
DOC
property :scope,
String,
:default => 'AllUsers'
['AllUsers', 'CurrentUser'],
:default => 'AllUsers',
:description => <<~DOC
Specifies the installation scope of the module.
See https://learn.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershellget-2.x#-scope
DOC

load_current_value do |new_resource|
# Returns an array
version powershell_out!(
version powershell_exec(
<<-EOH,
$splat = @{
Name = '#{new_resource.name}'
ListAvailable = $true
}
(Get-Module @splat).Version.ForEach({$_.ToString()})
EOH
).stdout.chomp.split("\r\n").map { |v| Gem::Version.new(v) }
).result.map { |v| Gem::Version.new(v) }
end

action :upgrade do
Expand Down Expand Up @@ -117,7 +141,8 @@
}
Install-Module @splat
EOH
powershell_out!(psscript)
psexec = powershell_exec(psscript)
psexec.error! # Throw if there's an error
end
end
end
Expand Down Expand Up @@ -166,7 +191,8 @@
Install-Module @splat
EOH

powershell_out!(psscript)
psexec = powershell_exec(psscript)
psexec.error! # Throw if there's an error
end
end
end
Expand Down Expand Up @@ -203,7 +229,7 @@ def get_repo_list
"Fetching all versions of #{new_resource.module_name} " +
"from #{new_resource.repository}.",
)
latest = powershell_out!(
psexec = powershell_exec(
<<-EOH,
$splat = @{
Name = "#{new_resource.module_name}"
Expand All @@ -212,7 +238,8 @@ def get_repo_list
}
(Find-Module @splat).Version.ForEach({$_.ToString()})
EOH
).stdout.to_s.chomp.split("\r\n")
).error!
latest = psexec.result
Chef::Log.debug("Available versions: #{latest.join(', ')}")

return latest.map { |v| Gem::Version.new(v) }
Expand Down

0 comments on commit 0f6aab9

Please sign in to comment.