Skip to content

Instantly share code, notes, and snippets.

@stonegao
Forked from nragaz/deploy.rb
Created August 6, 2010 15:11
Show Gist options
  • Select an option

  • Save stonegao/511459 to your computer and use it in GitHub Desktop.

Select an option

Save stonegao/511459 to your computer and use it in GitHub Desktop.
CURRENT_RUBY = 'ree-1.8.7-2010.02'
RUBY_PATH = "/home/rails/.rvm/rubies/#{CURRENT_RUBY}"
GEM_HOME = "/home/rails/.rvm/gems/#{CURRENT_RUBY}"
ssh_options[:paranoid] = false
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
set :stages, %w(staging production)
set :default_stage, "production"
require 'capistrano/ext/multistage'
require 'config/boot'
require 'hoptoad_notifier/capistrano'
set :application, "myapp"
set :domain, "myapp.com"
set :user, "rails"
set :use_sudo, false
set :deploy_to, "/srv/myapp"
set :deploy_via, :remote_cache
set :keep_releases, 2
set :repository, "[email protected]:nragaz/myapp.git"
set :scm, :git
set :branch, 'master'
set :git_enable_submodules, 1
set :rails_env, 'production'
set :default_environment, {
'PATH' => "#{GEM_HOME}/bin:#{GEM_HOME}@global/bin:#{RUBY_PATH}/bin:/home/rails/.rvm/bin:$PATH",
'RUBY_VERSION' => CURRENT_RUBY,
'GEM_HOME' => GEM_HOME,
'GEM_HOME' => "#{GEM_HOME}:#{GEM_HOME}@global",
'BUNDLE_PATH' => GEM_HOME
}
namespace :deploy do
task :start, :roles => :app, :except => { :no_release => true } do
god.start
end
task :stop, :roles => :app, :except => { :no_release => true } do
god.terminate
end
task :restart, :roles => :app, :except => { :no_release => true } do
god.reload
god.restart_resque
god.restart_unicorn
end
task :bundle do
run "cd #{release_path} && RAILS_ENV=#{rails_env} bundle install"
end
desc "Symlinks the database.yml"
task :symlink_db, :roles => :app do
run "ln -nfs #{deploy_to}/shared/config/database.yml #{release_path}/config/database.yml"
end
desc "Symlinks the downtime site"
task :symlink_downtime, :roles => :app do
run "ln -nfs #{release_path}/config/down #{deploy_to}/shared/down"
end
end
namespace :db do
desc "Copy local database.yml.example to server"
task :database_yml, :roles => :app do
run "mkdir #{deploy_to}/shared/config ; true"
upload("config/database.yml.example", "#{deploy_to}/shared/config/database.yml")
end
end
namespace :god do
task :restart_resque, :roles => :app do
sudo "#{GEM_HOME}/bin/god restart resque"
end
task :restart_unicorn, :roles => :app do
sudo "#{GEM_HOME}/bin/god restart unicorn"
end
desc "Reload config"
task :reload, :roles => :app do
sudo "#{GEM_HOME}/bin/god load #{deploy_to}/current/config/god/all.god"
end
desc "Start god"
task :start, :roles => :app do
sudo "#{GEM_HOME}/bin/god -c #{deploy_to}/current/config/god/all.god"
end
desc "Quit god, but not the processes it's monitoring"
task :quit, :roles => :app do
sudo "#{GEM_HOME}/bin/god quit"
end
desc "Terminate god and all monitored processes"
task :terminate, :roles => :app do
sudo "#{GEM_HOME}/bin/god terminate"
end
desc "Describe the status of the running tasks"
task :status, :roles => :app do
sudo "#{GEM_HOME}/bin/god status"
end
end
namespace :nginx do
desc "Deploy site configs from Github"
task :config do
# delete old configs
sudo "rm /etc/nginx/nginx.conf ; true"
sudo "rm /etc/nginx/sites-available/default ; true"
sudo "rm /etc/nginx/sites-enabled/default ; true"
sudo "rm /etc/nginx/sites-available/down ; true"
sudo "rm /etc/nginx/sites-available/up ; true"
# install new configs
run "cd ~ && rm -rf nginx-config* && git clone [email protected]:gist-with-nginx-files.git nginx-config"
sudo "mv /home/rails/nginx-config/nginx.conf /etc/nginx/nginx.conf"
sudo "mv /home/rails/nginx-config/down /etc/nginx/sites-available/down"
sudo "mv /home/rails/nginx-config/up /etc/nginx/sites-available/up"
sudo "ln -nfs /etc/nginx/sites-available/up /etc/nginx/sites-enabled/up"
run "rm -rf nginx-config"
end
desc "Restart nginx"
task :restart do
sudo "/etc/init.d/nginx restart"
end
desc "Stop nginx"
task :stop do
sudo "/etc/init.d/nginx stop"
end
desc "Start nginx"
task :start do
sudo "/etc/init.d/nginx start"
end
desc "Enable downtime site"
task :down do
sudo "rm /etc/nginx/sites-enabled/up ; true"
sudo "ln -nfs /etc/nginx/sites-available/down /etc/nginx/sites-enabled/down"
nginx.restart
end
desc "Enable uptime site"
task :up do
sudo "rm /etc/nginx/sites-enabled/down ; true"
sudo "ln -nfs /etc/nginx/sites-available/up /etc/nginx/sites-enabled/up"
nginx.restart
end
end
desc "Show memory consumption on the server"
task :free do
run "free -lm"
end
after "deploy:update_code" do
deploy.symlink_db
deploy.symlink_downtime
deploy.bundle
deploy.migrate
end
Capistrano Multi Stage Instructions
Features provided in the capistrano-ext gem allows you to setup multiple environments within your deploy.rb file giving you the ability to run commands such as:
cap development deploy:migrations
or
cap production deploy:migrations
and have your application deploy to the different environments properly. This document provides you instructions on how to properly accomplish this.
Contents
* 1 Step 1: Define Your Stages
* 2 Step 2: Create Custom Stage Configurations
* 3 Step 3: Define Custom Stage Configurations
* 4 Step 4: Run Your Deploys
Step 1: Define Your Stages
A stage is a separate environment in which you would want to deploy your application server to. You need to tell Capistrano about the various stages you want defined using these commands:
set :default_stage, "development"
set :stages, %w(production development)
require 'capistrano/ext/multistage'
Step 2: Create Custom Stage Configurations
You now need to define the custom variables for each stage. To do this, create a folder within your config folder called deploy. Then within that, create your stage files:
$ ls -l
total 56
-rw-r--r--@ 1 user staff 2657 Feb 22 14:37 boot.rb
-rw-r--r--@ 1 user staff 1082 Mar 31 13:56 database.yml
drwxr-xr-x 6 user staff 204 Mar 7 16:31 deploy
-rw-r--r--@ 1 user staff 563 Apr 10 12:30 deploy.rb
-rw-r--r--@ 1 user staff 2882 May 29 10:31 environment.rb
drwxr-xr-x 12 user staff 408 Mar 26 13:48 environments
-rw-r--r--@ 1 user staff 839 Apr 4 12:44 routes.rb
$ mkdir deploy
$ cd deploy
$ ls -l
total 24
-rw-r--r--@ 1 user staff 989 Apr 2 11:39 production.rb
-rw-r--r-- 1 user staff 506 Mar 7 16:31 development.rb
Step 3: Define Custom Stage Configurations
Now that your files exist, you need to edit them to tell Capistrano what's different in each environment. You can over ride almost every variable within this config as needed:
$ cat production.rb
miBook:deploy jesse$ cat production.rb
role :web, "productionserver.com"
role :app, "productionserver.com"
role :db, "productionserver.com", :primary => true
$ cat development.rb
miBook:deploy jesse$ cat production.rb
role :web, "developmentserver.com"
role :app, "developmentserver.com"
role :db, "developmentserver.com", :primary => true
Step 4: Run Your Deploys
You can now tell Capistrano which environment to use when you deploy:
$ cap development deploy
That's it!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment