Class: ChefMOTD

Inherits:
Chef::Handler
  • Object
show all
Defined in:
lib/chef-handler-motd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {:priority => '05'})
  @priority = options[:priority]
end

Instance Attribute Details

#priorityObject (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_outdatedObject



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_messageObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chef-handler-motd.rb', line 48

def generate_message
  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"

#reportObject



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(generate_message)
  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