Helper and Resource Order

I have a default recipe with this:

file ‘/tmp/foo’ do
end
::Chef::Recipe.send(:include, Helpers)
func()

and a helper function in libaries/ with this:

module Helpers
def func()
Chef::Log.info(“I am here”)
end
end

My chef run in vagrant shows this:

==> node1: [2015-08-28T20:24:53+00:00] INFO: I am here
==> node1: [2015-08-28T20:24:53+00:00] INFO: file[/tmp/foo] created file
/tmp/foo

Obviously, the helper function func() is being called and executed before
the file resource, even though the order dictates the file resource should
run first. How can I ensure a specific order?

Thanks,
Douglas

Douglas,

chef-client uses a multi-stage process. The first stage is known as the
"compile" phase in which all ruby code is evaluated and a resource
collection is built. The second stage, known as the "execute" phase,
performs the test-and-repair operations on each of the resources in the
resource collection, in order.

Since func() is straight ruby, it will be executed during the compile
phase.

If you'd like to log messages during the execute phase, please use a log
resource (log Resource).

Check the docs (
Chef Infra Client Overview) for a more
detailed description of what happens and when during a chef-client run.

Thanks,
Nathen

On Fri, Aug 28, 2015 at 4:27 PM, Douglas Garstang doug.garstang@gmail.com
wrote:

I have a default recipe with this:

file '/tmp/foo' do
end
::Chef::Recipe.send(:include, Helpers)
func()

and a helper function in libaries/ with this:

module Helpers
def func()
Chef::Log.info("I am here")
end
end

My chef run in vagrant shows this:

==> node1: [2015-08-28T20:24:53+00:00] INFO: I am here
==> node1: [2015-08-28T20:24:53+00:00] INFO: file[/tmp/foo] created file
/tmp/foo

Obviously, the helper function func() is being called and executed before
the file resource, even though the order dictates the file resource should
run first. How can I ensure a specific order?

Thanks,
Douglas

You can try putting it inside a ruby_block to delay running it until converge time:

file ‘/tmp/foo’ do

end

ruby_block ‘Call_helper’ do

block do

::Chef::Recipe.send(:include, Helpers)

func()

end

end

HTH

From: Douglas Garstang [mailto:doug.garstang@gmail.com]
Sent: Friday, August 28, 2015 1:27 PM
To: chef@lists.opscode.com
Subject: [chef] Helper and Resource Order

I have a default recipe with this:

file ‘/tmp/foo’ do
end
::Chef::Recipe.send(:include, Helpers)
func()

and a helper function in libaries/ with this:

module Helpers
def func()
Chef::Log.info(“I am here”)
end
end
My chef run in vagrant shows this:

==> node1: [2015-08-28T20:24:53+00:00] INFO: I am here
==> node1: [2015-08-28T20:24:53+00:00] INFO: file[/tmp/foo] created file /tmp/foo
Obviously, the helper function func() is being called and executed before the file resource, even though the order dictates the file resource should run first. How can I ensure a specific order?
Thanks,
Douglas
The contents of this message, together with any attachments, are intended only for the use of the individual or entity to which they are addressed and may contain information that is legally privileged, confidential and exempt from disclosure. If you are not the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this message, or any attachment, is strictly prohibited. If you have received this message in error, please notify the original sender or contact Practice Fusion at 415.346.7700 ext 4 immediately by telephone or by return E-mail and delete this message, along with any attachments, from your computer. Thank you

Unfortunately, that:

file '/tmp/foo' do
end

ruby_block 'Call_helper' do
block do
::Chef::Recipe.send(:include, Helpers)
func()
end
end

yields:

