how to require my library in chef ruby_block

后端 未结 1 1116
猫巷女王i
猫巷女王i 2021-01-01 18:47

I\'m developing a cookbook to deploy a simple ROR application. I write an app_helper.rb and put it into the libraries directory of my cookbook, here is the content:

相关标签:
1条回答
  • 2021-01-01 18:49

    Depending on load order, you might be including your module into an anonymous Ruby class instead of what you think.

    If you want to use your method in a recipe, do this at the top of your recipe:

    include AppHelper
    

    You could alternatively use :send at the end of your library:

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

    This is different because it will raise an exception if Chef::Recipe is not defined (whereas you are creating a new class if it doesn't exist).

    That's all you should need to do unless you want to use the helper in not_if and only_if guards. Then you need to include the helper in the resource:

    Chef::Resource.send(:include, AppHelper)
    

    Okay, now that I explained all of that, it won't actually help you. The Ruby Block provider simply calls the block - it doesn't instance eval it. So including the helper in the resource doesn't do anything.

    So, you'll need to use a singleton object in this instance (it's the only solution I can reliably think of). The way you've defined your method, you can call it directly from the global namespace:

    AppHelper.find_gem('...')
    

    So:

    ruby_block "find gem" do
      block do
        gem_bin = AppHelper.find_gem('...')
      end
    end
    
    0 讨论(0)
提交回复
热议问题