Slide 1

Slide 1 text

CHEF SUGAR

Slide 2

Slide 2 text

Chef Sugar? More like crème brûlée. Beautiful Ruby code and exceedingly useful! “

Slide 3

Slide 3 text

WHY?

Slide 4

Slide 4 text

COOK-494

Slide 5

Slide 5 text

COOK-494 "Create a core cookbook with useful primitives for system wrangling"

Slide 6

Slide 6 text

COOK-494

Slide 7

Slide 7 text

COOK-494

Slide 8

Slide 8 text

COOK-494

Slide 9

Slide 9 text

COOK-494

Slide 10

Slide 10 text

COOK-494

Slide 11

Slide 11 text

COOK-494 ✓

Slide 12

Slide 12 text

COOK-494 ✓

Slide 13

Slide 13 text

COOK-494 ✓

Slide 14

Slide 14 text

COOK-494 ✓ ✓

Slide 15

Slide 15 text

I LIED

Slide 16

Slide 16 text

WELL NOW THIS IS AWKWARD...

Slide 17

Slide 17 text

BACK UP

Slide 18

Slide 18 text

CHEF SUGAR

Slide 19

Slide 19 text

SETH

Slide 20

Slide 20 text

SETH VARGO

Slide 21

Slide 21 text

SETH VARGO @SETHVARGO

Slide 22

Slide 22 text

SETH VARGO FAUXHAI CHEFSPEC BERKSHELF STOVE TEST KITCHEN @SETHVARGO

Slide 23

Slide 23 text

SETH VARGO FAUXHAI CHEFSPEC BERKSHELF STOVE TEST KITCHEN #LEARNCHEF @SETHVARGO

Slide 24

Slide 24 text

SETH VARGO FAUXHAI CHEFSPEC BERKSHELF STOVE TEST KITCHEN #LEARNCHEF @SETHVARGO (STUFF) ...

Slide 25

Slide 25 text

SETH VARGO FAUXHAI CHEFSPEC BERKSHELF STOVE TEST KITCHEN #LEARNCHEF @SETHVARGO RELEASE ENGINEER (STUFF) ...

Slide 26

Slide 26 text

SETH VARGO FAUXHAI CHEFSPEC BERKSHELF STOVE TEST KITCHEN #LEARNCHEF @SETHVARGO RELEASE ENGINEER (STUFF) ...

Slide 27

Slide 27 text

THIS FUCKING GUARD

Slide 28

Slide 28 text

YOU HAVE ALL DONE IT

Slide 29

Slide 29 text

execute 'something' do command './configure && make && make install' not_if 'test -x /usr/local/bin/something' end

Slide 30

Slide 30 text

execute 'something' do command './configure && make && make install' not_if 'test -x /usr/local/bin/something' end VERSION?

Slide 31

Slide 31 text

execute 'something' do command './configure && make && make install' not_if <<-EOH.gsub(/^ {4}/, '') test -x /usr/local/bin/something && /usr/local/bin/something --version | grep 1.0 EOH end VERSION!

Slide 32

Slide 32 text

execute 'something' do command './configure && make && make install' not_if <<-EOH.gsub(/^ {4}/, '') test -x#{install_path} && #{install_path} --version | grep #{version} EOH end DYNAMIC

Slide 33

Slide 33 text

execute 'something' do command './configure && make && make install' not_if <<-EOH.gsub(/^ {4}/, '') test -x#{node['app']['config']['install_path']} && #{node['app']['config']['install_path']} --version | .. EOH end DYNAMIC

Slide 34

Slide 34 text

AND THIS KEPT HAPPENING OVER 'N OVER

Slide 35

Slide 35 text

AND IT IS VERY INEFFICIENT TO SHELL OUT

Slide 36

Slide 36 text

execute 'something' do command './configure && make && make install' not_if { installed_at_version?('something', '1.0') } end SPOILER

Slide 37

Slide 37 text

execute 'something' do command './configure && make && make install' not_if { installed_at_version?('something', '1.0') } end NO VERSION?

Slide 38

Slide 38 text

execute 'something' do command './configure && make && make install' not_if { installed?('something') } end NO PROBLEM!

Slide 39

Slide 39 text

execute 'something' do command './configure && make && make install' not_if do current = version_for('something').to_f current < 5.0 && current != 4.1 end end COMPLICATED?

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

YO DAWG,
 LOOK WHAT I MADE!

Slide 45

Slide 45 text

I'M PRETTY SURE THERE'S AN OPEN COOK TICKET...

Slide 46

Slide 46 text

I'M PRETTY SURE THERE'S AN OPEN COOK TICKET...

Slide 47

Slide 47 text

I'M PRETTY SURE THERE'S AN OPEN COOK TICKET...

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

COOK-494

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

+

Slide 53

Slide 53 text

+ =

Slide 54

Slide 54 text

+ = CHEF SUGAR

Slide 55

Slide 55 text

LÉ EXÄMPLES

Slide 56

Slide 56 text

"CLOUD" templates '/tmp/config' do variables(use_vpc: cloud?) end

Slide 57

Slide 57 text

DATA BAGS bag = Chef::EncryptedDataBagItem.load('accounts', 'hipchat') bag = encrypted_data_bag_item('accounts', 'hipchat')

Slide 58

Slide 58 text

ATTRIBUTES default['apache2']['config']['root'] = '/var/www' namespace 'apache2' do namespace 'config' do root '/var/www' end end

Slide 59

Slide 59 text

CONSTRAINTS Gem::Constraint.new('~> 11.0') .satisfied_by?(Gem::Version.new(Chef::VERSION)) chef_version.satisfies?('~> 11.0')

Slide 60

Slide 60 text

GEMS require 'nokogiri' #=> LoadError, ugly stacktrace require_chef_gem 'nokogiri' #=> ! Chef could not load the gem `nokogiri'! You may need to install the gem manually with `gem install nokogiri', or include a recipe before you can use this resource. Please consult the documentation for this cookbook for proper usage.

Slide 61

Slide 61 text

DEEP FETCH node['apache2']['config']['root'] #=> '/var/www' node.deep_fetch('apache2', 'config', 'root') #=> '/var/www'

Slide 62

Slide 62 text

DEEP FETCH node['apache2']['bad_key']['root'] #=> Undefined method :[] for nil:NilClass node.deep_fetch('apache2', 'bad_key', 'root') #=> nil

Slide 63

Slide 63 text

DEEP FETCH node['apache2']['bad_key']['root'] #=> Undefined method :[] for nil:NilClass node.deep_fetch!('apache2', 'bad_key', 'root') #=> No attribute node['apache2']['bad_key']['root'] exists on the current node. Please make sure you have spelled everything correctly.

Slide 64

Slide 64 text

PLATFORMS if node['platform'] == 'ubuntu' if ubuntu?

Slide 65

Slide 65 text

NAME PLATFORMS if node['platform'] == 'ubuntu' && node['platform_version'].to_f < 12.04 if ubuntu_before_precise?

Slide 66

Slide 66 text

NAME PLATFORMS debian_after_squeeze? debian_after_or_at_squeeze? debian_squeeze? debian_before_squeeze? debian_before_or_at_squeeze? debian_after_wheezy? debian_after_or_at_wheezy? debian_wheezy? debian_before_wheezy? debian_before_or_at_wheezy? debian_after_jessie? debian_after_or_at_jessie? debian_jessie? debian_before_jessie? debian_before_or_at_jessie? linuxmint_after_olivia? linuxmint_after_or_at_olivia? linuxmint_olivia? linuxmint_before_olivia? linuxmint_before_or_at_olivia? linuxmint_after_nadia? linuxmint_after_or_at_nadia? linuxmint_nadia? linuxmint_before_nadia? linuxmint_before_or_at_nadia? linuxmint_after_maya? linuxmint_after_or_at_maya? linuxmint_maya? linuxmint_before_maya? linuxmint_before_or_at_maya? linuxmint_after_lisa? linuxmint_after_or_at_lisa? linuxmint_lisa? linuxmint_before_lisa? linuxmint_before_or_at_lisa? mac_os_x_after_lion? mac_os_x_after_or_at_lion? mac_os_x_lion? mac_os_x_before_lion? mac_os_x_before_or_at_lion? mac_os_x_after_mountain_lion? mac_os_x_after_or_at_mountain_lion? mac_os_x_mountain_lion? mac_os_x_before_mountain_lion? mac_os_x_before_or_at_mountain_lion? mac_os_x_after_mavericks? mac_os_x_after_or_at_mavericks? mac_os_x_mavericks? mac_os_x_before_mavericks? mac_os_x_before_or_at_mavericks? ubuntu_after_lucid? ubuntu_after_or_at_lucid? ubuntu_lucid? ubuntu_before_lucid? ubuntu_before_or_at_lucid? ubuntu_after_maverick? ubuntu_after_or_at_maverick? ubuntu_maverick? ubuntu_before_maverick? ubuntu_before_or_at_maverick? ubuntu_after_natty? ubuntu_after_or_at_natty? ubuntu_natty? ubuntu_before_natty? ubuntu_before_or_at_natty? ubuntu_after_oneiric? ubuntu_after_or_at_oneiric? ubuntu_oneiric? ubuntu_before_oneiric? ubuntu_before_or_at_oneiric? ubuntu_after_precise? ubuntu_after_or_at_precise? ubuntu_precise? ubuntu_before_precise? ubuntu_before_or_at_precise? ubuntu_after_quantal? ubuntu_after_or_at_quantal? ubuntu_quantal? ubuntu_before_quantal? ubuntu_before_or_at_quantal? ubuntu_after_raring? ubuntu_after_or_at_raring? ubuntu_raring? ubuntu_before_raring? ubuntu_before_or_at_raring? ubuntu_after_saucy? ubuntu_after_or_at_saucy? ubuntu_saucy? ubuntu_before_saucy? ubuntu_before_or_at_saucy? linux_mint? mint? ubuntu? amazon_linux? amazon? centos? oracle_linux? oracle? scientific_linux? scientific? redhat_enterprise_linux? redhat_enterprise?

