minmb-capistrano 2.15.4

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