Class: ChefMOTD
- Inherits:
-
Chef::Handler
- Object
- Chef::Handler
- ChefMOTD
- Defined in:
- lib/chef-handler-motd.rb
Instance Attribute Summary collapse
-
#priority ⇒ Object
readonly
Returns the value of attribute priority.
Instance Method Summary collapse
- #delete_outdated ⇒ Object
- #generate_message ⇒ Object
-
#initialize(options = {:priority => '05'}) ⇒ ChefMOTD
constructor
A new instance of ChefMOTD.
- #report ⇒ Object
- #write_out(msg) ⇒ Object
Constructor Details
#initialize(options = {:priority => '05'}) ⇒ ChefMOTD
Returns a new instance of ChefMOTD.
29 30 31 |
# File 'lib/chef-handler-motd.rb', line 29 def initialize( = {:priority => '05'}) @priority = [:priority] end |
Instance Attribute Details
#priority ⇒ Object (readonly)
Returns the value of attribute priority.
27 28 29 |
# File 'lib/chef-handler-motd.rb', line 27 def priority @priority end |
Instance Method Details
#delete_outdated ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/chef-handler-motd.rb', line 33 def delete_outdated Dir.entries('/etc/update-motd.d').select do |entry| /chef-motd/.match(entry) && !/^#{@priority}/.match(entry) end.each do |del| Chef::Log.warn "Deleting #{del} as it does not match the current ChefMOTD priority" FileUtils.rm ::File.join('/etc', 'update-motd.d', del) end end |
#generate_message ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/chef-handler-motd.rb', line 48 def msg = "#!/bin/sh\necho \\\"Node \#{node.name} last success at \#{Time.now.to_s} in \#{run_status.elapsed_time} seconds\\\"\necho \\\"Updated resources on last run (total: \#{run_status.updated_resources.length}):\\\"\n eos\n run_status.updated_resources.each do |res|\n msg += \"echo \\\" \#{res.resource_name}[\#{res.name}]\\\"\\n\"\n end\n return msg\nend\n" |
#report ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/chef-handler-motd.rb', line 60 def report if run_status.success? Chef::Log.info 'Updating Chef info in MOTD ...' delete_outdated write_out() end end |
#write_out(msg) ⇒ Object
42 43 44 45 46 |
# File 'lib/chef-handler-motd.rb', line 42 def write_out(msg) file = "/etc/update-motd.d/#{@priority}-chef-motd" ::File.open(file, 'w') {|f| f.puts msg} ::File.chmod(0755, file) end |