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

Itamae - Infra as Code 現状確認会

Itamae - Infra as Code 現状確認会

Ryota Arai

October 03, 2014
Tweet

More Decks by Ryota Arai

Other Decks in Programming

Transcript

  1. ࢖͍ํ 1.#ϨγϐΛॻ͘ # nginx.rb service "nginx" # nginxΛΠϯετʔϧ͢Δ package "nginx"

    template "/etc/nginx/nginx.conf" do source "nginx.conf.erb" # ઃఆϑΝΠϧ͕ߋ৽͞ΕͨΒɺnginxΛϦϩʔυ͢Δ notifies :reload, "service[nginx]" end 11
  2. SSH͝͠ʹ࣮ߦ΋Ͱ͖Δ $ itamae ssh -h 192.168.10.10 \ -p 22 -u

    user recipe.rb VagrantͰಈ͍͍ͯΔVMʹͭͳ͙ $ vagrant up $ itamae ssh --vagrant -h vm_name recipe.rb VagrantϓϥάΠϯͷvagrant(itamaeΛ࢖͏ख΋ 13
  3. ͚ͬ͜͏͍ܰ $ time sudo chef-solo -c solo.rb -j node.json Recipe:

    hello::default * execute[echo Hello] action run - execute echo Hello 1.70s user 0.74s system 71% cpu 3.414 total $ time itamae local hello/recipes/default.rb INFO : Recipe: /private/tmp/... INFO : execute[echo Hello] INFO : action: run 0.33s user 0.12s system 87% cpu 0.516 total # ͜ͷ৔߹ɺItamae͸OhaiΛୟ͍ͯͳ͍ͷͰͪΐͬͱνʔτͬΆ͍ 15
  4. ͚ͬ͜͏খ͍͞ $ gem i itamae --pre Successfully installed thor-0.19.1 Successfully

    installed net-scp-1.2.1 Successfully installed specinfra-2.0.0.beta43 Successfully installed hashie-3.3.1 Successfully installed ansi-1.4.3 Successfully installed itamae-1.0.0.beta50 6 gems installed 16
  5. remote_file,*template*Ϧιʔε remote_file͸ChefͰ͍͏cookbook_file remote_file "/etc/nginx/nginx.conf" do # ϨγϐϑΝΠϧ͔Βͷ૬ରύε source "nginx.conf" end

    template "/etc/nginx/nginx.conf" do # ϨγϐϑΝΠϧ͔Βͷ૬ରύε source "nginx.conf.erb" variables(server_name: "itamae.kitchen") end 20
  6. ڞ௨Ͱ࢖͑ΔΞτϦϏϡʔτ package "sl" do # ΞΫγϣϯ action :install # ͋ΔίϚϯυ͕੒ޭ͚ͨ࣌ͩ͠

    only_if "test -e /tmp/you_like_sl" # ͋ΔίϚϯυ͕ࣦഊ͚ͨ࣌ͩ͠ not_if "test -e /tmp/you_do_not_like_sl" # ࣮ߦϢʔβ user "myuser" end 22
  7. defini&on ෳ਺ͷϦιʔε͔Β৽͍͠ϦιʔεΛఆٛ͢Δ define :install_and_enable_package, version: nil do package params[:name] do

    version params[:version] if params[:version] action :install end service params[:name] do action :enable end end install_and_enable_package 'nginx' do version '1.6.1' end 26
  8. include_recipe Ϩγϐ͔ΒଞͷϨγϐΛݺͼग़͢ # nginx.rb package "nginx" template "/etc/nginx/conf.d/hello" do source

    "nginx.erb" end # sl.rb package "sl" # web_server.rb include_recipe "nginx.rb" include_recipe "sl.rb" 27
  9. ϊʔυΞτϦϏϡʔτ • itamae -j node.jsonͳͲͱJSONϑΝΠϧΛ ࢦఆ͢Δͱ • Ϩγϐ΍ςϯϓϨʔτ಺Ͱnode[:key]ͷΑ͏ʹ ࢀরͰ͖Δ $

    echo '{"nginx": {"domain": "itamae.kitchen"}}' \ > node.json $ itamae local -j node.json your_recipe.rb 29
  10. OhaiΠϯςάϨʔγϣϯ Ohai? $ ohai | head { "cpu": { "real":

    4, "total": 8, "mhz": 2600, "vendor_id": "GenuineIntel\n", "model_name": "Intel(R) ...", "model": 70, "family": 6, "stepping": 1, 31
  11. ϦιʔεϓϥάΠϯ • ࣗ༝ʹϦιʔεΛఆٛͯ͠GemͰϓϥάΠϯ͔Ͱ ͖Δ # Gemfile gem 'itamae-plugin-resource-mail_alias' # your_recipe.rb

    mail_alias "alias_name" do recipient "recipient_name" end $ bundle exec itamae your_recipe.rb 35
  12. Itamae&in&Produc/on webϩʔϧΛ༻ҙͯ͠ΈΔ # recipes/nginx.rb package "nginx" # recipes/sl.rb package "dstat"

    # roles/web.rb include_recipe "../recipes/nginx.rb" include_recipe "../recipes/dstat.rb" 40
  13. Itamae&in&Produc/on # config/deploy/itamae.rb task :apply do # 1. ͍͍ײ͡ʹ஋Λऔ͖ͬͯͯnode.jsonΛஔ͘ #

    2. Ϩγϐͱ͔/tmp/itamaeʹrsync͓ͯ͘͠ # 3. ϩʔϧΛEC2ͷλάͳͲ͔Βऔಘ͢Δ role_recipe = "/tmp/itamae/roles/#{role}.rb" sudo "itamae", "local", "-j", "node.json", role_recipe end $ cap itamae apply HOSTS=your-web-server 41