dan-capistrano 2.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. data/CHANGELOG.rdoc +770 -0
  2. data/Manifest +104 -0
  3. data/README.rdoc +66 -0
  4. data/Rakefile +35 -0
  5. data/bin/cap +4 -0
  6. data/bin/capify +78 -0
  7. data/capistrano.gemspec +48 -0
  8. data/examples/sample.rb +14 -0
  9. data/lib/capistrano.rb +2 -0
  10. data/lib/capistrano/callback.rb +45 -0
  11. data/lib/capistrano/cli.rb +47 -0
  12. data/lib/capistrano/cli/execute.rb +84 -0
  13. data/lib/capistrano/cli/help.rb +125 -0
  14. data/lib/capistrano/cli/help.txt +75 -0
  15. data/lib/capistrano/cli/options.rb +224 -0
  16. data/lib/capistrano/cli/ui.rb +40 -0
  17. data/lib/capistrano/command.rb +283 -0
  18. data/lib/capistrano/configuration.rb +43 -0
  19. data/lib/capistrano/configuration/actions/file_transfer.rb +47 -0
  20. data/lib/capistrano/configuration/actions/inspect.rb +46 -0
  21. data/lib/capistrano/configuration/actions/invocation.rb +293 -0
  22. data/lib/capistrano/configuration/callbacks.rb +148 -0
  23. data/lib/capistrano/configuration/connections.rb +204 -0
  24. data/lib/capistrano/configuration/execution.rb +143 -0
  25. data/lib/capistrano/configuration/loading.rb +197 -0
  26. data/lib/capistrano/configuration/namespaces.rb +197 -0
  27. data/lib/capistrano/configuration/roles.rb +73 -0
  28. data/lib/capistrano/configuration/servers.rb +85 -0
  29. data/lib/capistrano/configuration/variables.rb +127 -0
  30. data/lib/capistrano/errors.rb +15 -0
  31. data/lib/capistrano/extensions.rb +57 -0
  32. data/lib/capistrano/logger.rb +59 -0
  33. data/lib/capistrano/processable.rb +53 -0
  34. data/lib/capistrano/recipes/compat.rb +32 -0
  35. data/lib/capistrano/recipes/deploy.rb +562 -0
  36. data/lib/capistrano/recipes/deploy/dependencies.rb +44 -0
  37. data/lib/capistrano/recipes/deploy/local_dependency.rb +54 -0
  38. data/lib/capistrano/recipes/deploy/remote_dependency.rb +105 -0
  39. data/lib/capistrano/recipes/deploy/scm.rb +19 -0
  40. data/lib/capistrano/recipes/deploy/scm/accurev.rb +169 -0
  41. data/lib/capistrano/recipes/deploy/scm/base.rb +196 -0
  42. data/lib/capistrano/recipes/deploy/scm/bzr.rb +83 -0
  43. data/lib/capistrano/recipes/deploy/scm/cvs.rb +152 -0
  44. data/lib/capistrano/recipes/deploy/scm/darcs.rb +85 -0
  45. data/lib/capistrano/recipes/deploy/scm/git.rb +278 -0
  46. data/lib/capistrano/recipes/deploy/scm/mercurial.rb +137 -0
  47. data/lib/capistrano/recipes/deploy/scm/none.rb +44 -0
  48. data/lib/capistrano/recipes/deploy/scm/perforce.rb +138 -0
  49. data/lib/capistrano/recipes/deploy/scm/subversion.rb +121 -0
  50. data/lib/capistrano/recipes/deploy/strategy.rb +19 -0
  51. data/lib/capistrano/recipes/deploy/strategy/base.rb +79 -0
  52. data/lib/capistrano/recipes/deploy/strategy/checkout.rb +20 -0
  53. data/lib/capistrano/recipes/deploy/strategy/copy.rb +210 -0
  54. data/lib/capistrano/recipes/deploy/strategy/export.rb +20 -0
  55. data/lib/capistrano/recipes/deploy/strategy/remote.rb +52 -0
  56. data/lib/capistrano/recipes/deploy/strategy/remote_cache.rb +56 -0
  57. data/lib/capistrano/recipes/deploy/templates/maintenance.rhtml +53 -0
  58. data/lib/capistrano/recipes/standard.rb +37 -0
  59. data/lib/capistrano/recipes/templates/maintenance.rhtml +53 -0
  60. data/lib/capistrano/recipes/upgrade.rb +33 -0
  61. data/lib/capistrano/role.rb +102 -0
  62. data/lib/capistrano/server_definition.rb +56 -0
  63. data/lib/capistrano/shell.rb +260 -0
  64. data/lib/capistrano/ssh.rb +99 -0
  65. data/lib/capistrano/task_definition.rb +70 -0
  66. data/lib/capistrano/transfer.rb +216 -0
  67. data/lib/capistrano/version.rb +18 -0
  68. data/setup.rb +1346 -0
  69. data/test/cli/execute_test.rb +132 -0
  70. data/test/cli/help_test.rb +165 -0
  71. data/test/cli/options_test.rb +317 -0
  72. data/test/cli/ui_test.rb +28 -0
  73. data/test/cli_test.rb +17 -0
  74. data/test/command_test.rb +286 -0
  75. data/test/configuration/actions/file_transfer_test.rb +61 -0
  76. data/test/configuration/actions/inspect_test.rb +65 -0
  77. data/test/configuration/actions/invocation_test.rb +224 -0
  78. data/test/configuration/callbacks_test.rb +220 -0
  79. data/test/configuration/connections_test.rb +349 -0
  80. data/test/configuration/execution_test.rb +175 -0
  81. data/test/configuration/loading_test.rb +132 -0
  82. data/test/configuration/namespace_dsl_test.rb +311 -0
  83. data/test/configuration/roles_test.rb +144 -0
  84. data/test/configuration/servers_test.rb +121 -0
  85. data/test/configuration/variables_test.rb +184 -0
  86. data/test/configuration_test.rb +88 -0
  87. data/test/deploy/local_dependency_test.rb +76 -0
  88. data/test/deploy/remote_dependency_test.rb +114 -0
  89. data/test/deploy/scm/accurev_test.rb +23 -0
  90. data/test/deploy/scm/base_test.rb +55 -0
  91. data/test/deploy/scm/git_test.rb +184 -0
  92. data/test/deploy/scm/mercurial_test.rb +129 -0
  93. data/test/deploy/scm/none_test.rb +35 -0
  94. data/test/deploy/strategy/copy_test.rb +258 -0
  95. data/test/extensions_test.rb +69 -0
  96. data/test/fixtures/cli_integration.rb +5 -0
  97. data/test/fixtures/config.rb +5 -0
  98. data/test/fixtures/custom.rb +3 -0
  99. data/test/logger_test.rb +123 -0
  100. data/test/role_test.rb +11 -0
  101. data/test/server_definition_test.rb +121 -0
  102. data/test/shell_test.rb +90 -0
  103. data/test/ssh_test.rb +104 -0
  104. data/test/task_definition_test.rb +101 -0
  105. data/test/transfer_test.rb +160 -0
  106. data/test/utils.rb +38 -0
  107. metadata +306 -0
