2011-08-17 12 views
5

场景:如何设置一个依赖到一个当前不存在的Ruby库中Chef加载食谱?

  • 食谱1:下载档案,提取它们。提供一个也定义Ruby库的CLI。
  • Recipe2:利用上述库中的Ruby API。

在RECIPE1 /食谱/ default.rb:

.. do work 
node[:recipe1][:filePath] = ".." #path to file 

在recipe2 /食谱/ default.rb:

require node[:recipe1][:filePath]/lib/Library 
.. do work 

然而,装载配方时,厨师宣布:

[Wed, 17 Aug 2011 19:32:23 +0800] DEBUG: Loading cookbook apache2's definitions from /var/chef/cookbooks/apache2/definitions/web_app.rb 
[Wed, 17 Aug 2011 19:32:23 +0800] DEBUG: Loading cookbook apache2's definitions from /var/chef/cookbooks/apache2/definitions/apache_module.rb 
[Wed, 17 Aug 2011 19:32:23 +0800] DEBUG: Loading Recipe Recipe1 via include_recipe 
[Wed, 17 Aug 2011 19:32:23 +0800] DEBUG: Found recipe default in cookbook Recipe1 
[Wed, 17 Aug 2011 19:32:23 +0800] ERROR: Running exception handlers 
[Wed, 17 Aug 2011 19:32:23 +0800] ERROR: Exception handlers complete 
[Wed, 17 Aug 2011 19:32:23 +0800] DEBUG: Re-raising exception: LoadError - no such file to load -- /path/to/library/Library 
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require' 
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require' 
/var/chef/cookbooks/hsltcli/recipes/default.rb:63:in `from_file' 
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.4/bin/../lib/chef/cookbook_version.rb:578:in `load_recipe' 
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.4/bin/../lib/chef/mixin/language_include_recipe.rb:40:in `include_recipe' 
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.4/bin/../lib/chef/mixin/language_include_recipe.rb:27:in `each' 
/usr/lib64/ruby/gems/1.8/gems/chef-0.10.4/bin/../lib/chef/mixin/language_include_recipe.rb:27:in `include_recipe' 

如何在所有配方都在p后声明/启用Ruby库跑步的过程?

回答

4

使用Gem包,您可以在资源上调用run_action()方法,以便在编译时发生,并确保清除Gem路径。例如:

r = gem_package "dynect_rest" do 
    action :nothing 
end 
r.run_action(:install) 
require 'rubygems' 
Gem.clear_paths 

或者,更为简洁:

gem_package "dynect_rest" do 
    action :nothing 
end.run_action(:install) 
require 'rubygems' 
Gem.clear_paths 

(该require 'rubygems'可能不是绝对必要的,因为它应该已经由厨师装,但我们要确保)

相关问题