Can't use helper method in chef
I want to use a method in a recipe, so I create a helper method for it.
my_cookbook / libraries / foo_helper.rb
module Foo
module Helper
def foo_daemon_command(action)
%Q{bash -c "export PATH='/usr/local/bin:/opt/rbenv/bin:$PATH'; eval '$(rbenv init -)'; cd /opt/foo; /opt/rbenv/shims/ruby foo_daemon.rb #{action} >>/var/log/foo/cron_#{action}.log 2>>/var/log/foo/cron_#{action}.log" }
end
end
end
And load the method from the recipe.
my_cookbook / Recipes / default.rb
Chef::Resource::User.send(:include, Foo::Helper)
execute "foo daemon restart" do
command foo_daemon_command("restart")
end
When I apply the recipe, I get an error undefined method
like this:
NoMethodError
-------------
undefined method `foo_daemon_command' for Chef::Resource::Execute
What am I doing wrong?
+3
source to share
1 answer
The specific error is that you are fixing the function in the user resource instead of Execute. But the best way to do it is to mix it with the current recipe. Just add extend Foo::Helper
to the beginning of the recipe. You can also make it a modular method and call it directly as Foo::Helper.foo_daemon_command
. In general, making global DSL changes should be done with great care and never from recipe code.
+1
source to share