Tbpgr Blog

Ruby プログラマ tbpgr(てぃーびー) のブログ

Capistrano | インストール + 疎通確認 ※Capistrano3.0.0 (検証用のVagrantfile付き)

概要

Capistranoのインストール + 疎通確認 ※Capistrano3.0.0

詳細

Capistranoのインストール + 疎通確認をインストールします。

配布物

sublime_snippetter(自作のSublime Text2向けスニペット生成ツールです。GitHubにあります)
https://github.com/tbpgr/sublime_snippetter_old

capistrano実行環境/配布先環境共通 前提

Ubuntu1204
Rbenv
Bundler
Ruby2.0.0.p0

capistrano実行環境 前提

192.168.33.12
capistrano 3.0.0

capistrano配布先環境 前提

192.168.33.13

事前準備

配布元、配布先の仮想環境構築
下記は配布元用。配布先用はipが192.168.33.13になること、仮想環境名が "vbcapistranoto"になるだけで他の中身は同じ。

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

$script = <<SCRIPT
echo 'start shell'
su - vagrant -c '
  PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

  echo "start install basic"
  sudo apt-get update
  sudo apt-get install build-essential bison git-core libreadline6-dev curl zlib1g-dev libssl-dev libyaml-dev libxml2-dev libxslt1-dev autoconf libncurses5-dev -y
  echo "end   install basic"

  echo "start install rbenv"
  git clone https://github.com/sstephenson/rbenv.git /home/vagrant/.rbenv

  echo "export PATH=/home/vagrant/.rbenv/bin:$PATH" >> /home/vagrant/.bash_profile
'
echo \'eval "$(rbenv init -)"\' >> /home/vagrant/.bash_profile
source /home/vagrant/.bash_profile
echo "end   install rbenv"

su - vagrant -c '
  echo "start install ruby 2.0.0-p0"
  git clone https://github.com/sstephenson/ruby-build.git /home/vagrant/ruby-build
  cd /home/vagrant/ruby-build
  sudo ./install.sh
  cd /home/vagrant
  rbenv install 2.0.0-p0
  rbenv global 2.0.0-p0
  rbenv rehash
  echo "end   install ruby 2.0.0-p0"

  echo "start install bundler"
  rbenv exec gem i bundler
  echo "end   install bundler"
'
SCRIPT
back = <<EOS

EOS

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.host_name = "vbcapistranofrom"
  config.vm.box = "ubuntu-12.04-x64"
  config.vm.network :private_network, ip: "192.168.33.12"

  config.ssh.username = "vagrant"
  config.vm.provision "shell", inline: $script
end

環境構築(配布元、配布先双方に対して行う)

vagrant up
vagrant provision
手順
vagrant@vbcapistranofrom:~$git clone https://github.com/tbpgr/sublime_snippetter_old.git
vagrant@vbcapistranofrom:~$ cd sublime_snippetter
vagrant@vbcapistranofrom:~/sublime_snippetter$ echo 'gem "capistrano", "~> 3.0.0"' >> Gemfile
vagrant@vbcapistranofrom:~/sublime_snippetter$ bundle

# capistrano構成の初期化
vagrant@vbcapistranofrom:~/sublime_snippetter$ bundle exec cap install
mkdir -p config/deploy
create config/deploy.rb
create config/deploy/staging.rb
create config/deploy/production.rb
mkdir -p lib/capistrano/tasks
Capified

# 上記補足 deploy.rbはstaging、production問わず利用する共通設定の定義
# 上記補足 staging.rbはstaging環境のデプロイのみで利用する設定の定義
# 上記補足 production.rbはproduction環境のデプロイのみで利用する設定の定義

