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

Spice up your recipes with Chef Sugar

Spice up your recipes with Chef Sugar

Slides from my ChefConf 2014 talk :)

Seth Vargo

April 16, 2014
Tweet

More Decks by Seth Vargo

Other Decks in Technology

Transcript

  1. execute 'something' do command './configure && make && make install'

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

    not_if 'test -x /usr/local/bin/something' end VERSION?
  3. 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!
  4. 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
  5. 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
  6. execute 'something' do command './configure && make && make install'

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

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

    not_if { installed?('something') } end NO PROBLEM!
  9. 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?
  10. +

  11. + =

  12. 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.
  13. 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.
  14. 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?
  15. STRUCTURE !"" Gemfile !"" Rakefile !"" chef-sugar.gemspec !"" lib #

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

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

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

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

    $"" chef # !"" sugar # # !"" ... # $"" sugar.rb !"" metadata.rb !"" recipes # $"" default.rb YES! ENHANCE!
  20. 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
  21. 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)
  22. 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)
  23. 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)
  24. 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)
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. SUGAR.RB class # ... end ! Chef::Recipe.send( Chef::Resource.send( Chef::Provider.send( !

    Object.send( ! ! ! ! ! ! ! ! Chef::Resource.send(:include, Chef::Sugar::DSL)