@@ -0,0 +1,32 @@
1
+ # A collection of compatibility scripts, to ease the transition between
2
+ # Capistrano 1.x and Capistrano 2.x.
3
+
4
+ # Depends on the deployment system
5
+ load 'deploy'
6
+
7
+ map = { "diff_from_last_deploy" => "deploy:pending:diff",
8
+ "update" => "deploy:update",
9
+ "update_code" => "deploy:update_code",
10
+ "symlink" => "deploy:symlink",
11
+ "restart" => "deploy:restart",
12
+ "rollback" => "deploy:rollback",
13
+ "cleanup" => "deploy:cleanup",
14
+ "disable_web" => "deploy:web:disable",
15
+ "enable_web" => "deploy:web:enable",
16
+ "cold_deploy" => "deploy:cold",
17
+ "deploy_with_migrations" => "deploy:migrations" }
18
+
19
+ map.each do |old, new|
20
+ desc "DEPRECATED: See #{new}."
21
+ eval "task(#{old.inspect}) do
22
+ warn \"[DEPRECATED] `#{old}' is deprecated. Use `#{new}' instead.\"
23
+ find_and_execute_task(#{new.inspect})
24
+ end"
25
+ end
26
+
27
+ desc "DEPRECATED: See deploy:start."
28
+ task :spinner do
29
+ warn "[DEPRECATED] `spinner' is deprecated. Use `deploy:start' instead."
30
+ set :runner, fetch(:spinner_user, "app")
31
+ deploy.start
32
+ end
@@ -0,0 +1,562 @@
1
+ require 'yaml'
2
+ require 'capistrano/recipes/deploy/scm'
3
+ require 'capistrano/recipes/deploy/strategy'
4
+
5
+ def _cset(name, *args, &block)
6
+ unless exists?(name)
7
+ set(name, *args, &block)
8
+ end
9
+ end
10
+
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
+
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'" }
18
+
19
+ # =========================================================================
20
+ # These variables may be set in the client capfile if their default values
21
+ # are not sufficient.
22
+ # =========================================================================
23
+
24
+ _cset :scm, :subversion
25
+ _cset :deploy_via, :checkout
26
+
27
+ _cset(:deploy_to) { "/u/apps/#{application}" }
28
+ _cset(:revision) { source.head }
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
+
36
+ _cset(:source) { Capistrano::Deploy::SCM.new(scm, self) }
37
+ _cset(:real_revision) { source.local.query_revision(revision) { |cmd| with_env("LC_ALL", "C") { run_locally(cmd) } } }
38
+
39
+ _cset(:strategy) { Capistrano::Deploy::Strategy.new(deploy_via, self) }
40
+
41
+ _cset(:release_name) { set :deploy_timestamped, true; Time.now.utc.strftime("%Y%m%d%H%M%S") }
42
+
43
+ _cset :version_dir, "releases"
44
+ _cset :shared_dir, "shared"
45
+ _cset :shared_children, %w(system log pids)
46
+ _cset :current_dir, "current"
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) }
51
+ _cset(:release_path) { File.join(releases_path, release_name) }
52
+
53
+ _cset(:releases) { capture("ls -xt #{releases_path}").split.reverse }
54
+ _cset(:current_release) { File.join(releases_path, releases.last) }
55
+ _cset(:previous_release) { releases.length > 1 ? File.join(releases_path, releases[-2]) : nil }
56
+
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 }
60
+
61
+ _cset(:run_method) { fetch(:use_sudo, true) ? :sudo : :run }
62
+
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.
68
+ _cset(:latest_release) { exists?(:deploy_timestamped) ? release_path : current_release }
69
+
70
+ # =========================================================================
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
+
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
+
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
+
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
+ #
104
+ # If :run_method is :sudo (or :use_sudo is true), this executes the given command
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))
121
+ via = fetch(:run_method, :sudo)
122
+ if command
123
+ invoke_command(command, :via => via, :as => as)
124
+ elsif via == :sudo
125
+ sudo(:as => as)
126
+ else
127
+ ""
128
+ end
129
+ end
130
+
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
+
139
+ # =========================================================================
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
+
145
+ namespace :deploy do
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 \
149
+ once. For a "cold" deploy, you'll want to take a look at the `deploy:cold' \
150
+ task, which handles the cold start specifically.
151
+ DESC
152
+ task :default do
153
+ update
154
+ restart
155
+ end
156
+
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 \
160
+ make sure all of your servers have been prepared with `cap deploy:setup'. When \
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:
163
+
164
+ $ cap HOSTS=new.server.com deploy:setup
165
+
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
169
+ task :setup, :except => { :no_release => true } do
170
+ dirs = [deploy_to, releases_path, shared_path]
171
+ dirs += shared_children.map { |d| File.join(shared_path, d) }
172
+ run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
173
+ end
174
+
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
183
+ task :update do
184
+ transaction do
185
+ update_code
186
+ symlink
187
+ end
188
+ end
189
+
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
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
207
+
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 \
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.
222
+ DESC
223
+ task :finalize_update, :except => { :no_release => true } do
224
+ run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
225
+
226
+ # mkdir -p is making sure that the directories are there for some SCM's that don't
227
+ # save empty folders
228
+ run <<-CMD
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
235
+ CMD
236
+
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
242
+ end
243
+
244
+ desc <<-DESC
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 \
247
+ you deploy a new version, this task's job is to update the `current' symlink \
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
253
+ task :symlink, :except => { :no_release => true } do
254
+ on_rollback do
255
+ if previous_release
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
+
262
+ run "rm -f #{current_path} && ln -s #{latest_release} #{current_path}"
263
+ end
264
+
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 \
275
+ deployment servers.
276
+
277
+ $ cap deploy:upload FILES=templates,controller.rb
278
+
279
+ Dir globs are also supported:
280
+
281
+ $ cap deploy:upload FILES='config/apache/*.conf'
282
+ DESC
283
+ task :upload, :except => { :no_release => true } do
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?
286
+
287
+ files.each { |file| top.upload(file, File.join(current_path, file)) }
288
+ end
289
+
290
+ desc <<-DESC
291
+ Restarts your application. This works by calling the script/process/reaper \
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
+
299
+ set :use_sudo, false
300
+ DESC
301
+ task :restart, :roles => :app, :except => { :no_release => true } do
302
+ try_runner "#{current_path}/script/process/reaper"
303
+ end
304
+
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
312
+ if previous_release
313
+ run "rm #{current_path}; ln -s #{previous_release} #{current_path}"
314
+ else
315
+ abort "could not rollback the code because there is no prior release"
316
+ end
317
+ end
318
+
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
349
+ end
350
+
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 \
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 \
359
+ rake executable by setting the rake variable. The defaults are:
360
+
361
+ set :rake, "rake"
362
+ set :rails_env, "production"
363
+ set :migrate_env, ""
364
+ set :migrate_target, :latest
365
+ DESC
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
+
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
388
+ task :migrations do
389
+ set :migrate_target, :latest
390
+ update_code
391
+ migrate
392
+ symlink
393
+ restart
394
+ end
395
+
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
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
+
413
+ try_sudo "rm -rf #{directories}"
414
+ end
415
+ end
416
+
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'.
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"
428
+ DESC
429
+ task :check, :except => { :no_release => true } do
430
+ dependencies = strategy.check!
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
+
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
452
+ abort
453
+ end
454
+ end
455
+
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 \
459
+ other reason) not currently running. It will deploy the code, run any \
460
+ pending migrations, and then instead of invoking `deploy:restart', it will \
461
+ invoke `deploy:start' to fire up the application servers.
462
+ DESC
463
+ task :cold do
464
+ update
465
+ migrate
466
+ start
467
+ end
468
+
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
482
+ run "cd #{current_path} && #{try_runner} nohup script/spin"
483
+ end
484
+
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
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"
499
+ end
500
+
501
+ namespace :pending do
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
507
+ task :diff, :except => { :no_release => true } do
508
+ system(source.local.diff(current_revision))
509
+ end
510
+
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
516
+ task :default, :except => { :no_release => true } do
517
+ from = source.next_revision(current_revision)
518
+ system(source.local.log(from))
519
+ end
520
+ end
521
+
522
+ namespace :web do
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 \\
534
+ REASON="hardware upgrade" \\
535
+ UNTIL="12pm Central Time"
536
+
537
+ Further customization will require that you write your own task.
538
+ DESC
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
+
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
558
+ task :enable, :roles => :web, :except => { :no_release => true } do
559
+ run "rm #{shared_path}/system/maintenance.html"
560
+ end
561
+ end
562
+ end