如何使主厨资源中的错误消息更具信息性?

时间:2017-02-17 21:59:23

标签: chef

以下是一些尝试将文件解压缩到某个目录的主厨代码。

从其他一些食谱中深入调用食谱,随机失败。

unless ::File.exists?(::File.join(node[:zookeeper][:install_dir], zk_basename))

  execute 'install zookeeper' do  # <-- Line 57
    user node[:zookeeper][:user]
    cwd Chef::Config[:file_cache_path]
    command "tar -C #{node[:zookeeper][:install_dir]} -zxf #{zk_basename}.tar.gz"
  end
end

我看到的错误是:

Mixlib::ShellOut::ShellCommandFailed: execute[install zookeeper] (zookeeper::add line 57) had an error:
Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '2' . ,
FATAL: Mixlib::ShellOut::ShellCommandFailed: execute[install zookeeper] (zookeeper::add line 57) had an error:
Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '2' .

由于错误在我没有ssh访问权限的环境中随机发生,我想改进主厨配方以捕获此错误并在错误消息中提供更多详细信息,如:

  1. tar.gz文件的大小和创建日期
  2. 目标目录的权限
  3. 请注意,我必须将这些内容放在异常的错误消息中,因为这是唯一可见的(没有ssh访问)复杂的厨师运行。

1 个答案:

答案 0 :(得分:0)

你可以添加这样的东西

log 'log_important_bits' do
  message lazy {
    the_dir = ::File.stat(node[:zookeeper][:install_dir])
    the_file = ::File.stat(::File.join(node[:zookeeper][:install_dir], zk_basename))
    "file.size = #{the_file.size} ... put whatever you want here"
  }
  level :error # So you'll always see it
end