customizing chef for fun and profit
DESCRIPTION
Chefconf 2014TRANSCRIPT
Customizing Chef
For Fun and Profit
@jonlives
Jon Cowie
Sr Operations Engineer
@jonlives
@jonlives
@jonlives
Beginning of 2010 Today
@jonlives
Chef at Etsy
@jonlives
Chef at Etsy
• Open Source Chef Server
• ~1500 Nodes
• CentOS, some Mac OS X
@jonlives
We Love Chef!
@jonlives
We Know Best.
@jonlives
Absorb what is useful.
Discard what is useless.
@jonlives
“I am not smart enough to build an ontology … that
can encompass all the variations in infrastructure.
Nobody is, the world moves too fast.”
@jonlives
There is no magic pill.
@jonlives
You are the expert.
@jonlives
Customizability
@jonlives
@jonlives
Thin Server
@jonlives
Thick Client
@jonlives
@jonlives
Get Config Data / Initialize
• Load client.rb
• Server URL
• Auth Credentials
• Paths
• Run ohai
• Collect attributes
• Plugins!
@jonlives
Ohai Plugins• Add attributes to your
node • Executed at start of
run • Single Ruby file • Highest precedence
@jonlives
Ohai Plugins - v6
provides ‘awesome’ !
awesome Mash.new awesome[:sauce] = "Sricacha" awesome[:level] = "11"
@jonlives
Ohai Plugins - v7 (Coming soon!)Ohai.plugin(:Awesome) do provides "awesome" collect_data(:darwin) do awesome Mash.new awesome[:sauce] = “Sriracha" awesome[:level] = 11 end end
@jonlives
Ohai Plugins - client.rb / solo.rb
Ohai::Config[:plugin_path] << /<path>/plugins
@jonlives
Authenticate / Register
• Authenticate to Server
• <clientkey>.pem
• Register if not already registered
• <validation>.pem
• If both fail, go directly to jail.
@jonlives
Load / Build Node
• Download node
• Combine attributes
• Expand run_list
• Order matters!
• Create RunStatus object
• Run start handlers
@jonlives
Handlers
• Respond to specific situations
• Start, Report, Exception • Common Code
@jonlives
Handlers - Common Coderequire "chef/handler" !
class HandlerName < Chef::Handler def report # Ruby code goes here end end
@jonlives
Handlers - client.rb / solo.rb
require “/path_to/mystarthandler.rb" !
my_start_handler = MyStartHandler.new !
start_handlers << my_start_handler
@jonlives
Synchronize Cookbooks
• Get cookbook files list
• Only required recipes, files & templates
• Compares to local cache
• Download changes
@jonlives
Set up RunContext
• Cookbook Collection
• Definitions List
• Events
• Notifications
• Node
• Resource Collection
• Empty for now…
@jonlives
Events• Respond to interesting
events • Pub / Sub model • EventDispatch::Base • Formatter • Custom Subscriber
@jonlives
Events - Custom Subscriberrequire “chef/event_dispatch/base" !
class AwesomeSubscriber < Chef::EventDispatch::Base def run_started(run_status) … end # Method for each interesting event end
@jonlives
Events - Register Subscriberrequire "chef/handler" require “/path/awesome_subscriber.rb' !
class AwesomeSubscriberStartHandler < Chef::Handler def report event_dispatcher_subscriber = AwesomeSubscriber.new @run_status.events.register(event_dispatcher_subscriber) end end
@jonlives
Load Cookbook Data
• Populate Resource Collection
• Libraries
• Attributes
• Resources / Providers
• Definitions
• Recipes
@jonlives
Converge Node
• Actually changes node
• Apply resource collection
• Again, order matters!
@jonlives
Finalize
• Successful Run
• Save Node
• Report Handlers
• Failed Run
• Exception Handlers
• 💩
@jonlives
Report & Exception Handlersdef report if @run_status.success? # Do a happy dance elsif @run_status.failed? # Sad panda. end end
@jonlives
Handlers - client.rb / solo.rbrequire “/var/chef/handlers/mystarthandler.rb"
require "/var/chef/handlers/mynewhandler.rb"
!
my_start_handler = MyStartHandler.new
my_new_handler = MyNewHandler.new
!
start_handlers << my_start_handler
report_handlers << my_new_handler
exception_handlers << my_new_handler
@jonlives
That RunStatus Object Again…• Accessible to handlers
• Run status & timings
• Exception and Backtrace
• All & updated resources
• RunContext
• Node object
@jonlives
Don’t fear the code!• https://github.com/opscode/chef
• lib/chef/handler.rb
• lib/chef/run_status.rb
• lib/chef/run_context.rb
• lib/chef/event_dispatch/base.rb
@jonlives
Um…wat?
@jonlives
Criteria for Customization
Simplicity
Modularity
Visibility
Maintainability
Scalability
@jonlives
Thanks! Questions?
!
Office Hours @ 3.15, Marina Room @jonlives / http://jonliv.es / [email protected]