Chef Summit 2014
Cookbook Reusability!Chef Community Summit2014
View Slide
Sean OMeara![email protected]!@someara
whoami
A Year in Review
Diversification
7zip!apache2!ark!application*!couchdb!Imagemagick!java
logrotate!mercurial!munin!nagios!nginx!ntp!openvpn
pacman!postgresql!python!reprepro!rabbitmq!runit!supervisor
selinux!tmux!varnish !wordpress!
Supermarkethttps://www.flickr.com/photos/mobilestreetlife/10885044043
• Escaped the tyranny of JIRA!• Moved to Github Issues!• Automated CLA checking!• OSS Artifact Repository
Rewrites
20090.5.2ChefVagrantChefSpec0.1.00.0.2Minitest-ChefBerkshelfTest Kitchen20100.9.020110.10.0201210.12.01.0.00.2.01.0.00.7.01.0.02013 20141.0.02.0.01.0.02.0.01.4.011.0.0 12.0.01.6.53.0.03.0.01.2.2ServerSpecChefDK0.0.1 2.0.00.2.2TDI Capability0.8.0
yum!yum-*!jenkins!mysql!httpd
The Evolution of a ChefCookbook
Stage 1 - Paradisehttps://www.flickr.com/photos/nattu/1385100375/
test / repairtest / repairtest / repair
Easy to read Easy to grok Easy to test}
Stage 2 - if statements
logiccompiled intoresource collection
ResourceDSL}JustRuby}
ResourceDSL}JustRuby} }Chef
Stage 3 - Crazytownhttps://www.flickr.com/photos/kwl/4595324641
wat
Why?
Reusable is useful
Cross-platform is useful
We desire useful things
Platform idioms are hard
Keeping the Dream Alivehttps://www.flickr.com/photos/kalexanderson/7014655351/
Most people just write theirown cookbooks fromscratch
I really really really wantreusable cross-platformcookbooks
Lessons Learned
Attributes are routinelyabusedhttps://www.flickr.com/photos/jabberwocky381/2828863789
Attributes are aninterface
Attributes are tunableknobs
You probably just want avariable
Or even a method
Using an attribute to trackstate during a Chef runcauses tears and sorrow
Prefer resource parameters
Primitives are more usefulthan opinionated policy
“They’re Just Resources”!!LWRP is Googleable
Zoom out a level!!Think about services andruntimes, not files and processes
Singleton resources are good butconfusing. Possibly evendangerous.!!Multiple instance support is better!!Cross-platform resources are best
BDD / TDD yields highquality cookbookshttps://www.flickr.com/photos/glenirah/4376553184
Test Kitchen!ServerSpec / Minitest / Bats!ChefSpec
20090.5.2ChefVagrantChefSpec0.1.00.0.2Minitest-ChefBerkshelfTest Kitchen20100.9.020110.10.0201210.12.01.0.00.2.01.0.00.7.01.0.02013 20141.0.02.0.01.0.02.0.01.4.011.0.0 12.0.01.6.53.0.03.0.01.2.2ServerSpecChefDK0.0.1 2.0.00.2.2This Slide Again0.8.0
Full test coverage is tedious
Full test coverage is totallyworth it
Let users bring their ownconfigurations
It is better to add to aresource_collection than tomonkey patch it
Configuration files are thebrains of a service
Manage minimalconfiguration to get a servicerunning
Offload further configurationto the user
conf.d is your friend
Hide everything elseinside a resource
Add resource parameterswhen appropriate
Cross-platform cookbooksare hardhttps://www.flickr.com/photos/glenirah/4376553184
Cross-platform resourcesare even harder
They can be done!
Create a resource
Create a provider
Subclass platform providers
Set provider default forplatforms
Do Repeat Yourself
Some resources are oftenthe duplicated acrossproviders
That’s fine. It’s the patternas a whole that’s important
Maximize for grokability
People will be reading yourcode for the first timeduring operations work
Init systems are annoying
Using a service resourceusually requires file ortemplate resources inaddition toservice[thingd]
Debconf!Docker!LaunchD!Runit!SMF!Simple!SystemD!Sysvinit!Upstart!Windows Services
Subclassing is awesome
Customize before recipecompilation
Going Forward
Cookbooks that shipresource primitives
More examples to copy
More breaking backwardscompatibility (sorry)
Providers implementedwith Docker containers(why not?)
chef-metal
Tell me what you think
Please don’t hurt me =)