カピストラーノ(Capistrano)を使ってみよう!(Basic編)

環境

とりあえず、実行する環境を提示します。

  • クライアント側

OS : Windows XP
Ruby : 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

  • サーバー側

OS : Linux
 $ cat /etc/redhat-release
 Red Hat Enterprise Linux ES release 4 (Nahant Update 4)
SSH : OpenSSH
 $ ssh -v
 OpenSSH_4.5p1, OpenSSL 0.9.7m 23 Feb 2007

カピストラーノ(Capistrano)って何?

Railsのデプロイに特化しているので、デプロイツールと思われますが、SSHでアクセスでアクセスして、シェルでうにょうにょやれるので、デプロイに限らず色々できます。
クライアント側からシェルが実行できますので、Linux上で書くシェルファイルを、クライアント側(たとえばwindowsとか)でかけて、それをRubyを使って実行できると思ってもよいかもしれません。


EURO 2016 News
↑やっぱ、本家を見るのが一番!ということで、探検です。

いきなり、

Simple.The way it should be.

と書いてあります。インストールや運用も簡単であってほしいと願いつつ続きを読みます。

・Great for automating tasks via SSH on remote servers, like software installation, application deployment, configuration management, ad hoc server monitoring, and more.

どうやら、クライアントからサーバーにSSHでアクセスして、うにょうにょやるようです。

・Ideal for system administrators, whether professional or incidental.

むむん。理想・・・。

・Easy to customize. Its configuration files use the Ruby programming language syntax, but you don't need to know Ruby to do most things with Capistrano.
・Easy to extend. Capistrano is written in the Ruby programming language, and may be extended easily by writing additional Ruby modules.

Easyと言っていますが・・・Easyであることを願います。


install

gem install -y capistrano

-y オプション

無条件に必要な依存するRubyをインストールしてください。

net-ssh や net-sftp なども使うようなので、net-ssh や net-sftpのバージョンが変わったり、インストールされるとアプリが動かなくなる>< みたいな環境の人は -y オプションは付けないで、すべて手動でインストールするとよいでしょう。

  • インストール
C:\>gem install -y capistrano
Need to update 8 gems from http://gems.rubyforge.org
........
complete
Successfully installed capistrano-2.4.3
Successfully installed net-ssh-2.0.3
Successfully installed net-sftp-2.0.1
Successfully installed net-scp-1.0.1
Successfully installed net-ssh-gateway-1.0.0
Successfully installed highline-1.4.0
Installing ri documentation for capistrano-2.4.3...
Installing ri documentation for net-ssh-2.0.3...
Installing ri documentation for net-sftp-2.0.1...
Installing ri documentation for net-scp-1.0.1...
Installing ri documentation for net-ssh-gateway-1.0.0...
Installing ri documentation for highline-1.4.0...
Installing RDoc documentation for capistrano-2.4.3...
Installing RDoc documentation for net-ssh-2.0.3...
Installing RDoc documentation for net-sftp-2.0.1...
Installing RDoc documentation for net-scp-1.0.1...
Installing RDoc documentation for net-ssh-gateway-1.0.0...
Installing RDoc documentation for highline-1.4.0...

インストール完了!

  • 確認
C:\>gem list

*** LOCAL GEMS ***
 ・
 ・
 ・
capistrano (2.4.3)
   Capistrano is a utility and framework for executing commands in
   parallel on multiple remote machines, via SSH.
 ・
 ・
 ・

Getting Started

Capistrano was originally written to ease the pain of deploying Rails applications. Although it can be (and is) used for a lot more than that now, Rails application deployment is still the primary way that most people use Capistrano, at least initially.
As a result, this “Getting Started” guide will actually be multiple guides in one. If you feel there is an essential guide missing from this section, please email jamis at 37signals dot com and let him know what you think needs to be added.

Railsのデプロイの辛さを緩和するために、つくられたものらしいが、他にもいろいろ使えるよ!って書いてあると思う。


Assumptions

Capistrano makes a few assumptions about your servers. In order to use Capistrano, you will need to comply with these assumptions:

カピストラーノを使うにはいくつかの前提がひつようだよ!って書いてあると思う。

You are using SSH to access your remote machines. Telnet and FTP are not supported.

SSHでアクセスできないとダメ!ってことですね。 TelnetFTPはサポートしていないとさ。

Your remote servers have a POSIX-compatible shell installed. The shell must be called “sh” and must reside in the default system path.

シェルは sh として呼べて、デフォルトのシステムパスにいないといけませんか・・・。デフォルトでいじってないので大丈夫だと思う。

