【问题标题】:Error running chef libraries运行厨师库时出错
【发布时间】:2017-09-30 02:37:13
【问题描述】:

我正在使用 chef-solo 来测试我的食谱。我正在尝试使用库来触发电子邮件。这是我所做的。

在我的食谱目录中创建了一个库目录

cat /tmp/chefrepo/cookbooks/poc/libraries/helper.rb
require 'net/smtp'

module HandlerSendEmail
  class Helper

    def send_email_on_run_failure(node_name)

      message = "From: Chef <chef@chef.io>\n"
      message << "To: Grant <grantmc@chef.io>\n"
      message << "Subject: Chef run failed\n"
      message << "Date: #{Time.now.rfc2822}\n\n"
      message << "Chef run failed on #{node_name}\n"
      Net::SMTP.start('localhost', 25) do |smtp|
        smtp.send_message message, 'chef@chef.io', 'grantmc@chef.io'
      end
    end
  end
end

还有一个简单的食谱

cat /tmp/chefrepo/cookbooks/poc/recipes/default.rb
#
# Cookbook:: poc
# Recipe:: default
#
# Copyright:: 2017, The Authors, All Rights Reserved.
ruby_block 'fail the run' do
  block do
    fail 'deliberately fail the run'
  end
end

Chef.event_handler do
  on :run_failed do
    HandlerSendEmail::Helper.new.send_email_on_run_failure(
      Chef.run_context.node.name
    )
  end
end

当我使用 chef-solo -c solo.rb -j node.json 单独运行 chef 我明白了

Running handlers:
[2017-09-29T12:49:09+00:00] ERROR: Running exception handlers
[2017-09-29T12:49:09+00:00] ERROR: Running exception handlers
Running handlers complete
[2017-09-29T12:49:09+00:00] ERROR: Exception handlers complete
[2017-09-29T12:49:09+00:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated in 03 seconds
[2017-09-29T12:49:09+00:00] ERROR: uninitialized constant #<Class:#<Chef::Recipe:0x0000000004875ee8>>::HandlerSendEmail
[2017-09-29T12:49:09+00:00] ERROR: uninitialized constant #<Class:#<Chef::Recipe:0x0000000004875ee8>>::HandlerSendEmail
[2017-09-29T12:49:09+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
[2017-09-29T12:49:09+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

我缺少什么吗?我尝试将库目录位置更改为与食谱目录相同的级别,但没有成功。

【问题讨论】:

  • 如果你这样做::HandlerSendEmail::Helper.new....会有效吗?

标签: chef-infra chef-recipe chef-solo


【解决方案1】:

您缺少混合辅助资源的命令。在库文件的底部添加以下内容

Chef::Recipe.include(HelperSendEmail) Chef::Resource.include(HelperSendEmail)

这将使您的资源可用。

来源:https://www.sidorenko.io/post/2016/10/writing-helper-cookbooks-with-shared-functions/

【讨论】:

  • 这与问题无关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多