Skip to content

Instantly share code, notes, and snippets.

@kurbmedia
Created November 12, 2010 06:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurbmedia/673794 to your computer and use it in GitHub Desktop.
Save kurbmedia/673794 to your computer and use it in GitHub Desktop.
Base deploy recipe set.
#
# Check the status of bluepill monitored daemons
#
namespace :monitor do
desc "Get status of bluepill monitored daemons."
task :daemons, :roles => :app do
sudo "bluepill status"
end
end
#
# Bundler related tasks
# setup to use vendor/bundle unless use_system_gems is set to true in deploy.yml
#
after 'deploy:update_code', 'bundler:bundle_new_release'
desc "Install bundler, and bundle new releases."
namespace :bundler do
task :install, :roles => :app do
run("gem install bundler --source=http://gemcutter.org")
end
task :bundle_new_release, :roles => :app do
without_env = DEPLOY_CONFIG['locations'].keys.delete_if{ |k| k.eql?("#{rails_env}") }.concat(['development','test']).uniq
without_env.concat(bundle_without.split(',')) if defined?(bundle_without) && !bundle_without.nil?
# Use system gems if specified in deploy.yml, otherwise use vendor/bundle
unless use_system_gems
run "mkdir -p #{shared_path}/gems"
run "cd #{release_path} && ln -nfs #{shared_path}/gems #{release_path}/vendor/bundle"
run "cd #{release_path} && bundle install vendor/bundle --deployment --without=#{without_env.join(' ')}"
else
run "cd #{release_path} && bundle install --deployment --system --without=#{without_env.join(' ')}"
end
end
desc 'Remove the bundled gems (packaged) so they can be reset.'
task :reset, :roles => :app do
run("cd #{shared_path}/gems && rm -rf ruby")
end
end
#
# SCP Configuration files into shared_path/config
# this way they can be removed from the repository
#
desc "Write config files"
task :configure, :roles => :app do
if DEPLOY_CONFIG['configs'].nil?
puts " No configuration files found... exiting."
exit
end
configs = DEPLOY_CONFIG['configs'].split(",")
exit if configs.empty? or configs.nil?
run "mkdir -p #{shared_path}/config"
configs.each do |file|
data = YAML::load(File::open("config/#{file}"))
run "rm #{shared_path}/config/#{file}" if capture("if [ -e #{shared_path}/config/#{file} ]; then echo 'true'; fi").strip.eql?('true')
put YAML::dump(data), "#{shared_path}/config/#{file}"
end
end
#
# This is a default deploy.rb
# it is included into the Rails 3 Basic Template by default.
#
require 'active_support'
require 'lib/deploy_helpers'
DEPLOY_CONFIG = YAML::load(File.open("#{Rails.root}/config/deploy.yml"))
default_run_options[:pty] = true
default_run_options[:max_hosts] = 2
# Make some defaults so we don't have to put everything in deploy.yml
set :use_sudo, false
set :scm, :git
set :deploy_via, :remote_cache
set :application, DEPLOY_CONFIG['application']
set :repository, DEPLOY_CONFIG['repository']
# Load recipes
DEPLOY_CONFIG['recipes'].split(',').each{ |r| require "#{r.chomp}" }
# Setup each location
DEPLOY_CONFIG['locations'].each_pair do |location, settings|
task "#{location}" do
settings.each_pair do |key, value|
set key.to_sym, value
end
domain.to_s.split(',').each{ |d| role(:app, d) }
domain.to_s.split(',').each{ |d| role(:web, d) }
unless settings['db_server']
domain.to_s.split(',').each{ |d| role(:db, d, :primary => true) }
else
role :db, settings['db_server'], :primary => true, :no_release => true
end
end
end
after "deploy:update_code","deploy:symlink_configs" # Symlink configuration files into the shared folder
after 'deploy:restart', 'deploy:cleanup' # Make sure our releases don't get out of control.
namespace :deploy do
# Symlink our database config and any other files in the "configs" list in deploy.yml
task :symlink_configs, :roles => :app do
configurations = DEPLOY_CONFIG['configs'].split(",")
unless configurations.empty?
configs = configurations.collect{ |file| "ln -nfs #{shared_path}/config/#{file} #{release_path}/config/#{file.chomp}" }
run "mkdir -p #{shared_path}/config && cd #{release_path} && #{configs.join(" && ")}"
end
run "mkdir -p #{shared_path}/cache && cd #{release_path} && ln -nfs #{shared_path}/cache #{release_path}/tmp/cache"
run "mkdir -p #{shared_path}/pids && cd #{release_path} && ln -nfs #{shared_path}/pids #{release_path}/tmp/pids"
# If an asset path is defined, link that one to our system dir instead.
if defined?(asset_path) && path_exists?("#{asset_path}")
run "cd #{release_path} && ln -nfs #{asset_path} #{release_path}/public/system"
else
run "cd #{release_path} && ln -nfs #{shared_path}/system #{release_path}/public/system"
end
end
end
application: "appname"
repository: "git://github.com/name/appname.git"
configs: "database.yml"
recipes: "configure,monitor,maintenance,passenger,bundler"
defaults: &defaults
use_system_gems: false
git_shallow_clone: 1
keep_releases: 3
branch: "master"
locations:
staging:
<<: *defaults
server: ""
domain: ""
db_server: ""
deploy_to: ""
asset_path: ""
user: "deploy"
port: 22
rails_env: "staging"
production:
<<: *defaults
server: ""
domain: ""
db_server: ""
branch: "production"
keep_releases: 5
deploy_to: ""
asset_path: ""
user: "deploy"
port: 22
rails_env: "production"
#
# Various helper functions used in tasks.
#
def path_exists?(path)
capture("if [ -e #{path} ]; then echo 'true'; fi").strip.eql?('true')
end
#
# Enable/disable maintenance mode.
# setup a particular template in deploy.yml
# uses layouts/maintenance.html.erb by default
#
require 'lib/deploy_helpers'
require 'erb'
namespace :maintenance do
task :on, :roles => :web do
maintenance_page = DEPLOY_CONFIG['maintenance_page'] || "#{Rails.root}/app/views/layouts/maintenance.html.erb"
if path_exists?("#{asset_path}")
maintenance = ERB.new(File.read("#{maintenance_page}")).result(binding)
put maintenance, "#{shared_path}/system/maintenance.html", :mode => 0644
end
end
task :off, :roles => :web do
run "rm #{shared_path}/system/maintenance.html" if path_exists?("#{asset_path}")
end
end
#
# Functionality to tail/view logs
#
namespace :monitor do
desc 'Tails the application log.'
task :default, :roles => :app do
run "tail -f #{shared_path}/log/#{rails_env}.log" do |channel, stream, data|
puts # for an extra line break before the host name
puts "#{channel[:server]} -> #{data}"
break if stream == :err
end
end
end
#
# Methods to start/stop/restart passenger
#
namespace :deploy do
desc "Restarting passenger"
task :restart, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
[:start, :stop].each do |t|
desc "#{t} task is a no-op with mod_rails"
task t, :roles => :app do ; end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment