ここまで4回,
Capistranoを使ってItamaeを複数ホストに対して実行する
CapistranoはRubyで書かれたデプロイツールで,
まず,cap install
で初期ファイルを生成します。
$ cd itamae-repo $ bundle init # Gemfileがない場合 $ echo 'gem "capistrano"' >> Gemfile $ bundle install $ bundle exec cap install STAGES=production
config/
を以下のように修正します。itamae:apply
タスクでitamae local
が実行されるようにしています。また,itamae:apply
の前にdeploy
を実行するように指定しているので,/tmp/
にコードがデプロイされます。
set :application, ‘itamae’
set :repo_url, 'git@github.com:your/itamae-repo.git'
set :deploy_to, '/tmp/itamae-repo'
# この部分はAWS EC2のAPIを呼ぶなどして,動的に定義すると良いかもしれません
role :app, [‘app-1’]
namespace :itamae do
# apply前にdeployを実行します
task :apply => [:deploy] do
# bootstrap.rbを実行します
recipe = File.join(fetch(:release_path), 'bootstrap.rb')
on roles(ENV['ROLES'].split(',')) do
# 対象ホスト側にItamaeがインストールされている必要があります
execute "itamae", "local", recipe
end
end
end
itamae:apply
タスクでItamaeを実行できます。
$ bundle exec cap production itamae:apply ROLES=app (中略) DEBUG [ed621171] Command: /usr/bin/env itamae local /tmp/itamae-repo/releases/20150927150652/bootstrap.rb DEBUG [ed621171] [0m INFO : Starting Itamae... DEBUG [ed621171] [0m DEBUG [ed621171] [0m INFO : Recipe: /tmp/itamae-repo/releases/20150927150652/bootstrap.rb DEBUG [ed621171] [0m[32m INFO : execute[echo Hello] executed will change from 'false' to 'true' DEBUG [ed621171] [0m INFO [ed621171] Finished in 0.317 seconds with exit status 0 (successful).
このように非常に簡単に複数台へのItamaeの実行が実現できます。上記の例ではitamae ssh
を使わずitamae local
を利用しています。itamae ssh
はitamae local
に比べ速度が遅いため,itamae local
は対象ホストにitamae
がインストールされている必要があるので,
もちろん,itamae ssh
を使うことも可能です。その場合はrun_
内でItamaeを実行しましょう。詳しくはCapistranoのドキュメントを参照してください。
PackerとItamaeを使ってAWS EC2用のイメージを作成する
PackerとItamaeを使ってAWS EC2のイメージ
まず,
$ wget https://dl.bintray.com/mitchellh/packer/packer_0.8.6_darwin_amd64.zip $ unzip packer_0.8.6_darwin_amd64.zip
Packerの設定ファイルを書きます。以下をubuntu.
として保存しますvpc_
,subnet_
,/path/
は環境に合わせて書き換えます)。
{
"builders": [{
"type": "amazon-ebs",
"region": "ap-northeast-1",
"source_ami": "ami-46990446",
"instance_type": "t2.micro",
"ssh_username": "ubuntu",
"ssh_timeout": "5m",
"ami_name": "ubuntu-trusty-hvm",
"vpc_id": "vpc-abcdef",
"availability_zone": "ap-northeast-1c",
"subnet_id": "subnet-abcdef",
"associate_public_ip_address": true,
"run_tags": {
"Name": "packer-tmp-ubuntu-trusty-hvm"
}
}],
"provisioners": [
{
"type": "file",
"source": "/path/to/itamae-repo",
"destination": "/tmp/itamae-repo"
},
{
"type": "shell",
"inline": [
"curl -s https://packagecloud.io/install/repositories/ryotarai/itamae/script.deb.sh | sudo bash",
"sudo apt-get install itamae",
"sudo /opt/itamae/embedded/bin/itamae local /tmp/itamae-repo/bootstrap.rb",
"sudo rm -rf /tmp/itamae-repo"
]
}
]
}
Packerの設定ファイルにはbuilders
とprovisioner
を指定する必要があり,amazon-ebs
file
Provisionerを使って,/path/
を転送しています),itamae
コマンドを実行しています。
あとはpacker build
コマンドに上記の設定ファイルを与えれば,
$ AWS_ACCESS_KEY_ID=foo AWS_SECRET_ACCESS_KEY=bar ./packer build ubuntu.json amazon-ebs output will be in this color. ==> amazon-ebs: Prevalidating AMI Name... ==> amazon-ebs: Inspecting the source AMI... (中略) ==> amazon-ebs: Provisioning with shell script: /var/folders/tj/0g3ny7c94mq9s_2dtbzk0hbr0000gn/T/packer-shell612616353 (中略) amazon-ebs: INFO : Starting Itamae... amazon-ebs: INFO : Recipe: /tmp/itamae-repo/bootstrap.rb amazon-ebs: INFO : execute[echo Hello] executed will change from 'false' to 'true' amazon-ebs: ==> amazon-ebs: Stopping the source instance... ==> amazon-ebs: Waiting for the instance to stop... ==> amazon-ebs: Creating the AMI: ubuntu-trusty-hvm amazon-ebs: AMI: ami-abcdef ==> amazon-ebs: Waiting for AMI to become ready... ==> amazon-ebs: Terminating the source AWS instance... ==> amazon-ebs: Cleaning up any extra volumes... ==> amazon-ebs: Deleting temporary security group... ==> amazon-ebs: Deleting temporary keypair... Build 'amazon-ebs' finished. ==> Builds finished. The artifacts of successful builds are: --> amazon-ebs: AMIs were created: ap-northeast-1: ami-abcdef