Slide 67

Slide 67 text

FAMILIES if node['platform_family'] == 'debian' if debian?

Slide 68

Slide 68 text

FILTERS package 'apache2' do action :nothing end.run_action(:install) compile_time do package 'apache2' end

Slide 69

Slide 69 text

THAT'S BASICALLY ALL OF CHEF SUGAR

Slide 70

Slide 70 text

STOP

Slide 71

Slide 71 text

STRUCTURE !"" Gemfile !"" Rakefile !"" chef-sugar.gemspec !"" lib # $"" chef # !"" sugar # # !"" ... # $"" sugar.rb !"" metadata.rb !"" recipes # $"" default.rb

Slide 72

Slide 72 text

STRUCTURE !"" Gemfile !"" Rakefile !"" chef-sugar.gemspec !"" lib # $"" chef # !"" sugar # # !"" ... # $"" sugar.rb !"" metadata.rb !"" recipes # $"" default.rb

Slide 73

Slide 73 text

STRUCTURE !"" Gemfile !"" Rakefile !"" chef-sugar.gemspec !"" lib # $"" chef # !"" sugar # # !"" ... # $"" sugar.rb !"" metadata.rb !"" recipes # $"" default.rb

Slide 74

Slide 74 text

STRUCTURE !"" Gemfile !"" Rakefile !"" chef-sugar.gemspec !"" lib # $"" chef # !"" sugar # # !"" ... # $"" sugar.rb !"" metadata.rb !"" recipes # $"" default.rb YES!

Slide 75

Slide 75 text

STRUCTURE !"" Gemfile !"" Rakefile !"" chef-sugar.gemspec !"" lib # $"" chef # !"" sugar # # !"" ... # $"" sugar.rb !"" metadata.rb !"" recipes # $"" default.rb YES! ENHANCE!

Slide 76

Slide 76 text

STRUCTURE !"" lib # $"" chef # !"" sugar # # !"" architecture.rb # # !"" cloud.rb # # !"" constraints.rb # # !"" core_extensions # # # !"" array.rb # # # $"" string.rb # # !"" core_extensions.rb # # !"" data_bag.rb # # !"" filters.rb # # !"" ip.rb # # !"" kernel.rb

Slide 77

Slide 77 text

SUGAR.RB class Chef module Sugar require_relative 'sugar/architecture' # ... end end ! Chef::Recipe.send(:include, Chef::Sugar::DSL) Chef::Resource.send(:include, Chef::Sugar::DSL) Chef::Provider.send(:include, Chef::Sugar::DSL) ! Object.send(:include, Chef::Sugar::Kernel)

Slide 78

Slide 78 text

SUGAR.RB class Chef module Sugar require_relative 'sugar/architecture' # ... end end ! Chef::Recipe.send(:include, Chef::Sugar::DSL) Chef::Resource.send(:include, Chef::Sugar::DSL) Chef::Provider.send(:include, Chef::Sugar::DSL) ! Object.send(:include, Chef::Sugar::Kernel)

