anonymous / rakefile.rb
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Rails deploy raketask for Windows - using linkd (windows symlinks)

View rakefile.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
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
Something went wrong with that request. Please try again.