这是我的deploy.rb,很久没有更新过了。我运行了两个SED命令来设置我的:更新后代码任务中的环境。我确信有一种更简单的方法,但这是我目前的方法。
require 'erb'
before "deploy:setup", :db
after "deploy:update_code", "db:symlink"
after "deploy", "deploy:cleanup"
def prompt_with_default(var, default)
set(var) do
Capistrano::CLI.ui.ask "Enter #{var} [#{default}] : "
end
set var, default if eval("#{var.to_s}.empty?")
end
default_run_options[:pty] = true
set :rake, "/usr/local/bin/rake"
set :user, "your username"
set :application, "application name"
set :repository, "svn repository path"
set :host, "domain name"
set :db_host, "DB host name"
set :db_user, "DB user name"
set :db_prefix, "any DB name prefix your host might require"
set :rails_env, "production"
set :deploy_to, "/home/username/rails/#{host}"
role :app, host
role :web, host
role :db, host, :primary => true
set :use_sudo, false
set :checkout, "export"
desc "Tasks to execute after code update"
task :after_update_code, :roles => [:app, :db, :web] do
run "sed -i -e '/ENV.*RAILS_ENV/s/# //' #{release_path}/config/environment.rb"
run "sed -i -e '/ENV.*RAILS_ENV/s/production/#{rails_env}/' #{release_path}/config/environment.rb"
deny_lines = []
File.readlines("#{release_path}/config/banned_ips").each {|ip|
deny_lines << "deny from #{ip}"
}
ip_ban_block = <<EOBAN
order allow,deny
allow from all
EOBAN
run "sed -i -e 's/# BANNED IPS/#{ip_ban_block}/' #{release_path}/public/.htaccess"
run "chmod +x #{release_path}/script/runner"
run "chmod +x #{release_path}/script/process/reaper"
run "chmod +x #{release_path}/script/process/spawner"
run "chmod 755 #{release_path}/public/dispatch.*"
end
desc "Restarting after deployment"
task :after_deploy, :roles => [:app, :db, :web] do
run "cd #{current_path} && rake RAILS_ENV=production db:sessions:clear tmp:clear"
run "dos2unix #{release_path}/public/dispatch.*"
run "dos2unix #{release_path}/public/.htaccess"
run "chmod -R 755 #{release_path}"
run "chmod -R 775 #{release_path}/log #{release_path}/tmp #{release_path}/script"
run "find #{release_path}/ | xargs touch"
run "touch #{deploy_to}/current/public/dispatch.fcgi"
end
desc "Restarting after rollback"
task :after_rollback, :roles => [:app, :db, :web] do
run "touch #{deploy_to}/current/public/dispatch.fcgi"
end
namespace :db do
desc "Create database yaml in shared path"
task :default do
prompt_with_default(:db_password, "")
db_config = ERB.new <<-EOF
base: &base
adapter: mysql
port: 3306
host:
username:
password:
development:
database:
<<: *base
test:
database:
<<: *base
production:
database:
<<: *base
EOF
run "mkdir -p #{shared_path}/config"
put db_config.result, "#{shared_path}/config/database.yml"
end
desc "Make symlink for database yaml"
task :symlink do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
end
一旦我设置了变量,我就有了一个运行的bash脚本:
svn export $svn_path . --force
cap deploy
这会要求我输入两次密码。一次用于从SVN导出,另一次用于提示在共享路径中自动生成database.yml。