如何捕获Chef异常

问题描述 投票:2回答:1

我现在正在处理厨师食谱,我需要根据代码结果用一些信息更新数据袋。基本上,我需要使用成功或失败来更新数据包。

代码看起来像这样:

begin
     node[:fileDeploy].each do |f|
        if f[:deploy]
          cookbook_file "#{f[:subdirectory]}/#{f[:targetFilename]}" do
             owner users['tomcatUser']
             group users['tomcatGroup']
             mode "0755"
             cookbook node[:cookbookName]
             source "#{f[:sourceFilename]}"
          end
        end
     end
     # Update data bag: succeeded
rescue Chef::Exceptions::FileNotFound => e
     # Update data bag: failed
end

问题是,即使文件丢失,也不会执行救援块,并且不会相应地更新数据包。因此,当我在服务器上运行命令sudo Chef-client时,它以Chef::Exceptions::FileNotFound异常结束,但是rescue块未对其进行处理。是否有意义?有帮助吗?

ruby chef chef-recipe
1个回答
6
投票

您的救援块未捕获异常,因为引发异常的代码未在异常处理程序的范围内执行。

在您的代码中,您声明了cookbook_file资源。声明进行得很好,并且安排了资源在收敛阶段执行的时间。您的救援块可能会捕获在资源声明期间发生的异常,而不是在实际执行时发生的异常。

请参阅About the chef-client Run,以了解有关厨师运行的两个阶段的更多信息,即资源集合的生成和以后的融合。

为了获得所需的结果,您可以在收敛期间检查源文件存在的条件,并做出相应的决定。

通常,在Chef中处理错误相当困难。这是设计使然,因为通常应将系统设计为与其他部分完全独立。因此,如果需要目录或文件,则应该使用适当的资源显式创建该目录或文件。然后,如果当前资源“已更改”(对于特定资源意味着什么),则可以使用notifications通知其他资源以执行特定操作。

下面的代码试图实现与您显然想要的东西类似的东西。在收敛期间,它仍然没有捕获异常,但是首先尝试不引发异常,而是检查所需条件并运行适当的资源。

node[:fileDeploy].each do |f|
  if f[:deploy]
    cookbook_file "#{f[:subdirectory]}/#{f[:targetFilename]}" do
      owner users['tomcatUser']
      group users['tomcatGroup']
      mode "0755"
      cookbook node[:cookbookName]
      source f[:sourceFilename]
      only_if{ Dir.exist?(File.base_name(f[:sourceFilename]) }
      notifies :run, "execute[success #{f[:sourceFilename]}]", :immediately
    end

    # This one gets notified (and run) when the cookbook_file is successful
    execute "success #{f[:sourceFilename]}" do
      action :nothing
      command "do whatever you like"
    end

    # This one runs only if the created file doesn't exist by now
    execute "error #{f[:subdirectory]}/#{f[:targetFilename]}" do
      action :run
      command "do whatever you like"
      creates "#{f[:subdirectory]}/#{f[:targetFilename]}"
    end
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.