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

Chef LWRP

Chef LWRP

Introduction to Chef's LWRP

23f4d5d797a91b6d17d627b90b5a42d9?s=128

Kentaro Kuribayashi

April 02, 2013
Tweet

More Decks by Kentaro Kuribayashi

Other Decks in Technology

Transcript

  1. Chef LWRP @kentaro

  2. @kentaro Software engineer Rubyist / Perl Monger Puppet Lover Kentaro

    Kuribayashi paperboy&co.
  3. None
  4. ͓͞Β͍

  5. γεςϜͷ͋Δ΂͖ঢ়ଶΛهड़ ͢ΔͨΊͷύʔπɻͨͱ͑͹ wpackageΠϯετʔϧ͞Ε͍ͯΔ΂ ͖ύοέʔδ wfile഑ஔ͞Ε͍ͯΔ΂͖ϑΝΠϧ wserviceಈ͍͍ͯΔ΂͖αʔϏε Resourceͱ͸

  6. γεςϜΛऩଋ͢Δ࣮ࡍͷํ๏ Λఏڙ͢Δɻͨͱ͑͹package Ϧιʔεʹ͸ҎԼ͕͋Δ w apt w yum w portage w

    etc. Providerͱ͸
  7. ಠࣗϦιʔεͷఆٛ

  8. plenv_install "5.16.3" do user "kentaro" install_options "-Dusethreads" end ͜͏͍͏͜ͱΛ͍ͨ͠

  9. • Definition • LWRP • LightWeight Resources and Providers

  10. Definition

  11. wؔ਺Έ͍ͨͳײ͡ͰಠࣗͷϦ ιʔεΛఆٛͰ͖Δ wಉ͡Α͏ͳॲཧΛ΍͍ͬͯΔ ͳͱࢥͬͨΒɺ͜ΕͰ؆୯ʹڞ ௨ԽͰ͖ΔͷͰศར wdefinitions/ҎԼʹεΫϦϓτ Λॻ͚ͩ͘

  12. cpanm 'Path::Tiny' do force true end @naoyaஶʰೖ໳Chef SoloʱΑΓ ͜Μͳײ͡ʹ࢖͏΋ͷΛʜ

  13. define :cpanm, :force => nil do bash "install-#{params[:name]}" do user

    node['user']['name'] cwd node['user']['home'] environment "HOME" => node['user']['home'] if params[:force] code <<-EOC source ~/perl5/perlbrew/etc/bashrc cpanm --force #{params[:name]} EOC else code <<-EOC source ~/perl5/perlbrew/etc/bashrc cpanm #{params[:name]} EOC end not_if <<-EOC, :user => node['user']['name'], :environment => { 'HOME' => node['user']['home'] } source ~/perl5/perlbrew/etc/bashrc && perl -m#{params[:name]} -e '' EOC end end @naoyaஶʰೖ໳Chef SoloʱΑΓ ͜͏ॻ͚Δ
  14. wProvider͕ผΕͯͳ͍ͷͰɺ େ͖ͳن໛ʹͳΔͱͪ͝Όͬͱ ͳΔɻҰൠԽ͕೉͍͠ɻ wόϦσʔγϣϯΛࣗ෼Ͱ͕Μ ͹Βͳ͍ͱͳΒͳ͍ wႈ౳ੑ͸ࣗ෼Ͱ୲อ͢Δ ͜Ε ͸LWRPͷ৔߹΋ಉ͡ ɻ

  15. LWRP

  16. wಠࣗͷresourceͱproviderΛ ఆٛ͢ΔͨΊͷDSL wΑΓҰൠతͳఆٛΛ͍ͨ͠৔ ߹ʹ࢖͏ wύϥϝλͷόϦσʔγϣϯͷ ࢓૊Έ΋ఏڙ͞Ε͍ͯΔ

  17. • example/resources/*.rb • example/providers/*.rb

  18. ϑΝΠϧ໊ ରԠ͢Δ໊લ Resource Provider default.rb example Chef::Resource::E xample Chef::Provider::E xample

    custom.rb example_custom Chef::Resource::E xampleCustom Chef::Provider::E xampleCustom http://docs.opscode.com/essentials_cookbook_lwrp_file_locations.html
  19. plenv_install "5.16.3" do user "kentaro" install_options "-Dusethreads" end https://github.com/kentaro/chef-plenv

  20. actions :install, :uninstall default_action :install attribute :version, :kind_of => String,

    :name_attribute => true attribute :user, :kind_of => String attribute :install_options, :kind_of => String resouces/install.rb https://github.com/kentaro/chef-plenv/blob/master/resources/install.rb
  21. • :default • :kind_of • :required • :regex • :equal_to

    • :name_attribute • :callbacks • :respond_to όϦσʔγϣϯ http://docs.opscode.com/essentials_cookbook_lwrp_resources_syntax.html#attributes-and-validation-parameters
  22. action :install do converge_by("Install perl version #{new_resource.name} using plenv") do

    bash "plenv install #{new_resource.name}" do user new_resource.user environment "HOME" => "#{node["plenv"]["user_home_root"]}/ #{new_resource.user}" path ["#{node["plenv"]["user_home_root"]}/ #{new_resource.user}/.plenv/bin"] # `path` option seems to not work correctly... code <<-COMMAND #{node["plenv"]["user_home_root"]}/#{new_resource.user}/.plenv/bin/plenv install #{new_resource.name} #{new_resource.install_options} COMMAND not_if { Dir.exists?("#{node["plenv"]["user_home_root"]}/ #{new_resource.user}/.plenv/versions/#{new_resource.name}") } end end end providers/install.rb https://github.com/kentaro/chef-plenv/blob/master/providers/install.rb
  23. Why-run

  24. w͍ΘΏΔdry-runΦϓγϣϯ w10.14.0͔Βಋೖ͞Εͨ A key part of why-run is that it

    tells you why it makes the decision that it did, so you can reason about the current and proposed state of your system, hence the name. http://www.opscode.com/blog/2012/09/07/chef-10-14-0-released/
  25. action :install do converge_by("install foo bar") do # ͜͜ʹɺ࣮ࡍʹγεςϜʹӨڹΛ༩͑ΔॲཧΛॻ͘ end

    end def whyrun_supported? true end Why-runʹରԠ͢Δ
  26. w Why-runͷ࣮ߦ୯Ґ͸ɺresouceʹඥͮ͘ provider w ͳͷͰɺطଘͷresouceΛ࢖Θͳ͍ಠࣗͷ providerΛॻ͘ શ෦RubyͷίʔυͰ͕Μ͹Δͱ ͔ ৔߹͸ରԠ͓ͯ͘͠ํ͕͍͍ ͱ͍͏͔͢Δ΂

    ͖ wChef͕ఏڙ͢ΔresourceΛ࢖ͬͯΔݶΓ͸ɺ ͦΕʹରԠ͢Δprovider͕ඞͣWhy-runରԠͯ͠ ͍ΔͷͰɺͳʹ΋͠ͳͯ͘΋࠷ऴతʹ͸Why-run ରԠ͍ͯ͠Δ͜ͱʹͳΔ wෳࡶͳͷͰઆ໌͕೉͍͠
  27. ·ͱΊ

  28. wಠࣗͷϦιʔεΛఆٛ͢Δ࢓૊Έ ͱͯ͠DefinitionͱLWRPͱ͕͋Δ w؆୯ͳ΋ͷ͸DefinitionͰΑ͍ ͕ɺΑΓෳࡶͩͬͨΓҰൠతͩͬͨ Γ͢Δ΋ͷɺ͋Δ͍͸طଘͷ resouceΛ࢖Θͳ͍ಠࣗͷprovider Λ࡞Γ͍ͨ৔߹͸LWRP wόϦσʔγϣϯ΋͍͍ײ͡ʹՄೳ