==> node1: NoMethodError
==> node1: -------------
==> node1: undefined method `func' for Chef::Resource::RubyBlock

Douglas.

On Fri, Aug 28, 2015 at 1:37 PM, Alex Myasnikov <
amyasnikov@practicefusion.com> wrote:

You can try putting it inside a ruby_block to delay running it until
converge time:

file '/tmp/foo' do

end

ruby_block 'Call_helper' do

block do

::Chef::Recipe.send(:include, Helpers)

func()

end

end

HTH

From: Douglas Garstang [mailto:doug.garstang@gmail.com]
Sent: Friday, August 28, 2015 1:27 PM
To: chef@lists.opscode.com
Subject: [chef] Helper and Resource Order

I have a default recipe with this:

file '/tmp/foo' do
end
::Chef::Recipe.send(:include, Helpers)
func()

and a helper function in libaries/ with this:

module Helpers
def func()
Chef::Log.info("I am here")
end
end

My chef run in vagrant shows this:

==> node1: [2015-08-28T20:24:53+00:00] INFO: I am here
==> node1: [2015-08-28T20:24:53+00:00] INFO: file[/tmp/foo] created file
/tmp/foo

Obviously, the helper function func() is being called and executed before
the file resource, even though the order dictates the file resource should
run first. How can I ensure a specific order?

Thanks,

Douglas
The contents of this message, together with any attachments, are intended
only for the use of the individual or entity to which they are addressed
and may contain information that is legally privileged, confidential and
exempt from disclosure. If you are not the intended recipient, you are
hereby notified that any dissemination, distribution, or copying of this
message, or any attachment, is strictly prohibited. If you have received
this message in error, please notify the original sender or contact
Practice Fusion at 415.346.7700 ext 4 immediately by telephone or by return
E-mail and delete this message, along with any attachments, from your
computer. Thank you

On Friday, August 28, 2015 at 1:45 PM, Doug Garstang wrote:

Unfortunately, that:

file '/tmp/foo' do
end

ruby_block 'Call_helper' do
block do
::Chef::Recipe.send(:include, Helpers)
func()
end
end

yields:

==> node1: NoMethodError
==> node1: -------------
==> node1: undefined method `func' for Chef::Resource::RubyBlock

Methods with no explicit receiver inside a resource will call methods on the resource and not the recipe.

Depending on your requirements, you can either rewrite your helpers so you can call them explicitly like this:

module MyStuff
def self.helper(args)
#code
end
end

or, you can get a reference to the recipe from outside the block via a local variable which gets “captured” by the code block:

recipe = self
ruby_block “thing” do
block do
recipe.func
end
end

Douglas.
HTH,

--
Daniel DeLeo

::Chef::Resource::RubyBlock.send(:include, Helpers)

On 08/28/2015 01:45 PM, Doug Garstang wrote:

Unfortunately, that:

file '/tmp/foo' do
end

ruby_block 'Call_helper' do
block do
::Chef::Recipe.send(:include, Helpers)
func()
end
end

yields:

==> node1: NoMethodError
==> node1: -------------
==> node1: undefined method `func' for Chef::Resource::RubyBlock

Douglas.

On Fri, Aug 28, 2015 at 1:37 PM, Alex Myasnikov
<amyasnikov@practicefusion.com mailto:amyasnikov@practicefusion.com>
wrote:

You can try putting it inside a ruby_block to delay running it
until converge time:

file '/tmp/foo' do

end

ruby_block 'Call_helper' do

  block do

::Chef::Recipe.send(:include, Helpers)

func()

  end

end

HTH

*From:*Douglas Garstang [mailto:doug.garstang@gmail.com
<mailto:doug.garstang@gmail.com>]
*Sent:* Friday, August 28, 2015 1:27 PM
*To:* chef@lists.opscode.com <mailto:chef@lists.opscode.com>
*Subject:* [chef] Helper and Resource Order

I have a default recipe with this:

file '/tmp/foo' do
end
::Chef::Recipe.send(:include, Helpers)
func()


and a helper function in libaries/ with this:

module Helpers
  def func()
    Chef::Log.info("I am here")
  end
end

My chef run in vagrant shows this:

==> node1: [2015-08-28T20:24:53+00:00] INFO: I am here
==> node1: [2015-08-28T20:24:53+00:00] INFO: file[/tmp/foo]
created file /tmp/foo

Obviously, the helper function func() is being called and executed
before the file resource, even though the order dictates the file
resource should run first. How can I ensure a specific order?

Thanks,

Douglas

The contents of this message, together with any attachments, are
intended only for the use of the individual or entity to which
they are addressed and may contain information that is legally
privileged, confidential and exempt from disclosure. If you are
not the intended recipient, you are hereby notified that any
dissemination, distribution, or copying of this message, or any
attachment, is strictly prohibited. If you have received this
message in error, please notify the original sender or contact
Practice Fusion at 415.346.7700 ext 4 immediately by telephone or
by return E-mail and delete this message, along with any
attachments, from your computer. Thank you