hand crafted artisanal chef resources

98
Hand Crafted Artisanal Chef Resources

Upload: sean-omeara

Post on 07-Jan-2017

1.122 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Hand Crafted Artisanal Chef Resources

Hand Crafted ArtisanalChef Resources

Page 2: Hand Crafted Artisanal Chef Resources

Config Management CampGhent, BelgiumFebruary 2016

Page 3: Hand Crafted Artisanal Chef Resources

Sean [email protected]

@someara

Page 4: Hand Crafted Artisanal Chef Resources

whoami

Page 5: Hand Crafted Artisanal Chef Resources

Part 1

Page 6: Hand Crafted Artisanal Chef Resources

Writing library cookbooks An opinionated workflow Step by step

Page 7: Hand Crafted Artisanal Chef Resources

Step 1 - Disregard dogma “Unit tests first” is a dirty lie

https://flic.kr/p/aTSiXF

Page 8: Hand Crafted Artisanal Chef Resources

Step 2 - Make things up Then work backwards

https://flic.kr/p/dXkQ5s

Page 9: Hand Crafted Artisanal Chef Resources

Contrived examples are the best examples

Page 10: Hand Crafted Artisanal Chef Resources
Page 11: Hand Crafted Artisanal Chef Resources

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

Page 12: Hand Crafted Artisanal Chef Resources

Step 3 - Create a cookbook By hand. Be a wizard.

https://flic.kr/p/3mMW6e

Page 13: Hand Crafted Artisanal Chef Resources

mkdir -p ~/src/custom-resources-tutorialcd ~/src/custom-resources-tutorialemacs metadata.rb

Page 14: Hand Crafted Artisanal Chef Resources
Page 15: Hand Crafted Artisanal Chef Resources

Step 4 - Embed a test cookbook. By hand. Be a wizard. Again.

https://flic.kr/p/ai36NG

Page 16: Hand Crafted Artisanal Chef Resources

mkdir -p test/cookbooks/my_file_testcd test/cookbooks/my_file_testecho “test cookbook” > README.mdmkdir recipestouch recipes/default.rbemacs metadata.rb

Page 17: Hand Crafted Artisanal Chef Resources
Page 18: Hand Crafted Artisanal Chef Resources

Step 5 - Tell Berkshelf about it

https://flic.kr/p/k5jmja

Page 19: Hand Crafted Artisanal Chef Resources

cd ~/src/custom-resources-tutorialemacs Berksfile

Page 20: Hand Crafted Artisanal Chef Resources
Page 21: Hand Crafted Artisanal Chef Resources

Step 6 - Configure Test Kitchen

https://flic.kr/p/r38F5c

Page 22: Hand Crafted Artisanal Chef Resources

emacs .kitchen.yml

Page 23: Hand Crafted Artisanal Chef Resources
Page 24: Hand Crafted Artisanal Chef Resources

kitchen test ; echo $?

Page 25: Hand Crafted Artisanal Chef Resources

Step 7 - Save game

https://flic.kr/p/6ZJ4G7

Page 26: Hand Crafted Artisanal Chef Resources

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

Page 27: Hand Crafted Artisanal Chef Resources

rubocop -agit initgit add .git commit -a -m 'v0.0.1 - cookbook skeleton' git tag v0.0.1

Page 28: Hand Crafted Artisanal Chef Resources

Step 8 - Use resource in test recipe

https://flic.kr/p/8MuUAX

Page 29: Hand Crafted Artisanal Chef Resources

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

Page 30: Hand Crafted Artisanal Chef Resources
Page 31: Hand Crafted Artisanal Chef Resources

kitchen converge ; echo $?

Page 32: Hand Crafted Artisanal Chef Resources

Step 9 - Implement resource

https://flic.kr/p/bkVKRb

Page 33: Hand Crafted Artisanal Chef Resources

cd ~/src/custom-resources-tutorialmkdir librariesemacs libraries/my_file.rb

Page 34: Hand Crafted Artisanal Chef Resources
Page 35: Hand Crafted Artisanal Chef Resources

kitchen converge ; echo $?kitchen converge ; echo $?

Page 36: Hand Crafted Artisanal Chef Resources

Step 10 - Test for behavior

https://i.ytimg.com/vi/1y8Rqvz-Jcg/maxresdefault.jpgz

Page 37: Hand Crafted Artisanal Chef Resources

mkdir -p test/integration/my_file/inspecemacs -p test/integration/my_file/inspec/run_spec.rb

Page 38: Hand Crafted Artisanal Chef Resources
Page 39: Hand Crafted Artisanal Chef Resources

kitchen verify ; echo $?

Page 40: Hand Crafted Artisanal Chef Resources

Step 11 - Add specs and commit

https://flic.kr/p/rrgfZh

Page 41: Hand Crafted Artisanal Chef Resources

cd ~/src/custom-resources-tutorialmkdir specemacs spec/spec_helper.rb

Page 42: Hand Crafted Artisanal Chef Resources
Page 43: Hand Crafted Artisanal Chef Resources

emacs spec/my_file_spec.rb

Page 44: Hand Crafted Artisanal Chef Resources
Page 45: Hand Crafted Artisanal Chef Resources

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