If you are using passwords to access your servers, they must all have the same password. Because this is not generally a good idea, the preferred way of accessing your servers is with a public key. Make sure you’ve got a good passphrase on your key.

パスワード認証であれば、サーバにアクセスする際、同じパスワードを使わなければならない。
とりあえず、パスワード認証が設定されているので、パスワード認証でいろいろ試してから、公開鍵認証に移行します。

You should be comfortable working from a command-line. You do not need to know advanced shell scripting techniques or anything like that (though it helps, if you want to start writing complex Capistrano recipes), but you should be able to navigate directories and execute commands from the command-line. Capistrano has no GUI interface; it is entirely command-line driven.

カピストラーノにはGUIで操作できるインターフェースがない、コマンドラインからディレクトリに行ってコマンドを事項出来るべきです。・・・てきな感じだと思う。

To take full advantage of Capistrano, you should be comfortable (or at least, minimally familiar) with the Ruby programming language. When you write your own Capistrano tasks, you do so in Ruby.

カピストラーノを最大限に利用するために、あなたはRubyプログラミング言語を使えるべきです。カピストラーノタスクを書く時に、あなたはRubyでそうします。


Capfiles

Capistranoはcapfileからを指示を読みます。
Capfilesはただ簡単なテキストファイルです、どんなテキストエディタでも使用できます。
ファイル名は何でもかまいません。

・・・的な感じだと思う。

A Simple example

task :search_libs, :hosts => "www.capify.org" do
  run "ls -x1 /usr/lib | grep -i xml"
end

これはなんでしょうか?

This defines a single task, called “search_libs”, and says that it should be executed only on the “www.capify.org” host. When executed, it will display all files and subdirectories in /usr/lib that include the text “xml” in their name. By default, “run” will display all output to the console.

ようするに、linuxssh でログインして、 ls -x1 /usr/lib | grep -i xml というコマンドを打って、その結果を出力するという感じのようです。

リモート操作する予定のサーバーにSSHでアクセスして、上記のコマンドを実行してみました。

  • Linux上で普通にコマンドを打ってみる
[hoge@fuga ~]$ ls -x1 /usr/lib | grep -i xml
libxml2.a
libxml2.so
libxml2.so.2
libxml2.so.2.6.27
xml2Conf.sh

こんなんでました、カピストラーノでこのような出力を取得できるのでしょうか?

Assuming your capfile is in the current directory, you would execute that task like this (from the command-line):

カレントディレクトリの中にあなたのcapfileがあると仮定する場合、あなたはこの(コマンドラインからの)ようにそのタスクを実行するでしょう。

cap search_libs

なるほど、では、capfile をメモ帳で作成し、そこのカレントディレクトリにコマンドプロンプトで移動して、 cap search_libs と打てばいいのか。やってみよう。

task :search_libs, :hosts => "www.xxxxxxx.com" do
  run "ls -x1 /usr/lib | grep -i xml"
end

※www.xxxxxxx.com は実際はちゃんとリモートするサーバーのドメインが入っています。

  • 実行してみた
C:\capistrano_example>cap search_libs
the task `search_libs' does not exist

(;゚ Д゚)
・・・・・・・・ファイル作って、書いたはいいけど、どうやって実行するんだ?(´・ω・`)

ということで、コマンドラインヘルプを見てみる。

C:\capistrano_example>cap -h
Usage: cap [options] action ...
   -d, --debug                      Prompts before each remote command execution.
   -e, --explain TASK               Displays help (if available) for the task.
   -F, --default-config             Always use default config, even with -f.
   -f, --file FILE                  A recipe file to load. May be given more than once.
   -H, --long-help                  Explain these options.
   -h, --help                       Display this help message.
   -p, --password                   Immediately prompt for the password.
   -q, --quiet                      Make the output as quiet as possible.
   -S, --set-before NAME=VALUE      Set a variable before the recipes are loaded.
   -s, --set NAME=VALUE             Set a variable after the recipes are loaded.
   -T, --tasks                      List all tasks in the loaded recipe files.
   -V, --version                    Display the Capistrano version, and exit.
   -v, --verbose                    Be more verbose. May be given more than once.
   -X, --skip-system-config         Don't load the system config file (capistrano.conf)
   -x, --skip-user-config           Don't load the user config file (.caprc)

なるほど、 -f でファイルを指定するのか。やってみよう。


