Skip to content

Instantly share code, notes, and snippets.

@burtlo
Last active August 29, 2015 14:25
Show Gist options
  • Save burtlo/d9fe55e0a7bb3016ccf1 to your computer and use it in GitHub Desktop.
Save burtlo/d9fe55e0a7bb3016ccf1 to your computer and use it in GitHub Desktop.
default['email_handler']['from_address'] = "chef@localhost"
default['email_handler']['to_address'] = "chef@localhost"
# BASE ROLE
name "base"
description "Base Server Role"
run_list "recipe[email_handler]", "recipe[chef-client::config]", "recipe[chef-client::delete_validation]", "recipe[chef-client]", "recipe[ntp]", "recipe[motd]", "recipe[users]"
default_attributes({
"ohai" => { "disabled_plugins" => [ ":Passwd" ] }
})
# Cookbook Name:: email_handler
# Recipe:: default
#
# Copyright 2015, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
chef_gem "pony" do
action :install
end
include_recipe "chef_handler"
include_recipe "postfix"
include_recipe "mailx"
cookbook_file "#{node['chef_handler']['handler_path']}/email_handler.rb" do
source "handlers/email_handler.rb"
owner "root"
group "root"
mode "0644"
end
chef_handler "MyCompany::EmailMe" do
source "#{node['chef_handler']['handler_path']}/email_handler.rb"
arguments [ node['email_handler']['from_address'],
node['email_handler']['to_address'] ]
action :enable
end
require 'rubygems'
require 'pony'
module MyCompany
class EmailMe < Chef::Handler
def initialize(from_address, to_address)
@from_address = from_address
@to_address = to_address
end
def report
require 'pry' ; binding.pry
status = "Failed"
if success?
status = "Successful"
end
subject = "#{status} Chef run report from #{node.name}"
body = ""
if ! run_status.updated_resources.empty?
run_status.updated_resources.each do |r|
body += "The resource #{r.name} was changed in the cookbook #{r.cookbook_name} at #{r.source_line}\n"
end
else
body += "No resources changed by chef-client\n"
end
require 'pry' ; binding.pry
Pony.mail(:to => @to_address,
:from => @from_address,
:subject => subject,
:body => body)
end
end
end
name 'email_handler'
maintainer 'YOUR_COMPANY_NAME'
maintainer_email 'YOUR_EMAIL'
license 'All rights reserved'
description 'Installs/Configures email_handler'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '0.1.0'
depends "chef_handler"
depends "postfix"
depends "mailx"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment