Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leehambley/158640 to your computer and use it in GitHub Desktop.
Save leehambley/158640 to your computer and use it in GitHub Desktop.
# Author: Michael van Rooijen
# Description:
# This recipe is designed to work for people using Phusion Passenger,
# Git as your repository.
#
# The script (initially) does the following:
# - It first (after you run cap:deploy setup) will set up the capistrano environment on your remote machine
# - After that, it will sync your current database.yml file to the "shared" location on the remote machine
# - You are now basically ready to deploy.
# - Once you run "cap:deploy" it will start the deployment
# - First, it will reach for your remote repository and pull out the files
# - The application will be placed in a new revision folder
# - The "current" symlink will be updated upon completion of the transfer above
# - It will then set up the symlinks from the current application to the "shared"
# section for the database.yml, production.sqlite3 and the "assets" directory.
# - Once this is done, Capistrano will initiallize the Passenger Instance and your application should be up and running!
# - One last task will be performed right after this, and that is that Capistrano will give Rails specific "rights" (www-data:www-data)
# to the whole application. This will make sure that nothing that should be writed/modified will kill the application, such as:
# stylesheets/javascripts when :cache => true, writing to sqlite3 database, using upload gems such as paperclip.
#
# So, now your application is up and running. Long Story Short:
#
# Create a new Rails App. Create a Git Repository. Add your remote repository to it.
# Push all your data to the remote repository. Configure this file. Run "cap:deploy setup" to setup the remote machine's environment.
# Run "cap:deploy" and your web application should be running in a matter of seconds.
#
# If you are not using sqlite3 and get a (500)error right off the bat, first place to look is whether your database exists on the remote server
# ( I always forget this part ;) )
#
# This Capistrano Recipe is just a base awaiting to be expanded!
# So feel free to do so.
# Application Domain (example.domain.com)
# Be sure to fill in the correct domain name for your web application!
set :application, "domain.com"
# Set server where the application is going to be uploaded to.
role :web, application
role :app, application
role :db, application
# Set the user
# :user => Set the user that will attempt to access the remote repository
# :deploy_to => Set the deployment location (destination) on the remote server
# :use_sudo => Set to false
set :user, "root"
set :deploy_to, "/var/rails/domain.com"
set :use_sudo, false
# Git Repository Location
# :scm => Specify the source code management tool you want to use (default is git)
# :repository => Assign the repository location where the application resides
# :branch => Select the branch that capistrano should fetch from (default is master)
set :scm, "git"
set :repository, "ssh://root@domain.com/var/git/domain.git"
set :branch, "master"
# Required: default_run_options[:pty] - This will allow the user to connect to protected repository after
# logging in when the system prompts for the server's root password
# default_run_options => (default is true)
default_run_options[:pty] = true
# You can add additional deployment tasks, or alter the ones below.
namespace :deploy do
desc "Restart Application with Passenger."
task :restart do
run "touch #{current_path}/tmp/restart.txt"
run "chown -R www-data:www-data #{deploy_to}"
end
desc "Setup the shared folder structure."
task :setup_shared do
run "touch #{shared_path}/log/production.log"
run "mkdir #{shared_path}/db #{shared_path}/assets #{shared_path}/config"
end
desc "Syncs the database.yml to the server"
task :sync_database_yaml do
system "rsync -vr --exclude='.DS_Store' config/database.yml #{user}@#{application}:#{shared_path}/config/"
end
desc "Sets up symbolic links."
task :setup_symlinks do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "ln -nfs #{shared_path}/db/production.sqlite3 #{release_path}/db/production.sqlite3"
run "ln -nfs #{shared_path}/assets #{release_path}/public/assets"
end
desc "Reset remote rails environment."
task :reset_rails_environment do
run "rm -rf #{deploy_to}"
system "cap deploy:setup"
end
desc "Update the crontab file"
task :update_crontab, :roles => :db do
run "cd #{release_path} && whenever --update-crontab #{application}"
end
end
after 'deploy:setup', 'deploy:setup_shared'
after 'deploy:setup', 'deploy:sync_database_yaml'
after 'deploy:update_code', 'deploy:setup_symlinks'
#uncomment if using the javan-whenever gem
# => after 'deploy:symlink', 'deploy:update_crontab'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment