Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hand Crafted Artisinal Chef Resources

Ea72b50eef37ebe730c37d96c5b5dd51?s=47 someara
February 02, 2016

Hand Crafted Artisinal Chef Resources

Configuration Management Camp
Ghent Belgium
February 2016

Ea72b50eef37ebe730c37d96c5b5dd51?s=128

someara

February 02, 2016
Tweet

Transcript

  1. Hand Crafted Artisanal Chef Resources

  2. Config Management Camp Ghent, Belgium February 2016

  3. Sean OMeara sean@chef.io @someara

  4. whoami

  5. Part 1

  6. Writing library cookbooks An opinionated workflow Step by step

  7. Step 1 - Disregard dogma “Unit tests first” is a

    dirty lie https://flic.kr/p/aTSiXF
  8. Step 2 - Make things up Then work backwards https://flic.kr/p/dXkQ5s

  9. Contrived examples are the best examples

  10. None
  11. Place this off to the side, we will come back

    for it later.
  12. Step 3 - Create a cookbook By hand. Be a

    wizard. https://flic.kr/p/3mMW6e
  13. mkdir -p ~/src/custom-resources-tutorial cd ~/src/custom-resources-tutorial emacs metadata.rb

  14. None
  15. Step 4 - Embed a test cookbook. By hand. Be

    a wizard. Again. https://flic.kr/p/ai36NG
  16. mkdir -p test/cookbooks/my_file_test cd test/cookbooks/my_file_test echo “test cookbook” > README.md

    mkdir recipes touch recipes/default.rb emacs metadata.rb
  17. None
  18. Step 5 - Tell Berkshelf about it https://flic.kr/p/k5jmja

  19. cd ~/src/custom-resources-tutorial emacs Berksfile

  20. None
  21. Step 6 - Configure Test Kitchen https://flic.kr/p/r38F5c

  22. emacs .kitchen.yml

  23. None
  24. kitchen test ; echo $?

  25. Step 7 - Save game https://flic.kr/p/6ZJ4G7

  26. cd ~/src/custom-resources-tutorial cp ~/src/chef-cookbooks/docker/.gitignore . cp ~/src/chef-cookbooks/docker/.rubocop.yml .

  27. rubocop -a git init git add . git commit -a

    -m 'v0.0.1 - cookbook skeleton' git tag v0.0.1
  28. Step 8 - Use resource in test recipe https://flic.kr/p/8MuUAX

  29. emacs test/cookbooks/my_file_test/recipes/ default.rb

  30. None
  31. kitchen converge ; echo $?

  32. Step 9 - Implement resource https://flic.kr/p/bkVKRb

  33. cd ~/src/custom-resources-tutorial mkdir libraries emacs libraries/my_file.rb

  34. None
  35. kitchen converge ; echo $? kitchen converge ; echo $?

  36. Step 10 - Test for behavior https://i.ytimg.com/vi/1y8Rqvz-Jcg/ maxresdefault.jpgz

  37. mkdir -p test/integration/my_file/inspec emacs -p test/integration/my_file/inspec/ run_spec.rb

  38. None
  39. kitchen verify ; echo $?

  40. Step 11 - Add specs and commit https://flic.kr/p/rrgfZh

  41. cd ~/src/custom-resources-tutorial mkdir spec emacs spec/spec_helper.rb

  42. None
  43. emacs spec/my_file_spec.rb

  44. None
  45. rubocop -a rspec ; echo $? git add . git

    commit -a -m “v0.1.0 - my_file resource” git tag v0.1.0
  46. stove 0.1.0

  47. Step 12 - Iterate indefinitely https://flic.kr/p/8Ny8Jt

  48. Congratulations, you have now written one software.

  49. You must now maintain it forever.

  50. muahaha.

  51. None
  52. Part 2

  53. Custom Resources Tips and Tricks

  54. Be explicit https://flic.kr/p/pz1JAo

  55. None
  56. None
  57. None
  58. None
  59. Converge twice during development Use markers for “negative” actions https://flic.kr/p/pqwd5U

  60. None
  61. None
  62. Tests let you refactor with confidence https://flic.kr/p/4WnjM4

  63. None
  64. STAR WIPE

  65. None
  66. Pure Ruby resources https://flic.kr/p/5km2fM

  67. Pure Ruby resources utilize - The Ruby Standard Library -

    Chef helper libs (mixlib-shellout) - Ruby gems
  68. None
  69. It might make sense to vendor gems

  70. load_current_value - Runs at converge time - Just before the

    action body - Loads desired state values
  71. None
  72. converge_if_changed - Runs if any desired_state: true property differs from

    user input - desired_state: false avoids comparison
  73. None
  74. coerce transforms values - Called when property is set -

    Called when property is read - Use this to raise errors on bad input
  75. The docker_container resource uses coercion heavily https://github.com/chef-cookbooks/ docker/blob/master/libraries/ docker_container.rb

  76. lazy defaults - Delays evaluation until used - Useful for

    calculated values
  77. None
  78. Composite resources https://flic.kr/p/fm58oo

  79. Composite resources - Reuse Chef resources - Actions look like

    recipes - Do not use load_current_value
  80. etcd_installation_binary :create

  81. httpd_config_rhel :create

  82. Avoid leaky abstractions https://flic.kr/p/7d1kDc

  83. Resources should be minimalist

  84. Early versions might be simple

  85. None
  86. They rarely stay that way

  87. Iterate and add more features Break down into smaller resources

  88. Use naming semantics to describe strategy

  89. etcd_installation_binary etcd_installation_docker etcd_installation_package etcd_service_manager_docker etcd_service_manager_execute etcd_service_manager_systemd etcd_service_manager_sysvinit etcd_service_manager_upstart

  90. Recycle patterns as they emerge

  91. docker_installation_binary docker_installation_package docker_installation_script docker_service_manager_execute docker_service_manager_systemd docker_service_manager_sysvinit docker_service_manager_upstart

  92. Chef selects defaults with the provider resolution system

  93. Compose resources Most specific to least specific

  94. None
  95. Enjoy

  96. fin