Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

deploy your rails apps on edge rails

lib/tasks/rails.rake
desc "Checks out rails"
task :init do
  ENV['SHARED_PATH']  = '../../shared' unless ENV['SHARED_PATH']
  ENV['RAILS_PATH'] ||= File.join(ENV['SHARED_PATH'], 'rails')
 
  checkout_path = File.join(ENV['RAILS_PATH'], 'trunk')
  export_path   = "#{ENV['RAILS_PATH']}/rev_#{ENV['REVISION']}"
  symlink_path  = 'vendor/rails'

  # do we need to checkout the file?
  unless File.exists?(checkout_path)
    puts 'setting up rails trunk'    
    get_framework_for checkout_path do |framework|
      system "svn co http://dev.rubyonrails.org/svn/rails/trunk/#{framework}/lib #{checkout_path}/#{framework}/lib --quiet"
    end
  end

  # do we need to export the revision?
  unless File.exists?(export_path)
    puts "setting up rails rev #{ENV['REVISION']}"
    get_framework_for export_path do |framework|
      system "svn up #{checkout_path}/#{framework}/lib -r #{ENV['REVISION']} --quiet"
      system "svn export #{checkout_path}/#{framework}/lib #{export_path}/#{framework}/lib"
    end
  end

  puts 'linking rails'
  rm_rf   symlink_path
  mkdir_p symlink_path

  get_framework_for symlink_path do |framework|
    ln_s File.expand_path("#{export_path}/#{framework}/lib"), "#{symlink_path}/#{framework}/lib"
  end
end

def get_framework_for(*paths)
  %w( railties actionpack activerecord actionmailer activesupport actionwebservice ).each do |framework|
    paths.each { |path| mkdir_p "#{path}/#{framework}" }
    yield framework
  end
end


Switchtower recipe code:
set :rails_version, 3517
desc "Checks out rails rev ##{rails_version}"
task :after_symlink do
  run <<-CMD
    cd #{current_release} &&
    rake init REVISION=#{rails_version} RAILS_PATH=/home/username/projects/rails
  CMD
end

freeze_edge for switchtower deployments

I run this rake task in the after_symlink event of switchtower to set up the newly deployed app's config files. I rewrote the freeze_edge task to be a little more efficient:

1. checks out rails trunk to shared/rails/trunk if needed
2. updates rails trunk to revision used by the app
3. exports that revision to shared/rails/rev_xxxx
4. links that revision to current/vendor/rails

This way, my app only has a single checkout of the rails trunk. Multiple deployments do not checkout fresh copies, but update to the desired revision and export. If you make multiple deployments using the same rails revision, then only the symlink action is performed. This speeds the task up considerably, and conserves disk space and network traffic.

Set rails version in deploy.rb:
set :rails_version, 2871


Create after_symlink task:
desc "Checks out rails rev ##{rails_version}"
task :after_symlink do
  run "cd #{current_release} && rake init SHARED_PATH=#{shared_path} CURRENT_RELEASE=#{current_release} REVISION=#{rails_version}"
end


init task:
desc "Performs an export of the rails trunk"
task :init do
  return unless ENV['SHARED_PATH'] and ENV['CURRENT_RELEASE']
  puts 'copying files...'
  cp "#{ENV['SHARED_PATH']}/database.yml",  "#{ENV['CURRENT_RELEASE']}/config"
  cp "#{ENV['SHARED_PATH']}/dispatch.fcgi", "#{ENV['CURRENT_RELEASE']}/public"

  puts 'setting permissions...'
  chmod 0600, "#{ENV['CURRENT_RELEASE']}/config/database.yml"
  chmod 0700, "#{ENV['CURRENT_RELEASE']}/public/dispatch.fcgi"
  
  checkout_path = "#{ENV['SHARED_PATH']}/rails/trunk"
  export_path = "#{ENV['SHARED_PATH']}/rails/rev_#{ENV['REVISION']}"
  symlink_path = "#{ENV['CURRENT_RELEASE']}/vendor/rails"

  # do we need to checkout the file?
  unless File.exists?(checkout_path)
    puts 'setting up rails trunk'    
    get_framework_for checkout_path do |framework|
      system "svn co http://dev.rubyonrails.org/svn/rails/trunk/#{framework}/lib #{checkout_path}/#{framework}/lib --quiet"
    end
  end

  # do we need to export the revision?
  unless File.exists?(export_path)
    puts "setting up rails rev #{ENV['REVISION']}"
    get_framework_for export_path, symlink_path do |framework|
      system "svn up #{checkout_path}/#{framework}/lib -r #{ENV['REVISION']} --quiet"
      system "svn export #{checkout_path}/#{framework}/lib #{export_path}/#{framework}/lib"
    end
  end

  puts 'linking rails'
  rm_rf   "vendor/rails"
  mkdir_p "vendor/rails"

  get_framework_for checkout_path, export_path, symlink_path do |framework|
    ln_s File.expand_path("#{export_path}/#{framework}/lib"), "#{ENV['CURRENT_RELEASE']}/vendor/rails/#{framework}/lib"
  end
end

def get_framework_for(*paths)
  %w( railties actionpack activerecord actionmailer activesupport actionwebservice ).each do |framework|
    paths.each { |path| mkdir_p "#{path}/#{framework}" }
    yield framework
  end
end

restart rails app in switchtower on textdrive

You'll have to override the standard Switchtower restart task if you don't have sudo access:

desc "Restart the FCGI processes on the app server."
task :restart, :roles => :app do
  run "ruby #{current_path}/script/process/reaper"
end

manual reaping of fcgi processes

These are the signals that SwitchTower's reaper uses:

# reload
kill -s HUP @pid

# graceful
kill -s TERM @pid

# kill
kill -9 @pid

# usr1
kill -s USR1 @pid

checkout rails and apply patches with a rake task

see my blog post about this...

desc "Performs an export of the rails trunk"
task :rails_export => :environment do
  FileUtils.rm_rf "#{RAILS_ROOT}/vendor/rails"
  puts 'exporting... ' + ENV["VERSION"].to_s 
  `svn export http://dev.rubyonrails.org/svn/rails/trunk#{ENV["VERSION"] ? "@#{ENV['VERSION']}" : ""} #{RAILS_ROOT}/vendor/rails`
  puts 'applying patches...'
  FileList[File.expand_path("#{RAILS_ROOT}/config/diffs/*.diff")].sort.each do |diff_file|
    %x{cd #{File.expand_path("#{RAILS_ROOT}/vendor/rails")} && cat #{File.expand_path(diff_file)} | patch -p0}
  end
end


Does an svn export from rails. Provide a revision if you don't want to check out the latest. It then applies patches in config/diffs in alphabetical order.

rake rails_export VERSION=2000


Here is a SwitchTower recipe to perform this action automatically:

set :rails_version, '2031' # custom ST variable

desc "Checks out rails rev ##{rails_version}"
task :after_symlink do
  run "cd #{current_release} && rake rails_export VERSION=#{rails_version}"
end
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS