Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Chef infra 使用Chef.event\u处理程序时是否可以访问run\u status对象?_Chef Infra - Fatal编程技术网

Chef infra 使用Chef.event\u处理程序时是否可以访问run\u status对象?

Chef infra 使用Chef.event\u处理程序时是否可以访问run\u status对象?,chef-infra,Chef Infra,我有一点第二十二条军规,似乎找不到解决办法。我可以成功地做到这一点: # # Cookbook:: core_unix_base_cmcb # Recipe:: email_on_fail email_hash = node.default['core_unix_base_cmcb']['email_hash'] email_hash['mailserver'] = node['postfix']['main']['relayhost'] email_hash['subject'] = &qu

我有一点第二十二条军规,似乎找不到解决办法。我可以成功地做到这一点:

#
# Cookbook:: core_unix_base_cmcb
# Recipe:: email_on_fail

email_hash = node.default['core_unix_base_cmcb']['email_hash']
email_hash['mailserver'] = node['postfix']['main']['relayhost']
email_hash['subject'] = "Subject: Chef-client run failed on #{email_hash['hostname']}"
email_hash['message'] = "A Chef-client run failed on #{email_hash['hostname']}."
email_hash['recipient'] = [ 'someone@somewhere.com' ]

Chef.event_handler do
  on :run_failed do
    SendEmail::Helper.new.send_email(email_hash)
  end
end
需要“net/smtp”
模块SendEmailOnFail
类sendmailmsg一种解决方法是将电子邮件散列设置为属性,报表处理程序可以使用node方法访问节点对象,您可以执行
运行状态。node['email\u hash'][xxx]
而不是
电子邮件散列[xxx]

处理程序中运行状态对象周围的文档为


要进行更详细的交流,请随时加入Chef's slack以获得社区帮助(那里也有Chef's Dev,对内部位有很大帮助):

运行状态仅在
报告
界面中可用,如中所述


在为任何处理程序运行
报告
界面之前,Chef Infra Client会初始化
运行状态
对象

但是…

那么,如何才能在电子邮件正文中获得chef客户端运行失败的信息,并将参数传递给库(或自定义处理程序)

可以从
事件处理程序
运行失败
块中获取异常详细信息。其中一些方法是:
消息
回溯
完整消息
,等等

有一个参考,用于为
:run\u failed
块设置变量

例如,在我的配方中我正在将
消息
添加到我的
电子邮件散列中
。然后,这将传递给用于发送电子邮件的助手方法

#为了完整起见添加此项,所需的更改位于“Chef.event\u handler”块中
电子邮件\u hash=hash.new
电子邮件\u散列['node\u name']=Chef.run\u context.node.name
email_hash['subject']=“厨师在{email_hash['node_name']}上运行失败”
电子邮件\u散列['sender']='admin@example.io'
电子邮件\u散列['recipient']='user@example.io'
#在这里,我们可以访问“异常”的详细信息
Chef.event\u处理程序是什么
on:run|U失败执行|异常|
电子邮件\u散列['error\u msg']=exception.message
SendEmail::Helper.new.send\u email(email\u散列)
结束
结束
再次添加我的
sendmail
helper以确保完整性:

需要“net/smtp”
发送电子邮件模块
类助手
def发送电子邮件(电子邮件散列)
message=“From:#{email_hash['sender']}\n”

消息谢谢你们两位的回复。在发布我的问题后,我继续在互联网上搜索,终于找到了一块我可以用的金块。

我很幸运有了这个例子:

on :run_failed do |exception, run_status|
我真的不明白它是如何工作的,或者它是在哪里被记录的,但它本质上和你正在做的是一样的,seshadri_c,并且工作得很好。我可以访问撰写消息所需的内容,例如

run_status.run_context.cookbook_collection

我希望这些选项被记录在案。我找到了
do | exception
示例。我将把这一点补充到我的答案中。
chef_handler 'SendEmailOnFail::SendEmail' do # Full class name
  # relative path to your handler
  source ::File.expand_path('../../files/default/handlers/email_on_fail_helper.rb', __FILE__)
  # source 'email_on_fail_helper.rb'
  arguments email_hash  # Hash or Array to pass to handler constructor
end
require 'net/smtp'

module SendEmailOnFail
  class SendEmail < Chef::Handler
    def report(email_hash)
      if run_status.failed?
        msg = "From: #{email_hash[:sender]} < #{email_hash[:sender]}@#{email_hash[:hostname]}\n"
        msg << "To: #{email_hash[:recipient].join('; ')}\n"
        msg << "#{email_hash[:subject]}\n"
        msg << "Date: #{Time.now.rfc2822}\n\n"
        msg << "#{email_hash[:message]}\n"
        msg << "#{run_status.formatted_exception}\n"
        msg << Array(backtrace).join('\n')
        sender = 'chef-client-noreply@somewhere.com'
        Net::SMTP.start(email_hash[:mailserver], 25) do |smtp|
          smtp.send_message msg, sender, email_hash[:recipient]
        end
      end
    end
  end
end
on :run_failed do |exception, run_status|
run_status.run_context.cookbook_collection