厨师如何知道如何从包名称安装包?

How does chef know how to install a package from its package name?

我是 Chef 的新手。我了解到 Chef 为资源提供了一个声明性接口。比方说:

我有一个简单的 recipe.rb 文件,其中包含以下行:

package 'foo'

这一行足以让 Chef 知道如何在配方 运行 所在的任何环境中安装包 foo

所以我运行:

chef-apply recipe.rb

我的问题是..
package 资源的上下文中,Chef 在内部做了什么将 "declarative" 食谱翻译成 "procedural" 食谱? (即,将声明性配方命令转换为获取和安装包的一系列操作所需的步骤是什么)

P.S: 关于 Chef 如何解决这个问题的细节很有趣,因为不同 OS 包管理器的包名称不需要相同。 apt 索引中作为包 foo 存在的内容可能存在于 yum 索引中作为包 bar 或者可能根本不存在.而且 Chef 不会将食谱强加给特定的 OS 或包管理器。所以我很好奇 Chef 在使用 chef-apply 时如何处理将此类声明性语句转换为 apt-get、yum 或 brew 等命令。

来自Chef documentation on the package resource

This resource is the base resource for several other resources used for package management on specific platforms. While it is possible to use each of these specific resources, it is recommended to use the package resource as often as possible. For more information about specific resources for specific platforms, see the following topics:

  • apt_package
  • bff_package
  • chef_gem
  • dpkg_package
  • easy_install_package
  • freebsd_package
  • ...

包资源根据需要委托给适当的 OS 级别包。如果各个 OSes 之间的包名称相同,那么您很幸运。 Chef 将找出要使用的包管理器并安装该包。

在您提到的情况下,包名为 foo for apt 和 bar for yum,您必须添加一些额外的逻辑。

case node[:platform]
when "ubuntu", "debian"
  package "foo" do
    action :install
  end
when "centos"
  package "bar" do
    action :install
  end
end

Chef 仍会处理确定要使用的底层包管理器,因此您不必指定 apt_packageyum_package 资源,但您必须为每个平台提供正确的名称.