Configuration Management Camp Ghent Belgium February 2016
Hand Crafted ArtisanalChef Resources
View Slide
Config Management CampGhent, BelgiumFebruary 2016
Sean OMeara[email protected]@someara
whoami
Part 1
Writing library cookbooksAn opinionated workflowStep by step
Step 1 - Disregard dogma“Unit tests first” is a dirty liehttps://flic.kr/p/aTSiXF
Step 2 - Make things upThen work backwardshttps://flic.kr/p/dXkQ5s
Contrived examples are the bestexamples
Place this off to the side, we willcome back for it later.
Step 3 - Create a cookbookBy hand. Be a wizard.https://flic.kr/p/3mMW6e
mkdir -p ~/src/custom-resources-tutorialcd ~/src/custom-resources-tutorialemacs metadata.rb
Step 4 - Embed a test cookbook.By hand. Be a wizard. Again.https://flic.kr/p/ai36NG
mkdir -p test/cookbooks/my_file_testcd test/cookbooks/my_file_testecho “test cookbook” > README.mdmkdir recipestouch recipes/default.rbemacs metadata.rb
Step 5 - Tell Berkshelf about ithttps://flic.kr/p/k5jmja
cd ~/src/custom-resources-tutorialemacs Berksfile
Step 6 - Configure Test Kitchenhttps://flic.kr/p/r38F5c
emacs .kitchen.yml
kitchen test ; echo $?
Step 7 - Save gamehttps://flic.kr/p/6ZJ4G7
cd ~/src/custom-resources-tutorialcp ~/src/chef-cookbooks/docker/.gitignore .cp ~/src/chef-cookbooks/docker/.rubocop.yml .
rubocop -agit initgit add .git commit -a -m 'v0.0.1 - cookbook skeleton'git tag v0.0.1
Step 8 - Use resource in test recipehttps://flic.kr/p/8MuUAX
emacs test/cookbooks/my_file_test/recipes/default.rb
kitchen converge ; echo $?
Step 9 - Implement resourcehttps://flic.kr/p/bkVKRb
cd ~/src/custom-resources-tutorialmkdir librariesemacs libraries/my_file.rb
kitchen converge ; echo $?kitchen converge ; echo $?
Step 10 - Test for behaviorhttps://i.ytimg.com/vi/1y8Rqvz-Jcg/maxresdefault.jpgz
mkdir -p test/integration/my_file/inspecemacs -p test/integration/my_file/inspec/run_spec.rb
kitchen verify ; echo $?
Step 11 - Add specs and commithttps://flic.kr/p/rrgfZh
cd ~/src/custom-resources-tutorialmkdir specemacs spec/spec_helper.rb
emacs spec/my_file_spec.rb
rubocop -arspec ; echo $?git add .git commit -a -m “v0.1.0 - my_file resource”git tag v0.1.0
stove 0.1.0
Step 12 - Iterate indefinitelyhttps://flic.kr/p/8Ny8Jt
Congratulations, you have now written onesoftware.
You must now maintain it forever.
muahaha.
Part 2
Custom ResourcesTips and Tricks
Be explicithttps://flic.kr/p/pz1JAo
Converge twice during developmentUse markers for “negative” actionshttps://flic.kr/p/pqwd5U
Tests let you refactor with confidencehttps://flic.kr/p/4WnjM4
STAR WIPE
Pure Ruby resourceshttps://flic.kr/p/5km2fM
Pure Ruby resources utilize- The Ruby Standard Library- Chef helper libs (mixlib-shellout)- Ruby gems
It might make sense to vendor gems
load_current_value- Runs at converge time- Just before the action body- Loads desired state values
converge_if_changed- Runs if any desired_state: trueproperty differs from user input- desired_state: false avoidscomparison
coerce transforms values- Called when property is set- Called when property is read- Use this to raise errors on bad input
The docker_container resource usescoercion heavilyhttps://github.com/chef-cookbooks/docker/blob/master/libraries/docker_container.rb
lazy defaults- Delays evaluation until used- Useful for calculated values
Composite resourceshttps://flic.kr/p/fm58oo
Composite resources- Reuse Chef resources- Actions look like recipes- Do not use load_current_value
etcd_installation_binary :create
httpd_config_rhel :create
Avoid leaky abstractionshttps://flic.kr/p/7d1kDc
Resources should be minimalist
Early versions might be simple
They rarely stay that way
Iterate and add more featuresBreak down into smaller resources
Use naming semantics todescribe strategy
etcd_installation_binaryetcd_installation_dockeretcd_installation_packageetcd_service_manager_dockeretcd_service_manager_executeetcd_service_manager_systemdetcd_service_manager_sysvinitetcd_service_manager_upstart
Recycle patterns as they emerge
docker_installation_binarydocker_installation_packagedocker_installation_scriptdocker_service_manager_executedocker_service_manager_systemddocker_service_manager_sysvinitdocker_service_manager_upstart
Chef selects defaults with theprovider resolution system
…
Compose resourcesMost specific to least specific
Enjoy
fin