Capistrano 基本用法

  1. 安装gem
    gem ‘capistrano’, ‘2.12.0’
    gem ‘capistrano-rbenv’, ‘1.0.1’
    bundle
  2. capify .
    会生成两个文件: Capfile , config/deploy.rb
  3. cap deploy:setup 会建立必须的 文件夹 (眼疾手快 及时停止executing “if test -f /etc/debian_version; then echo debian; elif test -f /etc/redhat-release; then echo redhat; elif test -f /etc/system-release; then echo redhat; else echo unknown; fi;”)
  4. 到服务器上 shared目录下创建config 文件夹 里面创建secrets.yml database.yml. application.yml 文件并配置
  5. 执行bundle exec cap deploy

一个deploy.rb 部署多个服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
SERVER_TEST_32 = "10.103.23.32"
SERVER_TEST_103 = "10.103.23.103"
SERVER_PRODUCTION_121 = "10.103.23.121"
SERVER_PRODUCTION_11 = "10.103.23.11"
SERVER_PRODUCTION_73 = "10.103.23.73"
SERVER_PRODUCTION_74 = "10.103.23.74"

set(:server_type) {
puts "== 测试服务器是: test103 (.32 已经废弃,不过有需要的话仍然可以上去)"
puts "== 正式服务器是: 73, 74 "
Capistrano::CLI.ui.ask("== which server do you want to deploy to? (test103/test32/73/74)? ")
}
case server_type.chomp
when 'test32'
server = SERVER_TEST_32
password = 'Q7sKxQoM8'
when 'test103'
server = SERVER_TEST_103
password = 'v6piiz9cx'
when '11'
server = SERVER_PRODUCTION_11
password = 'no need for password'
when '121'
server = SERVER_PRODUCTION_121
password = 'YopmwECgn'
when '73'
server = SERVER_PRODUCTION_73
password = 'avCNz7gv6'
when '74'
server = SERVER_PRODUCTION_74
password = 'N0NYxtZCo'
end

注:deploy.rb examle:
role :db, server, :primary => true
role :db, server

set :deploy_to, “/opt/app/test.wondercv.com”
default_run_options[:pty] = true

#change to your username
set :user, SSH_USER

namespace :deploy do
task :start do
run “cd #{release_path} && bundle exec thin start -C config/thin.yml”
end
task :stop do
run “cd #{release_path} && bundle exec thin stop -C config/thin.yml”
end
task :restart, :roles => :app, :except => { :no_release => true } do
db_migrate
stop
sleep 2
start
end
task :db_migrate do
run “cd #{release_path} && bundle install”
run “cd #{release_path} && RAILS_ENV=test bundle exec rake db:migrate”
end

namespace :assets do
task :precompile do

#puts “======= should run precompile”

#command = “cd #{release_path} && bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile “

#puts “== please run == \n #{command} \n == manually after deploy is done”

#run “bundle install”

#run “cd #{release_path} && bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile “
end
end
end

desc “Copy database.yml to release_path”
task :cp_database_yml do
puts “=== executing my customized command: “
run “cp -r #{shared_path}/config/* #{release_path}/config/“
run “ln -s #{shared_path}/files #{release_path}/public/files”
puts “=== done (executing my customized command)”
end

before “deploy:assets:precompile”, :cp_database_yml

#after “deploy”, “deploy:restart”