Customizing Chef for Fun and Profit

  • 4,296 views
Uploaded on

Chefconf 2014

Chefconf 2014

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,296
On Slideshare
0
From Embeds
0
Number of Embeds
5

Actions

Shares
Downloads
51
Comments
0
Likes
18

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Customizing Chef For Fun and Profit
  • 2. @jonlives Jon Cowie Sr Operations Engineer
  • 3. @jonlives
  • 4. @jonlives
  • 5. @jonlives Beginning of 2010 Today
  • 6. @jonlives Chef at Etsy
  • 7. @jonlives Chef at Etsy • Open Source Chef Server
 • ~1500 Nodes
 • CentOS, some Mac OS X
  • 8. @jonlives We Love Chef!
  • 9. @jonlives We Know Best.
  • 10. @jonlives Absorb what is useful. Discard what is useless.
  • 11. @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.”
  • 12. @jonlives There is no magic pill.
  • 13. @jonlives You are the expert.
  • 14. @jonlives Customizability
  • 15. @jonlives
  • 16. @jonlives Thin Server
  • 17. @jonlives Thick Client
  • 18. @jonlives
  • 19. @jonlives Get Config Data / Initialize • Load client.rb • Server URL • Auth Credentials • Paths • Run ohai • Collect attributes • Plugins!
  • 20. @jonlives Ohai Plugins • Add attributes to your node • Executed at start of run • Single Ruby file • Highest precedence
  • 21. @jonlives Ohai Plugins - v6 provides ‘awesome’ ! awesome Mash.new awesome[:sauce] = "Sricacha" awesome[:level] = "11"
  • 22. @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
  • 23. @jonlives Ohai Plugins - client.rb / solo.rb Ohai::Config[:plugin_path] << /<path>/plugins
  • 24. @jonlives Authenticate / Register • Authenticate to Server • <clientkey>.pem • Register if not already registered • <validation>.pem • If both fail, go directly to jail.
  • 25. @jonlives Load / Build Node • Download node • Combine attributes • Expand run_list • Order matters! • Create RunStatus object • Run start handlers
  • 26. @jonlives Handlers • Respond to specific situations • Start, Report, Exception • Common Code
  • 27. @jonlives Handlers - Common Code require "chef/handler" ! class HandlerName < Chef::Handler def report # Ruby code goes here end end
  • 28. @jonlives Handlers - client.rb / solo.rb require “/path_to/mystarthandler.rb" ! my_start_handler = MyStartHandler.new ! start_handlers << my_start_handler
  • 29. @jonlives Synchronize Cookbooks • Get cookbook files list • Only required recipes, files & templates • Compares to local cache • Download changes
  • 30. @jonlives Set up RunContext • Cookbook Collection • Definitions List • Events • Notifications • Node • Resource Collection • Empty for now…
  • 31. @jonlives Events • Respond to interesting events • Pub / Sub model • EventDispatch::Base • Formatter • Custom Subscriber
  • 32. @jonlives Events - Custom Subscriber require “chef/event_dispatch/base" ! class AwesomeSubscriber < Chef::EventDispatch::Base def run_started(run_status) … end # Method for each interesting event end
  • 33. @jonlives Events - Register Subscriber require "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
  • 34. @jonlives Load Cookbook Data • Populate Resource Collection • Libraries • Attributes • Resources / Providers • Definitions • Recipes
  • 35. @jonlives Converge Node • Actually changes node • Apply resource collection • Again, order matters!
  • 36. @jonlives Finalize • Successful Run • Save Node • Report Handlers • Failed Run • Exception Handlers • 💩
  • 37. @jonlives Report & Exception Handlers def report if @run_status.success? # Do a happy dance elsif @run_status.failed? # Sad panda. end end
  • 38. @jonlives Handlers - client.rb / solo.rb require “/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
  • 39. @jonlives That RunStatus Object Again… • Accessible to handlers • Run status & timings • Exception and Backtrace • All & updated resources • RunContext • Node object
  • 40. @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
  • 41. @jonlives Um…wat?
  • 42. @jonlives Criteria for Customization Simplicity Modularity Visibility Maintainability Scalability
  • 43. @jonlives November 2014 ! http://jonliv.es/book ! O’Reilly Stand
  • 44. @jonlives Thanks! Questions? ! Office Hours @ 3.15, Marina Room @jonlives / http://jonliv.es / jcowie@etsy.com