Skip to content

Instantly share code, notes, and snippets.

Created July 22, 2011 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1100338 to your computer and use it in GitHub Desktop.
Save anonymous/1100338 to your computer and use it in GitHub Desktop.
Rails deploy raketask for Windows - using linkd (windows symlinks)
live_config = {
:app_path => 'd:\rails\live',
:services => ['Apache2.2','mongrel_live1','mongrel_live2'],
:svn_repos => 'file:///d:/svnrepos/yourrepos/trunk',
:config_files_to_keep => ['backgroundrb.yml', 'database.yml', 'mongrel_cluster.yml', 'ferret_server.yml']
}
test_config = {
:app_path => 'd:\rails\test',
:services => ['Apache2.2','mongrel_test1'],
:svn_repos => 'file:///d:/svnrepos/yourrepos/trunk',
:config_files_to_keep => ['backgroundrb.yml', 'database.yml', 'mongrel_cluster.yml', 'ferret_server.yml']
}
namespace :restart do
desc "Restart Services For Live\n (By default backgroundrb is left running;\n Optional param: backgroundrb=restart)\n Optional param: with_migrate=yes)"
task :live do
#Need to add backgroundrb if requested to restart this as well
live_config[:backgroundrb] = 'BackgrounDRb' if ENV['backgroundrb'] == "restart"
WinDeploy.restart_services(live_config[:services], live_config[:backgroundrb])
end
desc "Restart Services For Test\n (currently no backgroundrb service for test)"
task :test do
WinDeploy.restart_services(test_config[:services], test_config[:backgroundrb])
end
end
namespace :start do
desc "Start Services For Live\n (By default backgroundrb is left running;\n Optional param: backgroundrb=restart)\n Optional param: with_migrate=yes)"
task :live do
#Need to add backgroundrb if requested to restart this as well
live_config[:backgroundrb] = 'BackgrounDRb' if ENV['backgroundrb'] == "restart"
WinDeploy.start_services(live_config[:services], live_config[:backgroundrb])
end
desc "Start Services For Test\n (currently no backgroundrb service for test)"
task :test do
WinDeploy.start_services(test_config[:services], test_config[:backgroundrb])
end
end
namespace :stop do
desc "Stop Services For Live\n (By default backgroundrb is left running;\n Optional param: backgroundrb=restart)\n Optional param: with_migrate=yes)"
task :live do
#Need to add backgroundrb if requested to restart this as well
live_config[:backgroundrb] = 'BackgrounDRb' if ENV['backgroundrb'] == "restart"
WinDeploy.stop_services(live_config[:services], live_config[:backgroundrb])
end
desc "Stop Services For Test\n (currently no backgroundrb service for test)"
task :test do
WinDeploy.stop_services(test_config[:services], test_config[:backgroundrb])
end
end
namespace :deploy do
task :to_live do
puts "Deploying to Live"
puts '---------------------'
#Need to add backgroundrb if requested to restart this as well
live_config[:backgroundrb] = 'BackgrounDRb' if ENV['backgroundrb'] == "restart"
live_config[:migrate] = true if ENV['with_migrate'] == "yes"
WinDeploy.deploy(live_config)
puts '---------------------'
puts "Deployment Completed"
end
desc "Deploy the current SVN trunk to Test\n (currently no backgroundrb service for test)"
task :to_test do
puts "Deploying to Test"
puts '---------------------'
WinDeploy.deploy(test_config)
puts '---------------------'
puts "Deployment Completed"
end
end
namespace :setup do
task :live do
puts "Setting Up Live Dir Structure"
puts '---------------------'
WinDeploy.setup(live_config)
puts '---------------------'
puts "Setup Completed"
end
task :test do
puts "Setting Up Test Dir Structure"
puts '---------------------'
WinDeploy.setup(test_config)
puts '---------------------'
puts "Setup Completed"
end
end
class WinDeploy
def self.setup(options)
current_path = "#{options[:app_path]}\\current"
config_path = "#{current_path}\\config"
releases_path = "#{options[:app_path]}\\releases"
shared_path = "#{options[:app_path]}\\shared"
mkdir("#{releases_path}\\init_setup")
`linkd #{current_path} #{releases_path}\\init_setup`
`linkd #{current_path}\\log #{shared_path}\\log`
mkdir("#{current_path}\\tmp")
`linkd #{current_path}\\tmp\\pids #{shared_path}\\pids`
`linkd #{current_path}\\index #{shared_path}\\index`
mkdir("#{current_path}\\public")
`linkd #{current_path}\\public\\system #{shared_path}\\system`
`linkd #{current_path}\\public\\cache #{shared_path}\\cache`
end
def self.deploy(options)
current_path = "#{options[:app_path]}\\current"
config_path = "#{current_path}\\config"
releases_path = "#{options[:app_path]}\\releases"
shared_path = "#{options[:app_path]}\\shared"
export_dir = Time.now.utc.strftime("%Y%m%d%H%M%S")
export_path = "#{releases_path}\\#{export_dir}"
#Copy existing config files to app root so that we dont lose the current db connection etc
config_files_to_copy = options[:config_files_to_keep].map{|file_name| "#{config_path}\\#{file_name}"}
cp(config_files_to_copy, options[:app_path])
#Export code to new timestamped directory
puts '1) Exporting from SVN'
puts '---------------------'
puts `svn export #{options[:svn_repos]} #{export_path}`
# Stop Running Services
puts '2) Stopping Services'
puts '---------------------'
self.stop_services(options[:services], options[:backgroundrb])
#Delete 'Current' hard-link using linkd /d
puts '3) Recreating current symlinks'
puts '---------------------'
#delete log, tmp & shared directory symlinks
`linkd #{current_path}\\log /d`
`linkd #{current_path}\\tmp\\pids /d`
`linkd #{current_path}\\index /d`
`linkd #{current_path}\\reports /d`
`linkd #{current_path}\\public\\system /d`
`linkd #{current_path}\\public\\cache /d`
#delete current directory symlink
`linkd #{current_path} /d`
#recreate current based on new release
`linkd #{current_path} #{export_path}`
#add in tmp & log symlinks
`linkd #{current_path}\\log #{shared_path}\\log`
#wont be a tmp directory from source so create it first or linkd will not work for the nested pids dir
mkdir("#{current_path}\\tmp")
`linkd #{current_path}\\tmp\\pids #{shared_path}\\pids`
`linkd #{current_path}\\index #{shared_path}\\index`
`linkd #{current_path}\\reports #{shared_path}\\reports`
`linkd #{current_path}\\public\\system #{shared_path}\\system`
`linkd #{current_path}\\public\\cache #{shared_path}\\cache`
#Copy over original config files
config_files_to_copy = options[:config_files_to_keep].map{|file_name| "#{options[:app_path]}\\#{file_name}"}
mv(config_files_to_copy, config_path)
if options[:migrate]
#Backup database
`#{options[:db_backup_batch_file]}` unless options[:db_backup_batch_file].nil?
#Do any migrations
`ruby #{options[:app_path]}\\rake db:migrate RAILS_ENV=production`
end
#Restart Services
puts '4) Restarting Services'
puts '---------------------'
#restart services in reverse order
self.start_services(options[:services], options[:backgroundrb])
end
def self.start_services(services, backgroundrb = nil)
services.reverse_each do |service|
puts "Starting: #{service}"
`net start \"#{service}\"`
end
#StartBackgrounDRb after all other services
unless backgroundrb.nil?
puts "Starting: #{backgroundrb}"
`net start \"#{backgroundrb}\"`
puts "*** NOTE: Please manually check that the backgroundrb service has restarted. ***"
end
end
def self.stop_services(services, backgroundrb = nil)
services.each do |service|
puts "Stopping: #{service}"
`net stop \"#{service}\"`
end
#Stop BackgrounDRb after all other services
unless backgroundrb.nil?
puts "Stopping: #{backgroundrb}"
`net stop \"#{backgroundrb}\"`
end
end
def self.restart_services(services = [], backgroundrb = nil)
self.stop_services(services, backgroundrb)
self.start_services(services, backgroundrb)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment