0

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"
Jeff
  • 35,755
  • 15
  • 108
  • 220
  • What is your recipe code ? Guards should apply to custom_resource as any other resource – Tensibai Jul 06 '16 at 15:28
  • But to really answer you, you should implement a `load_current_resource` and use the `converge_if_changed` as shown [here](https://docs.chef.io/custom_resources.html#converge-if-changed) and get rid of the guard in your powershell resource. Quoting the documentation in an answer sounds unproductive. – Tensibai Jul 06 '16 at 15:30
  • if I do that, how do i invoke the powershell script block without the powershell_script resource? – Jeff Jul 06 '16 at 16:30
  • 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 Jul 06 '16 at 17:23
  • 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 Jul 06 '16 at 17:43
  • Does the powershell resource by itself in a recipe work as expected ? I really don't get why you custom_resource would be updated if the guard works. (Without a debug log it's hard to tell what is failing here) – Tensibai Jul 06 '16 at 18:23
  • yes it does...I think it's just that the first time it executes, it happens to report 2 resources updated instead of just 1...then 0 after that...very strange – Jeff Jul 06 '16 at 18:31
  • A log with `-l debug` would really help to get what happens here – Tensibai Jul 06 '16 at 18:44
  • Maybe it is related to [this Pull request](https://github.com/chef/chef/pull/5006) – Tensibai Jul 08 '16 at 15:26

1 Answers1

1

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.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • Isn't it the reason of the `not_if` guard on those resources ? not executing them and hence not marking them as updated when the guard command return true (or 0 when it's a string). I don't get it there. – Tensibai Jul 07 '16 at 07:27
  • You're right but it still says x resources converging. Any idea why? – Jeff Jul 07 '16 at 13:39
  • All resources get added to the collection when compiling a recipe. The guard prevents the action from getting run, but it's still there for the purposes of the total count. – coderanger Jul 07 '16 at 16:49