HeSYINUvSBZfxqA-capistrano 2.5.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/.gitignore +10 -0
  2. data/CHANGELOG +866 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +31 -0
  5. data/HeSYINUvSBZfxqA-capistrano.gemspec +49 -0
  6. data/README.mdown +65 -0
  7. data/Rakefile +11 -0
  8. data/VERSION +1 -0
  9. data/bin/cap +4 -0
  10. data/bin/capify +86 -0
  11. data/lib/capistrano.rb +2 -0
  12. data/lib/capistrano/callback.rb +45 -0
  13. data/lib/capistrano/cli.rb +47 -0
  14. data/lib/capistrano/cli/execute.rb +85 -0
  15. data/lib/capistrano/cli/help.rb +125 -0
  16. data/lib/capistrano/cli/help.txt +78 -0
  17. data/lib/capistrano/cli/options.rb +243 -0
  18. data/lib/capistrano/cli/ui.rb +40 -0
  19. data/lib/capistrano/command.rb +286 -0
  20. data/lib/capistrano/configuration.rb +44 -0
  21. data/lib/capistrano/configuration/actions/file_transfer.rb +47 -0
  22. data/lib/capistrano/configuration/actions/inspect.rb +46 -0
  23. data/lib/capistrano/configuration/actions/invocation.rb +295 -0
  24. data/lib/capistrano/configuration/callbacks.rb +148 -0
  25. data/lib/capistrano/configuration/connections.rb +204 -0
  26. data/lib/capistrano/configuration/execution.rb +143 -0
  27. data/lib/capistrano/configuration/loading.rb +197 -0
  28. data/lib/capistrano/configuration/namespaces.rb +197 -0
  29. data/lib/capistrano/configuration/roles.rb +73 -0
  30. data/lib/capistrano/configuration/servers.rb +98 -0
  31. data/lib/capistrano/configuration/variables.rb +127 -0
  32. data/lib/capistrano/errors.rb +19 -0
  33. data/lib/capistrano/extensions.rb +57 -0
  34. data/lib/capistrano/logger.rb +59 -0
  35. data/lib/capistrano/processable.rb +53 -0
  36. data/lib/capistrano/recipes/compat.rb +32 -0
  37. data/lib/capistrano/recipes/deploy.rb +597 -0
  38. data/lib/capistrano/recipes/deploy/dependencies.rb +44 -0
  39. data/lib/capistrano/recipes/deploy/local_dependency.rb +54 -0
  40. data/lib/capistrano/recipes/deploy/remote_dependency.rb +111 -0
  41. data/lib/capistrano/recipes/deploy/scm.rb +19 -0
  42. data/lib/capistrano/recipes/deploy/scm/accurev.rb +169 -0
  43. data/lib/capistrano/recipes/deploy/scm/base.rb +196 -0
  44. data/lib/capistrano/recipes/deploy/scm/bzr.rb +86 -0
  45. data/lib/capistrano/recipes/deploy/scm/cvs.rb +153 -0
  46. data/lib/capistrano/recipes/deploy/scm/darcs.rb +96 -0
  47. data/lib/capistrano/recipes/deploy/scm/git.rb +274 -0
  48. data/lib/capistrano/recipes/deploy/scm/mercurial.rb +137 -0
  49. data/lib/capistrano/recipes/deploy/scm/none.rb +44 -0
  50. data/lib/capistrano/recipes/deploy/scm/perforce.rb +138 -0
  51. data/lib/capistrano/recipes/deploy/scm/subversion.rb +121 -0
  52. data/lib/capistrano/recipes/deploy/strategy.rb +19 -0
  53. data/lib/capistrano/recipes/deploy/strategy/base.rb +88 -0
  54. data/lib/capistrano/recipes/deploy/strategy/checkout.rb +20 -0
  55. data/lib/capistrano/recipes/deploy/strategy/copy.rb +223 -0
  56. data/lib/capistrano/recipes/deploy/strategy/export.rb +20 -0
  57. data/lib/capistrano/recipes/deploy/strategy/remote.rb +52 -0
  58. data/lib/capistrano/recipes/deploy/strategy/remote_cache.rb +57 -0
  59. data/lib/capistrano/recipes/deploy/templates/maintenance.rhtml +53 -0
  60. data/lib/capistrano/recipes/standard.rb +37 -0
  61. data/lib/capistrano/recipes/templates/maintenance.rhtml +53 -0
  62. data/lib/capistrano/role.rb +102 -0
  63. data/lib/capistrano/server_definition.rb +56 -0
  64. data/lib/capistrano/shell.rb +260 -0
  65. data/lib/capistrano/ssh.rb +101 -0
  66. data/lib/capistrano/task_definition.rb +75 -0
  67. data/lib/capistrano/transfer.rb +216 -0
  68. data/lib/capistrano/version.rb +18 -0
  69. data/rvmrc.sample +1 -0
  70. data/test/cli/execute_test.rb +132 -0
  71. data/test/cli/help_test.rb +165 -0
  72. data/test/cli/options_test.rb +329 -0
  73. data/test/cli/ui_test.rb +28 -0
  74. data/test/cli_test.rb +17 -0
  75. data/test/command_test.rb +286 -0
  76. data/test/configuration/actions/file_transfer_test.rb +61 -0
  77. data/test/configuration/actions/inspect_test.rb +65 -0
  78. data/test/configuration/actions/invocation_test.rb +225 -0
  79. data/test/configuration/callbacks_test.rb +220 -0
  80. data/test/configuration/connections_test.rb +349 -0
  81. data/test/configuration/execution_test.rb +175 -0
  82. data/test/configuration/loading_test.rb +132 -0
  83. data/test/configuration/namespace_dsl_test.rb +311 -0
  84. data/test/configuration/roles_test.rb +144 -0
  85. data/test/configuration/servers_test.rb +158 -0
  86. data/test/configuration/variables_test.rb +190 -0
  87. data/test/configuration_test.rb +88 -0
  88. data/test/deploy/local_dependency_test.rb +76 -0
  89. data/test/deploy/remote_dependency_test.rb +135 -0
  90. data/test/deploy/scm/accurev_test.rb +23 -0
  91. data/test/deploy/scm/base_test.rb +55 -0
  92. data/test/deploy/scm/bzr_test.rb +51 -0
  93. data/test/deploy/scm/darcs_test.rb +37 -0
  94. data/test/deploy/scm/git_test.rb +184 -0
  95. data/test/deploy/scm/mercurial_test.rb +134 -0
  96. data/test/deploy/scm/none_test.rb +35 -0
  97. data/test/deploy/scm/subversion_test.rb +32 -0
  98. data/test/deploy/strategy/copy_test.rb +302 -0
  99. data/test/extensions_test.rb +69 -0
  100. data/test/fixtures/cli_integration.rb +5 -0
  101. data/test/fixtures/config.rb +5 -0
  102. data/test/fixtures/custom.rb +3 -0
  103. data/test/logger_test.rb +123 -0
  104. data/test/role_test.rb +11 -0
  105. data/test/server_definition_test.rb +121 -0
  106. data/test/shell_test.rb +90 -0
  107. data/test/ssh_test.rb +113 -0
  108. data/test/task_definition_test.rb +116 -0
  109. data/test/transfer_test.rb +160 -0
  110. data/test/utils.rb +39 -0
  111. metadata +271 -0
