Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 576 lines (494 sloc) 23.454 kB
20b168d @cgriego Added elapsed time to remote and local command finished log messages
cgriego authored
1 require 'benchmark'
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
2 require 'yaml'
3 require 'capistrano/recipes/deploy/scm'
4 require 'capistrano/recipes/deploy/strategy'
5
cdb38cd @jamis Make sure variables are conditionally set in the deploy recipes, so a…
jamis authored
6 def _cset(name, *args, &block)
7 unless exists?(name)
8 set(name, *args, &block)
9 end
10 end
11
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
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
cdb38cd @jamis Make sure variables are conditionally set in the deploy recipes, so a…
jamis authored
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'" }
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
19
20 # =========================================================================
21 # These variables may be set in the client capfile if their default values
22 # are not sufficient.
23 # =========================================================================
24
cdb38cd @jamis Make sure variables are conditionally set in the deploy recipes, so a…
jamis authored
25 _cset :scm, :subversion
26 _cset :deploy_via, :checkout
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
27
cdb38cd @jamis Make sure variables are conditionally set in the deploy recipes, so a…
jamis authored
28 _cset(:deploy_to) { "/u/apps/#{application}" }
29 _cset(:revision) { source.head }
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
30
f68fcae @cgriego Rails 3.1 Asset Pipeline support
cgriego authored
31 _cset :rails_env, "production"
32 _cset :rake, "rake"
33
0c45e4a @tinogomes New variable "maintenance_basename" to set maintenance file basename
tinogomes authored
34 _cset :maintenance_basename, "maintenance"
eb34ca8 @mtylty adds maintenance_template_path configuration option
mtylty authored
35 _cset(:maintenance_template_path) { File.join(File.dirname(__FILE__), "templates", "maintenance.rhtml") }
0c45e4a @tinogomes New variable "maintenance_basename" to set maintenance file basename
tinogomes authored
36
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
37 # =========================================================================
38 # These variables should NOT be changed unless you are very confident in
39 # what you are doing. Make sure you understand all the implications of your
40 # changes if you do decide to muck with these!
41 # =========================================================================
42
cdb38cd @jamis Make sure variables are conditionally set in the deploy recipes, so a…
jamis authored
43 _cset(:source) { Capistrano::Deploy::SCM.new(scm, self) }
27d91a7 @evansj locally executed commands also get logged in verbose mode
evansj authored
44 _cset(:real_revision) { source.local.query_revision(revision) { |cmd| with_env("LC_ALL", "C") { run_locally(cmd) } } }
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
45
cdb38cd @jamis Make sure variables are conditionally set in the deploy recipes, so a…
jamis authored
46 _cset(:strategy) { Capistrano::Deploy::Strategy.new(deploy_via, self) }
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
47
92941e8 @leehambley Altered the `ls` command used to list older releases, should fix #88 …
leehambley authored
48 # If overriding release name, please also select an appropriate setting for :releases below.
cdb38cd @jamis Make sure variables are conditionally set in the deploy recipes, so a…
jamis authored
49 _cset(:release_name) { set :deploy_timestamped, true; Time.now.utc.strftime("%Y%m%d%H%M%S") }
d257a6c @jamis Add version_dir, current_dir, and shared_dir variables for naming dir…
jamis authored
50
d9d46ce @jamis set the variables using the correct syntax
jamis authored
51 _cset :version_dir, "releases"
52 _cset :shared_dir, "shared"
44e96a4 @HectorMalot shared_children variable now also used for symlink creation in finali…
HectorMalot authored
53 _cset :shared_children, %w(public/system log tmp/pids)
d9d46ce @jamis set the variables using the correct syntax
jamis authored
54 _cset :current_dir, "current"
d257a6c @jamis Add version_dir, current_dir, and shared_dir variables for naming dir…
jamis authored
55
56 _cset(:releases_path) { File.join(deploy_to, version_dir) }
57 _cset(:shared_path) { File.join(deploy_to, shared_dir) }
58 _cset(:current_path) { File.join(deploy_to, current_dir) }
cdb38cd @jamis Make sure variables are conditionally set in the deploy recipes, so a…
jamis authored
59 _cset(:release_path) { File.join(releases_path, release_name) }
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
60
af3331c @cgriego Only try to lookup files and folders in the releases path on servers …
cgriego authored
61 _cset(:releases) { capture("ls -x #{releases_path}", :except => { :no_release => true }).split.sort }
f2f23f3 @carols10cents On a dry run, don't throw an error when trying to use the current_rel…
carols10cents authored
62 _cset(:current_release) { releases.length > 0 ? File.join(releases_path, releases.last) : nil }
c68c9d9 @roidrage Make previous_release return nil if there is no previous release
roidrage authored
63 _cset(:previous_release) { releases.length > 1 ? File.join(releases_path, releases[-2]) : nil }
09ad893 @jamis namespaces are great, but backwards-compatibility must trump niftines…
jamis authored
64
af3331c @cgriego Only try to lookup files and folders in the releases path on servers …
cgriego authored
65 _cset(:current_revision) { capture("cat #{current_path}/REVISION", :except => { :no_release => true }).chomp }
66 _cset(:latest_revision) { capture("cat #{current_release}/REVISION", :except => { :no_release => true }).chomp }
67 _cset(:previous_revision) { capture("cat #{previous_release}/REVISION", :except => { :no_release => true }).chomp if previous_release }
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
68
cdb38cd @jamis Make sure variables are conditionally set in the deploy recipes, so a…
jamis authored
69 _cset(:run_method) { fetch(:use_sudo, true) ? :sudo : :run }
815ee2b @jamis put the use_sudo toggle back in
jamis authored
70
b749367 @jamis Make sure symlink and finalize_update tasks reference the most recent…
jamis authored
71 # some tasks, like symlink, need to always point at the latest release, but
72 # they can also (occassionally) be called standalone. In the standalone case,
73 # the timestamped release_path will be inaccurate, since the directory won't
74 # actually exist. This variable lets tasks like symlink work either in the
75 # standalone case, or during deployment.
cdb38cd @jamis Make sure variables are conditionally set in the deploy recipes, so a…
jamis authored
76 _cset(:latest_release) { exists?(:deploy_timestamped) ? release_path : current_release }
b749367 @jamis Make sure symlink and finalize_update tasks reference the most recent…
jamis authored
77
dae044f @jamis Pull the server selection code out of the task definition and into th…
jamis authored
78 # =========================================================================
12feba4 @jamis allow custom dependencies to be declared, which deploy:check will verify
jamis authored
79 # These are helper methods that will be available to your recipes.
80 # =========================================================================
81
82 # Auxiliary helper method for the `deploy:check' task. Lets you set up your
83 # own dependencies.
84 def depend(location, type, *args)
85 deps = fetch(:dependencies, {})
86 deps[location] ||= {}
87 deps[location][type] ||= []
88 deps[location][type] << args
89 set :dependencies, deps
90 end
91
67595be @jamis Set LC_ALL=C before querying the revision, to make sure the output is…
jamis authored
92 # Temporarily sets an environment variable, yields to a block, and restores
93 # the value when it is done.
94 def with_env(name, value)
95 saved, ENV[name] = ENV[name], value
96 yield
97 ensure
98 ENV[name] = saved
99 end
100
27d91a7 @evansj locally executed commands also get logged in verbose mode
evansj authored
101 # logs the command then executes it locally.
102 # returns the command output as a string
103 def run_locally(cmd)
104 logger.trace "executing locally: #{cmd.inspect}" if logger
6387299 @cgriego Even with benchmarking, run_locally should return the output instead …
cgriego authored
105 output_on_stdout = nil
20b168d @cgriego Added elapsed time to remote and local command finished log messages
cgriego authored
106 elapsed = Benchmark.realtime do
107 output_on_stdout = `#{cmd}`
108 end
af2402f @leehambley Improving a prior fix for jRuby users, and other people who were stil…
leehambley authored
109 if $?.to_i > 0 # $? is command exit code (posix style)
110 raise Capistrano::LocalArgumentError, "Command #{cmd} returned status code #{$?}"
111 end
20b168d @cgriego Added elapsed time to remote and local command finished log messages
cgriego authored
112 logger.trace "command finished in #{(elapsed * 1000).round}ms" if logger
af2402f @leehambley Improving a prior fix for jRuby users, and other people who were stil…
leehambley authored
113 output_on_stdout
7497ab0 @jweiss test if local commands are present and log if not, helps to debug pro…
jweiss authored
114 end
115
af2402f @leehambley Improving a prior fix for jRuby users, and other people who were stil…
leehambley authored
116
19e6e3b @jamis Fix deployment recipes to use the updated sudo helper
jamis authored
117 # If a command is given, this will try to execute the given command, as
118 # described below. Otherwise, it will return a string for use in embedding in
119 # another command, for executing that command as described below.
120 #
552a492 @jamis Make deploy:setup obey the :use_sudo and :runner directives, and gene…
jamis authored
121 # If :run_method is :sudo (or :use_sudo is true), this executes the given command
41c0da7 @jamis Only use :runner for tasks that affect the application's execution.
jamis authored
122 # via +sudo+. Otherwise is uses +run+. If :as is given as a key, it will be
123 # passed as the user to sudo as, if using sudo. If the :as key is not given,
124 # it will default to whatever the value of the :admin_runner variable is,
125 # which (by default) is unset.
126 #
127 # THUS, if you want to try to run something via sudo, and what to use the
128 # root user, you'd just to try_sudo('something'). If you wanted to try_sudo as
129 # someone else, you'd just do try_sudo('something', :as => "bob"). If you
130 # always wanted sudo to run as a particular user, you could do
131 # set(:admin_runner, "bob").
132 def try_sudo(*args)
133 options = args.last.is_a?(Hash) ? args.pop : {}
134 command = args.shift
135 raise ArgumentError, "too many arguments" if args.any?
136
137 as = options.fetch(:as, fetch(:admin_runner, nil))
552a492 @jamis Make deploy:setup obey the :use_sudo and :runner directives, and gene…
jamis authored
138 via = fetch(:run_method, :sudo)
19e6e3b @jamis Fix deployment recipes to use the updated sudo helper
jamis authored
139 if command
140 invoke_command(command, :via => via, :as => as)
141 elsif via == :sudo
142 sudo(:as => as)
143 else
144 ""
145 end
552a492 @jamis Make deploy:setup obey the :use_sudo and :runner directives, and gene…
jamis authored
146 end
147
41c0da7 @jamis Only use :runner for tasks that affect the application's execution.
jamis authored
148 # Same as sudo, but tries sudo with :as set to the value of the :runner
149 # variable (which defaults to "app").
150 def try_runner(*args)
151 options = args.last.is_a?(Hash) ? args.pop : {}
152 args << options.merge(:as => fetch(:runner, "app"))
153 try_sudo(*args)
154 end
155
12feba4 @jamis allow custom dependencies to be declared, which deploy:check will verify
jamis authored
156 # =========================================================================
dae044f @jamis Pull the server selection code out of the task definition and into th…
jamis authored
157 # These are the tasks that are available to help with deploying web apps,
158 # and specifically, Rails applications. You can have cap give you a summary
159 # of them with `cap -T'.
160 # =========================================================================
161
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
162 namespace :deploy do
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
163 desc <<-DESC
164 Deploys your project. This calls both `update' and `restart'. Note that \
165 this will generally only work for applications that have already been deployed \
7ae3dd9 @jamis Doc corrections. Fix dependency checking for remote cache strategy.
jamis authored
166 once. For a "cold" deploy, you'll want to take a look at the `deploy:cold' \
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
167 task, which handles the cold start specifically.
168 DESC
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
169 task :default do
09ad893 @jamis namespaces are great, but backwards-compatibility must trump niftines…
jamis authored
170 update
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
171 restart
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
172 end
173
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
174 desc <<-DESC
175 Prepares one or more servers for deployment. Before you can use any \
176 of the Capistrano deployment tasks with your project, you will need to \
208581a @jamis Fix incorrect reference to the 'setup' task (closes #10819)
jamis authored
177 make sure all of your servers have been prepared with `cap deploy:setup'. When \
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
178 you add a new server to your cluster, you can easily run the setup task \
179 on just that server by specifying the HOSTS environment variable:
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
180
208581a @jamis Fix incorrect reference to the 'setup' task (closes #10819)
jamis authored
181 $ cap HOSTS=new.server.com deploy:setup
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
182
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
183 It is safe to run this task on servers that have already been set up; it \
184 will not destroy any deployed revisions or data.
185 DESC
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
186 task :setup, :except => { :no_release => true } do
187 dirs = [deploy_to, releases_path, shared_path]
44e96a4 @HectorMalot shared_children variable now also used for symlink creation in finali…
HectorMalot authored
188 dirs += shared_children.map { |d| File.join(shared_path, d.split('/').last) }
0e021b9 @marxarelli Task deploy:setup now respects :group_writable.
marxarelli authored
189 run "#{try_sudo} mkdir -p #{dirs.join(' ')}"
190 run "#{try_sudo} chmod g+w #{dirs.join(' ')}" if fetch(:group_writable, true)
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
191 end
192
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
193 desc <<-DESC
194 Copies your project and updates the symlink. It does this in a \
195 transaction, so that if either `update_code' or `symlink' fail, all \
196 changes made to the remote servers will be rolled back, leaving your \
197 system in the same state it was in before `update' was invoked. Usually, \
198 you will want to call `deploy' instead of `update', but `update' can be \
199 handy if you want to deploy, but not immediately restart your application.
200 DESC
09ad893 @jamis namespaces are great, but backwards-compatibility must trump niftines…
jamis authored
201 task :update do
202 transaction do
203 update_code
c46cefb @leehambley Modify for Rake 0.9.x Compatibility.
leehambley authored
204 create_symlink
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
205 end
09ad893 @jamis namespaces are great, but backwards-compatibility must trump niftines…
jamis authored
206 end
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
207
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
208 desc <<-DESC
209 Copies your project to the remote servers. This is the first stage \
210 of any deployment; moving your updated code and assets to the deployment \
211 servers. You will rarely call this task directly, however; instead, you \
212 should call the `deploy' task (to do a complete deploy) or the `update' \
213 task (if you want to perform the `restart' task separately).
214
215 You will need to make sure you set the :scm variable to the source \
216 control software you are using (it defaults to :subversion), and the \
217 :deploy_via variable to the strategy you want to use to deploy (it \
218 defaults to :checkout).
219 DESC
09ad893 @jamis namespaces are great, but backwards-compatibility must trump niftines…
jamis authored
220 task :update_code, :except => { :no_release => true } do
221 on_rollback { run "rm -rf #{release_path}; true" }
222 strategy.deploy!
223 finalize_update
224 end
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
225
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
226 desc <<-DESC
227 [internal] Touches up the released code. This is called by update_code \
228 after the basic deploy finishes. It assumes a Rails project was deployed, \
229 so if you are deploying something else, you may want to override this \
230 task with your own environment's requirements.
231
232 This task will make the release group-writable (if the :group_writable \
233 variable is set to true, which is the default). It will then set up \
234 symlinks to the shared directory for the log, system, and tmp/pids \
235 directories, and will lastly touch all assets in public/images, \
236 public/stylesheets, and public/javascripts so that the times are \
5d1d9d9 @jtrupiano Added a :normalize_asset_timestamps property.
jtrupiano authored
237 consistent (so that asset timestamping works). This touch process \
238 is only carried out if the :normalize_asset_timestamps variable is \
07558ca @cgriego Parameterize the asset paths used when normalizing timestamps using a…
cgriego authored
239 set to true, which is the default The asset directories can be overridden \
240 using the :public_children variable.
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
241 DESC
09ad893 @jamis namespaces are great, but backwards-compatibility must trump niftines…
jamis authored
242 task :finalize_update, :except => { :no_release => true } do
b749367 @jamis Make sure symlink and finalize_update tasks reference the most recent…
jamis authored
243 run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
244
245 # mkdir -p is making sure that the directories are there for some SCM's that don't
246 # save empty folders
247 run <<-CMD
248 rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
249 mkdir -p #{latest_release}/public &&
44e96a4 @HectorMalot shared_children variable now also used for symlink creation in finali…
HectorMalot authored
250 mkdir -p #{latest_release}/tmp
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
251 CMD
44e96a4 @HectorMalot shared_children variable now also used for symlink creation in finali…
HectorMalot authored
252 shared_children.map do |d|
253 run "ln -s #{shared_path}/#{d.split('/').last} #{latest_release}/#{d}"
254 end
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
255
256 if fetch(:normalize_asset_timestamps, true)
257 stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
07558ca @cgriego Parameterize the asset paths used when normalizing timestamps using a…
cgriego authored
258 asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
259 run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
260 end
09ad893 @jamis namespaces are great, but backwards-compatibility must trump niftines…
jamis authored
261 end
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
262
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
263 desc <<-DESC
7ae3dd9 @jamis Doc corrections. Fix dependency checking for remote cache strategy.
jamis authored
264 Updates the symlink to the most recently deployed version. Capistrano works \
265 by putting each new release of your application in its own directory. When \
b749367 @jamis Make sure symlink and finalize_update tasks reference the most recent…
jamis authored
266 you deploy a new version, this task's job is to update the `current' symlink \
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
267 to point at the new version. You will rarely need to call this task \
268 directly; instead, use the `deploy' task (which performs a complete \
269 deploy, including `restart') or the 'update' task (which does everything \
270 except `restart').
271 DESC
c46cefb @leehambley Modify for Rake 0.9.x Compatibility.
leehambley authored
272 task :create_symlink, :except => { :no_release => true } do
06daaf7 @jamis Rollback of deploy:symlink should not rollback if there is no previou…
jamis authored
273 on_rollback do
907d907 @jamis cleanup some conditions now that previous_release is sane
jamis authored
274 if previous_release
06daaf7 @jamis Rollback of deploy:symlink should not rollback if there is no previou…
jamis authored
275 run "rm -f #{current_path}; ln -s #{previous_release} #{current_path}; true"
276 else
277 logger.important "no previous release to rollback to, rollback of symlink skipped"
278 end
279 end
280
b749367 @jamis Make sure symlink and finalize_update tasks reference the most recent…
jamis authored
281 run "rm -f #{current_path} && ln -s #{latest_release} #{current_path}"
09ad893 @jamis namespaces are great, but backwards-compatibility must trump niftines…
jamis authored
282 end
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
283
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
284 desc <<-DESC
285 Copy files to the currently deployed version. This is useful for updating \
286 files piecemeal, such as when you need to quickly deploy only a single \
287 file. Some files, such as updated templates, images, or stylesheets, \
288 might not require a full deploy, and especially in emergency situations \
289 it can be handy to just push the updates to production, quickly.
290
291 To use this task, specify the files and directories you want to copy as a \
292 comma-delimited list in the FILES environment variable. All directories \
293 will be processed recursively, with all files being pushed to the \
919f868 @jamis Make deploy:upload use the upload() helper for more efficient directo…
jamis authored
294 deployment servers.
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
295
296 $ cap deploy:upload FILES=templates,controller.rb
919f868 @jamis Make deploy:upload use the upload() helper for more efficient directo…
jamis authored
297
298 Dir globs are also supported:
299
300 $ cap deploy:upload FILES='config/apache/*.conf'
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
301 DESC
aab2ea2 @jamis Add deploy:check test for verifying that dependencies are in order fo…
jamis authored
302 task :upload, :except => { :no_release => true } do
919f868 @jamis Make deploy:upload use the upload() helper for more efficient directo…
jamis authored
303 files = (ENV["FILES"] || "").split(",").map { |f| Dir[f.strip] }.flatten
304 abort "Please specify at least one file or directory to update (via the FILES environment variable)" if files.empty?
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
305
919f868 @jamis Make deploy:upload use the upload() helper for more efficient directo…
jamis authored
306 files.each { |file| top.upload(file, File.join(current_path, file)) }
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
307 end
308
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
309 desc <<-DESC
59d5015 @leehambley Blank out the start, stop and restart tasks
leehambley authored
310 Blank task exists as a hook into which to install your own environment \
311 specific behaviour.
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
312 DESC
313 task :restart, :roles => :app, :except => { :no_release => true } do
59d5015 @leehambley Blank out the start, stop and restart tasks
leehambley authored
314 # Empty Task to overload with your platform specifics
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
315 end
316
0e5c38b @jamis make rollbacks work with processes that need the cwd to be valid to r…
jamis authored
317 namespace :rollback do
318 desc <<-DESC
319 [internal] Points the current symlink at the previous revision.
320 This is called by the rollback sequence, and should rarely (if
321 ever) need to be called directly.
322 DESC
323 task :revision, :except => { :no_release => true } do
907d907 @jamis cleanup some conditions now that previous_release is sane
jamis authored
324 if previous_release
0e5c38b @jamis make rollbacks work with processes that need the cwd to be valid to r…
jamis authored
325 run "rm #{current_path}; ln -s #{previous_release} #{current_path}"
907d907 @jamis cleanup some conditions now that previous_release is sane
jamis authored
326 else
327 abort "could not rollback the code because there is no prior release"
0e5c38b @jamis make rollbacks work with processes that need the cwd to be valid to r…
jamis authored
328 end
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
329 end
330
0e5c38b @jamis make rollbacks work with processes that need the cwd to be valid to r…
jamis authored
331 desc <<-DESC
332 [internal] Removes the most recently deployed release.
333 This is called by the rollback sequence, and should rarely
334 (if ever) need to be called directly.
335 DESC
336 task :cleanup, :except => { :no_release => true } do
337 run "if [ `readlink #{current_path}` != #{current_release} ]; then rm -rf #{current_release}; fi"
338 end
339
340 desc <<-DESC
341 Rolls back to the previously deployed version. The `current' symlink will \
342 be updated to point at the previously deployed version, and then the \
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
343 current release will be removed from the servers. You'll generally want \
344 to call `rollback' instead, as it performs a `restart' as well.
0e5c38b @jamis make rollbacks work with processes that need the cwd to be valid to r…
jamis authored
345 DESC
346 task :code, :except => { :no_release => true } do
347 revision
348 cleanup
349 end
350
351 desc <<-DESC
352 Rolls back to a previous version and restarts. This is handy if you ever \
353 discover that you've deployed a lemon; `cap rollback' and you're right \
354 back where you were, on the previously deployed version.
355 DESC
356 task :default do
357 revision
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
358 restart
0e5c38b @jamis make rollbacks work with processes that need the cwd to be valid to r…
jamis authored
359 cleanup
360 end
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
361 end
362
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
363 desc <<-DESC
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
364 Run the migrate rake task. By default, it runs this in most recently \
365 deployed version of the app. However, you can specify a different release \
366 via the migrate_target variable, which must be one of :latest (for the \
367 default behavior), or :current (for the release indicated by the \
368 `current' symlink). Strings will work for those values instead of symbols, \
369 too. You can also specify additional environment variables to pass to rake \
370 via the migrate_env variable. Finally, you can specify the full path to the \
371 rake executable by setting the rake variable. The defaults are:
372
373 set :rake, "rake"
374 set :rails_env, "production"
375 set :migrate_env, ""
376 set :migrate_target, :latest
377 DESC
378 task :migrate, :roles => :db, :only => { :primary => true } do
7b4c66b @ppg Fix issue with missing rake and rails_env defined in migrate deploy r…
ppg authored
379 rake = fetch(:rake, "rake")
380 rails_env = fetch(:rails_env, "production")
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
381 migrate_env = fetch(:migrate_env, "")
382 migrate_target = fetch(:migrate_target, :latest)
383
384 directory = case migrate_target.to_sym
385 when :current then current_path
a69d76d @shame Fix deploy:migrations to use latest_release
shame authored
386 when :latest then latest_release
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
387 else raise ArgumentError, "unknown migration target #{migrate_target.inspect}"
388 end
389
f68fcae @cgriego Rails 3.1 Asset Pipeline support
cgriego authored
390 run "cd #{directory} && #{rake} RAILS_ENV=#{rails_env} #{migrate_env} db:migrate"
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
391 end
392
393 desc <<-DESC
394 Deploy and run pending migrations. This will work similarly to the \
395 `deploy' task, but will also run any pending migrations (via the \
396 `deploy:migrate' task) prior to updating the symlink. Note that the \
397 update in this case it is not atomic, and transactions are not used, \
398 because migrations are not guaranteed to be reversible.
399 DESC
400 task :migrations do
401 set :migrate_target, :latest
402 update_code
403 migrate
c46cefb @leehambley Modify for Rake 0.9.x Compatibility.
leehambley authored
404 create_symlink
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
405 restart
406 end
407
408 desc <<-DESC
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
409 Clean up old releases. By default, the last 5 releases are kept on each \
410 server (though you can change this with the keep_releases variable). All \
411 other deployed revisions are removed from the servers. By default, this \
412 will use sudo to clean up the old releases, but if sudo is not available \
413 for your environment, set the :use_sudo variable to false instead.
414 DESC
ca4e998 @philrosenstein Edited lib/capistrano/recipes/deploy.rb via GitHub
philrosenstein authored
415 task :cleanup, :except => { :no_release => true } do
9a6d2fb @jamis make the compat script more general. Add some missing deploy tasks. M…
jamis authored
416 count = fetch(:keep_releases, 5).to_i
a35da47 @philrosenstein make deploy:cleanup work with multi-host configuration
philrosenstein authored
417 local_releases = capture("ls -xt #{releases_path}").split.reverse
418 if count >= local_releases.length
9a6d2fb @jamis make the compat script more general. Add some missing deploy tasks. M…
jamis authored
419 logger.important "no old releases to clean up"
420 else
a35da47 @philrosenstein make deploy:cleanup work with multi-host configuration
philrosenstein authored
421 logger.info "keeping #{count} of #{local_releases.length} deployed releases"
422 directories = (local_releases - local_releases.last(count)).map { |release|
9a6d2fb @jamis make the compat script more general. Add some missing deploy tasks. M…
jamis authored
423 File.join(releases_path, release) }.join(" ")
424
552a492 @jamis Make deploy:setup obey the :use_sudo and :runner directives, and gene…
jamis authored
425 try_sudo "rm -rf #{directories}"
9a6d2fb @jamis make the compat script more general. Add some missing deploy tasks. M…
jamis authored
426 end
427 end
428
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
429 desc <<-DESC
430 Test deployment dependencies. Checks things like directory permissions, \
431 necessary utilities, and so forth, reporting on the things that appear to \
432 be incorrect or missing. This is good for making sure a deploy has a \
433 chance of working before you actually run `cap deploy'.
12feba4 @jamis allow custom dependencies to be declared, which deploy:check will verify
jamis authored
434
435 You can define your own dependencies, as well, using the `depend' method:
436
437 depend :remote, :gem, "tzinfo", ">=0.3.3"
438 depend :local, :command, "svn"
439 depend :remote, :directory, "/u/depot/files"
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
440 DESC
aab2ea2 @jamis Add deploy:check test for verifying that dependencies are in order fo…
jamis authored
441 task :check, :except => { :no_release => true } do
442 dependencies = strategy.check!
12feba4 @jamis allow custom dependencies to be declared, which deploy:check will verify
jamis authored
443
444 other = fetch(:dependencies, {})
445 other.each do |location, types|
446 types.each do |type, calls|
447 if type == :gem
448 dependencies.send(location).command(fetch(:gem_command, "gem")).or("`gem' command could not be found. Try setting :gem_command")
449 end
450
451 calls.each do |args|
452 dependencies.send(location).send(type, *args)
453 end
454 end
455 end
456
aab2ea2 @jamis Add deploy:check test for verifying that dependencies are in order fo…
jamis authored
457 if dependencies.pass?
458 puts "You appear to have all necessary dependencies installed"
459 else
460 puts "The following dependencies failed. Please check them and try again:"
461 dependencies.reject { |d| d.pass? }.each do |d|
462 puts "--> #{d.message}"
463 end
8f7556b @jamis Make sure deploy:check aborts if it fails
jamis authored
464 abort
aab2ea2 @jamis Add deploy:check test for verifying that dependencies are in order fo…
jamis authored
465 end
466 end
467
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
468 desc <<-DESC
469 Deploys and starts a `cold' application. This is useful if you have not \
470 deployed your application before, or if your application is (for some \
0964dc1 @jamis Make sure deploy:cold also runs migrations before starting the app. H…
jamis authored
471 other reason) not currently running. It will deploy the code, run any \
7ae3dd9 @jamis Doc corrections. Fix dependency checking for remote cache strategy.
jamis authored
472 pending migrations, and then instead of invoking `deploy:restart', it will \
2b5d913 @jamis Kill the "deploy:app" namespace and move those tasks into deploy, dir…
jamis authored
473 invoke `deploy:start' to fire up the application servers.
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
474 DESC
3a5724a @jamis add deploy:app:start, deploy:app:stop, and deploy:cold, and correspon…
jamis authored
475 task :cold do
476 update
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
477 migrate
478 start
479 end
480
481 desc <<-DESC
59d5015 @leehambley Blank out the start, stop and restart tasks
leehambley authored
482 Blank task exists as a hook into which to install your own environment \
483 specific behaviour.
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
484 DESC
485 task :start, :roles => :app do
59d5015 @leehambley Blank out the start, stop and restart tasks
leehambley authored
486 # Empty Task to overload with your platform specifics
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
487 end
488
489 desc <<-DESC
59d5015 @leehambley Blank out the start, stop and restart tasks
leehambley authored
490 Blank task exists as a hook into which to install your own environment \
491 specific behaviour.
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
492 DESC
493 task :stop, :roles => :app do
59d5015 @leehambley Blank out the start, stop and restart tasks
leehambley authored
494 # Empty Task to overload with your platform specifics
3a5724a @jamis add deploy:app:start, deploy:app:stop, and deploy:cold, and correspon…
jamis authored
495 end
496
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
497 namespace :pending do
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
498 desc <<-DESC
499 Displays the `diff' since your last deploy. This is useful if you want \
500 to examine what changes are about to be deployed. Note that this might \
501 not be supported on all SCM's.
502 DESC
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
503 task :diff, :except => { :no_release => true } do
241ebb0 @jamis Allow (e.g.) scm_command and local_scm_command to be set in the event…
jamis authored
504 system(source.local.diff(current_revision))
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
505 end
506
0e774ac @jamis Make the task description parser a bit smarter, so that heredocs can …
jamis authored
507 desc <<-DESC
508 Displays the commits since your last deploy. This is good for a summary \
509 of the changes that have occurred since the last deploy. Note that this \
510 might not be supported on all SCM's.
511 DESC
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
512 task :default, :except => { :no_release => true } do
577fffe @jamis Fix deploy:pending to query SCM for the subsequent revision so that i…
jamis authored
513 from = source.next_revision(current_revision)
514 system(source.local.log(from))
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
515 end
516 end
9a6d2fb @jamis make the compat script more general. Add some missing deploy tasks. M…
jamis authored
517
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
518 namespace :web do
519 desc <<-DESC
520 Present a maintenance page to visitors. Disables your application's web \
0c45e4a @tinogomes New variable "maintenance_basename" to set maintenance file basename
tinogomes authored
521 interface by writing a "#{maintenance_basename}.html" file to each web server. The \
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
522 servers must be configured to detect the presence of this file, and if \
523 it is present, always display it instead of performing the request.
524
525 By default, the maintenance page will just say the site is down for \
526 "maintenance", and will be back "shortly", but you can customize the \
527 page by specifying the REASON and UNTIL environment variables:
528
529 $ cap deploy:web:disable \\
530 REASON="hardware upgrade" \\
531 UNTIL="12pm Central Time"
532
eb34ca8 @mtylty adds maintenance_template_path configuration option
mtylty authored
533 You can use a different template for the maintenance page by setting the \
534 :maintenance_template_path variable in your deploy.rb file. The template file \
535 should either be a plaintext or an erb file.
536
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
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'
0c45e4a @tinogomes New variable "maintenance_basename" to set maintenance file basename
tinogomes authored
541 on_rollback { run "rm #{shared_path}/system/#{maintenance_basename}.html" }
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
542
5e126d7 @leehambley Includes a warn()ing from the web:disable task with the example block…
leehambley authored
543 warn <<-EOHTACCESS
0c45e4a @tinogomes New variable "maintenance_basename" to set maintenance file basename
tinogomes authored
544
5e126d7 @leehambley Includes a warn()ing from the web:disable task with the example block…
leehambley authored
545 # Please add something like this to your site's htaccess to redirect users to the maintenance page.
546 # More Info: http://www.shiftcommathree.com/articles/make-your-rails-maintenance-page-respond-with-a-503
0c45e4a @tinogomes New variable "maintenance_basename" to set maintenance file basename
tinogomes authored
547
548 ErrorDocument 503 /system/#{maintenance_basename}.html
5e126d7 @leehambley Includes a warn()ing from the web:disable task with the example block…
leehambley authored
549 RewriteEngine On
550 RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|png)$
0c45e4a @tinogomes New variable "maintenance_basename" to set maintenance file basename
tinogomes authored
551 RewriteCond %{DOCUMENT_ROOT}/system/#{maintenance_basename}.html -f
552 RewriteCond %{SCRIPT_FILENAME} !#{maintenance_basename}.html
5e126d7 @leehambley Includes a warn()ing from the web:disable task with the example block…
leehambley authored
553 RewriteRule ^.*$ - [redirect=503,last]
554 EOHTACCESS
555
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
556 reason = ENV['REASON']
557 deadline = ENV['UNTIL']
558
eb34ca8 @mtylty adds maintenance_template_path configuration option
mtylty authored
559 template = File.read(maintenance_template_path)
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
560 result = ERB.new(template).result(binding)
561
0c45e4a @tinogomes New variable "maintenance_basename" to set maintenance file basename
tinogomes authored
562 put result, "#{shared_path}/system/#{maintenance_basename}.html", :mode => 0644
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
563 end
564
565 desc <<-DESC
566 Makes the application web-accessible again. Removes the \
0c45e4a @tinogomes New variable "maintenance_basename" to set maintenance file basename
tinogomes authored
567 "#{maintenance_basename}.html" page generated by deploy:web:disable, which (if your \
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
568 web servers are configured correctly) will make your application \
569 web-accessible again.
570 DESC
571 task :enable, :roles => :web, :except => { :no_release => true } do
0c45e4a @tinogomes New variable "maintenance_basename" to set maintenance file basename
tinogomes authored
572 run "rm #{shared_path}/system/#{maintenance_basename}.html"
9477133 @leehambley Reverting the deploy recipe to Jamis' SHA1: 907d9073eadf9468284da1717…
leehambley authored
573 end
574 end
14770c8 @jamis Add (opt-in) deployment system. Add `deployify' script.
jamis authored
575 end
Something went wrong with that request. Please try again.