summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMatt Wrock <matt@mattwrock.com>2020-10-30 21:36:27 +0000
committerGitHub <noreply@github.com>2020-10-30 21:36:27 +0000
commit8248fa5b5542840bc6adbeaf32445e008397dc55 (patch)
tree1196f8804fb746d8c5dca2270c786da086770fad /lib
parent4b56eedf7c805d4ba649bd514cad8f16c0d892d2 (diff)
parent31e13be0222e96d3f9b194b2530d6a0a1892a117 (diff)
downloadchef-8248fa5b5542840bc6adbeaf32445e008397dc55.tar.gz
Merge pull request #10545 from chef/out_to_exec
Improve Windows resource performance by converting powershell_out usage to powershell_exec
Diffstat (limited to 'lib')
-rw-r--r--lib/chef/powershell.rb12
-rw-r--r--lib/chef/provider/package/chocolatey.rb8
-rw-r--r--lib/chef/resource/hostname.rb2
-rw-r--r--lib/chef/resource/powershell_package_source.rb37
-rw-r--r--lib/chef/resource/windows_ad_join.rb18
-rw-r--r--lib/chef/resource/windows_certificate.rb10
-rw-r--r--lib/chef/resource/windows_dfs_server.rb11
-rw-r--r--lib/chef/resource/windows_firewall_profile.rb19
-rw-r--r--lib/chef/resource/windows_firewall_rule.rb20
-rw-r--r--lib/chef/resource/windows_security_policy.rb10
-rw-r--r--lib/chef/resource/windows_share.rb36
-rw-r--r--lib/chef/resource/windows_workgroup.rb8
12 files changed, 97 insertions, 94 deletions
diff --git a/lib/chef/powershell.rb b/lib/chef/powershell.rb
index 5063e599c6..4800708dfc 100644
--- a/lib/chef/powershell.rb
+++ b/lib/chef/powershell.rb
@@ -34,8 +34,6 @@ class Chef
# @param script [String] script to run
# @return [Object] output
def initialize(script)
- raise "Chef::PowerShell can only be used on the Windows platform." unless RUBY_PLATFORM.match?(/mswin|mingw32|windows/)
-
@dll ||= "Chef.PowerShell.Wrapper.dll"
exec(script)
end
@@ -65,7 +63,15 @@ class Chef
def exec(script)
FFI.ffi_lib @dll
FFI.attach_function :execute_powershell, :ExecuteScript, [:string], :pointer
- execution = FFI.execute_powershell(script).read_utf16string
+ # This is a temporary fix for running in a Habitat environment
+ # In habitat we set CHEF_POWERSHELL_BIN so that .Net resolves our
+ # managed shim assembly from the correct location.
+ # It seems that that is preventing .Net from successfully loading GAC assemblies
+ # and can break all sorts of edge (and not so edge) scenarios. Once we are actually
+ # inside the powershell run space, we know our shim was loaded and can unset
+ # CHEF_POWERSHELL_BIN which will bypass our custom resolver logic. The real fix is
+ # to fix our resolver. Oh and OH MY GOD this was a pain to track down.
+ execution = FFI.execute_powershell("$ENV:CHEF_POWERSHELL_BIN=$NULL;#{script}").read_utf16string
hashed_outcome = Chef::JSONCompat.parse(execution)
@result = Chef::JSONCompat.parse(hashed_outcome["result"])
@errors = hashed_outcome["errors"]
diff --git a/lib/chef/provider/package/chocolatey.rb b/lib/chef/provider/package/chocolatey.rb
index 498a98f2d0..156568e584 100644
--- a/lib/chef/provider/package/chocolatey.rb
+++ b/lib/chef/provider/package/chocolatey.rb
@@ -151,7 +151,7 @@ class Chef
@choco_exe ||= begin
# if this check is in #define_resource_requirements, it won't get
# run before choco.exe gets called from #load_current_resource.
- exe_path = ::File.join(choco_install_path.to_s, "bin", "choco.exe")
+ exe_path = ::File.join(choco_install_path, "bin", "choco.exe")
raise Chef::Exceptions::MissingLibrary, CHOCO_MISSING_MSG unless ::File.exist?(exe_path)
exe_path
@@ -160,9 +160,9 @@ class Chef
# lets us mock out an incorrect value for testing.
def choco_install_path
- @choco_install_path ||= powershell_out!(
- PATHFINDING_POWERSHELL_COMMAND
- ).stdout.chomp
+ result = powershell_exec!(PATHFINDING_POWERSHELL_COMMAND).result
+ result = "" if result.empty?
+ result
end
# Helper to dispatch a choco command through shell_out using the timeout
diff --git a/lib/chef/resource/hostname.rb b/lib/chef/resource/hostname.rb
index 07a71864c2..27903e5807 100644
--- a/lib/chef/resource/hostname.rb
+++ b/lib/chef/resource/hostname.rb
@@ -240,7 +240,7 @@ class Chef
unless Socket.gethostbyname(Socket.gethostname).first == new_resource.hostname
converge_by "set hostname to #{new_resource.hostname}" do
- powershell_out! <<~EOH
+ powershell_exec! <<~EOH
$sysInfo = Get-WmiObject -Class Win32_ComputerSystem
$sysInfo.Rename("#{new_resource.hostname}")
EOH
diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb
index e17c23abe2..066efc6a72 100644
--- a/lib/chef/resource/powershell_package_source.rb
+++ b/lib/chef/resource/powershell_package_source.rb
@@ -16,7 +16,6 @@
#
require_relative "../resource"
-require_relative "../json_compat"
class Chef
class Resource
@@ -57,11 +56,11 @@ class Chef
load_current_value do
cmd = load_resource_state_script(source_name)
- repo = powershell_out!(cmd)
- if repo.stdout.empty?
+ repo = powershell_exec!(cmd)
+ if repo.result.empty?
current_value_does_not_exist!
else
- status = Chef::JSONCompat.from_json(repo.stdout)
+ status = repo.result
end
url status["url"]
trusted status["trusted"]
@@ -78,28 +77,28 @@ class Chef
if package_source_exists?
converge_if_changed :url, :trusted, :publish_location, :script_source_location, :script_publish_location do
update_cmd = build_ps_repository_command("Set", new_resource)
- res = powershell_out(update_cmd)
- raise "Failed to update #{new_resource.source_name}: #{res.stderr}" unless res.stderr.empty?
+ res = powershell_exec(update_cmd)
+ raise "Failed to update #{new_resource.source_name}: #{res.errors}" if res.error?
end
else
converge_by("register source: #{new_resource.source_name}") do
register_cmd = build_ps_repository_command("Register", new_resource)
- res = powershell_out(register_cmd)
- raise "Failed to register #{new_resource.source_name}: #{res.stderr}" unless res.stderr.empty?
+ res = powershell_exec(register_cmd)
+ raise "Failed to register #{new_resource.source_name}: #{res.errors}" if res.error?
end
end
else
if package_source_exists?
converge_if_changed :url, :trusted, :provider_name do
update_cmd = build_package_source_command("Set", new_resource)
- res = powershell_out(update_cmd)
- raise "Failed to update #{new_resource.source_name}: #{res.stderr}" unless res.stderr.empty?
+ res = powershell_exec(update_cmd)
+ raise "Failed to update #{new_resource.source_name}: #{res.errors}" if res.error?
end
else
converge_by("register source: #{new_resource.source_name}") do
register_cmd = build_package_source_command("Register", new_resource)
- res = powershell_out(register_cmd)
- raise "Failed to register #{new_resource.source_name}: #{res.stderr}" unless res.stderr.empty?
+ res = powershell_exec(register_cmd)
+ raise "Failed to register #{new_resource.source_name}: #{res.errors}" if res.error?
end
end
end
@@ -110,16 +109,16 @@ class Chef
if package_source_exists?
unregister_cmd = "Get-PackageSource -Name '#{new_resource.source_name}' | Unregister-PackageSource"
converge_by("unregister source: #{new_resource.source_name}") do
- res = powershell_out(unregister_cmd)
- raise "Failed to unregister #{new_resource.source_name}: #{res.stderr}" unless res.stderr.empty?
+ res = powershell_exec(unregister_cmd)
+ raise "Failed to unregister #{new_resource.source_name}: #{res.errors}" if res.error?
end
end
end
action_class do
def package_source_exists?
- cmd = powershell_out!("(Get-PackageSource -Name '#{new_resource.source_name}' -WarningAction SilentlyContinue).Name")
- cmd.stdout.downcase.strip == new_resource.source_name.downcase
+ cmd = powershell_exec!("(Get-PackageSource -Name '#{new_resource.source_name}' -ErrorAction SilentlyContinue).Name")
+ !cmd.result.empty? && cmd.result.to_s.downcase.strip == new_resource.source_name.downcase
end
def psrepository_cmdlet_appropriate?
@@ -133,6 +132,7 @@ class Chef
cmd << " -PublishLocation '#{new_resource.publish_location}'" if new_resource.publish_location
cmd << " -ScriptSourceLocation '#{new_resource.script_source_location}'" if new_resource.script_source_location
cmd << " -ScriptPublishLocation '#{new_resource.script_publish_location}'" if new_resource.script_publish_location
+ cmd << " | Out-Null"
cmd
end
@@ -141,6 +141,7 @@ class Chef
cmd << " -Location '#{new_resource.url}'" if new_resource.url
cmd << " -Trusted:#{new_resource.trusted ? "$true" : "$false"}"
cmd << " -ProviderName '#{new_resource.provider_name}'" if new_resource.provider_name
+ cmd << " | Out-Null"
cmd
end
end
@@ -157,11 +158,11 @@ class Chef
if ((Get-PackageSource -Name '#{name}').ProviderName -eq 'PowerShellGet') {
(Get-PSRepository -Name '#{name}') | Select @{n='source_name';e={$_.Name}}, @{n='url';e={$_.SourceLocation}},
@{n='trusted';e={$_.Trusted}}, @{n='provider_name';e={$_.PackageManagementProvider}}, @{n='publish_location';e={$_.PublishLocation}},
- @{n='script_source_location';e={$_.ScriptSourceLocation}}, @{n='script_publish_location';e={$_.ScriptPublishLocation}} | ConvertTo-Json
+ @{n='script_source_location';e={$_.ScriptSourceLocation}}, @{n='script_publish_location';e={$_.ScriptPublishLocation}}
}
else {
(Get-PackageSource -Name '#{name}') | Select @{n='source_name';e={$_.Name}}, @{n='url';e={$_.Location}},
- @{n='provider_name';e={$_.ProviderName}}, @{n='trusted';e={$_.IsTrusted}} | ConvertTo-Json
+ @{n='provider_name';e={$_.ProviderName}}, @{n='trusted';e={$_.IsTrusted}}
}
}
EOH
diff --git a/lib/chef/resource/windows_ad_join.rb b/lib/chef/resource/windows_ad_join.rb
index 6201b57379..731ce9333e 100644
--- a/lib/chef/resource/windows_ad_join.rb
+++ b/lib/chef/resource/windows_ad_join.rb
@@ -109,12 +109,12 @@ class Chef
cmd << " -Force"
converge_by("join Active Directory domain #{new_resource.domain_name}") do
- ps_run = powershell_out(cmd)
+ ps_run = powershell_exec(cmd)
if ps_run.error?
if sensitive?
raise "Failed to join the domain #{new_resource.domain_name}: *suppressed sensitive resource output*"
else
- raise "Failed to join the domain #{new_resource.domain_name}: #{ps_run.stderr}"
+ raise "Failed to join the domain #{new_resource.domain_name}: #{ps_run.errors}"
end
end
@@ -143,12 +143,12 @@ class Chef
cmd << " -Force"
converge_by("leave Active Directory domain #{node_domain}") do
- ps_run = powershell_out(cmd)
+ ps_run = powershell_exec(cmd)
if ps_run.error?
if sensitive?
raise "Failed to leave the domain #{node_domain}: *suppressed sensitive resource output*"
else
- raise "Failed to leave the domain #{node_domain}: #{ps_run.stderr}"
+ raise "Failed to leave the domain #{node_domain}: #{ps_run.errors}"
end
end
@@ -170,10 +170,10 @@ class Chef
# workgroup the node is a member of.
#
def node_domain
- node_domain = powershell_out!("(Get-WmiObject Win32_ComputerSystem).Domain")
- raise "Failed to check if the system is joined to the domain #{new_resource.domain_name}: #{node_domain.stderr}}" if node_domain.error?
+ node_domain = powershell_exec!("(Get-WmiObject Win32_ComputerSystem).Domain")
+ raise "Failed to check if the system is joined to the domain #{new_resource.domain_name}: #{node_domain.errors}}" if node_domain.error?
- node_domain.stdout.downcase.strip
+ node_domain.result.downcase.strip
end
#
@@ -182,10 +182,10 @@ class Chef
# workgroup.
#
def node_workgroup
- node_workgroup = powershell_out!("(Get-WmiObject Win32_ComputerSystem).Workgroup")
+ node_workgroup = powershell_exec!("(Get-WmiObject Win32_ComputerSystem).Workgroup")
raise "Failed to check if the system is currently a member of a workgroup" if node_workgroup.error?
- node_workgroup.stdout.downcase.strip
+ node_workgroup.result
end
#
diff --git a/lib/chef/resource/windows_certificate.rb b/lib/chef/resource/windows_certificate.rb
index 68874af4d0..2c8c7c72ff 100644
--- a/lib/chef/resource/windows_certificate.rb
+++ b/lib/chef/resource/windows_certificate.rb
@@ -207,16 +207,16 @@ class Chef
when ".der"
out_file.puts(cert_obj.to_der)
when ".cer"
- cert_out = powershell_out("openssl x509 -text -inform DER -in #{cert_obj.to_pem} -outform CER").stdout
+ cert_out = shell_out("openssl x509 -text -inform DER -in #{cert_obj.to_pem} -outform CER").stdout
out_file.puts(cert_out)
when ".crt"
- cert_out = powershell_out("openssl x509 -text -inform DER -in #{cert_obj.to_pem} -outform CRT").stdout
+ cert_out = shell_out("openssl x509 -text -inform DER -in #{cert_obj.to_pem} -outform CRT").stdout
out_file.puts(cert_out)
when ".pfx"
- cert_out = powershell_out("openssl pkcs12 -export -nokeys -in #{cert_obj.to_pem} -outform PFX").stdout
+ cert_out = shell_out("openssl pkcs12 -export -nokeys -in #{cert_obj.to_pem} -outform PFX").stdout
out_file.puts(cert_out)
when ".p7b"
- cert_out = powershell_out("openssl pkcs7 -export -nokeys -in #{cert_obj.to_pem} -outform P7B").stdout
+ cert_out = shell_out("openssl pkcs7 -export -nokeys -in #{cert_obj.to_pem} -outform P7B").stdout
out_file.puts(cert_out)
else
Chef::Log.info("Supported certificate format .pem, .der, .cer, .crt, .pfx and .p7b")
@@ -327,7 +327,7 @@ class Chef
# @return [Boolean] Whether the certificate file is binary encoded or not
#
def binary_cert?
- powershell_out!("file -b --mime-encoding #{new_resource.source}").stdout.strip == "binary"
+ shell_out!("file -b --mime-encoding #{new_resource.source}").stdout.strip == "binary"
end
# Imports the certificate object into cert store
diff --git a/lib/chef/resource/windows_dfs_server.rb b/lib/chef/resource/windows_dfs_server.rb
index 84b2a18c91..fc161f8189 100644
--- a/lib/chef/resource/windows_dfs_server.rb
+++ b/lib/chef/resource/windows_dfs_server.rb
@@ -49,14 +49,14 @@ class Chef
default: 3600
load_current_value do
- ps_results = powershell_out("Get-DfsnServerConfiguration -ComputerName '#{ENV["COMPUTERNAME"]}' | Select LdapTimeoutSec, PreferLogonDC, EnableSiteCostedReferrals, SyncIntervalSec, UseFqdn | ConvertTo-Json")
+ ps_results = powershell_exec("Get-DfsnServerConfiguration -ComputerName '#{ENV["COMPUTERNAME"]}' | Select LdapTimeoutSec, PreferLogonDC, EnableSiteCostedReferrals, SyncIntervalSec, UseFqdn")
if ps_results.error?
raise "The dfs_server resource failed to fetch the current state via the Get-DfsnServerConfiguration PowerShell cmdlet. Is the DFS Windows feature installed?"
end
- Chef::Log.debug("The Get-DfsnServerConfiguration results were #{ps_results.stdout}")
- results = Chef::JSONCompat.from_json(ps_results.stdout)
+ Chef::Log.debug("The Get-DfsnServerConfiguration results were #{ps_results.result}")
+ results = ps_results.result
use_fqdn results["UseFqdn"] || false
ldap_timeout_secs results["LdapTimeoutSec"]
@@ -69,7 +69,10 @@ class Chef
description "Configure DFS settings."
converge_if_changed do
- powershell_out("Set-DfsnServerConfiguration -ComputerName '#{ENV["COMPUTERNAME"]}' EnableSiteCostedReferrals $#{new_resource.enable_site_costed_referrals} -UseFqdn $#{new_resource.use_fqdn} -LdapTimeoutSec #{new_resource.ldap_timeout_secs} -PreferLogonDC $#{new_resource.prefer_login_dc} -SyncIntervalSec #{new_resource.sync_interval_secs}")
+ dfs_cmd = "Set-DfsnServerConfiguration -ComputerName '#{ENV["COMPUTERNAME"]}' -UseFqdn $#{new_resource.use_fqdn} -LdapTimeoutSec #{new_resource.ldap_timeout_secs} -SyncIntervalSec #{new_resource.sync_interval_secs}"
+ dfs_cmd << " -EnableSiteCostedReferrals $#{new_resource.enable_site_costed_referrals}" if new_resource.enable_site_costed_referrals != current_resource.enable_site_costed_referrals
+ dfs_cmd << " -PreferLogonDC $#{new_resource.prefer_login_dc}" if new_resource.prefer_login_dc != current_resource.prefer_login_dc
+ powershell_exec!(dfs_cmd)
end
end
end
diff --git a/lib/chef/resource/windows_firewall_profile.rb b/lib/chef/resource/windows_firewall_profile.rb
index f67d8fb8ed..ada9729699 100644
--- a/lib/chef/resource/windows_firewall_profile.rb
+++ b/lib/chef/resource/windows_firewall_profile.rb
@@ -83,11 +83,11 @@ class Chef
load_current_value do |desired|
ps_get_net_fw_profile = load_firewall_state(desired.profile)
- output = powershell_out(ps_get_net_fw_profile)
- if output.stdout.empty?
+ output = powershell_exec(ps_get_net_fw_profile)
+ if output.result.empty?
current_value_does_not_exist!
else
- state = Chef::JSONCompat.from_json(output.stdout)
+ state = output.result
end
default_inbound_action state["default_inbound_action"]
@@ -130,7 +130,7 @@ class Chef
unless firewall_enabled?(new_resource.profile)
converge_by "Enable the #{new_resource.profile} Firewall Profile" do
cmd = "Set-NetFirewallProfile -Profile #{new_resource.profile} -Enabled \"True\""
- powershell_out!(cmd)
+ powershell_exec!(cmd)
end
end
end
@@ -139,7 +139,7 @@ class Chef
if firewall_enabled?(new_resource.profile)
converge_by "Disable the #{new_resource.profile} Firewall Profile" do
cmd = "Set-NetFirewallProfile -Profile #{new_resource.profile} -Enabled \"False\""
- powershell_out!(cmd)
+ powershell_exec!(cmd)
end
end
end
@@ -166,12 +166,7 @@ class Chef
return $true
} else {return $false}
CODE
- firewall_status = powershell_out(cmd).stdout
- if /True/.match?(firewall_status)
- true
- elsif /False/.match?(firewall_status)
- false
- end
+ powershell_exec!(cmd).result
end
end
@@ -193,7 +188,7 @@ class Chef
allow_user_ports = $#{profile_name}.AllowUserPorts.ToString()
allow_unicast_response = $#{profile_name}.AllowUnicastResponseToMulticast.ToString()
display_notification = $#{profile_name}.NotifyOnListen.ToString()
- }) | ConvertTo-Json
+ })
EOH
end
end
diff --git a/lib/chef/resource/windows_firewall_rule.rb b/lib/chef/resource/windows_firewall_rule.rb
index 2010c15f89..a6f0614362 100644
--- a/lib/chef/resource/windows_firewall_rule.rb
+++ b/lib/chef/resource/windows_firewall_rule.rb
@@ -19,8 +19,6 @@
# limitations under the License.
#
-require_relative "../json_compat"
-
class Chef
class Resource
class WindowsFirewallRule < Chef::Resource
@@ -159,11 +157,11 @@ class Chef
load_current_value do
load_state_cmd = load_firewall_state(rule_name)
- output = powershell_out(load_state_cmd)
- if output.stdout.empty?
+ output = powershell_exec(load_state_cmd)
+ if output.result.empty?
current_value_does_not_exist!
else
- state = Chef::JSONCompat.from_json(output.stdout)
+ state = output.result
end
# Need to reverse `$rule.Profile.ToString()` in powershell command
@@ -194,17 +192,17 @@ class Chef
:remote_port, :direction, :protocol, :icmp_type, :firewall_action, :profile, :program, :service,
:interface_type, :enabled do
cmd = firewall_command("Set")
- powershell_out!(cmd)
+ powershell_exec!(cmd)
end
converge_if_changed :group do
- powershell_out!("Remove-NetFirewallRule -Name '#{new_resource.rule_name}'")
+ powershell_exec!("Remove-NetFirewallRule -Name '#{new_resource.rule_name}'")
cmd = firewall_command("New")
- powershell_out!(cmd)
+ powershell_exec!(cmd)
end
else
converge_by("create firewall rule #{new_resource.rule_name}") do
cmd = firewall_command("New")
- powershell_out!(cmd)
+ powershell_exec!(cmd)
end
end
end
@@ -214,7 +212,7 @@ class Chef
if current_resource
converge_by("delete firewall rule #{new_resource.rule_name}") do
- powershell_out!("Remove-NetFirewallRule -Name '#{new_resource.rule_name}'")
+ powershell_exec!("Remove-NetFirewallRule -Name '#{new_resource.rule_name}'")
end
else
Chef::Log.info("Firewall rule \"#{new_resource.rule_name}\" doesn't exist. Skipping.")
@@ -320,7 +318,7 @@ class Chef
service = $serviceFilter.Service
interface_type = $interfaceTypeFilter.InterfaceType.ToString()
enabled = [bool]::Parse($rule.Enabled.ToString())
- }) | ConvertTo-Json
+ })
EOH
end
end
diff --git a/lib/chef/resource/windows_security_policy.rb b/lib/chef/resource/windows_security_policy.rb
index 069f240ce5..1b0a285197 100644
--- a/lib/chef/resource/windows_security_policy.rb
+++ b/lib/chef/resource/windows_security_policy.rb
@@ -106,11 +106,11 @@ class Chef
MinimumPasswordAge = $security_options_hash.MinimumPasswordAge
NewGuestName = $security_options_hash.NewGuestName
LockoutBadCount = $security_options_hash.LockoutBadCount
- }) | ConvertTo-Json
+ })
CODE
- output = powershell_out(powershell_code)
- current_value_does_not_exist! if output.stdout.empty?
- state = Chef::JSONCompat.from_json(output.stdout)
+ output = powershell_exec(powershell_code)
+ current_value_does_not_exist! if output.result.empty?
+ state = output.result
if desired.secoption == "ResetLockoutCount" || desired.secoption == "LockoutDuration"
if state["LockoutBadCount"] == "0"
@@ -144,7 +144,7 @@ class Chef
Remove-Item $env:TEMP\\#{security_option}_Export.inf -force
EOH
- powershell_out!(cmd)
+ powershell_exec!(cmd)
end
end
end
diff --git a/lib/chef/resource/windows_share.rb b/lib/chef/resource/windows_share.rb
index 47bf6cfb19..fe1e976747 100644
--- a/lib/chef/resource/windows_share.rb
+++ b/lib/chef/resource/windows_share.rb
@@ -20,7 +20,6 @@
#
require_relative "../resource"
-require_relative "../json_compat"
require_relative "../util/path_helper"
class Chef
@@ -123,19 +122,19 @@ class Chef
# this command selects individual objects because EncryptData & CachingMode have underlying
# types that get converted to their Integer values by ConvertTo-Json & we need to make sure
# those get written out as strings
- share_state_cmd = "Get-SmbShare -Name '#{desired.share_name}' | Select-Object Name,Path, Description, Temporary, CATimeout, ContinuouslyAvailable, ConcurrentUserLimit, EncryptData | ConvertTo-Json -Compress"
+ share_state_cmd = "Get-SmbShare -Name '#{desired.share_name}' | Select-Object Name,Path, Description, Temporary, CATimeout, ContinuouslyAvailable, ConcurrentUserLimit, EncryptData"
Chef::Log.debug("Running '#{share_state_cmd}' to determine share state'")
- ps_results = powershell_out(share_state_cmd)
+ ps_results = powershell_exec(share_state_cmd)
# detect a failure without raising and then set current_resource to nil
if ps_results.error?
- Chef::Log.debug("Error fetching share state: #{ps_results.stderr}")
+ Chef::Log.debug("Error fetching share state: #{ps_results.errors}")
current_value_does_not_exist!
end
- Chef::Log.debug("The Get-SmbShare results were #{ps_results.stdout}")
- results = Chef::JSONCompat.from_json(ps_results.stdout)
+ Chef::Log.debug("The Get-SmbShare results were #{ps_results.result}")
+ results = ps_results.result
path results["Path"]
description results["Description"]
@@ -147,18 +146,18 @@ class Chef
encrypt_data results["EncryptData"]
# folder_enumeration_mode results['FolderEnumerationMode']
- perm_state_cmd = %{Get-SmbShareAccess -Name "#{desired.share_name}" | Select-Object AccountName,AccessControlType,AccessRight | ConvertTo-Json -Compress}
+ perm_state_cmd = %{Get-SmbShareAccess -Name "#{desired.share_name}" | Select-Object AccountName,AccessControlType,AccessRight}
Chef::Log.debug("Running '#{perm_state_cmd}' to determine share permissions state'")
- ps_perm_results = powershell_out(perm_state_cmd)
+ ps_perm_results = powershell_exec(perm_state_cmd)
# we raise here instead of warning like above because we'd only get here if the above Get-SmbShare
# command was successful and that continuing would leave us with 1/2 known state
raise "Could not determine #{desired.share_name} share permissions by running '#{perm_state_cmd}'" if ps_perm_results.error?
- Chef::Log.debug("The Get-SmbShareAccess results were #{ps_perm_results.stdout}")
+ Chef::Log.debug("The Get-SmbShareAccess results were #{ps_perm_results.result}")
- f_users, c_users, r_users = parse_permissions(ps_perm_results.stdout)
+ f_users, c_users, r_users = parse_permissions(ps_perm_results.result)
full_users f_users
change_users c_users
@@ -167,8 +166,7 @@ class Chef
# given the string output of Get-SmbShareAccess parse out
# arrays of full access users, change users, and read only users
- def parse_permissions(results_string)
- json_results = Chef::JSONCompat.from_json(results_string)
+ def parse_permissions(json_results)
json_results = [json_results] unless json_results.is_a?(Array) # single result is not an array
f_users = []
@@ -246,14 +244,16 @@ class Chef
delete_command = "Remove-SmbShare -Name '#{new_resource.share_name}' -Force"
Chef::Log.debug("Running '#{delete_command}' to remove the share")
- powershell_out!(delete_command)
+ powershell_exec!(delete_command)
end
def update_share
- update_command = "Set-SmbShare -Name '#{new_resource.share_name}' -Description '#{new_resource.description}' -Force"
+ update_command = "Set-SmbShare -Name '#{new_resource.share_name}' -Description '#{new_resource.description}' -ConcurrentUserLimit #{new_resource.concurrent_user_limit} -CATimeout #{new_resource.ca_timeout} -EncryptData:#{bool_string(new_resource.encrypt_data)} -ContinuouslyAvailable:#{bool_string(new_resource.continuously_available)} -Force"
+ update_command << " -ScopeName #{new_resource.scope_name}" unless new_resource.scope_name == "*" # passing * causes the command to fail
+ update_command << " -Temporary:#{bool_string(new_resource.temporary)}" if new_resource.temporary # only set true
Chef::Log.debug("Running '#{update_command}' to update the share")
- powershell_out!(update_command)
+ powershell_exec!(update_command)
end
def create_share
@@ -264,7 +264,7 @@ class Chef
share_cmd << " -Temporary:#{bool_string(new_resource.temporary)}" if new_resource.temporary # only set true
Chef::Log.debug("Running '#{share_cmd}' to create the share")
- powershell_out!(share_cmd)
+ powershell_exec!(share_cmd)
# New-SmbShare adds the "Everyone" user with read access no matter what so we need to remove it
# before we add our permissions
@@ -299,7 +299,7 @@ class Chef
grant_command = "Grant-SmbShareAccess -Name '#{new_resource.share_name}' -AccountName \"#{new_resource.send("#{perm_type}_users").join('","')}\" -Force -AccessRight #{perm_type}"
Chef::Log.debug("Running '#{grant_command}' to update the share permissions")
- powershell_out!(grant_command)
+ powershell_exec!(grant_command)
end
end
@@ -330,7 +330,7 @@ class Chef
def revoke_user_permissions(users)
revoke_command = "Revoke-SmbShareAccess -Name '#{new_resource.share_name}' -AccountName \"#{users.join('","')}\" -Force"
Chef::Log.debug("Running '#{revoke_command}' to revoke share permissions")
- powershell_out!(revoke_command)
+ powershell_exec!(revoke_command)
end
# convert True/False into "$True" & "$False"
diff --git a/lib/chef/resource/windows_workgroup.rb b/lib/chef/resource/windows_workgroup.rb
index e506b51257..3c49f7cb3e 100644
--- a/lib/chef/resource/windows_workgroup.rb
+++ b/lib/chef/resource/windows_workgroup.rb
@@ -92,8 +92,8 @@ class Chef
unless workgroup_member?
converge_by("join workstation workgroup #{new_resource.workgroup_name}") do
- ps_run = powershell_out(join_command)
- raise "Failed to join the workgroup #{new_resource.workgroup_name}: #{ps_run.stderr}}" if ps_run.error?
+ ps_run = powershell_exec(join_command)
+ raise "Failed to join the workgroup #{new_resource.workgroup_name}: #{ps_run.errors}}" if ps_run.error?
unless new_resource.reboot == :never
reboot "Reboot to join workgroup #{new_resource.workgroup_name}" do
@@ -119,10 +119,10 @@ class Chef
# @return [Boolean] is the node a member of the workgroup specified in the resource
def workgroup_member?
- node_workgroup = powershell_out!("(Get-WmiObject -Class Win32_ComputerSystem).Workgroup")
+ node_workgroup = powershell_exec!("(Get-WmiObject -Class Win32_ComputerSystem).Workgroup")
raise "Failed to determine if system already a member of workgroup #{new_resource.workgroup_name}" if node_workgroup.error?
- node_workgroup.stdout.downcase.strip == new_resource.workgroup_name.downcase
+ String(node_workgroup.result).downcase.strip == new_resource.workgroup_name.downcase
end
end
end