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

Hand Crafted Artisinal Chef Resources

someara
February 02, 2016

Hand Crafted Artisinal Chef Resources

Configuration Management Camp
Ghent Belgium
February 2016

someara

February 02, 2016
Tweet

More Decks by someara

Other Decks in Technology

Transcript

  1. Hand Crafted Artisanal
    Chef Resources

    View full-size slide

  2. Config Management Camp
    Ghent, Belgium
    February 2016

    View full-size slide

  3. Writing library cookbooks
    An opinionated workflow
    Step by step

    View full-size slide

  4. Step 1 - Disregard dogma
    “Unit tests first” is a dirty lie
    https://flic.kr/p/aTSiXF

    View full-size slide

  5. Step 2 - Make things up
    Then work backwards
    https://flic.kr/p/dXkQ5s

    View full-size slide

  6. Contrived examples are the best
    examples

    View full-size slide

  7. Place this off to the side, we will
    come back for it later.

    View full-size slide

  8. Step 3 - Create a cookbook
    By hand. Be a wizard.
    https://flic.kr/p/3mMW6e

    View full-size slide

  9. mkdir -p ~/src/custom-resources-tutorial
    cd ~/src/custom-resources-tutorial
    emacs metadata.rb

    View full-size slide

  10. Step 4 - Embed a test cookbook.
    By hand. Be a wizard. Again.
    https://flic.kr/p/ai36NG

    View full-size slide

  11. 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

    View full-size slide

  12. Step 5 - Tell Berkshelf about it
    https://flic.kr/p/k5jmja

    View full-size slide

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

    View full-size slide

  14. Step 6 - Configure Test Kitchen
    https://flic.kr/p/r38F5c

    View full-size slide

  15. emacs .kitchen.yml

    View full-size slide

  16. kitchen test ; echo $?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  19. rubocop -a
    git init
    git add .
    git commit -a -m 'v0.0.1 - cookbook skeleton'
    git tag v0.0.1

    View full-size slide

  20. Step 8 - Use resource in test recipe
    https://flic.kr/p/8MuUAX

    View full-size slide

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

    View full-size slide

  22. kitchen converge ; echo $?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  25. kitchen converge ; echo $?
    kitchen converge ; echo $?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  28. kitchen verify ; echo $?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  31. emacs spec/my_file_spec.rb

    View full-size slide

  32. rubocop -a
    rspec ; echo $?
    git add .
    git commit -a -m “v0.1.0 - my_file resource”
    git tag v0.1.0

    View full-size slide

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

    View full-size slide

  34. Congratulations, you have now written one
    software.

    View full-size slide

  35. You must now maintain it forever.

    View full-size slide

  36. Custom Resources
    Tips and Tricks

    View full-size slide

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

    View full-size slide

  38. Converge twice during development
    Use markers for “negative” actions
    https://flic.kr/p/pqwd5U

    View full-size slide

  39. Tests let you refactor with confidence
    https://flic.kr/p/4WnjM4

    View full-size slide

  40. Pure Ruby resources
    https://flic.kr/p/5km2fM

    View full-size slide

  41. Pure Ruby resources utilize
    - The Ruby Standard Library
    - Chef helper libs (mixlib-shellout)
    - Ruby gems

    View full-size slide

  42. It might make sense to vendor gems

    View full-size slide

  43. load_current_value
    - Runs at converge time
    - Just before the action body
    - Loads desired state values

    View full-size slide

  44. converge_if_changed
    - Runs if any desired_state: true
    property differs from user input
    - desired_state: false avoids
    comparison

    View full-size slide

  45. coerce transforms values
    - Called when property is set
    - Called when property is read
    - Use this to raise errors on bad input

    View full-size slide

  46. The docker_container resource uses
    coercion heavily
    https://github.com/chef-cookbooks/
    docker/blob/master/libraries/
    docker_container.rb

    View full-size slide

  47. lazy defaults
    - Delays evaluation until used
    - Useful for calculated values

    View full-size slide

  48. Composite resources
    https://flic.kr/p/fm58oo

    View full-size slide

  49. Composite resources
    - Reuse Chef resources
    - Actions look like recipes
    - Do not use load_current_value

    View full-size slide

  50. etcd_installation_binary :create

    View full-size slide

  51. httpd_config_rhel :create

    View full-size slide

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

    View full-size slide

  53. Resources should be minimalist

    View full-size slide

  54. Early versions might be simple

    View full-size slide

  55. They rarely stay that way

    View full-size slide

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

    View full-size slide

  57. Use naming semantics to
    describe strategy

    View full-size slide

  58. 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

    View full-size slide

  59. Recycle patterns as they emerge

    View full-size slide

  60. 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

    View full-size slide

  61. Chef selects defaults with the
    provider resolution system

    View full-size slide

  62. Compose resources
    Most specific to least specific

    View full-size slide