C:\capistrano_example>cap -f capfile search_libs
 * executing `search_libs'
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
connection failed for: www.xxxxxxx.com (Net::SSH::AuthenticationFailed: 福重伸太朗)

(;゚ Д゚)
もしかして、windowsでログインしているユーザー名でアクセスしようとしていませんか・・・・・?


  • set :user でユーザーを指定する

set :user でユーザー名が指定できるらしい>< nag++

set :user, "your_user_name"
task :search_libs, :hosts => "www.xxxxxxx.com",:user =>"your_user_name" do
 run "ls -x1 /usr/lib | grep -i xml"
end
  • 再度実行
C:\capistrano_example>cap -f capfile search_libs
 * executing `search_libs'
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished

いけたー"o(>ω< )o"
ちゃんと、出力情報がとれたお><

You can define as many tasks as you want:

すきなだけ、task を追加できます。おおぉ。

task :search_libs, :hosts => "www.xxxxxxx.com" do
  run "ls -x1 /usr/lib | grep -i xml"
end

task :count_libs, :hosts => "www.xxxxxxx.com" do
  run "ls -x1 /usr/lib | wc -l"
end

例では、こんな感じで書いてますね。

Here we’ve added a second task, “count_libs”, which will display the number of entries in /usr/lib. We could add more, but even with just two tasks, having to specify the host over and over is getting a little unwieldy. Here is where “roles” come into play:

なるほど、task が増えると、何回も同じ hots を書かないといけなくて、面倒だといいたいのですね。

role :libs, "www.xxxxxxx.com"

task :search_libs do
  run "ls -x1 /usr/lib | grep -i xml"
end

task :count_libs do
  run "ls -x1 /usr/lib | wc -l"
end

だから、↑こう書けるよということですね。

(Note: Capistrano’s login defaults to whatever user you are currently logged into your local machine as. If you need to log in as a different user, you can encode that username in the server definition, “joe@www.capify.org”. Alternatively, you can set the :user variable to the username you want to use. We’ll get to variables in a minute.)

なるほど。ちゃんと、書いてあった>< もうちょっと前にかくべきなんだお><

task :search_libs, :hosts => "your_user_name@www.xxxxxxx.com" do
  run "ls -x1 /usr/lib | grep -i xml"
end

これでもいけるし、nagさんに教えてもらった、

set :user, "your_user_name"

task :search_libs, :hosts => "www.xxxxxxx.com" do
  run "ls -x1 /usr/lib | grep -i xml"
end

これでも、いけるってことですね。


Gateway servers

In the “real world”, we have to worry about bad guys trying to sneak into our servers, so many server clusters are hidden behind NATs and firewalls, to prevent direct access. Instead, you have to log into some “gateway” server, and then log into the servers you want, from there.

悪さする奴がいるから、ゲートウェイサーバーにログインして、そこからサーバにログインしなければなりませんと書いてあるが・・・・。
なるほど、特定のゲートウェイを通過して確立した接続のみ操作できるという設定・・・だと思うのですが、l
まずは、必要なさそうなので、とりあえずあることだけ覚えておこう。l


Multiple servers

role :libs, "crimson.capify.org", "magenta.capify.org"

これで、2つのサーバーに並行して命令を実行できるよ!って書いてあると思う。

role :libs, "www.xxxxxxx.com","www2.xxxxxxx.com"

task :search_libs do
  run "ls -x1 /usr/lib | grep -i xml"
end

task :count_libs do
  run "ls -x1 /usr/lib | wc -l"
end

たとえば、これで "www.xxxxxxx.com","www2.xxxxxxx.com" に並行して命令を飛ばせるわけですね。
ただし、"www.xxxxxxx.com","www2.xxxxxxx.com" に ssh でログインできるユーザーとパスワードは同じものでないといけません。


Multiple roles

role :libs, "www.xxxxxxx.com"
role :files, "www2.xxxxxxx.com"

task :search_libs, :roels => :libs do
  run "ls -x1 /usr/lib | grep -i xml"
end

task :count_libs, :roels => :files do
  run "ls -x1 /usr/lib | wc -l"
end

このように、役割を付けてあげれば、サーバーごとに、分けて実行できますよ!と書いてあると思う。
これなら、ユーザーとパスワードは同じでなくても大丈夫ですね。



Documenting tasks

それぞれの、tasksに説明を付けることができます。
tasksの名前は、自由に付けることができますので、それで分かればよいですが、分かりにくい場合は説明を書くとトができます。
それを表示するのが、 cat -T です。やってみましょう。

role :libs, "www.xxxxxxx.com"
role :files, "www2.xxxxxxx.com"

desc "Search /usr/lib for files named xml."
task :search_libs, :roles => :libs do
 run "ls -x1 /usr/lib | grep -i xml"
end

desc "This is a test task."
task :test_tasks, :roles => :files do
 run "ls -x1 /usr/lib | wc -l"
