LWRP编译和收敛阶段

时间:2016-01-18 13:36:13

标签: ruby chef lwrp

我们发现LWRP操作(特别是库)中的ruby代码总是在收敛阶段执行(也没有ruby_block资源!)。这是预期的行为吗?我们没有找到任何关于它的具体文档,并且知道在编译阶段通常会执行ruby代码现在让我们感到困惑。 我们使用Chef 12.0.3。

这是一个小例子:

in recipes / default.rb:

time = Time.new
puts "Current Time : " + time.inspect

converge_test_my_resource "test_resource" do
  action :install
end

在providers / my_resource.rb中:

action :install do
  time = Time.new
  puts "Current Time : " + time.inspect
end

厨房的输出是:

   Compiling Cookbooks...
   Current Time : 2016-01-18 16:37:52 +0100
   Converging 1 resources
   Recipe: converge_test::default
     * converge_test_my_resource[test_resource] action install
       Current Time : 2016-01-18 16:37:52 +0100
    (up to date)

所以你看到时间戳的第一个打印是在编译阶段,但是下一个时间戳的打印处于收敛阶段。

提前致谢!

2 个答案:

答案 0 :(得分:1)

你过分概括了会发生什么。

编译阶段(资源收集阶段)

执行 recipe 代码,该代码将资源添加到资源堆栈。

执行阶段(提供者执行阶段)

For each resource on the resource stack create a provider for that resource execute the provider action code specified in the resource declaration ** next

** LWRP提供商

创建一个新的执行上下文,并将此lwrp提供程序操作视为在其自己的chef-client运行中运行的配方。

这意味着在LWRP提供程序之外声明的所有资源都会消失,而您在这个新环境中实际上拥有一个独立的主厨。这包括资源收集阶段和提供者执行阶段。当这个内部厨师运行完成后,您将丢弃所有这些资源并退出LWRP提供程序。

答案 1 :(得分:0)

有关此流程的更具体概述,请参阅https://coderanger.net/two-pass/。在内部动作块中,它的行为类似于配方,但具有自己的编译和收敛阶段。