LoginSignup
10
6

More than 5 years have passed since last update.

capistrano-itamaeというgemを作った

Last updated at Posted at 2016-07-19
1 / 15

このgemについて

capistrano から itamae を実行するためのgemです

rubygems


モチベーション

  • capistranoとitamaeの2ヶ所でホスト情報を管理するのがつらくなったのでcapistranoからitamaeを呼べるようにした
    • capistranoで持ってるホスト情報をもとに itamae ssh
  • 複数サーバにsshしてコマンド発行する時はcapistranoは便利

使い方

初期設定

それぞれ追加

Gemfile
group :development do
  gem "capistrano-itamae", require: false
end
Capfile
require "capistrano/itamae"

基本的な使い方

on roles の中で itamae_ssh メソッドを呼ぶだけ

desc "Run itamae"
task :itamae do
  on roles(:all) do
    itamae_ssh "recipe.rb"
  end
end

実際に実行されるコマンド

itamae ssh cookbooks/recipe.rb --host=HOST 〜

ssh関係のパラメータ 1 はcapistranoに設定済のものが自動で itamae sshに渡される


複数のレシピを実行したい時

配列で渡す

desc "Run itamae"
task :itamae do
  on roles(:all) do
    itamae_ssh ["recipe1.rb", "recipe2.rb"]
  end
end

実際に実行されるコマンド

itamae ssh cookbooks/recipe1.rb cookbooks/recipe2.rb --host=HOST 〜

オプションを渡したい時(その1)

:itamae_ssh_default_options にセットすると全体に適用される

set :itamae_ssh_default_options, "--node-yaml=node.yml"

desc "Run itamae"
task :itamae do
  on roles(:all) do
    itamae_ssh "recipe.rb"
  end
end

実際に実行されるコマンド

itamae ssh cookbooks/recipe.rb --node-yaml=node.yml --host=HOST 〜

オプションを渡したい時(その2)

itamae_ssh の第2引数に渡すとその場所だけオプションが有効になる

set :itamae_ssh_default_options, "--node-yaml=node.yml"

desc "Run itamae"
task :itamae do
  on roles(:all) do
    itamae_ssh "recipe.rb", "--dry-run"
  end
end

実際に実行されるコマンド

itamae ssh cookbooks/recipe.rb --node-yaml=node.yml --dry-run --host=HOST 〜

itamae-templateとの差異

  • itamae-templateitamae local 2 前提なのに対し、capistrano-itamaeは(今のところ) itamae ssh 3 前提なこと
    • itamae local だと itamae ssh に比べて速いんだけど、レシピ実行のための環境をサーバにセットアップする手間があって手間
    • アプリ実行に不要なものは極力サーバに入れたくない派
  • itamae-templateだと雑にgemを入れて雑にレシピ実行するにはちょっと向いてない
  • メリデメはあるので好きな方を使えばいいと思います

まとめ

  • capistranoとitamaeを一緒に使いたい時にご利用ください
    • 詳しいことはREADME参照

おまけ:capistranoプラグインのインテグレーションテスト

今回の場合

  • capistranoを適用したダミーアプリを用意して
  • Vagrantで立てたVMに cap deploy
    • cap taskの中で itamae_ssh
  • Serverspec実行
  • Wercker + DigitalOcean でCI実行

ディレクトリ構成

spec/integration/
├── Capfile
├── Rakefile
├── Vagrantfile
├── config
│   ├── deploy
│   │   └── development.rb
│   └── deploy.rb
├── cookbooks
│   ├── memcached.rb
│   ├── node.yml
│   └── tmux.rb
└── spec
    ├── memcached_spec.rb
    ├── spec_helper.rb
    └── tmux_spec.rb

インテグレーションテスト作った感想

リファクタリングする時にテストがある安心感ぱない

安心感 :innocent:


参考URL


  1. --host, --user, --port, --key など 

  2. サーバに入ってitamaeを実行 

  3. sshごしにitamaeを実行 

10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6