@@ -0,0 +1,597 @@
1
+ require 'benchmark'
2
+ require 'yaml'
3
+ require 'capistrano/recipes/deploy/scm'
4
+ require 'capistrano/recipes/deploy/strategy'
5
+
6
+ def _cset(name, *args, &block)
7
+ unless exists?(name)
8
+ set(name, *args, &block)
9
+ end
10
+ end
11
+
12
+ # =========================================================================
13
+ # These variables MUST be set in the client capfiles. If they are not set,
14
+ # the deploy will fail with an error.
15
+ # =========================================================================
16
+
17
+ _cset(:application) { abort "Please specify the name of your application, set :application, 'foo'" }
18
+ _cset(:repository) { abort "Please specify the repository that houses your application's code, set :repository, 'foo'" }
19
+
20
+ # =========================================================================
21
+ # These variables may be set in the client capfile if their default values
22
+ # are not sufficient.
23
+ # =========================================================================
24
+
25
+ _cset :scm, :subversion
26
+ _cset :deploy_via, :checkout
27
+
28
+ _cset(:deploy_to) { "/u/apps/#{application}" }
29
+ _cset(:revision) { source.head }
30
+
31
+ # Maintenance base filename
32
+ _cset :maintenance_basename, "maintenance"
33
+
34
+ # =========================================================================
35
+ # These variables should NOT be changed unless you are very confident in
36
+ # what you are doing. Make sure you understand all the implications of your
37
+ # changes if you do decide to muck with these!
38
+ # =========================================================================
39
+
40
+ _cset(:source) { Capistrano::Deploy::SCM.new(scm, self) }
41
+ _cset(:real_revision) { source.local.query_revision(revision) { |cmd| with_env("LC_ALL", "C") { run_locally(cmd) } } }
42
+
43
+ _cset(:strategy) { Capistrano::Deploy::Strategy.new(deploy_via, self) }
44
+
45
+ # If overriding release name, please also select an appropriate setting for :releases below.
46
+ _cset(:release_name) { set :deploy_timestamped, true; Time.now.utc.strftime("%Y%m%d%H%M%S") }
47
+
48
+ _cset :version_dir, "releases"
49
+ _cset :shared_dir, "shared"
50
+ _cset :shared_children, %w(system log pids)
51
+ _cset :current_dir, "current"
52
+
53
+ _cset(:releases_path) { File.join(deploy_to, version_dir) }
54
+ _cset(:shared_path) { File.join(deploy_to, shared_dir) }
55
+ _cset(:current_path) { File.join(deploy_to, current_dir) }
56
+ _cset(:release_path) { File.join(releases_path, release_name) }
57
+
58
+ _cset(:releases) { capture("ls -x #{releases_path}", :except => { :no_release => true }).split.sort }
59
+ _cset(:current_release) { File.join(releases_path, releases.last) }
60
+ _cset(:previous_release) { releases.length > 1 ? File.join(releases_path, releases[-2]) : nil }
61
+
62
+ _cset(:current_revision) { capture("cat #{current_path}/REVISION", :except => { :no_release => true }).chomp }
63
+ _cset(:latest_revision) { capture("cat #{current_release}/REVISION", :except => { :no_release => true }).chomp }
64
+ _cset(:previous_revision) { capture("cat #{previous_release}/REVISION", :except => { :no_release => true }).chomp if previous_release }
65
+
66
+ _cset(:run_method) { fetch(:use_sudo, true) ? :sudo : :run }
67
+
68
+ # some tasks, like symlink, need to always point at the latest release, but
69
+ # they can also (occassionally) be called standalone. In the standalone case,
70
+ # the timestamped release_path will be inaccurate, since the directory won't
71
+ # actually exist. This variable lets tasks like symlink work either in the
72
+ # standalone case, or during deployment.
73
+ _cset(:latest_release) { exists?(:deploy_timestamped) ? release_path : current_release }
74
+
75
+ # =========================================================================
76
+ # These are helper methods that will be available to your recipes.
77
+ # =========================================================================
78
+
79
+ # Auxiliary helper method for the `deploy:check' task. Lets you set up your
80
+ # own dependencies.
81
+ def depend(location, type, *args)
82
+ deps = fetch(:dependencies, {})
83
+ deps[location] ||= {}
84
+ deps[location][type] ||= []
85
+ deps[location][type] << args
86
+ set :dependencies, deps
87
+ end
88
+
89
+ # Temporarily sets an environment variable, yields to a block, and restores
90
+ # the value when it is done.
91
+ def with_env(name, value)
92
+ saved, ENV[name] = ENV[name], value
93
+ yield
94
+ ensure
95
+ ENV[name] = saved
96
+ end
97
+
98
+ # logs the command then executes it locally.
99
+ # returns the command output as a string
100
+ def run_locally(cmd)
101
+ logger.trace "executing locally: #{cmd.inspect}" if logger
102
+ output_on_stdout = nil
103
+ elapsed = Benchmark.realtime do
104
+ output_on_stdout = `#{cmd}`
105
+ end
106
+ if $?.to_i > 0 # $? is command exit code (posix style)
107
+ raise Capistrano::LocalArgumentError, "Command #{cmd} returned status code #{$?}"
108
+ end
109
+ logger.trace "command finished in #{(elapsed * 1000).round}ms" if logger
110
+ output_on_stdout
111
+ end
112
+
113
+
114
+ # If a command is given, this will try to execute the given command, as
115
+ # described below. Otherwise, it will return a string for use in embedding in
116
+ # another command, for executing that command as described below.
117
+ #
118
+ # If :run_method is :sudo (or :use_sudo is true), this executes the given command
119
+ # via +sudo+. Otherwise is uses +run+. If :as is given as a key, it will be
120
+ # passed as the user to sudo as, if using sudo. If the :as key is not given,
121
+ # it will default to whatever the value of the :admin_runner variable is,
122
+ # which (by default) is unset.
123
+ #
124
+ # THUS, if you want to try to run something via sudo, and what to use the
125
+ # root user, you'd just to try_sudo('something'). If you wanted to try_sudo as
126
+ # someone else, you'd just do try_sudo('something', :as => "bob"). If you
127
+ # always wanted sudo to run as a particular user, you could do
128
+ # set(:admin_runner, "bob").
129
+ def try_sudo(*args)
130
+ options = args.last.is_a?(Hash) ? args.pop : {}
131
+ command = args.shift
132
+ raise ArgumentError, "too many arguments" if args.any?
133
+
134
+ as = options.fetch(:as, fetch(:admin_runner, nil))
135
+ via = fetch(:run_method, :sudo)
136
+ if command
137
+ invoke_command(command, :via => via, :as => as)
138
+ elsif via == :sudo
139
+ sudo(:as => as)
140
+ else
141
+ ""
142
+ end
143
+ end
144
+
145
+ # Same as sudo, but tries sudo with :as set to the value of the :runner
146
+ # variable (which defaults to "app").
147
+ def try_runner(*args)
148
+ options = args.last.is_a?(Hash) ? args.pop : {}
149
+ args << options.merge(:as => fetch(:runner, "app"))
150
+ try_sudo(*args)
151
+ end
152
+
153
+ # =========================================================================
154
+ # These are the tasks that are available to help with deploying web apps,
155
+ # and specifically, Rails applications. You can have cap give you a summary
156
+ # of them with `cap -T'.
157
+ # =========================================================================
158
+
159
+ namespace :deploy do
160
+ desc <<-DESC
161
+ Deploys your project. This calls both `update' and `restart'. Note that \
162
+ this will generally only work for applications that have already been deployed \
163
+ once. For a "cold" deploy, you'll want to take a look at the `deploy:cold' \
164
+ task, which handles the cold start specifically.
165
+ DESC
166
+ task :default do
167
+ update
168
+ restart
169
+ end
170
+
171
+ desc <<-DESC
172
+ Prepares one or more servers for deployment. Before you can use any \
173
+ of the Capistrano deployment tasks with your project, you will need to \
174
+ make sure all of your servers have been prepared with `cap deploy:setup'. When \
175
+ you add a new server to your cluster, you can easily run the setup task \
176
+ on just that server by specifying the HOSTS environment variable:
177
+
178
+ $ cap HOSTS=new.server.com deploy:setup
179
+
180
+ It is safe to run this task on servers that have already been set up; it \
181
+ will not destroy any deployed revisions or data.
182
+ DESC
183
+ task :setup, :except => { :no_release => true } do
184
+ dirs = [deploy_to, releases_path, shared_path]
185
+ dirs += shared_children.map { |d| File.join(shared_path, d) }
186
+ run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
187
+ end
188
+
189
+ desc <<-DESC
190
+ Copies your project and updates the symlink. It does this in a \
191
+ transaction, so that if either `update_code' or `symlink' fail, all \
192
+ changes made to the remote servers will be rolled back, leaving your \
193
+ system in the same state it was in before `update' was invoked. Usually, \
194
+ you will want to call `deploy' instead of `update', but `update' can be \
195
+ handy if you want to deploy, but not immediately restart your application.
196
+ DESC
197
+ task :update do
198
+ transaction do
199
+ update_code
200
+ symlink
201
+ end
202
+ end
203
+
204
+ desc <<-DESC
205
+ Copies your project to the remote servers. This is the first stage \
206
+ of any deployment; moving your updated code and assets to the deployment \
207
+ servers. You will rarely call this task directly, however; instead, you \
208
+ should call the `deploy' task (to do a complete deploy) or the `update' \
209
+ task (if you want to perform the `restart' task separately).
210
+
211
+ You will need to make sure you set the :scm variable to the source \
212
+ control software you are using (it defaults to :subversion), and the \
213
+ :deploy_via variable to the strategy you want to use to deploy (it \
214
+ defaults to :checkout).
215
+ DESC
216
+ task :update_code, :except => { :no_release => true } do
217
+ on_rollback { run "rm -rf #{release_path}; true" }
218
+ strategy.deploy!
219
+ finalize_update
220
+ end
221
+
222
+ desc <<-DESC
223
+ [internal] Touches up the released code. This is called by update_code \
224
+ after the basic deploy finishes. It assumes a Rails project was deployed, \
225
+ so if you are deploying something else, you may want to override this \
226
+ task with your own environment's requirements.
227
+
228
+ This task will make the release group-writable (if the :group_writable \
229
+ variable is set to true, which is the default). It will then set up \
230
+ symlinks to the shared directory for the log, system, and tmp/pids \
231
+ directories, and will lastly touch all assets in public/images, \
232
+ public/stylesheets, and public/javascripts so that the times are \
233
+ consistent (so that asset timestamping works). This touch process \
234
+ is only carried out if the :normalize_asset_timestamps variable is \
235
+ set to true, which is the default The asset directories can be overridden \
236
+ using the :public_children variable.
237
+ DESC
238
+ task :finalize_update, :except => { :no_release => true } do
239
+ run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
240
+
241
+ # mkdir -p is making sure that the directories are there for some SCM's that don't
242
+ # save empty folders
243
+ run <<-CMD
244
+ rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
245
+ mkdir -p #{latest_release}/public &&
246
+ mkdir -p #{latest_release}/tmp &&
247
+ ln -s #{shared_path}/log #{latest_release}/log &&
248
+ ln -s #{shared_path}/system #{latest_release}/public/system &&
249
+ ln -s #{shared_path}/pids #{latest_release}/tmp/pids
250
+ CMD
251
+
252
+ if fetch(:normalize_asset_timestamps, true)
253
+ stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
254
+ asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
255
+ run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
256
+ end
257
+ end
258
+
259
+ desc <<-DESC
260
+ Updates the symlink to the most recently deployed version. Capistrano works \
261
+ by putting each new release of your application in its own directory. When \
262
+ you deploy a new version, this task's job is to update the `current' symlink \
263
+ to point at the new version. You will rarely need to call this task \
264
+ directly; instead, use the `deploy' task (which performs a complete \
265
+ deploy, including `restart') or the 'update' task (which does everything \
266
+ except `restart').
267
+ DESC
268
+ task :symlink, :except => { :no_release => true } do
269
+ on_rollback do
270
+ if previous_release
271
+ run "rm -f #{current_path}; ln -s #{previous_release} #{current_path}; true"
272
+ else
273
+ logger.important "no previous release to rollback to, rollback of symlink skipped"
274
+ end
275
+ end
276
+
277
+ run "rm -f #{current_path} && ln -s #{latest_release} #{current_path}"
278
+ end
279
+
280
+ desc <<-DESC
281
+ Copy files to the currently deployed version. This is useful for updating \
282
+ files piecemeal, such as when you need to quickly deploy only a single \
283
+ file. Some files, such as updated templates, images, or stylesheets, \
284
+ might not require a full deploy, and especially in emergency situations \
285
+ it can be handy to just push the updates to production, quickly.
286
+
287
+ To use this task, specify the files and directories you want to copy as a \
288
+ comma-delimited list in the FILES environment variable. All directories \
289
+ will be processed recursively, with all files being pushed to the \
290
+ deployment servers.
291
+
292
+ $ cap deploy:upload FILES=templates,controller.rb
293
+
294
+ Dir globs are also supported:
295
+
296
+ $ cap deploy:upload FILES='config/apache/*.conf'
297
+ DESC
298
+ task :upload, :except => { :no_release => true } do
299
+ files = (ENV["FILES"] || "").split(",").map { |f| Dir[f.strip] }.flatten
300
+ abort "Please specify at least one file or directory to update (via the FILES environment variable)" if files.empty?
301
+
302
+ files.each { |file| top.upload(file, File.join(current_path, file)) }
303
+ end
304
+
305
+ desc <<-DESC
306
+ Restarts your application. This works by calling the script/process/reaper \
307
+ script under the current path.
308
+
309
+ If you are deploying a Rails 2.3.x application, you will need to install
310
+ these http://github.com/rails/irs_process_scripts (more info about why
311
+ on that page.)
312
+
313
+ By default, this will be invoked via sudo as the `app' user. If \
314
+ you wish to run it as a different user, set the :runner variable to \
315
+ that user. If you are in an environment where you can't use sudo, set \
316
+ the :use_sudo variable to false:
317
+
318
+ set :use_sudo, false
319
+ DESC
320
+ task :restart, :roles => :app, :except => { :no_release => true } do
321
+ warn "[DEPRECATED] `deploy:restart` is going to be changed to Passenger mod_rails' method after 2.5.9 - see http://is.gd/2BPeA"
322
+ try_runner "#{current_path}/script/process/reaper"
323
+ end
324
+
325
+ namespace :rollback do
326
+ desc <<-DESC
327
+ [internal] Points the current symlink at the previous revision.
328
+ This is called by the rollback sequence, and should rarely (if
329
+ ever) need to be called directly.
330
+ DESC
331
+ task :revision, :except => { :no_release => true } do
332
+ if previous_release
333
+ run "rm #{current_path}; ln -s #{previous_release} #{current_path}"
334
+ else
335
+ abort "could not rollback the code because there is no prior release"
336
+ end
337
+ end
338
+
339
+ desc <<-DESC
340
+ [internal] Removes the most recently deployed release.
341
+ This is called by the rollback sequence, and should rarely
342
+ (if ever) need to be called directly.
343
+ DESC
344
+ task :cleanup, :except => { :no_release => true } do
345
+ run "if [ `readlink #{current_path}` != #{current_release} ]; then rm -rf #{current_release}; fi"
346
+ end
347
+
348
+ desc <<-DESC
349
+ Rolls back to the previously deployed version. The `current' symlink will \
350
+ be updated to point at the previously deployed version, and then the \
351
+ current release will be removed from the servers. You'll generally want \
352
+ to call `rollback' instead, as it performs a `restart' as well.
353
+ DESC
354
+ task :code, :except => { :no_release => true } do
355
+ revision
356
+ cleanup
357
+ end
358
+
359
+ desc <<-DESC
360
+ Rolls back to a previous version and restarts. This is handy if you ever \
361
+ discover that you've deployed a lemon; `cap rollback' and you're right \
362
+ back where you were, on the previously deployed version.
363
+ DESC
364
+ task :default do
365
+ revision
366
+ restart
367
+ cleanup
368
+ end
369
+ end
370
+
371
+ desc <<-DESC
372
+ Run the migrate rake task. By default, it runs this in most recently \
373
+ deployed version of the app. However, you can specify a different release \
374
+ via the migrate_target variable, which must be one of :latest (for the \
375
+ default behavior), or :current (for the release indicated by the \
376
+ `current' symlink). Strings will work for those values instead of symbols, \
377
+ too. You can also specify additional environment variables to pass to rake \
378
+ via the migrate_env variable. Finally, you can specify the full path to the \
379
+ rake executable by setting the rake variable. The defaults are:
380
+
381
+ set :rake, "rake"
382
+ set :rails_env, "production"
383
+ set :migrate_env, ""
384
+ set :migrate_target, :latest
385
+ DESC
386
+ task :migrate, :roles => :db, :only => { :primary => true } do
387
+ rake = fetch(:rake, "rake")
388
+ rails_env = fetch(:rails_env, "production")
389
+ migrate_env = fetch(:migrate_env, "")
390
+ migrate_target = fetch(:migrate_target, :latest)
391
+
392
+ directory = case migrate_target.to_sym
393
+ when :current then current_path
394
+ when :latest then latest_release
395
+ else raise ArgumentError, "unknown migration target #{migrate_target.inspect}"
396
+ end
397
+
398
+ run "cd #{directory}; #{rake} RAILS_ENV=#{rails_env} #{migrate_env} db:migrate"
399
+ end
400
+
401
+ desc <<-DESC
402
+ Deploy and run pending migrations. This will work similarly to the \
403
+ `deploy' task, but will also run any pending migrations (via the \
404
+ `deploy:migrate' task) prior to updating the symlink. Note that the \
405
+ update in this case it is not atomic, and transactions are not used, \
406
+ because migrations are not guaranteed to be reversible.
407
+ DESC
408
+ task :migrations do
409
+ set :migrate_target, :latest
410
+ update_code
411
+ migrate
412
+ symlink
413
+ restart
414
+ end
415
+
416
+ desc <<-DESC
417
+ Clean up old releases. By default, the last 5 releases are kept on each \
418
+ server (though you can change this with the keep_releases variable). All \
419
+ other deployed revisions are removed from the servers. By default, this \
420
+ will use sudo to clean up the old releases, but if sudo is not available \
421
+ for your environment, set the :use_sudo variable to false instead.
422
+ DESC
423
+ task :cleanup, :except => { :no_release => true } do
424
+ count = fetch(:keep_releases, 5).to_i
425
+ if count >= releases.length
426
+ logger.important "no old releases to clean up"
427
+ else
428
+ logger.info "keeping #{count} of #{releases.length} deployed releases"
429
+
430
+ directories = (releases - releases.last(count)).map { |release|
431
+ File.join(releases_path, release) }.join(" ")
432
+
433
+ try_sudo "rm -rf #{directories}"
434
+ end
435
+ end
436
+
437
+ desc <<-DESC
438
+ Test deployment dependencies. Checks things like directory permissions, \
439
+ necessary utilities, and so forth, reporting on the things that appear to \
440
+ be incorrect or missing. This is good for making sure a deploy has a \
441
+ chance of working before you actually run `cap deploy'.
442
+
443
+ You can define your own dependencies, as well, using the `depend' method:
444
+
445
+ depend :remote, :gem, "tzinfo", ">=0.3.3"
446
+ depend :local, :command, "svn"
447
+ depend :remote, :directory, "/u/depot/files"
448
+ DESC
449
+ task :check, :except => { :no_release => true } do
450
+ dependencies = strategy.check!
451
+
452
+ other = fetch(:dependencies, {})
453
+ other.each do |location, types|
454
+ types.each do |type, calls|
455
+ if type == :gem
456
+ dependencies.send(location).command(fetch(:gem_command, "gem")).or("`gem' command could not be found. Try setting :gem_command")
457
+ end
458
+
459
+ calls.each do |args|
460
+ dependencies.send(location).send(type, *args)
461
+ end
462
+ end
463
+ end
464
+
465
+ if dependencies.pass?
466
+ puts "You appear to have all necessary dependencies installed"
467
+ else
468
+ puts "The following dependencies failed. Please check them and try again:"
469
+ dependencies.reject { |d| d.pass? }.each do |d|
470
+ puts "--> #{d.message}"
471
+ end
472
+ abort
473
+ end
474
+ end
475
+
476
+ desc <<-DESC
477
+ Deploys and starts a `cold' application. This is useful if you have not \
478
+ deployed your application before, or if your application is (for some \
479
+ other reason) not currently running. It will deploy the code, run any \
480
+ pending migrations, and then instead of invoking `deploy:restart', it will \
481
+ invoke `deploy:start' to fire up the application servers.
482
+ DESC
483
+ task :cold do
484
+ update
485
+ migrate
486
+ start
487
+ end
488
+
489
+ desc <<-DESC
490
+ Start the application servers. This will attempt to invoke a script \
491
+ in your application called `script/spin', which must know how to start \
492
+ your application listeners. For Rails applications, you might just have \
493
+ that script invoke `script/process/spawner' with the appropriate \
494
+ arguments.
495
+
496
+ By default, the script will be executed via sudo as the `app' user. If \
497
+ you wish to run it as a different user, set the :runner variable to \
498
+ that user. If you are in an environment where you can't use sudo, set \
499
+ the :use_sudo variable to false.
500
+ DESC
501
+ task :start, :roles => :app do
502
+ warn "[DEPRECATED] `deploy:start` is going to be removed after 2.5.9 - see http://is.gd/2BPeA"
503
+ run "cd #{current_path} && #{try_runner} nohup script/spin"
504
+ end
505
+
506
+ desc <<-DESC
507
+ Stop the application servers. This will call script/process/reaper for \
508
+ both the spawner process, and all of the application processes it has \
509
+ spawned. As such, it is fairly Rails specific and may need to be \
510
+ overridden for other systems.
511
+
512
+ By default, the script will be executed via sudo as the `app' user. If \
513
+ you wish to run it as a different user, set the :runner variable to \
514
+ that user. If you are in an environment where you can't use sudo, set \
515
+ the :use_sudo variable to false.
516
+ DESC
517
+ task :stop, :roles => :app do
518
+ warn "[DEPRECATED] `deploy:start` is going to be removed after 2.5.9 - see http://is.gd/2BPeA"
519
+ 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"
520
+ try_runner "#{current_path}/script/process/reaper -a kill"
521
+ end
522
+
523
+ namespace :pending do
524
+ desc <<-DESC
525
+ Displays the `diff' since your last deploy. This is useful if you want \
526
+ to examine what changes are about to be deployed. Note that this might \
527
+ not be supported on all SCM's.
528
+ DESC
529
+ task :diff, :except => { :no_release => true } do
530
+ system(source.local.diff(current_revision))
531
+ end
532
+
533
+ desc <<-DESC
534
+ Displays the commits since your last deploy. This is good for a summary \
535
+ of the changes that have occurred since the last deploy. Note that this \
536
+ might not be supported on all SCM's.
537
+ DESC
538
+ task :default, :except => { :no_release => true } do
539
+ from = source.next_revision(current_revision)
540
+ system(source.local.log(from))
541
+ end
542
+ end
543
+
544
+ namespace :web do
545
+ desc <<-DESC
546
+ Present a maintenance page to visitors. Disables your application's web \
547
+ interface by writing a "#{maintenance_basename}.html" file to each web server. The \
548
+ servers must be configured to detect the presence of this file, and if \
549
+ it is present, always display it instead of performing the request.
550
+
551
+ By default, the maintenance page will just say the site is down for \
552
+ "maintenance", and will be back "shortly", but you can customize the \
553
+ page by specifying the REASON and UNTIL environment variables:
554
+
555
+ $ cap deploy:web:disable \\
556
+ REASON="hardware upgrade" \\
557
+ UNTIL="12pm Central Time"
558
+
559
+ Further customization will require that you write your own task.
560
+ DESC
561
+ task :disable, :roles => :web, :except => { :no_release => true } do
562
+ require 'erb'
563
+ on_rollback { run "rm #{shared_path}/system/#{maintenance_basename}.html" }
564
+
565
+ warn <<-EOHTACCESS
566
+
567
+ # Please add something like this to your site's htaccess to redirect users to the maintenance page.
568
+ # More Info: http://www.shiftcommathree.com/articles/make-your-rails-maintenance-page-respond-with-a-503
569
+
570
+ ErrorDocument 503 /system/#{maintenance_basename}.html
571
+ RewriteEngine On
572
+ RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|png)$
573
+ RewriteCond %{DOCUMENT_ROOT}/system/#{maintenance_basename}.html -f
574
+ RewriteCond %{SCRIPT_FILENAME} !#{maintenance_basename}.html
575
+ RewriteRule ^.*$ - [redirect=503,last]
576
+ EOHTACCESS
577
+
578
+ reason = ENV['REASON']
579
+ deadline = ENV['UNTIL']
580
+
581
+ template = File.read(File.join(File.dirname(__FILE__), "templates", "maintenance.rhtml"))
582
+ result = ERB.new(template).result(binding)
583
+
584
+ put result, "#{shared_path}/system/#{maintenance_basename}.html", :mode => 0644
585
+ end
586
+
587
+ desc <<-DESC
588
+ Makes the application web-accessible again. Removes the \
589
+ "#{maintenance_basename}.html" page generated by deploy:web:disable, which (if your \
590
+ web servers are configured correctly) will make your application \
591
+ web-accessible again.
592
+ DESC
593
+ task :enable, :roles => :web, :except => { :no_release => true } do
594
+ run "rm #{shared_path}/system/#{maintenance_basename}.html"
595
+ end
596
+ end
597
+ end