Hi partners,
I am trying to send the log of a node when chef run failed with this code:
mail_to = node["mail"]["to"]
Chef.event_handler do
on :run_failed do
HandlerSendEmail::Helper.new.send_email mail_to
end
end
The HandlerSendEmail::Helper.new.send_email
code is:
module HandlerSendEmail
require 'net/smtp'
class Helper
def unindent string
first = string[/\A\s*/]
string.gsub /^#{first}/, ''
end
def send_email(to,opts={})
opts[:server] ||= 'ipaddress'
opts[:from] ||= 'sender'
opts[:from_alias] ||= 'Chef Reporter'
opts[:subject] ||= "Chef Run on Node #{Chef.run_context.node.name}"
opts[:message] ||= "Chef run failed at #{Time.now.iso8601}, check the attached log."
filename = "C:\\chef\\log-#{Chef.run_context.node.name}"
# Read a file and encode it into base64 format
encodedcontent = [File.read(filename)].pack("m") # base64
marker = "AUNIQUEMARKER"
# Define the main headers.
header = <<-HEADER
From: #{opts[:from_alias]} <#{opts[:from]}>
To: <#{to}>
Subject: #{opts[:subject]}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=#{marker}
--#{marker}
HEADER
# Define the message action
body = <<-BODY
Content-Type: text/plain
Content-Transfer-Encoding:8bit
#{opts[:message]}
--#{marker}
BODY
# Define the attachment section
attached = <<-ATTACHED
Content-Type: multipart/mixed; name=\"#{filename}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{filename}"
#{encodedcontent}
--#{marker}--
ATTACHED
mailtext = unindent header + body + attached
Net::SMTP.start(opts[:server], 25) do |smtp|
smtp.send_message mailtext, opts[:from], to
end
end
end
end
The module work but it send the log immediately and the log’s content don’t show me what is the error. For example:
INFO: *** Chef 12.11.18 ***
INFO: Platform: x64-mingw32
INFO: Chef-client pid: 1252
INFO: Run List is [recipe[deploy_war]]
INFO: Run List expands to [deploy_war]
INFO: Starting Chef Run for PosW10
INFO: Running start handlers
INFO: Start handlers complete.
INFO: Loading cookbooks [deploy_war@1.0.3, windows@1.44.1, chef_handler@1.4.0]
INFO: Storing updated cookbooks/deploy_war/recipes/default.rb in the cache.
INFO: Processing chef_gem[mechanize] action install (deploy_war::prepare line 12)
INFO: Processing file[C:\chef\redsis-validator.pem] action delete (deploy_war::prepare line 18)
INFO: Processing powershell_script[Start SQLSERVER] action run (deploy_war::prepare line 24)
INFO: powershell_script[Start SQLSERVER] ran successfully
INFO: Processing ruby_block[wait for sql service to start] action run (deploy_war::prepare line 29)
INFO: Running queued delayed notifications before re-raising exception
ERROR: Running exception handlers
ERROR: Exception handlers complete
INFO: Sending resource update report (run-id: 6706d426-8cb3-45b1-a72b-d9d0cf5c276f)
As you can see don’t show me the error like normal log, which is:
INFO: *** Chef 12.11.18 ***
INFO: Platform: x64-mingw32
INFO: Chef-client pid: 1252
INFO: Run List is [recipe[deploy_war]]
INFO: Run List expands to [deploy_war]
INFO: Starting Chef Run for PosW10
INFO: Running start handlers
INFO: Start handlers complete.
INFO: Loading cookbooks [deploy_war@1.0.3, windows@1.44.1, chef_handler@1.4.0]
INFO: Storing updated cookbooks/deploy_war/recipes/default.rb in the cache.
INFO: Processing chef_gem[mechanize] action install (deploy_war::prepare line 12)
INFO: Processing file[C:\chef\redsis-validator.pem] action delete (deploy_war::prepare line 18)
INFO: Processing powershell_script[Start SQLSERVER] action run (deploy_war::prepare line 24)
INFO: powershell_script[Start SQLSERVER] ran successfully
INFO: Processing ruby_block[wait for sql service to start] action run (deploy_war::prepare line 29)
INFO: Running queued delayed notifications before re-raising exception
ERROR: Running exception handlers
ERROR: Exception handlers complete
INFO: Sending resource update report (run-id: 6706d426-8cb3-45b1-a72b-d9d0cf5c276f)
FATAL: Stacktrace dumped to C:/chef/cache/chef-stacktrace.out
FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
FATAL: NameError: ruby_block[wait for sql service to start] (deploy_war::prepare line 29) had an error: NameError: uninitialized constant Chef::Recipe::Tols
Some help to send the log when it’s fully completed ? (The error is intentional to test the module)