class Chef::Provider::DscResource
Public Class Methods
new(new_resource, run_context)
click to toggle source
Calls superclass method
Chef::Provider::new
# File lib/chef/provider/dsc_resource.rb, line 29 def initialize(new_resource, run_context) super @new_resource = new_resource @module_name = new_resource.module_name @module_version = new_resource.module_version @reboot_resource = nil end
Public Instance Methods
define_resource_requirements()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 48 def define_resource_requirements requirements.assert(:run) do |a| a.assertion { supports_dsc_invoke_resource? } err = ["You must have PowerShell version >= 5.0.10018.0 to use dsc_resource."] a.failure_message Chef::Exceptions::ProviderNotFound, err a.whyrun err + ["Assuming a previous resource installs PowerShell 5.0.10018.0 or higher."] a.block_action! end requirements.assert(:run) do |a| a.assertion { supports_refresh_mode_enabled? || dsc_refresh_mode_disabled? } err = ["The LCM must have its RefreshMode set to Disabled for" \ " PowerShell versions before 5.0.10586.0."] a.failure_message Chef::Exceptions::ProviderNotFound, err.join(" ") a.whyrun err + ["Assuming a previous resource sets the RefreshMode."] a.block_action! end requirements.assert(:run) do |a| a.assertion { module_usage_valid? } err = ["module_name must be supplied along with module_version."] a.failure_message Chef::Exceptions::DSCModuleNameMissing, err a.block_action! end end
load_current_resource()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 46 def load_current_resource; end
Protected Instance Methods
add_dsc_verbose_log(result)
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 144 def add_dsc_verbose_log(result) # We really want this information from the verbose stream, # however in some versions of WMF, Invoke-DscResource is not correctly # writing to that stream and instead just dumping to stdout verbose_output = result.verbose.join("\n") verbose_output = result.result if verbose_output.empty? if @converge_description.nil? || @converge_description.empty? @converge_description = verbose_output else @converge_description << "\n\n" @converge_description << verbose_output end end
create_reboot_resource()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 172 def create_reboot_resource @reboot_resource = Chef::Resource::Reboot.new( "Reboot for #{new_resource.name}", run_context ).tap do |r| r.reason("Reboot for #{new_resource.resource}.") end end
dsc_refresh_mode_disabled?()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 91 def dsc_refresh_mode_disabled? Chef::Platform.dsc_refresh_mode_disabled?(node) end
dsc_resource_name()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 107 def dsc_resource_name new_resource.resource.to_s end
generate_description()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 103 def generate_description @converge_description end
invoke_resource(method)
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 163 def invoke_resource(method) properties = translate_type(new_resource.properties) switches = "-Method #{method} -Name #{new_resource.resource}"\ " -Property #{properties} -Module #{module_info_object} -Verbose" Timeout.timeout(new_resource.timeout) { powershell_exec!("Invoke-DscResource #{switches}") } end
local_configuration_manager()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 76 def local_configuration_manager @local_configuration_manager ||= Chef::Util::DSC::LocalConfigurationManager.new( node, nil ) end
module_info_object()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 159 def module_info_object @module_version.nil? ? module_name : "@{ModuleName='#{module_name}';ModuleVersion='#{@module_version}'}" end
module_name()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 111 def module_name @module_name ||= begin found = resource_store.find(dsc_resource_name) r = case found.length when 0 raise Chef::Exceptions::ResourceNotFound, "Could not find #{dsc_resource_name}. Check to make "\ "sure that it shows up when running Get-DscResource" when 1 if found[0]["Module"].nil? "PSDesiredStateConfiguration" # default DSC module else found[0]["Module"]["Name"] end else raise Chef::Exceptions::MultipleDscResourcesFound, found end end end
module_usage_valid?()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 99 def module_usage_valid? !(!@module_name && @module_version) end
reboot_if_required()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 181 def reboot_if_required reboot_action = new_resource.reboot_action unless @reboot_resource.nil? case reboot_action when :nothing logger.trace("A reboot was requested by the DSC resource, but reboot_action is :nothing.") logger.trace("This dsc_resource will not reboot the node.") else logger.trace("Requesting node reboot with #{reboot_action}.") @reboot_resource.run_action(reboot_action) end end end
resource_store()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 83 def resource_store Chef::Util::DSC::ResourceStore.instance end
set_resource()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 137 def set_resource result = invoke_resource(:set) add_dsc_verbose_log(result) create_reboot_resource if result.result["RebootRequired"] result end
supports_dsc_invoke_resource?()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 87 def supports_dsc_invoke_resource? run_context && Chef::Platform.supports_dsc_invoke_resource?(node) end
supports_refresh_mode_enabled?()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 95 def supports_refresh_mode_enabled? Chef::Platform.supports_refresh_mode_enabled?(node) end
test_resource()
click to toggle source
# File lib/chef/provider/dsc_resource.rb, line 131 def test_resource result = invoke_resource(:test) add_dsc_verbose_log(result) result.result["InDesiredState"] end