ruby - 如何设置对在 Chef 加载 Recipe 时不存在的 Ruby 库的依赖项?

标签 ruby chef-infra recipe chef-recipe

场景:

  • 方法 1:下载文件,解压它们。提供一个 CLI,它还定义了一个 Ruby 库。
  • 秘诀 2:利用上述库中的 Ruby API。

在 recipe1/recipes/default.rb 中:

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

在 recipe2/recipes/default.rb 中:

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

但是,当加载 Recipe 时,Chef 宣布:

[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'

在所有配方都在运行之后,如何声明/启用 Ruby 库?

最佳答案

使用 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' 可能不是绝对必要的,因为它应该已经由 Chef 加载,但我们想确定)

关于ruby - 如何设置对在 Chef 加载 Recipe 时不存在的 Ruby 库的依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7098552/

相关文章:

ruby-on-rails - 如何在 Ruby on Rails 中循环几个月

ruby - 如何将cookbook_file与bash结合使用?

seo - 架构 hRecipe fn/name 不起作用?

c - GNU、autotools 的 makefile - 构建 C 语言源代码的规则和方法

ruby - CSV 仅返回字符串。我需要保留值类型

Ruby:并发/多线程任务的 CPU 负载下降?

ruby - 如何根据操作系统控制 extconf.rb 生成的输出?

chef-infra - 如何查看 Chef Recipe 的所有版本?

chef-infra - Chef 执行资源, "creates"参数有什么作用?

git - 如何配置 yocto 以使用最新的 git commit?