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

Chef LWRP

Chef LWRP

Introduction to Chef's LWRP

Kentaro Kuribayashi

April 02, 2013
Tweet

More Decks by Kentaro Kuribayashi

Other Decks in Technology

Transcript

  1. 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ʱΑΓ ͜͏ॻ͚Δ
  2. ϑΝΠϧ໊ ରԠ͢Δ໊લ 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
  3. 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
  4. • :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
  5. 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
  6. 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/
  7. w Why-runͷ࣮ߦ୯Ґ͸ɺresouceʹඥͮ͘ provider w ͳͷͰɺطଘͷresouceΛ࢖Θͳ͍ಠࣗͷ providerΛॻ͘ શ෦RubyͷίʔυͰ͕Μ͹Δͱ ͔ ৔߹͸ରԠ͓ͯ͘͠ํ͕͍͍ ͱ͍͏͔͢Δ΂

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