end
  • 実行結果
C:\capistrano_example>cap -T
cap invoke      # Invoke a single command on the remote servers.
cap search_libs # Search /usr/lib for files named xml.
cap shell       # Begin an interactive Capistrano session.
cap test_tasks  # This is a test task.

Extended help may be available for these tasks.
Type `cap -e taskname' to view it.

おおおぉ。説明が追加されていますね。
ただし、同じタスク名が存在した場合は、ひとつしか表示しません。



また、ひとつのタスクだけ見ることができます。-e オプションを使います。

C:\capistrano_example>cap -e search_libs
------------------------------------------------------------
cap search_libs
------------------------------------------------------------
Search /usr/lib for files named xml.

cap invoke

ファイルに task を記述しなくても、 invoke を使えば呼び出せちゃうよ!ってもの。

role :libs, "www.xxxxxxx.com"
  • 実行結果
C:\capistrano_example>cap invoke COMMAND="df -h"
 * executing `invoke'
 * executing "df -h"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] Filesystem            Size  Used Avail Use% Mounted on
** [out :: www.xxxxxxx.com] /dev/simfs             20G  5.2G   15G  26% /
   command finished

C:\capistrano_example>

roleが複数ある場合は、ROLES をしていすれば、見るサーバーを指定できるよ!

role :libs, "www.xxxxxxx.com"
role :libs_2", "www2.xxxxxxx.com"
  • 実行結果
C:\capistrano_example>cap invoke COMMAND="df -h" ROLES=libs_2
 * executing `invoke'
 * executing "df -h"
   servers: ["www2.xxxxxxx.com"]
Password:
   [www2.xxxxxxx.com] executing command
** [out :: www2.xxxxxxx.com] Filesystem            Size  Used Avail Use% Mounted on
** [out :: www2.xxxxxxx.com] /dev/simfs             20G  10.0G   15G  50% /
   command finished

C:\capistrano_example>


そして、なんと capfile なんてなくても実行できるよ!

空っぽ

  • 実行結果
C:\capistrano_example>cap invoke COMMAND="df -h" HOSTS=your_user_name@www.xxxxxxx.com
 * executing `invoke'
 * executing "df -h"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] Filesystem            Size  Used Avail Use% Mounted on
** [out :: www.xxxxxxx.com] /dev/simfs             20G  5.2G   15G  26% /
   command finished

C:\capistrano_example>

cap shell

たしかに、cap invoke はいいけど、毎回コネクションをやり直すので、色々なシェルを何回も行うのは苦痛でしょ?ってことで、cap shell がある。

