public
Description: Remote multi-server automation tool. This repository is no longer being actively maintained. Please ask on the mailing list to find someone who has a well-maintained fork. Thanks!
Homepage: http://www.capify.org
Clone URL: git://github.com/jamis/capistrano.git
jamis (author)
Fri Feb 13 19:05:25 -0800 2009
commit  2ce539d87c928f41d82f7bfda84e228d3948d82b
tree    7b5964513a8a02c39f1dfd2216ec8b053926007a
parent  42ff8b9e7dd9ec1c1de59b8f7dc0c0461cc2741b
capistrano / lib / capistrano / recipes / deploy.rb
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 1 require 'yaml'
2 require 'capistrano/recipes/deploy/scm'
3 require 'capistrano/recipes/deploy/strategy'
4
cdb38cd2 » jamis 2007-07-21 Make sure variables are con... 5 def _cset(name, *args, &block)
6 unless exists?(name)
7 set(name, *args, &block)
8 end
9 end
10
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 11 # =========================================================================
12 # These variables MUST be set in the client capfiles. If they are not set,
13 # the deploy will fail with an error.
14 # =========================================================================
15
cdb38cd2 » jamis 2007-07-21 Make sure variables are con... 16 _cset(:application) { abort "Please specify the name of your application, set :application, 'foo'" }
17 _cset(:repository) { abort "Please specify the repository that houses your application's code, set :repository, 'foo'" }
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 18
19 # =========================================================================
20 # These variables may be set in the client capfile if their default values
21 # are not sufficient.
22 # =========================================================================
23
cdb38cd2 » jamis 2007-07-21 Make sure variables are con... 24 _cset :scm, :subversion
25 _cset :deploy_via, :checkout
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 26
cdb38cd2 » jamis 2007-07-21 Make sure variables are con... 27 _cset(:deploy_to) { "/u/apps/#{application}" }
28 _cset(:revision) { source.head }
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 29
30 # =========================================================================
31 # These variables should NOT be changed unless you are very confident in
32 # what you are doing. Make sure you understand all the implications of your
33 # changes if you do decide to muck with these!
34 # =========================================================================
35
cdb38cd2 » jamis 2007-07-21 Make sure variables are con... 36 _cset(:source) { Capistrano::Deploy::SCM.new(scm, self) }
27d91a7d » Jon Evans 2008-09-24 locally executed commands a... 37 _cset(:real_revision) { source.local.query_revision(revision) { |cmd| with_env("LC_ALL", "C") { run_locally(cmd) } } }
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 38
cdb38cd2 » jamis 2007-07-21 Make sure variables are con... 39 _cset(:strategy) { Capistrano::Deploy::Strategy.new(deploy_via, self) }
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 40
cdb38cd2 » jamis 2007-07-21 Make sure variables are con... 41 _cset(:release_name) { set :deploy_timestamped, true; Time.now.utc.strftime("%Y%m%d%H%M%S") }
d257a6c1 » jamis 2007-08-30 Add version_dir, current_di... 42
d9d46ceb » jamis 2007-08-31 set the variables using the... 43 _cset :version_dir, "releases"
44 _cset :shared_dir, "shared"
e70fa828 » jamis 2008-08-19 Add :shared_children variab... 45 _cset :shared_children, %w(system log pids)
d9d46ceb » jamis 2007-08-31 set the variables using the... 46 _cset :current_dir, "current"
d257a6c1 » jamis 2007-08-30 Add version_dir, current_di... 47
48 _cset(:releases_path) { File.join(deploy_to, version_dir) }
49 _cset(:shared_path) { File.join(deploy_to, shared_dir) }
50 _cset(:current_path) { File.join(deploy_to, current_dir) }
cdb38cd2 » jamis 2007-07-21 Make sure variables are con... 51 _cset(:release_path) { File.join(releases_path, release_name) }
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 52
8cc60c70 » skwp 2008-08-20 Made :releases ordering by ... 53 _cset(:releases) { capture("ls -xt #{releases_path}").split.reverse }
cdb38cd2 » jamis 2007-07-21 Make sure variables are con... 54 _cset(:current_release) { File.join(releases_path, releases.last) }
c68c9d98 » mattmatt 2008-11-25 Make previous_release retur... 55 _cset(:previous_release) { releases.length > 1 ? File.join(releases_path, releases[-2]) : nil }
09ad893c » jamis 2007-03-26 namespaces are great, but b... 56
cdb38cd2 » jamis 2007-07-21 Make sure variables are con... 57 _cset(:current_revision) { capture("cat #{current_path}/REVISION").chomp }
58 _cset(:latest_revision) { capture("cat #{current_release}/REVISION").chomp }
59 _cset(:previous_revision) { capture("cat #{previous_release}/REVISION").chomp }
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 60
cdb38cd2 » jamis 2007-07-21 Make sure variables are con... 61 _cset(:run_method) { fetch(:use_sudo, true) ? :sudo : :run }
815ee2bd » jamis 2007-03-29 put the use_sudo toggle bac... 62
b7493674 » jamis 2007-06-16 Make sure symlink and final... 63 # some tasks, like symlink, need to always point at the latest release, but
64 # they can also (occassionally) be called standalone. In the standalone case,
65 # the timestamped release_path will be inaccurate, since the directory won't
66 # actually exist. This variable lets tasks like symlink work either in the
67 # standalone case, or during deployment.
cdb38cd2 » jamis 2007-07-21 Make sure variables are con... 68 _cset(:latest_release) { exists?(:deploy_timestamped) ? release_path : current_release }
b7493674 » jamis 2007-06-16 Make sure symlink and final... 69
dae044f5 » jamis 2007-03-31 Pull the server selection c... 70 # =========================================================================
12feba43 » jamis 2007-04-02 allow custom dependencies t... 71 # These are helper methods that will be available to your recipes.
72 # =========================================================================
73
74 # Auxiliary helper method for the `deploy:check' task. Lets you set up your
75 # own dependencies.
76 def depend(location, type, *args)
77 deps = fetch(:dependencies, {})
78 deps[location] ||= {}
79 deps[location][type] ||= []
80 deps[location][type] << args
81 set :dependencies, deps
82 end
83
67595be1 » jamis 2007-06-26 Set LC_ALL=C before queryin... 84 # Temporarily sets an environment variable, yields to a block, and restores
85 # the value when it is done.
86 def with_env(name, value)
87 saved, ENV[name] = ENV[name], value
88 yield
89 ensure
90 ENV[name] = saved
91 end
92
27d91a7d » Jon Evans 2008-09-24 locally executed commands a... 93 # logs the command then executes it locally.
94 # returns the command output as a string
95 def run_locally(cmd)
96 logger.trace "executing locally: #{cmd.inspect}" if logger
97 `#{cmd}`
98 end
99
19e6e3b9 » jamis 2008-05-24 Fix deployment recipes to u... 100 # If a command is given, this will try to execute the given command, as
101 # described below. Otherwise, it will return a string for use in embedding in
102 # another command, for executing that command as described below.
103 #
552a4924 » jamis 2008-04-30 Make deploy:setup obey the ... 104 # If :run_method is :sudo (or :use_sudo is true), this executes the given command
41c0da75 » jamis 2008-06-05 Only use :runner for tasks ... 105 # via +sudo+. Otherwise is uses +run+. If :as is given as a key, it will be
106 # passed as the user to sudo as, if using sudo. If the :as key is not given,
107 # it will default to whatever the value of the :admin_runner variable is,
108 # which (by default) is unset.
109 #
110 # THUS, if you want to try to run something via sudo, and what to use the
111 # root user, you'd just to try_sudo('something'). If you wanted to try_sudo as
112 # someone else, you'd just do try_sudo('something', :as => "bob"). If you
113 # always wanted sudo to run as a particular user, you could do
114 # set(:admin_runner, "bob").
115 def try_sudo(*args)
116 options = args.last.is_a?(Hash) ? args.pop : {}
117 command = args.shift
118 raise ArgumentError, "too many arguments" if args.any?
119
120 as = options.fetch(:as, fetch(:admin_runner, nil))
552a4924 » jamis 2008-04-30 Make deploy:setup obey the ... 121 via = fetch(:run_method, :sudo)
19e6e3b9 » jamis 2008-05-24 Fix deployment recipes to u... 122 if command
123 invoke_command(command, :via => via, :as => as)
124 elsif via == :sudo
125 sudo(:as => as)
126 else
127 ""
128 end
552a4924 » jamis 2008-04-30 Make deploy:setup obey the ... 129 end
130
41c0da75 » jamis 2008-06-05 Only use :runner for tasks ... 131 # Same as sudo, but tries sudo with :as set to the value of the :runner
132 # variable (which defaults to "app").
133 def try_runner(*args)
134 options = args.last.is_a?(Hash) ? args.pop : {}
135 args << options.merge(:as => fetch(:runner, "app"))
136 try_sudo(*args)
137 end
138
12feba43 » jamis 2007-04-02 allow custom dependencies t... 139 # =========================================================================
dae044f5 » jamis 2007-03-31 Pull the server selection c... 140 # These are the tasks that are available to help with deploying web apps,
141 # and specifically, Rails applications. You can have cap give you a summary
142 # of them with `cap -T'.
143 # =========================================================================
144
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 145 namespace :deploy do
0e774ac0 » jamis 2007-03-30 Make the task description p... 146 desc <<-DESC
147 Deploys your project. This calls both `update' and `restart'. Note that \
148 this will generally only work for applications that have already been deployed \
7ae3dd9f » jamis 2007-07-10 Doc corrections. Fix depend... 149 once. For a "cold" deploy, you'll want to take a look at the `deploy:cold' \
0e774ac0 » jamis 2007-03-30 Make the task description p... 150 task, which handles the cold start specifically.
151 DESC
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 152 task :default do
09ad893c » jamis 2007-03-26 namespaces are great, but b... 153 update
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 154 restart
155 end
156
0e774ac0 » jamis 2007-03-30 Make the task description p... 157 desc <<-DESC
158 Prepares one or more servers for deployment. Before you can use any \
159 of the Capistrano deployment tasks with your project, you will need to \
208581ae » jamis 2008-02-20 Fix incorrect reference to ... 160 make sure all of your servers have been prepared with `cap deploy:setup'. When \
0e774ac0 » jamis 2007-03-30 Make the task description p... 161 you add a new server to your cluster, you can easily run the setup task \
162 on just that server by specifying the HOSTS environment variable:
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 163
208581ae » jamis 2008-02-20 Fix incorrect reference to ... 164 $ cap HOSTS=new.server.com deploy:setup
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 165
0e774ac0 » jamis 2007-03-30 Make the task description p... 166 It is safe to run this task on servers that have already been set up; it \
167 will not destroy any deployed revisions or data.
168 DESC
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 169 task :setup, :except => { :no_release => true } do
170 dirs = [deploy_to, releases_path, shared_path]
e70fa828 » jamis 2008-08-19 Add :shared_children variab... 171 dirs += shared_children.map { |d| File.join(shared_path, d) }
19e6e3b9 » jamis 2008-05-24 Fix deployment recipes to u... 172 run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 173 end
174
0e774ac0 » jamis 2007-03-30 Make the task description p... 175 desc <<-DESC
176 Copies your project and updates the symlink. It does this in a \
177 transaction, so that if either `update_code' or `symlink' fail, all \
178 changes made to the remote servers will be rolled back, leaving your \
179 system in the same state it was in before `update' was invoked. Usually, \
180 you will want to call `deploy' instead of `update', but `update' can be \
181 handy if you want to deploy, but not immediately restart your application.
182 DESC
09ad893c » jamis 2007-03-26 namespaces are great, but b... 183 task :update do
184 transaction do
185 update_code
186 symlink
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 187 end
09ad893c » jamis 2007-03-26 namespaces are great, but b... 188 end
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 189
0e774ac0 » jamis 2007-03-30 Make the task description p... 190 desc <<-DESC
191 Copies your project to the remote servers. This is the first stage \
192 of any deployment; moving your updated code and assets to the deployment \
193 servers. You will rarely call this task directly, however; instead, you \
194 should call the `deploy' task (to do a complete deploy) or the `update' \
195 task (if you want to perform the `restart' task separately).
196
197 You will need to make sure you set the :scm variable to the source \
198 control software you are using (it defaults to :subversion), and the \
199 :deploy_via variable to the strategy you want to use to deploy (it \
200 defaults to :checkout).
201 DESC
09ad893c » jamis 2007-03-26 namespaces are great, but b... 202 task :update_code, :except => { :no_release => true } do
203 on_rollback { run "rm -rf #{release_path}; true" }
204 strategy.deploy!
205 finalize_update
206 end
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 207
0e774ac0 » jamis 2007-03-30 Make the task description p... 208 desc <<-DESC
209 [internal] Touches up the released code. This is called by update_code \
210 after the basic deploy finishes. It assumes a Rails project was deployed, \
211 so if you are deploying something else, you may want to override this \
212 task with your own environment's requirements.
213
214 This task will make the release group-writable (if the :group_writable \
215 variable is set to true, which is the default). It will then set up \
216 symlinks to the shared directory for the log, system, and tmp/pids \
217 directories, and will lastly touch all assets in public/images, \
218 public/stylesheets, and public/javascripts so that the times are \
5d1d9d9d » jtrupiano 2008-06-07 Added a :normalize_asset_ti... 219 consistent (so that asset timestamping works). This touch process \
220 is only carried out if the :normalize_asset_timestamps variable is \
221 set to true, which is the default.
0e774ac0 » jamis 2007-03-30 Make the task description p... 222 DESC
09ad893c » jamis 2007-03-26 namespaces are great, but b... 223 task :finalize_update, :except => { :no_release => true } do
b7493674 » jamis 2007-06-16 Make sure symlink and final... 224 run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
09ad893c » jamis 2007-03-26 namespaces are great, but b... 225
ef0c790b » jamis 2007-05-31 Make sure all directories e... 226 # mkdir -p is making sure that the directories are there for some SCM's that don't
227 # save empty folders
09ad893c » jamis 2007-03-26 namespaces are great, but b... 228 run <<-CMD
b7493674 » jamis 2007-06-16 Make sure symlink and final... 229 rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
230 mkdir -p #{latest_release}/public &&
231 mkdir -p #{latest_release}/tmp &&
232 ln -s #{shared_path}/log #{latest_release}/log &&
233 ln -s #{shared_path}/system #{latest_release}/public/system &&
234 ln -s #{shared_path}/pids #{latest_release}/tmp/pids
09ad893c » jamis 2007-03-26 namespaces are great, but b... 235 CMD
236
5d1d9d9d » jtrupiano 2008-06-07 Added a :normalize_asset_ti... 237 if fetch(:normalize_asset_timestamps, true)
238 stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
239 asset_paths = %w(images stylesheets javascripts).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
240 run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
241 end
09ad893c » jamis 2007-03-26 namespaces are great, but b... 242 end
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 243
0e774ac0 » jamis 2007-03-30 Make the task description p... 244 desc <<-DESC
7ae3dd9f » jamis 2007-07-10 Doc corrections. Fix depend... 245 Updates the symlink to the most recently deployed version. Capistrano works \
246 by putting each new release of your application in its own directory. When \
b7493674 » jamis 2007-06-16 Make sure symlink and final... 247 you deploy a new version, this task's job is to update the `current' symlink \
0e774ac0 » jamis 2007-03-30 Make the task description p... 248 to point at the new version. You will rarely need to call this task \
249 directly; instead, use the `deploy' task (which performs a complete \
250 deploy, including `restart') or the 'update' task (which does everything \
251 except `restart').
252 DESC
09ad893c » jamis 2007-03-26 namespaces are great, but b... 253 task :symlink, :except => { :no_release => true } do
06daaf71 » jamis 2008-11-25 Rollback of deploy:symlink ... 254 on_rollback do
907d9073 » jamis 2008-11-25 cleanup some conditions now... 255 if previous_release
06daaf71 » jamis 2008-11-25 Rollback of deploy:symlink ... 256 run "rm -f #{current_path}; ln -s #{previous_release} #{current_path}; true"
257 else
258 logger.important "no previous release to rollback to, rollback of symlink skipped"
259 end
260 end
261
b7493674 » jamis 2007-06-16 Make sure symlink and final... 262 run "rm -f #{current_path} && ln -s #{latest_release} #{current_path}"
09ad893c » jamis 2007-03-26 namespaces are great, but b... 263 end
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 264
0e774ac0 » jamis 2007-03-30 Make the task description p... 265 desc <<-DESC
266 Copy files to the currently deployed version. This is useful for updating \
267 files piecemeal, such as when you need to quickly deploy only a single \
268 file. Some files, such as updated templates, images, or stylesheets, \
269 might not require a full deploy, and especially in emergency situations \
270 it can be handy to just push the updates to production, quickly.
271
272 To use this task, specify the files and directories you want to copy as a \
273 comma-delimited list in the FILES environment variable. All directories \
274 will be processed recursively, with all files being pushed to the \
919f8685 » jamis 2008-06-05 Make deploy:upload use the ... 275 deployment servers.
0e774ac0 » jamis 2007-03-30 Make the task description p... 276
277 $ cap deploy:upload FILES=templates,controller.rb
919f8685 » jamis 2008-06-05 Make deploy:upload use the ... 278
279 Dir globs are also supported:
280
281 $ cap deploy:upload FILES='config/apache/*.conf'
0e774ac0 » jamis 2007-03-30 Make the task description p... 282 DESC
aab2ea25 » jamis 2007-03-28 Add deploy:check test for v... 283 task :upload, :except => { :no_release => true } do
919f8685 » jamis 2008-06-05 Make deploy:upload use the ... 284 files = (ENV["FILES"] || "").split(",").map { |f| Dir[f.strip] }.flatten
285 abort "Please specify at least one file or directory to update (via the FILES environment variable)" if files.empty?
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 286
919f8685 » jamis 2008-06-05 Make deploy:upload use the ... 287 files.each { |file| top.upload(file, File.join(current_path, file)) }
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 288 end
289
0e774ac0 » jamis 2007-03-30 Make the task description p... 290 desc <<-DESC
291 Restarts your application. This works by calling the script/process/reaper \
db1a8509 » jamis 2007-08-30 use the :runner variable to... 292 script under the current path.
293
294 By default, this will be invoked via sudo as the `app' user. If \
295 you wish to run it as a different user, set the :runner variable to \
296 that user. If you are in an environment where you can't use sudo, set \
297 the :use_sudo variable to false:
298
0e774ac0 » jamis 2007-03-30 Make the task description p... 299 set :use_sudo, false
300 DESC
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 301 task :restart, :roles => :app, :except => { :no_release => true } do
41c0da75 » jamis 2008-06-05 Only use :runner for tasks ... 302 try_runner "#{current_path}/script/process/reaper"
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 303 end
304
0e5c38b0 » jamis 2008-08-21 make rollbacks work with pr... 305 namespace :rollback do
306 desc <<-DESC
307 [internal] Points the current symlink at the previous revision.
308 This is called by the rollback sequence, and should rarely (if
309 ever) need to be called directly.
310 DESC
311 task :revision, :except => { :no_release => true } do
907d9073 » jamis 2008-11-25 cleanup some conditions now... 312 if previous_release
0e5c38b0 » jamis 2008-08-21 make rollbacks work with pr... 313 run "rm #{current_path}; ln -s #{previous_release} #{current_path}"
907d9073 » jamis 2008-11-25 cleanup some conditions now... 314 else
315 abort "could not rollback the code because there is no prior release"
0e5c38b0 » jamis 2008-08-21 make rollbacks work with pr... 316 end
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 317 end
318
0e5c38b0 » jamis 2008-08-21 make rollbacks work with pr... 319 desc <<-DESC
320 [internal] Removes the most recently deployed release.
321 This is called by the rollback sequence, and should rarely
322 (if ever) need to be called directly.
323 DESC
324 task :cleanup, :except => { :no_release => true } do
325 run "if [ `readlink #{current_path}` != #{current_release} ]; then rm -rf #{current_release}; fi"
326 end
327
328 desc <<-DESC
329 Rolls back to the previously deployed version. The `current' symlink will \
330 be updated to point at the previously deployed version, and then the \
331 current release will be removed from the servers. You'll generally want \
332 to call `rollback' instead, as it performs a `restart' as well.
333 DESC
334 task :code, :except => { :no_release => true } do
335 revision
336 cleanup
337 end
338
339 desc <<-DESC
340 Rolls back to a previous version and restarts. This is handy if you ever \
341 discover that you've deployed a lemon; `cap rollback' and you're right \
342 back where you were, on the previously deployed version.
343 DESC
344 task :default do
345 revision
346 restart
347 cleanup
348 end
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 349 end
350
0e774ac0 » jamis 2007-03-30 Make the task description p... 351 desc <<-DESC
352 Run the migrate rake task. By default, it runs this in most recently \
353 deployed version of the app. However, you can specify a different release \
354 via the migrate_target variable, which must be one of :latest (for the \
355 default behavior), or :current (for the release indicated by the \
7ae3dd9f » jamis 2007-07-10 Doc corrections. Fix depend... 356 `current' symlink). Strings will work for those values instead of symbols, \
357 too. You can also specify additional environment variables to pass to rake \
358 via the migrate_env variable. Finally, you can specify the full path to the \
0964dc18 » jamis 2007-04-26 Make sure deploy:cold also ... 359 rake executable by setting the rake variable. The defaults are:
0e774ac0 » jamis 2007-03-30 Make the task description p... 360
361 set :rake, "rake"
362 set :rails_env, "production"
363 set :migrate_env, ""
364 set :migrate_target, :latest
365 DESC
9a6d2fb7 » jamis 2007-03-26 make the compat script more... 366 task :migrate, :roles => :db, :only => { :primary => true } do
367 rake = fetch(:rake, "rake")
368 rails_env = fetch(:rails_env, "production")
369 migrate_env = fetch(:migrate_env, "")
370 migrate_target = fetch(:migrate_target, :latest)
371
372 directory = case migrate_target.to_sym
373 when :current then current_path
374 when :latest then current_release
375 else raise ArgumentError, "unknown migration target #{migrate_target.inspect}"
376 end
377
378 run "cd #{directory}; #{rake} RAILS_ENV=#{rails_env} #{migrate_env} db:migrate"
379 end
380
0e774ac0 » jamis 2007-03-30 Make the task description p... 381 desc <<-DESC
382 Deploy and run pending migrations. This will work similarly to the \
383 `deploy' task, but will also run any pending migrations (via the \
384 `deploy:migrate' task) prior to updating the symlink. Note that the \
385 update in this case it is not atomic, and transactions are not used, \
386 because migrations are not guaranteed to be reversible.
387 DESC
3a5724a3 » jamis 2007-03-29 add deploy:app:start, deplo... 388 task :migrations do
9a6d2fb7 » jamis 2007-03-26 make the compat script more... 389 set :migrate_target, :latest
390 update_code
391 migrate
392 symlink
393 restart
394 end
395
0e774ac0 » jamis 2007-03-30 Make the task description p... 396 desc <<-DESC
397 Clean up old releases. By default, the last 5 releases are kept on each \
398 server (though you can change this with the keep_releases variable). All \
399 other deployed revisions are removed from the servers. By default, this \
400 will use sudo to clean up the old releases, but if sudo is not available \
401 for your environment, set the :use_sudo variable to false instead.
402 DESC
9a6d2fb7 » jamis 2007-03-26 make the compat script more... 403 task :cleanup, :except => { :no_release => true } do
404 count = fetch(:keep_releases, 5).to_i
405 if count >= releases.length
406 logger.important "no old releases to clean up"
407 else
408 logger.info "keeping #{count} of #{releases.length} deployed releases"
409
410 directories = (releases - releases.last(count)).map { |release|
411 File.join(releases_path, release) }.join(" ")
412
552a4924 » jamis 2008-04-30 Make deploy:setup obey the ... 413 try_sudo "rm -rf #{directories}"
9a6d2fb7 » jamis 2007-03-26 make the compat script more... 414 end
415 end
416
0e774ac0 » jamis 2007-03-30 Make the task description p... 417 desc <<-DESC
418 Test deployment dependencies. Checks things like directory permissions, \
419 necessary utilities, and so forth, reporting on the things that appear to \
420 be incorrect or missing. This is good for making sure a deploy has a \
421 chance of working before you actually run `cap deploy'.
12feba43 » jamis 2007-04-02 allow custom dependencies t... 422
423 You can define your own dependencies, as well, using the `depend' method:
424
425 depend :remote, :gem, "tzinfo", ">=0.3.3"
426 depend :local, :command, "svn"
427 depend :remote, :directory, "/u/depot/files"
0e774ac0 » jamis 2007-03-30 Make the task description p... 428 DESC
aab2ea25 » jamis 2007-03-28 Add deploy:check test for v... 429 task :check, :except => { :no_release => true } do
430 dependencies = strategy.check!
12feba43 » jamis 2007-04-02 allow custom dependencies t... 431
432 other = fetch(:dependencies, {})
433 other.each do |location, types|
434 types.each do |type, calls|
435 if type == :gem
436 dependencies.send(location).command(fetch(:gem_command, "gem")).or("`gem' command could not be found. Try setting :gem_command")
437 end
438
439 calls.each do |args|
440 dependencies.send(location).send(type, *args)
441 end
442 end
443 end
444
aab2ea25 » jamis 2007-03-28 Add deploy:check test for v... 445 if dependencies.pass?
446 puts "You appear to have all necessary dependencies installed"
447 else
448 puts "The following dependencies failed. Please check them and try again:"
449 dependencies.reject { |d| d.pass? }.each do |d|
450 puts "--> #{d.message}"
451 end
8f7556bd » jamis 2007-07-06 Make sure deploy:check abor... 452 abort
aab2ea25 » jamis 2007-03-28 Add deploy:check test for v... 453 end
454 end
455
0e774ac0 » jamis 2007-03-30 Make the task description p... 456 desc <<-DESC
457 Deploys and starts a `cold' application. This is useful if you have not \
458 deployed your application before, or if your application is (for some \
0964dc18 » jamis 2007-04-26 Make sure deploy:cold also ... 459 other reason) not currently running. It will deploy the code, run any \
7ae3dd9f » jamis 2007-07-10 Doc corrections. Fix depend... 460 pending migrations, and then instead of invoking `deploy:restart', it will \
2b5d913f » jamis 2007-05-07 Kill the "deploy:app" names... 461 invoke `deploy:start' to fire up the application servers.
0e774ac0 » jamis 2007-03-30 Make the task description p... 462 DESC
3a5724a3 » jamis 2007-03-29 add deploy:app:start, deplo... 463 task :cold do
464 update
0964dc18 » jamis 2007-04-26 Make sure deploy:cold also ... 465 migrate
2b5d913f » jamis 2007-05-07 Kill the "deploy:app" names... 466 start
3a5724a3 » jamis 2007-03-29 add deploy:app:start, deplo... 467 end
468
2b5d913f » jamis 2007-05-07 Kill the "deploy:app" names... 469 desc <<-DESC
470 Start the application servers. This will attempt to invoke a script \
471 in your application called `script/spin', which must know how to start \
472 your application listeners. For Rails applications, you might just have \
473 that script invoke `script/process/spawner' with the appropriate \
474 arguments.
475
476 By default, the script will be executed via sudo as the `app' user. If \
477 you wish to run it as a different user, set the :runner variable to \
478 that user. If you are in an environment where you can't use sudo, set \
479 the :use_sudo variable to false.
480 DESC
481 task :start, :roles => :app do
41c0da75 » jamis 2008-06-05 Only use :runner for tasks ... 482 run "cd #{current_path} && #{try_runner} nohup script/spin"
2b5d913f » jamis 2007-05-07 Kill the "deploy:app" names... 483 end
3a5724a3 » jamis 2007-03-29 add deploy:app:start, deplo... 484
2b5d913f » jamis 2007-05-07 Kill the "deploy:app" names... 485 desc <<-DESC
486 Stop the application servers. This will call script/process/reaper for \
487 both the spawner process, and all of the application processes it has \
488 spawned. As such, it is fairly Rails specific and may need to be \
489 overridden for other systems.
490
491 By default, the script will be executed via sudo as the `app' user. If \
492 you wish to run it as a different user, set the :runner variable to \
493 that user. If you are in an environment where you can't use sudo, set \
494 the :use_sudo variable to false.
495 DESC
496 task :stop, :roles => :app do
41c0da75 » jamis 2008-06-05 Only use :runner for tasks ... 497 run "if [ -f #{current_path}/tmp/pids/dispatch.spawner.pid ]; then #{try_runner} #{current_path}/script/process/reaper -a kill -r dispatch.spawner.pid; fi"
498 try_runner "#{current_path}/script/process/reaper -a kill"
3a5724a3 » jamis 2007-03-29 add deploy:app:start, deplo... 499 end
500
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 501 namespace :pending do
0e774ac0 » jamis 2007-03-30 Make the task description p... 502 desc <<-DESC
503 Displays the `diff' since your last deploy. This is useful if you want \
504 to examine what changes are about to be deployed. Note that this might \
505 not be supported on all SCM's.
506 DESC
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 507 task :diff, :except => { :no_release => true } do
241ebb08 » jamis 2007-05-08 Allow (e.g.) scm_command an... 508 system(source.local.diff(current_revision))
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 509 end
510
0e774ac0 » jamis 2007-03-30 Make the task description p... 511 desc <<-DESC
512 Displays the commits since your last deploy. This is good for a summary \
513 of the changes that have occurred since the last deploy. Note that this \
514 might not be supported on all SCM's.
515 DESC
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 516 task :default, :except => { :no_release => true } do
577fffeb » jamis 2007-10-13 Fix deploy:pending to query... 517 from = source.next_revision(current_revision)
518 system(source.local.log(from))
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 519 end
520 end
9a6d2fb7 » jamis 2007-03-26 make the compat script more... 521
522 namespace :web do
0e774ac0 » jamis 2007-03-30 Make the task description p... 523 desc <<-DESC
524 Present a maintenance page to visitors. Disables your application's web \
525 interface by writing a "maintenance.html" file to each web server. The \
526 servers must be configured to detect the presence of this file, and if \
527 it is present, always display it instead of performing the request.
528
529 By default, the maintenance page will just say the site is down for \
530 "maintenance", and will be back "shortly", but you can customize the \
531 page by specifying the REASON and UNTIL environment variables:
532
533 $ cap deploy:web:disable \\
d8d46ee8 » jamis 2007-06-05 fix typo in web:disable exa... 534 REASON="hardware upgrade" \\
0e774ac0 » jamis 2007-03-30 Make the task description p... 535 UNTIL="12pm Central Time"
536
537 Further customization will require that you write your own task.
538 DESC
9a6d2fb7 » jamis 2007-03-26 make the compat script more... 539 task :disable, :roles => :web, :except => { :no_release => true } do
540 require 'erb'
541 on_rollback { run "rm #{shared_path}/system/maintenance.html" }
542
543 reason = ENV['REASON']
544 deadline = ENV['UNTIL']
545
546 template = File.read(File.join(File.dirname(__FILE__), "templates", "maintenance.rhtml"))
547 result = ERB.new(template).result(binding)
548
549 put result, "#{shared_path}/system/maintenance.html", :mode => 0644
550 end
551
0e774ac0 » jamis 2007-03-30 Make the task description p... 552 desc <<-DESC
553 Makes the application web-accessible again. Removes the \
554 "maintenance.html" page generated by deploy:web:disable, which (if your \
555 web servers are configured correctly) will make your application \
556 web-accessible again.
557 DESC
9a6d2fb7 » jamis 2007-03-26 make the compat script more... 558 task :enable, :roles => :web, :except => { :no_release => true } do
559 run "rm #{shared_path}/system/maintenance.html"
560 end
561 end
14770c8a » jamis 2007-03-26 Add (opt-in) deployment sys... 562 end