# タスクリストの確認
vagrant@vbcapistranofrom:~/sublime_snippetter$ bundle exec cap -vT
cap deploy                         # Deploy a new release
cap deploy:check                   # Check required files and directories e...
cap deploy:check:directories       # Check shared and release directories e...
cap deploy:check:linked_dirs       # Check directories to be linked exist i...
cap deploy:check:linked_files      # Check files to be linked exist in shared
cap deploy:check:make_linked_dirs  # Check directories of files to be linke...
cap deploy:cleanup                 # Clean up old releases
cap deploy:cleanup_rollback        # Remove and archive rolled-back release
cap deploy:finished                # Finished
cap deploy:finishing               # Finish the deployment, clean up server(s)
cap deploy:finishing_rollback      # Finish the rollback, clean up server(s)
cap deploy:log_revision            # Log details of the deploy
cap deploy:published               # Published
cap deploy:publishing              # Publish the release
cap deploy:revert_release          # Revert to previous release timestamp
cap deploy:reverted                # Reverted
cap deploy:reverting               # Revert server(s) to previous release
cap deploy:rollback                # Rollback to previous release
cap deploy:started                 # Started
cap deploy:starting                # Start a deployment, make sure server(s...
cap deploy:symlink:linked_dirs     # Symlink linked directories
cap deploy:symlink:linked_files    # Symlink linked files
cap deploy:symlink:release         # Symlink release to current
cap deploy:symlink:shared          # Symlink files and directories from sha...
cap deploy:updated                 # Updated
cap deploy:updating                # Update server(s) by setting up a new r...
cap install                        # Install Capistrano, cap install STAGES...

# 共通のデプロイ設定の編集
vagrant@vbcapistranofrom:~/sublime_snippetter$ cd config
vagrant@vbcapistranofrom:~/sublime_snippetter/config$ vi deploy.rb # 編集内容は後述

# staging環境へのデプロイ設定の編集
vagrant@vbcapistranofrom:~/sublime_snippetter$ cd deploy
vagrant@vbcapistranofrom:~/sublime_snippetter/config/deploy$ vi staging.rb # 編集内容は後述

・配布元、配布先の公開鍵の設定を行う
※設定方法については下記参照
vagrant検証用に仮想環境を2環境作成してSSH接続確認を行う
http://d.hatena.ne.jp/tbpg/20131025/1382717749

・デプロイの実行

vagrant@vbcapistranofrom:~/sublime_snippetter$ bundle exec cap staging deploy
# デプロイ完了

・デプロイ先で結果を確認

# デプロイ先にフォルダがCloneされていることを確認
vagrant@vbcapistranoto:~$ ls
postinstall.sh  ruby-build  sublime_sunippetter # sublime_sunippetterが作成されたことを確認

# カレントに移動
vagrant@vbcapistranoto:~/sublime_sunippetter$ cd current

# sublime_snippetterを実行。sublime_snippetterについては「https://github.com/tbpgr/sublime_snippetter_old」参照
vagrant@vbcapistranoto:~/sublime_sunippetter/current/lib$ ruby se.rb
create for hoge method snippet => /home/vagrant/sublime_sunippetter/releases/20131029091115/lib/snippets/hoge.sublime-snippet
create for hige method snippet => /home/vagrant/sublime_sunippetter/releases/20131029091115/lib/snippets/hige.sublime-snippet

# 作成されたsnippetを確認
vagrant@vbcapistranoto:~/sublime_sunippetter/current/lib$ cd snippets/
vagrant@vbcapistranoto:~/sublime_sunippetter/current/lib/snippets$ ls
hige.sublime-snippet  hoge.sublime-snippet
vagrant@vbcapistranoto:~/sublime_sunippetter/current/lib/snippets$ cat hoge.sublime-snippet
<snippet>
  <content><![CDATA[
hoge ${1:param1}, ${2:param2}
]]></content>
  <tabTrigger>hoge</tabTrigger>
  <scope>source.ruby</scope>
  <description>hoge method</description>
</snippet>
deploy.rb
set :application, 'sublime_snippetter'
set :repo_url, 'https://github.com/tbpgr/sublime_snippetter_old.git'
set :deploy_to, '/home/vbcapistranoto/sublime_snippet'

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      # execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
    end
  end

  after :finishing, 'deploy:cleanup'

end
staging.rb
set :stage, :staging

role :app, %w{vagrant@192.168.33.13}
role :web, %w{vagrant@192.168.33.13}
role :db,  %w{vagrant@192.168.33.13}
server '192.168.33.13', user: 'vagrant', roles: %w{web app}, my_property: :my_value