C:\capistrano_example>cap shell
 * executing `shell'
====================================================================
Welcome to the interactive Capistrano shell! This is an experimental
feature, and is liable to change in future releases. Type 'help' for
a summary of how to use the shell.
--------------------------------------------------------------------
cap> df -h
[establishing connection(s) to www.xxxxxxx.com]
Password:
** [out :: www.xxxxxxx.com] Filesystem            Size  Used Avail Use% Mounted on
** [out :: www.xxxxxxx.com] /dev/simfs             20G  5.2G   15G  26% /
cap>

一度ログインしてしまえば、Linuxにログインしていのと同じような感じで操作できる。すごいわー。


Namespaces

task を分類することができます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"

namespace :libs do
 desc "Search /usr/lib for files named xml."
 task :search, :roles => :libs do
   run "ls -x1 /usr/lib | grep -i xml"
 end

 desc "counte test"
 task :count, :roles => :libs do
   run "ls -x1 /usr/lib | wc -l"
 end
end


libs:search このように実行します。

  • 実行結果
C:\capistrano_example>cap -f capfile libs:search
 * executing `libs:search'
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [wwww.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished


また、namespace のデフォルト task を設定できます。
そうすると、 libs だけで実行できます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"

namespace :libs do
 desc "Search /usr/lib for files named xml."
 task :default, :roles => :libs do
   run "ls -x1 /usr/lib | grep -i xml"
 end

 desc "counte test"
 task :count, :roles => :libs do
   run "ls -x1 /usr/lib | wc -l"
 end
end
  • 実行結果
C:\capistrano_example>cap -f capfile libs
 * executing `libs'
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished

Variables

なんと、変数も設置できます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"
set :term, "xml"

task :search, :roles => :libs do
 run "ls -x1 /usr/lib | grep -i #{term}"
end
  • 実行結果
C:\capistrano_example>cap -f capfile libs
 * executing `libs'
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished


しかも、コマンドライン入力時に、対話型で入力することもできます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"
set(:term) do
 print "Gimme a search term: "
 STDOUT.flush
 STDIN.gets.chomp
end

task :search, :roles => :libs do
 run "ls -x1 /usr/lib | grep -i #{term}"
end
  • 実行結果
C:\capistrano_example>cap -f capfile search
 * executing `search'
Gimme a search term: xml
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished


もっと簡単にかけます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"
set(:term) do
 Capistrano::CLI.ui.ask "Gimme a search term: "
end

task :search, :roles => :libs do
 run "ls -x1 /usr/lib | grep -i #{term}"
end
  • 実行結果
C:\capistrano_example>cap -f capfile search
 * executing `search'
Gimme a search term: xml
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished


-s オプションで最初から、指定して渡せます。

C:\capistrano_example>cap -f capfile -s term=xml search
 * executing `search'
 * executing "ls -x1 /usr/lib | grep -i xml"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] libxml2.a
** [out :: www.xxxxxxx.com] libxml2.so
** [out :: www.xxxxxxx.com] libxml2.so.2
** [out :: www.xxxxxxx.com] libxml2.so.2.6.27
** [out :: www.xxxxxxx.com] xml2Conf.sh
   command finished


そのほかもろもろ、いろんなオプションが使えます。

C:\capistrano_example>cap -h
Usage: cap [options] action ...
   -d, --debug                      Prompts before each remote command execution.
   -e, --explain TASK               Displays help (if available) for the task.
   -F, --default-config             Always use default config, even with -f.
   -f, --file FILE                  A recipe file to load. May be given more than once.
   -H, --long-help                  Explain these options.
   -h, --help                       Display this help message.
   -p, --password                   Immediately prompt for the password.
   -q, --quiet                      Make the output as quiet as possible.
   -S, --set-before NAME=VALUE      Set a variable before the recipes are loaded.
   -s, --set NAME=VALUE             Set a variable after the recipes are loaded.
   -T, --tasks                      List all tasks in the loaded recipe files.
   -V, --version                    Display the Capistrano version, and exit.
   -v, --verbose                    Be more verbose. May be given more than once.
   -X, --skip-system-config         Don't load the system config file (capistrano.conf)
   -x, --skip-user-config           Don't load the user config file (.caprc)

Transactions

なんと、トランザクションも使えます。処理が失敗したら、ロールバックしてくれるんです。
とはいっても、自分で記述することで、ロールバックを実現します。
もし、なにか不具合が起こった場合、on_rollback が実行されます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"

task :test_task do
 transaction do
   make_test_file
 end
end

task :make_test_file, :roles => :libs do
 on_rollback { run "rm -f test_file" }
 run "touch test_file"
 run "echo 'aaaaaa' > test_file"
end
  • 実行結果
C:\capistrano_example>cap -f capfile test_task
 * executing `test_task'
** transaction: start
 * executing `make_test_file'
 * executing "touch test_file"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
   command finished
 * executing "echo 'aaaaaa' > test_file"
   servers: ["www.xxxxxxx.com"]
   [www.xxxxxxx.com] executing command
   command finished
** transaction: commit

Loading files

capfile が複雑になってきたら、ファイルを分けて、そのファイルをロードしたりできます。そうしてカプセル化するなどできます。
ファイルのロードパスはデフォルトは、カレントディレクトを探すでしょう。
もし、ロードパスを追加したいなら、ロードパスを追加することもできます。

role :libs, "www.xxxxxxx.com"
set :user, "your_user_name"
load "capfile2"
load_paths << "C:/capistrano_example/files"
load "capfile3"

task :search_libs do
 run "ls -x1 /usr/lib | grep -i xml"
end
task :count_libs do
 run "ls -x1 /usr/lib | wc -l"
end
  • ファイル : C:\capistrano_example\files\capfile3
task :hdd_check do
 run "df -h"
end
  • 実行結果 capfile2
C:\capistrano_example>cap -f capfile count_libs
 * executing `count_libs'
 * executing "ls -x1 /usr/lib | wc -l"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] 775
   command finished
  • 実行結果 capfile3
C:\capistrano_example>cap -f capfile hdd_check
 * executing `hdd_check'
 * executing "df -h"
   servers: ["www.xxxxxxx.com"]
Password:
   [www.xxxxxxx.com] executing command
** [out :: www.xxxxxxx.com] Filesystem            Size  Used Avail Use% Mounted on
** [out :: www.xxxxxxx.com] /dev/simfs             20G  5.2G   15G  26% /
   command finished

That's it!

これらは、カピストラーノの基本機能です。他の多くの特徴は別のチュートアルに記述されているでしょう。


次回

Railsのデプロイをやってみます。