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