IT井戸端会議

IT井戸端会議

インフラ、ネットワーク、アプリケーション開発、IT界隈の話等々を東京都千代田区界隈から発信します。

capistranoをほんのちょっと動かしてみる①(mkdir実行)

capistrano2.x とcapistrano3.xではかなりコマンドや動きが違うように見えるのでちょっと動かしてみる。

やることはSSH接続で対象サーバにパスワードにてログインし、ディレクトリを作成するだけ。

ワークディレクトリを作成

mkdir cap-pro

雛形を作成

$ cd cap-pro/
$ 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
create Capfile
Capified

ファイルを修正

./Capfileは修正しない。

# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'

# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
# https://github.com/capistrano/passenger
#
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
# require 'capistrano/bundler'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
# require 'capistrano/passenger'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

./libディレクトリ配下は修正しない。

./config/deploy.rbの中身を下記以外削除する。 ※不要なのでバージョン情報のみ残す。

lock '3.4.0'

./config/deploy/manage.rbに実際にやりたいタスクを記載する。

# userがroot,パスワードが****にてログインさせる
SSHKit::Backend::Netssh.configure do |ssh|
    ssh.ssh_options = {
    :user => 'root',
    :password => '****',
}
end

# chefというホスト名のサーバのロール名をclientとする
server 'chef', :roles => [ :client ]

# タスク名をmkdirにする
task :mkdir do

    # dir変数を設定する
    dir = '/root/test'

    # ロール名がclientに対し,do~endまでに記載したコマンドを実行する
    on roles( :client ) do

        # mkdirとlsコマンドを実行する
        execute "mkdir -p #{dir}"
        execute "ls -ld #{dir}"
    end
end

Capfileがあるディレクトリでcapコマンドを実行する。

$ cap manage mkdir
INFO [f7c8ab7c] Running /usr/bin/env mkdir -p /root/test on chef
DEBUG [f7c8ab7c] Command: mkdir -p /root/test
INFO [f7c8ab7c] Finished in 0.326 seconds with exit status 0 (successful).
INFO [54178f59] Running /usr/bin/env ls -ld /root/test on chef
DEBUG [54178f59] Command: ls -ld /root/test
DEBUG [54178f59]        drwxr-xr-x. 2 root root 4096 Jul  7 01:36 /root/test
INFO [54178f59] Finished in 0.031 seconds with exit status 0 (successful).

cap  : capistranoのコマンド manage: ./config/deploy/manage.rbを指定 mkdir : ./config/deploy/manage.rbにあるmkdirというタスクを指定