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

MItamae Hacking Guide

MItamae Hacking Guide

Roppongi.rb #2 Infrastructure x Ruby
http://roppongirb.connpass.com/event/42633/

08d5432a5bc31e6d9edec87b94cb1db1?s=128

Takashi Kokubun

November 01, 2016
Tweet

Transcript

  1. MItamae Hacking Guide Roppongi.rb #2 @k0kubun

  2. ࣗݾ঺հ

  3. ࠓ೔࿩͢͜ͱ • 1. Itamaeͱ͸ • 2. Itamae Goͱ͸ • 3.

    MItamaeͷ͝঺հ
  4. 1. Itamaeͱ͸

  5. Itamaeͱ͸ • https://github.com/itamae-kitchen/itamae • ChefϥΠΫͳߏ੒؅ཧπʔϧ • αʔόʔͷߏ੒ΛRuby DSLͰهड़͢Δ

  6. Itamae DSL package 'nginx' do action :install end service 'nginx'

    do action [:enable, :start] end
  7. ItamaeΛ࢖͏ͱԿ͕خ͍͠ͷ͔ • Puppet, Ansibleͱҧ͍ઃఆϑΝΠϧ͕Ruby • มͳDSLͱ͔YAMLΑΓRubyΛಡΉɾॻ͘ํָ͕͍͠ • Chefʹൺ΂ͯγϯϓϧͳπʔϧͳͷͰอक͕͠΍͍͢ • Chefʹൺ΂ίʔυϕʔε͕খ͘͞ɺ͙͢ʹ࢓૊ΈΛ೺Ѳ

    Ͱ͖Δ • Berkshelfͷ͔ΘΓʹrubygemsΛ࢖͏
  8. ΫοΫύουʹ͓͚ΔItamae • αʔόʔͷߏ੒؅ཧʹ࢖ΘΕ͍ͯΔ • ݹ͍΋ͷ͸Puppet͕ͩItamaeʹҠߦத • ৽͍͠αʔόʔ͸جຊతʹItamaeͰ؅ཧ͞Ε͍ͯΔ • ͨͩ͠AppαʔόʔͳͲঢ়ଶΛ࣋ͨͳ͍΋ͷ͸Docker •

    ։ൃऀͷϚγϯͷηοτΞοϓʹ࢖ΘΕΔʮ༧ఆʯ • RubyͰॻ͔Εͨ಺੡πʔϧ͔ΒItamaeϕʔεͷ΋ͷʹҠߦத
  9. Itamaeͷӡ༻Ͱൃੜͨ͠՝୊ • Itamaeͷ࣮ߦͷͨΊʹrubyΛηοτΞοϓ͠ͳ͚Ε͹ͳͳ͍ • itamae ssh(sshܦ༝ͷద༻)͕஗͍ͷͰαʔόʔଆʹItamaeΛೖΕ࣮ͯߦ͢ Δ • ։ൃ؀ڥͷηοτΞοϓʹ͓͍ͯɺLinuxͩͱruby͕ೖͬͯͳ͍͜ͱ͕͋Δ •

    ItamaeͷͨΊ͚ͩʹrubyͷ؀ڥҰࣜΛύοέʔδϯά͢Δͷ͸໘౗ͩ͠ɺ͔ ͱ͍ͬͯී௨ʹ࢖͏rubyͷ؀ڥ΋Ԛͨ͘͠ͳ͍ • ύοέʔδ͸طʹ͋Δ΋ͷͷɺ࠷ॳ͔ΒγϯάϧόΠφϦͰ࡞ΒΕͨ Itamae͕͋Δͱखܰͦ͏
  10. 2. Itamae Goͱ͸

  11. Itamae Go • https://github.com/k0kubun/itamae-go • GolangͰγϯάϧόΠφϦItamaeΛ࡞ͬͨ

  12. ࡞ͬͨಈػ

  13. Itamae Goͷ࢓૊Έ • mrubyͷVMΛ mitchellh/go-mruby Ͱ૊ΈࠐΜͰ͍Δ • mrubyͷΦϒδΣΫτΛҾ਺ʹGoͷstructΛ࡞ΔGoͷؔ ਺Λmrubyͷϝιουͱͯ͠ఆٛͨ͠ΫϥεΛ࡞Γɺͦ ͷΫϥε্ͰItamaeϨγϐΛinstance_eval

    • ͜Ε͕ItamaeͷDSLʹͳΔ • ࡞ΒΕͨGoͷstructΛݟͯGoͷؔ਺ͰΦϖϨʔγϣϯ
  14. Itamae Goͷϝϯςਏ͍ • GoͷίʔυͰRubyͷϝιουΛఆ͍ٛͯ͘͠ ͷ͕͔ͳΓ໘౗ • SpecinfraΛGoͰ࠶࣮૷͢Δͷେม • libmruby.aΛϦϯΫ͠ͳ͕ΒCGOόΠϯσΟϯ άͷΫϩείϯύΠϧ͢Δͷଟ෼ී௨ʹͰ͖Δ

    ͚Ͳ໘౗
  15. go-mrubyͷ࣮༻ྫͱͯ͠͸໘ന͍͕… • ͦ΋ͦ΋GoΛ࢖͏ඞཁ͋Δͷ͔ʁ • ผʹGo͡Όͳ͍ͱ࢖͑ͳ͍ϥΠϒϥϦͱ͔ݴޠػೳΛ࢖ͬͯ ͳ͍ • mruby-cliͱ͍͏ɺRubyΛධՁͰ͖ΔγϯάϧόΠφϦΛ࡞Δผ ͷιϦϡʔγϣϯ͕͋ͬͨ •

    ͍ͯ͏͔ͿͬͪΌ͚ڈ೥ͷRubyConfͰ஌͚ͬͯͨͲPokemon Goʹ͔͚ͨํ͕ྲྀߦΔͱࢥ͍ͬͯͭ΍ͬͯ͠·ͬͨ
  16. 3. MItamaeͷ͝঺հ

  17. MItamae • https://github.com/k0kubun/mitamae • mruby-cliΛ࢖ͬͨItamaeͷpure mruby࣮૷ • ʮݟڅ͑ʯʁ • ΤϜ(mruby)ɾΠλϚΤͰ͢

  18. mruby-cliͱ͸ • https://github.com/hone/mruby-cli • mrubyͰγϯάϧόΠφϦΛ࡞ΔͨΊͷϘΠϥʔϓϨʔτ • ίϚϯυϥΠϯҾ਺ΛmrubyͷArrayʹม׵͠ɺmrubyͰఆٛ͞Εͨ __main__ Λݺͼग़͢Cݴޠͷmainؔ਺ •

    ͜ͷ __main__ ͷ࣮૷͚ͩ͢Ε͹ྑ͍ͷͰɺRubyҎ֎Λॻ͔ͣʹࡁΉ • macOS, Windows޲͚ʹΫϩείϯύΠϧ͢ΔͨΊͷ؀ڥΛ࣋ͭ Dockerfile • ࣮ମ: https://github.com/hone/mruby-cli-docker
  19. MItamae: ΅͕͔͘Μ͕͍͖͑ͨ͞ΐ͏ͷItamae • ruby΍gemʹґଘ͠ͳ͍ͨΊɺόΠφϦΛμ΢ϯϩʔυ͢Δ͚ͩͰଈ࣮ߦՄೳ • CRubyΛ࢖͏৔߹ͱҧͬͯίϯύΠϧࡁͳͷͰɺىಈ͕ߴ଎ • ιʔείʔυΛύʔεͯ͠YARV iseqʹίϯύΠϧ͢Δ·Ͱͷίετ͕ͳ͍ •

    2.5GHz Intel Core i7Ͱɺitamae: 499ms, mitamae: 23ms (21.7ഒ଎) • itamae sshΛαϙʔτ͍ͯ͠ͳ͍ͷͰɺSpecinfra(ඞͣԿΒ͔ͷίϚϯυΛ࢖͏) ͷ͔ΘΓʹී௨ʹCͷؔ਺Λ࢖͏͜ͱͰߴ଎Խ͕Մೳ • Ͳ͏ͤitamae ssh͸஗͍ͷͰɺαʔόʔ΁ͷItamaeͷॳճΠϯετʔϧͱ͔ʹ ͔͠࢖Θͳ͍͠ɺ΍Δʹͯ͠΋ผͷπʔϧʹͯ͠Αͦ͞͏
  20. MItamaeͷεςʔλε • େମͷػೳ͸͋Δ • https://github.com/k0kubun/mitamae#supported-features • fileͷedit, host inventory, verify,

    subscribes(notifies͸͋Δ)͕ͳ͍ • remote_directory, http_request, local_ruby_block ͕ͳ͍ • Arch Linux, CentOS, Debian, Gentoo, macOS, UbuntuΛαϙʔτ • ItamaeຊՈͷ݁߹ςετ͕େମ௨Δ • Travis্ͰDockerίϯςφʹapplyͯ͠ServerspecͰςετͯ͠Δ
  21. Itamae 2ʁ • ҿΈձͰryotarai͞Μ(Itamae࡞ऀ)͕itamae- mruby(MItamaeͷ౰࣌ͷ໊લ)ΛItamae 2ʹ͢Δͬͯ ݴͬͯͨ • ͱΓ͋͑ͣػೳ͕ἧͬͯར༻࣮੷΋૿͑Δඞཁ͕͋Γ ͦ͏

    • ͳͷͰɺ͓͢͢Ίͷ༻్ͱɺMItamaeͷ಺෦ઃܭ΍ ։ൃํ๏ʹ͍ͭͯઆ໌͠·͢
  22. MItamaeͷ༻్

  23. ๻ͷMItamaeͷ༻్ • dotfilesͷద༻ • https://github.com/k0kubun/dotfiles • ϨγϐΛࢀߟʹ͍ͨ͠ํ͸ https://github.com/k0kubun/ legacy-dotfiles ΋Ͳ͏ͧ

    • ࣗ୐Ͱ΋ձࣾͰ΋σεΫτοϓϚγϯʹLinuxΛ࢖͍ͬͯΔ͕ɺ ࠷ॳͷηοτΞοϓલʹrubyΛ͍ΕΔඞཁ͕ͳͯ͘ඍົʹॿ͔Δ • ىಈ଎͍͠ɺgem install / bundle install΋ෆཁͩ͠Կ͔ͱָ
  24. ΫοΫύουͰͷར༻ঢ়گ • ։ൃ؀ڥͷηοτΞοϓπʔϧΛItamaeϕʔεͷ΋ͷʹҠߦதͰɺͦ͜ʹ MItamae͕࠾༻͞Ε͍ͯΔ • αʔόʔͷߏ੒؅ཧ޲͚ʹ͸·ͩ࢖ΘΕͯͳ͍ • ࣾ಺޲͚ͷपลπʔϧ͕Itamaeͱ݁߹͍ͯͯ͠Ҡߦ͕एׯେม • ͦ΋ͦ΋Itamaeͩͱ໘౗ͳͷ͸ύοέʔδϯά͕ͩɺࣾ಺Ͱ࢖͍ͬͯΔ

    αʔόʔ޲͚ʹ͸طʹύοέʔδ͕͋ΔͷͰͦΕ΄ͲϞνϕʔγϣϯ͕ͳ ͍ • Ͱ΋γϯάϧόΠφϦͰΫϦʔϯ͔ͭ଎͍ͷͰ͜Ε͔Βಋೖ͢Δํʹ͸͓ ͢͢Ί
  25. Inside MItamae

  26. MItamaeͷ಺෦࣮૷ • جຊతʹ͸mruby-cli࢖ͬͯΔ͚ͩ • Itamae෦෼͸εΫϥον • Specinfra෦෼͸΄΅ͦͷ··͖࣋ͬͯͨ • ΋ͪΖΜAuthorsͱLICENSEදهʹmizzy͞Μ ͕͍·͢

  27. MItamaeͱItamaeͷ࣮૷ͷҧ͍ • resourceͱresourceΛద༻͢Δ੹຿͕෼཭͞Ε͍ͯͯɺresource ͸ͨͩͷϞσϧʹ͗͢ͳ͍ • ryotarai͞ΜͷΞΠσΞ • ๻ͷझຯ͕ೖ͍ͬͯΔ • ͳΔ΂͘method_missingΛආ͚ϕλॻ͖͢Δ(΋͏গ͠ݮΒͤ

    Δ͚Ͳ)ɺͳͲ • defineͨ͠resourceʹωετͨ͠resource಺Ͱparams͕࢖͑Δ
  28. defineͨ͠resourceʹωετͨ͠ resource಺Ͱparams͕࢖͑Δ ???? # Itamae ͷ৔߹ define :ruby do version

    = params[:name] execute "..." do not_if "... #{version}" end end ruby '2.3.1'
  29. defineͨ͠resourceʹωετͨ͠ resource಺Ͱparams͕࢖͑Δ ???? # MItamaeͷ৔߹ (Itamaeͩͱparams͕ࢀরෆೳ) define :ruby do execute

    "..." do not_if "... #{params[:name]}" end end ruby '2.3.1'
  30. Hacking MItamae

  31. cloneͯ͠ίϯύΠϧ࣮ͯ͠ߦ $ git clone https://github.com/k0kubun/mitamae $ cd mitamae $ rake

    compile $ mruby/build/host/bin/mitamae local recipe.rb
  32. ςετͷ࣮ߦ # Ϣχοτςετͷ࣮ߦ $ rake test:mtest # DockerͱServerspecΛ࢖ͬͨςετ $ rake

    test:integration
  33. ΫϩείϯύΠϧ # mitamae-buildσΟϨΫτϦʹ࡞ΒΕΔ $ rake release:build

  34. ·ͱΊ • MItamae͸଎ͯ͘Πϯετʔϧ͕؆୯ͳͷͰ ࢖ͬͯཉ͍͠ • ։ൃ؀ڥ͸੔ͬͯΔͷͰMItamaeͷ։ൃʹࢀ Ճ͠Α͏!