Page 46: Hand Crafted Artisanal Chef Resources

stove 0.1.0

Page 47: Hand Crafted Artisanal Chef Resources

Step 12 - Iterate indefinitely

https://flic.kr/p/8Ny8Jt

Page 48: Hand Crafted Artisanal Chef Resources

Congratulations, you have now written one software.

Page 49: Hand Crafted Artisanal Chef Resources

You must now maintain it forever.

Page 50: Hand Crafted Artisanal Chef Resources

muahaha.

Page 51: Hand Crafted Artisanal Chef Resources
Page 52: Hand Crafted Artisanal Chef Resources

Part 2

Page 53: Hand Crafted Artisanal Chef Resources

Custom Resources Tips and Tricks

Page 54: Hand Crafted Artisanal Chef Resources

Be explicit

https://flic.kr/p/pz1JAo

Page 55: Hand Crafted Artisanal Chef Resources
Page 56: Hand Crafted Artisanal Chef Resources
Page 57: Hand Crafted Artisanal Chef Resources
Page 58: Hand Crafted Artisanal Chef Resources
Page 59: Hand Crafted Artisanal Chef Resources

Converge twice during development Use markers for “negative” actions

https://flic.kr/p/pqwd5U

Page 60: Hand Crafted Artisanal Chef Resources
Page 61: Hand Crafted Artisanal Chef Resources
Page 62: Hand Crafted Artisanal Chef Resources

Tests let you refactor with confidence

https://flic.kr/p/4WnjM4

Page 63: Hand Crafted Artisanal Chef Resources
Page 64: Hand Crafted Artisanal Chef Resources

STAR WIPE

Page 65: Hand Crafted Artisanal Chef Resources
Page 66: Hand Crafted Artisanal Chef Resources

Pure Ruby resources

https://flic.kr/p/5km2fM

Page 67: Hand Crafted Artisanal Chef Resources

Pure Ruby resources utilize

- The Ruby Standard Library - Chef helper libs (mixlib-shellout) - Ruby gems

Page 68: Hand Crafted Artisanal Chef Resources
Page 69: Hand Crafted Artisanal Chef Resources

It might make sense to vendor gems

Page 70: Hand Crafted Artisanal Chef Resources

load_current_value

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

Page 71: Hand Crafted Artisanal Chef Resources
Page 72: Hand Crafted Artisanal Chef Resources

converge_if_changed

- Runs if any desired_state: true property differs from user input

- desired_state: false avoids comparison

Page 73: Hand Crafted Artisanal Chef Resources
Page 74: Hand Crafted Artisanal Chef Resources

coerce transforms values

- Called when property is set - Called when property is read - Use this to raise errors on bad input

Page 75: Hand Crafted Artisanal Chef Resources

The docker_container resource uses coercion heavily

https://github.com/chef-cookbooks/docker/blob/master/libraries/docker_container.rb

Page 76: Hand Crafted Artisanal Chef Resources

lazy defaults

- Delays evaluation until used - Useful for calculated values

Page 77: Hand Crafted Artisanal Chef Resources
Page 78: Hand Crafted Artisanal Chef Resources

Composite resources

https://flic.kr/p/fm58oo

Page 79: Hand Crafted Artisanal Chef Resources

Composite resources

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

Page 80: Hand Crafted Artisanal Chef Resources

etcd_installation_binary :create

Page 81: Hand Crafted Artisanal Chef Resources

httpd_config_rhel :create

Page 82: Hand Crafted Artisanal Chef Resources

Avoid leaky abstractions

https://flic.kr/p/7d1kDc

Page 83: Hand Crafted Artisanal Chef Resources

Resources should be minimalist

Page 84: Hand Crafted Artisanal Chef Resources

Early versions might be simple

Page 85: Hand Crafted Artisanal Chef Resources
Page 86: Hand Crafted Artisanal Chef Resources

They rarely stay that way

Page 87: Hand Crafted Artisanal Chef Resources

Iterate and add more features Break down into smaller resources

Page 88: Hand Crafted Artisanal Chef Resources

Use naming semantics to describe strategy

Page 89: Hand Crafted Artisanal Chef Resources

etcd_installation_binaryetcd_installation_dockeretcd_installation_package

etcd_service_manager_dockeretcd_service_manager_executeetcd_service_manager_systemdetcd_service_manager_sysvinitetcd_service_manager_upstart

Page 90: Hand Crafted Artisanal Chef Resources

Recycle patterns as they emerge

Page 91: Hand Crafted Artisanal Chef Resources

docker_installation_binarydocker_installation_packagedocker_installation_script

docker_service_manager_executedocker_service_manager_systemddocker_service_manager_sysvinitdocker_service_manager_upstart

Page 92: Hand Crafted Artisanal Chef Resources

Chef selects defaults with the provider resolution system

Page 93: Hand Crafted Artisanal Chef Resources

Page 94: Hand Crafted Artisanal Chef Resources

Page 95: Hand Crafted Artisanal Chef Resources

Compose resources Most specific to least specific

Page 96: Hand Crafted Artisanal Chef Resources
Page 97: Hand Crafted Artisanal Chef Resources

Enjoy

Page 98: Hand Crafted Artisanal Chef Resources

fin