require 'rvm/capistrano'
require 'bundler/capistrano'
set :server_name, "example.com"
set :application, "application_name"
set :repository, "git@github.com:username/#{application}.git"
# Uses local instead of remote server keys
ssh_options[:forward_agent] = true
set :deploy_via, :remote_cache
set :rvm_ruby_string, 'default'
set :rvm_type, :user
set :use_sudo, false
set :user, "deploy"
set :deploy_to, "/home/deploy/apps/#{application}"
set :rails_env, "production"
set :branch, "master"
set :keep_releases, 5
default_run_options[:pty] = true
ssh_options[:port] = 56
set :scm, :git
after "deploy:restart", "deploy:cleanup"
role :web, "#{server_name}" # Your HTTP server, Apache/etc
role :app, "#{server_name}" # This may be the same as your `Web` server
role :db, "#{server_name}", :primary => true # This is where Rails migrations will run
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{File.join(current_path,'tmp','restart.txt')}"
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /opt/nginx/sites-enabled/#{application}"
sudo "service nginx restart"
end
end
before "deploy:setup", "db:configure"
after "deploy:update_code", "db:symlink"
namespace :db do
desc "Create database yaml in shared path"
task :configure do
set :database_username do
"rails"
end
set :database_password do
Capistrano::CLI.password_prompt "Database Password: "
end
db_config = <<-EOF
base: &base
adapter: mysql2
encoding: utf8
reconnect: false
pool: 5
username: #{database_username}
password: #{database_password}
development:
database: #{application}_development
<<: *base
test:
database: #{application}_test
<<: *base
production:
database: #{application}_production
<<: *base
EOF
run "mkdir -p #{shared_path}/config"
put db_config, "#{shared_path}/config/database.yml"
end
desc "Make symlink for database yaml"
task :symlink do
run "ln -nfs #{shared_path}/config/database.yml #{latest_release}/config/database.yml"
end
end