class Chef::Pwsh

Public Class Methods

dll() click to toggle source
# File lib/chef/pwsh.rb, line 61
def self.dll
  # This Powershell DLL source lives here: https://github.com/chef/chef-powershell-shim
  # Every merge into that repo triggers a Habitat build and promotion. Running
  # the rake :update_chef_exec_dll task in this (chef/chef) repo will pull down
  # the built packages and copy the binaries to distro/ruby_bin_folder. Bundle install
  # ensures that the correct architecture binaries are installed into the path.
  # Also note that the version of pwsh is determined by which assemblies the dll was
  # built with. To update powershell, those dependencies must be bumped.
  @dll ||= Dir.glob("#{RbConfig::CONFIG["bindir"]}/**/Chef.PowerShell.Wrapper.Core.dll").last
end
new(script, timeout: -1) click to toggle source

Run a command under pwsh (powershell core) via FFI This implementation requires the managed dll, native wrapper and a published, self contained dotnet core directory tree to exist in the bindir directory.

@param script [String] script to run @param timeout [Integer, nil] timeout in seconds. @return [Object] output

Calls superclass method
# File lib/chef/pwsh.rb, line 29
def initialize(script, timeout: -1)
  @dll = Pwsh.dll
  super
end

Protected Instance Methods

exec(script, timeout: -1) click to toggle source
Calls superclass method
# File lib/chef/pwsh.rb, line 36
def exec(script, timeout: -1)
  # Note that we need to override the location of the shared dotnet core library
  # location. With most .net core applications, you can simply publish them as a
  # "self-contained" application allowing consumers of the application to run them
  # and use its own stand alone version of the .net core runtime. However because
  # this is simply a dll and not an exe, it will look for the runtime in the shared
  # .net core installation folder. By setting DOTNET_MULTILEVEL_LOOKUP to 0 we can
  # override that folder's location with DOTNET_ROOT. To avoid the possibility of
  # interfering with other .net core processes that might rely on the common shared
  # location, we revert these variables after the script completes.
  original_dml = ENV["DOTNET_MULTILEVEL_LOOKUP"]
  original_dotnet_root = ENV["DOTNET_ROOT"]
  original_dotnet_root_x86 = ENV["DOTNET_ROOT(x86)"]

  ENV["DOTNET_MULTILEVEL_LOOKUP"] = "0"
  ENV["DOTNET_ROOT"] = RbConfig::CONFIG["bindir"]
  ENV["DOTNET_ROOT(x86)"] = RbConfig::CONFIG["bindir"]

  super
ensure
  ENV["DOTNET_MULTILEVEL_LOOKUP"] = original_dml
  ENV["DOTNET_ROOT"] = original_dotnet_root
  ENV["DOTNET_ROOT(x86)"] = original_dotnet_root_x86
end