0
votes

I have a custom resource (in a my cookbook's /resources folder in a file called dsc_class.rb). Basically, this is a way of invoking a DSC class resource easily without installing it and using the full DSC engine.

resource_name :dsc_class

default_action :create

property :file_name, kind_of: String, required: true, name_property: true

action :create do  
  powershell_script file_name do
    cwd ::File.dirname(file_name)
    code <<-EOH
    gc "#{file_name}" -Raw | iex
    $o = New-Object #{file_name.split('/').last.split('.').first}
    $o.Set()
    EOH
    not_if <<-EOH
    gc "#{file_name}" -Raw | iex
    $o = New-Object #{file_name.split('/').last.split('.').first}
    o.Test()
    EOH
  end
end

Works great - but action always "executes" so event when the resource guards and the powershell script doesn't execute, I get "x resources updated" in the log/output to the chef-client run.

How can I guard appropriately in my custom resource?

UPDATE: recipe contains

 dsc_class "/install/dsc/MyDscResource.ps1"
1
What is your recipe code ? Guards should apply to custom_resource as any other resource - Tensibai
But to really answer you, you should implement a load_current_resource and use the converge_if_changed as shown here and get rid of the guard in your powershell resource. Quoting the documentation in an answer sounds unproductive. - Tensibai
if I do that, how do i invoke the powershell script block without the powershell_script resource? - Jeff
Misunderstood the Test part, sounds like you should take advantage of the dsc resource there. BTW you should not have this resource marked as updated is the powershell script didn't run. I suspect your guard is not behaving properly. - Tensibai
I don't want to use dsc_resource as stated in the question...it's slow, requires installing the module, having it be a psm1 file and several other things - DSC is terrible by itself. - Jeff

1 Answers

1
votes

powershell_script is like all other execute-style resources, it always runs the command/script/whatever and relies on outer-level not_if or only_if clauses. In this case you would put those on the dsc_class resource instance in your recipe code.