Slide 79

Slide 79 text

SUGAR.RB class Chef module Sugar require_relative 'sugar/architecture' # ... end end ! Chef::Recipe.send(:include, Chef::Sugar::DSL) Chef::Resource.send(:include, Chef::Sugar::DSL) Chef::Provider.send(:include, Chef::Sugar::DSL) ! Object.send(:include, Chef::Sugar::Kernel)

Slide 80

Slide 80 text

SUGAR.RB class Chef module Sugar require_relative 'sugar/architecture' # ... end end ! Chef::Recipe.send(:include, Chef::Sugar::DSL) Chef::Resource.send(:include, Chef::Sugar::DSL) Chef::Provider.send(:include, Chef::Sugar::DSL) ! Object.send(:include, Chef::Sugar::Kernel)

Slide 81

Slide 81 text

SHELL.RB class Chef module Sugar::Shell extend self ! def which(cmd) # ... magic codez end end ! module DSL def which(cmd); Chef::Sugar::Shell.which(cmd); end end end

Slide 82

Slide 82 text

SHELL.RB class Chef module Sugar::Shell extend self ! def which(cmd) # ... magic codez end end ! module DSL def which(cmd); Chef::Sugar::Shell.which(cmd); end end end

Slide 83

Slide 83 text

SHELL.RB class Chef module Sugar::Shell extend self ! def which(cmd) # ... magic codez end end ! module DSL def which(cmd); Chef::Sugar::Shell.which(cmd); end end end

Slide 84

Slide 84 text

SHELL.RB class Chef module Sugar::Shell extend self ! def which(cmd) # ... magic codez end end ! module DSL def which(cmd); Chef::Sugar::Shell.which(cmd); end end end

Slide 85

Slide 85 text

SHELL.RB class Chef module Sugar::Shell extend self ! def which(cmd) # ... magic codez end end ! module DSL def which(cmd); Chef::Sugar::Shell.which(cmd); end end end

Slide 86

Slide 86 text

SHELL.RB class Chef module Sugar::Shell extend self ! def which(cmd) # ... magic codez end end ! module DSL def which(cmd); Chef::Sugar::Shell.which(cmd); end end end

Slide 87

Slide 87 text

SUGAR.RB class # ... end ! Chef::Recipe.send( Chef::Resource.send( Chef::Provider.send( ! Object.send( ! ! ! ! ! ! ! ! Chef::Resource.send(:include, Chef::Sugar::DSL)

Slide 88

Slide 88 text

CORE EXTENSIONS ARE EVIL

Slide 89

Slide 89 text

SO I ADDED SOME INSIDE CHEF SUGAR

Slide 90

Slide 90 text

BUT THEY ARE NOT REQUIRED

Slide 91

Slide 91 text

REQUIRING BY DEFAULT IS AN ANTI-PATTERN

Slide 92

Slide 92 text

RECIPE.RB include_recipe 'chef_sugar::default' require 'chef/sugar/core_extensions'

Slide 93

Slide 93 text

RECIPE.RB include_recipe 'chef_sugar::default' require 'chef/sugar/core_extensions'

Slide 94

Slide 94 text

RECIPE.RB include_recipe 'chef_sugar::default' require 'chef/sugar/core_extensions'

Slide 95

Slide 95 text

RECIPE.RB include_recipe 'chef_sugar::default' require 'chef/sugar/core_extensions' ! version('1.2.3').satisfies?('~> 1.2.0')

Slide 96

Slide 96 text

RECIPE.RB include_recipe 'chef_sugar::default' require 'chef/sugar/core_extensions' ! version('1.2.3').satisfies?('~> 1.2.0') ! ! ! '1.2.3'.satisfies?('~> 1.2.0')

Slide 97

Slide 97 text

THAT'S ALL FOLKS

Slide 98

Slide 98 text

SETH VARGO EXTRA RESOURCES @SETHVARGO Info Q: http://www.infoq.com/news/2014/01/chef-sugar Code: https://github.com/sethvargo/chef-sugar Blog: https://sethvargo.com/spice-up-your-recipes-with-chef-sugar/

Slide 99

Slide 99 text

SETH VARGO QUESTIONS? @SETHVARGO Info Q: http://www.infoq.com/news/2014/01/chef-sugar Code: https://github.com/sethvargo/chef-sugar Blog: https://sethvargo.com/spice-up-your-recipes-with-chef-sugar/