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

About this user

Rick Olson http://techno-weenie.net

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

deploy your rails apps on edge rails

lib/tasks/rails.rake
   1  
   2  desc "Checks out rails"
   3  task :init do
   4    ENV['SHARED_PATH']  = '../../shared' unless ENV['SHARED_PATH']
   5    ENV['RAILS_PATH'] ||= File.join(ENV['SHARED_PATH'], 'rails')
   6   
   7    checkout_path = File.join(ENV['RAILS_PATH'], 'trunk')
   8    export_path   = "#{ENV['RAILS_PATH']}/rev_#{ENV['REVISION']}"
   9    symlink_path  = 'vendor/rails'
  10  
  11    # do we need to checkout the file?
  12    unless File.exists?(checkout_path)
  13      puts 'setting up rails trunk'    
  14      get_framework_for checkout_path do |framework|
  15        system "svn co http://dev.rubyonrails.org/svn/rails/trunk/#{framework}/lib #{checkout_path}/#{framework}/lib --quiet"
  16      end
  17    end
  18  
  19    # do we need to export the revision?
  20    unless File.exists?(export_path)
  21      puts "setting up rails rev #{ENV['REVISION']}"
  22      get_framework_for export_path do |framework|
  23        system "svn up #{checkout_path}/#{framework}/lib -r #{ENV['REVISION']} --quiet"
  24        system "svn export #{checkout_path}/#{framework}/lib #{export_path}/#{framework}/lib"
  25      end
  26    end
  27  
  28    puts 'linking rails'
  29    rm_rf   symlink_path
  30    mkdir_p symlink_path
  31  
  32    get_framework_for symlink_path do |framework|
  33      ln_s File.expand_path("#{export_path}/#{framework}/lib"), "#{symlink_path}/#{framework}/lib"
  34    end
  35  end
  36  
  37  def get_framework_for(*paths)
  38    %w( railties actionpack activerecord actionmailer activesupport actionwebservice ).each do |framework|
  39      paths.each { |path| mkdir_p "#{path}/#{framework}" }
  40      yield framework
  41    end
  42  end


Switchtower recipe code:
   1  
   2  set :rails_version, 3517
   3  desc "Checks out rails rev ##{rails_version}"
   4  task :after_symlink do
   5    run <<-CMD
   6      cd #{current_release} &&
   7      rake init REVISION=#{rails_version} RAILS_PATH=/home/username/projects/rails
   8    CMD
   9  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:
   1  set :rails_version, 2871


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


init task:
   1  desc "Performs an export of the rails trunk"
   2  task :init do
   3    return unless ENV['SHARED_PATH'] and ENV['CURRENT_RELEASE']
   4    puts 'copying files...'
   5    cp "#{ENV['SHARED_PATH']}/database.yml",  "#{ENV['CURRENT_RELEASE']}/config"
   6    cp "#{ENV['SHARED_PATH']}/dispatch.fcgi", "#{ENV['CURRENT_RELEASE']}/public"
   7  
   8    puts 'setting permissions...'
   9    chmod 0600, "#{ENV['CURRENT_RELEASE']}/config/database.yml"
  10    chmod 0700, "#{ENV['CURRENT_RELEASE']}/public/dispatch.fcgi"
  11    
  12    checkout_path = "#{ENV['SHARED_PATH']}/rails/trunk"
  13    export_path = "#{ENV['SHARED_PATH']}/rails/rev_#{ENV['REVISION']}"
  14    symlink_path = "#{ENV['CURRENT_RELEASE']}/vendor/rails"
  15  
  16    # do we need to checkout the file?
  17    unless File.exists?(checkout_path)
  18      puts 'setting up rails trunk'    
  19      get_framework_for checkout_path do |framework|
  20        system "svn co http://dev.rubyonrails.org/svn/rails/trunk/#{framework}/lib #{checkout_path}/#{framework}/lib --quiet"
  21      end
  22    end
  23  
  24    # do we need to export the revision?
  25    unless File.exists?(export_path)
  26      puts "setting up rails rev #{ENV['REVISION']}"
  27      get_framework_for export_path, symlink_path do |framework|
  28        system "svn up #{checkout_path}/#{framework}/lib -r #{ENV['REVISION']} --quiet"
  29        system "svn export #{checkout_path}/#{framework}/lib #{export_path}/#{framework}/lib"
  30      end
  31    end
  32  
  33    puts 'linking rails'
  34    rm_rf   "vendor/rails"
  35    mkdir_p "vendor/rails"
  36  
  37    get_framework_for checkout_path, export_path, symlink_path do |framework|
  38      ln_s File.expand_path("#{export_path}/#{framework}/lib"), "#{ENV['CURRENT_RELEASE']}/vendor/rails/#{framework}/lib"
  39    end
  40  end
  41  
  42  def get_framework_for(*paths)
  43    %w( railties actionpack activerecord actionmailer activesupport actionwebservice ).each do |framework|
  44      paths.each { |path| mkdir_p "#{path}/#{framework}" }
  45      yield framework
  46    end
  47  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:

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

manual reaping of fcgi processes

These are the signals that SwitchTower's reaper uses:

   1  # reload
   2  kill -s HUP @pid
   3  
   4  # graceful
   5  kill -s TERM @pid
   6  
   7  # kill
   8  kill -9 @pid
   9  
  10  # usr1
  11  kill -s USR1 @pid

checkout rails and apply patches with a rake task

see my blog post about this...

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

   1  rake rails_export VERSION=2000


Here is a SwitchTower recipe to perform this action automatically:

   1  
   2  set :rails_version, '2031' # custom ST variable
   3  
   4  desc "Checks out rails rev ##{rails_version}"
   5  task :after_symlink do
   6    run "cd #{current_release} && rake rails_export VERSION=#{rails_version}"
   7  end
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS