Skip to content
Snippets Groups Projects
Commit d0d0e9b7 authored by Christopher Huhn's avatar Christopher Huhn
Browse files

Use Socket.gethostname instead of node['fqdn'] which might be nil

parent 90b75c13
No related branches found
No related tags found
No related merge requests found
From: Christopher Huhn <C.Huhn@gsi.de>
Date: Fri, 13 Mar 2020 16:15:19 +0100
Subject: Use Socket.gethostname for hostname information
---
lib/chef/handler/mail.rb | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/lib/chef/handler/mail.rb b/lib/chef/handler/mail.rb
index c00aaba..3be01cc 100644
--- a/lib/chef/handler/mail.rb
+++ b/lib/chef/handler/mail.rb
@@ -16,12 +16,14 @@ require 'chef'
require 'chef/handler'
require 'erubis'
require 'mail'
+require 'socket'
class MailHandler < Chef::Handler
attr_reader :options
def initialize(opts = {})
@options = {
:to_address => "root",
+ :from_address => "chef-client@#{Socket.gethostname}",
:template_path => File.join(File.dirname(__FILE__), "mail.erb")
}
@options.merge! opts
@@ -29,13 +31,18 @@ class MailHandler < Chef::Handler
def report
status = success? ? "Successful" : "Failed"
- subjectstr = "[CHEF] #{status} chef-client run on #{node.fqdn} (#{node.ipaddress})"
+ subjectstr = format(
+ '[CHEF] %s chef-client run on %s' ,
+ status,
+ Socket.gethostname
+ )
Chef::Log.debug("mail handler template path: #{options[:template_path]}")
if File.exists? options[:template_path]
template = IO.read(options[:template_path]).chomp
else
- Chef::Log.error("mail handler template not found: #{options[:template_path]}")
+ Chef::Log.error("mail handler template not found: " +
+ options[:template_path])
raise Errno::ENOENT
end
@@ -48,10 +55,10 @@ class MailHandler < Chef::Handler
bodystr = Erubis::Eruby.new(template).evaluate(context)
to_address = options[:to_address]
- from_address = "chef-client@#{node.fqdn}"
+ from_address = "chef-client@#{Socket.gethostname}"
mail = Mail.new do
- from from_address
+ from options[:from_address]
to to_address
subject subjectstr
body bodystr
0001-get-rid-of-pony-dependency.patch 0001-get-rid-of-pony-dependency.patch
0002-add-more-information-and-refactor-format.patch 0002-add-more-information-and-refactor-format.patch
0001-No-require-rubygems.patch 0001-No-require-rubygems.patch
0004-Use-Socket.gethostname-for-hostname-information.patch
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment