leehambley-railsless-deploy 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/railsless-deploy.rb +422 -0
  2. metadata +53 -0
@@ -0,0 +1,422 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+
3
+ require 'yaml'
4
+ require 'capistrano/recipes/deploy/scm'
5
+ require 'capistrano/recipes/deploy/strategy'
6
+
7
+ def _cset(name, *args, &block)
8
+ unless exists?(name)
9
+ set(name, *args, &block)
10
+ end
11
+ end
12
+
13
+ # =========================================================================
14
+ # These variables MUST be set in the client capfiles. If they are not set,
15
+ # the deploy will fail with an error.
16
+ # =========================================================================
17
+
18
+ _cset(:application) { abort "Please specify the name of your application, set :application, 'foo'" }
19
+ _cset(:repository) { abort "Please specify the repository that houses your application's code, set :repository, 'foo'" }
20
+
21
+ # =========================================================================
22
+ # These variables may be set in the client capfile if their default values
23
+ # are not sufficient.
24
+ # =========================================================================
25
+
26
+ _cset :scm, :subversion
27
+ _cset :deploy_via, :checkout
28
+
29
+ _cset(:deploy_to) { "/u/apps/#{application}" }
30
+ _cset(:revision) { source.head }
31
+
32
+ # =========================================================================
33
+ # These variables should NOT be changed unless you are very confident in
34
+ # what you are doing. Make sure you understand all the implications of your
35
+ # changes if you do decide to muck with these!
36
+ # =========================================================================
37
+
38
+ _cset(:source) { Capistrano::Deploy::SCM.new(scm, self) }
39
+ _cset(:real_revision) { source.local.query_revision(revision) { |cmd| with_env("LC_ALL", "C") { run_locally(cmd) } } }
40
+
41
+ _cset(:strategy) { Capistrano::Deploy::Strategy.new(deploy_via, self) }
42
+
43
+ _cset(:release_name) { set :deploy_timestamped, true; Time.now.utc.strftime("%Y%m%d%H%M%S") }
44
+
45
+ _cset :version_dir, "releases"
46
+ _cset :shared_dir, "shared"
47
+ _cset :shared_children, %w(system log pids)
48
+ _cset :current_dir, "current"
49
+
50
+ _cset(:releases_path) { File.join(deploy_to, version_dir) }
51
+ _cset(:shared_path) { File.join(deploy_to, shared_dir) }
52
+ _cset(:current_path) { File.join(deploy_to, current_dir) }
53
+ _cset(:release_path) { File.join(releases_path, release_name) }
54
+
55
+ _cset(:releases) { capture("ls -xt #{releases_path}").split.reverse }
56
+ _cset(:current_release) { File.join(releases_path, releases.last) }
57
+ _cset(:previous_release) { releases.length > 1 ? File.join(releases_path, releases[-2]) : nil }
58
+
59
+ _cset(:current_revision) { capture("cat #{current_path}/REVISION").chomp }
60
+ _cset(:latest_revision) { capture("cat #{current_release}/REVISION").chomp }
61
+ _cset(:previous_revision) { capture("cat #{previous_release}/REVISION").chomp }
62
+
63
+ _cset(:run_method) { fetch(:use_sudo, true) ? :sudo : :run }
64
+
65
+ # some tasks, like symlink, need to always point at the latest release, but
66
+ # they can also (occassionally) be called standalone. In the standalone case,
67
+ # the timestamped release_path will be inaccurate, since the directory won't
68
+ # actually exist. This variable lets tasks like symlink work either in the
69
+ # standalone case, or during deployment.
70
+ _cset(:latest_release) { exists?(:deploy_timestamped) ? release_path : current_release }
71
+
72
+ # =========================================================================
73
+ # These are helper methods that will be available to your recipes.
74
+ # =========================================================================
75
+
76
+ # Auxiliary helper method for the `deploy:check' task. Lets you set up your
77
+ # own dependencies.
78
+ def depend(location, type, *args)
79
+ deps = fetch(:dependencies, {})
80
+ deps[location] ||= {}
81
+ deps[location][type] ||= []
82
+ deps[location][type] << args
83
+ set :dependencies, deps
84
+ end
85
+
86
+ # Temporarily sets an environment variable, yields to a block, and restores
87
+ # the value when it is done.
88
+ def with_env(name, value)
89
+ saved, ENV[name] = ENV[name], value
90
+ yield
91
+ ensure
92
+ ENV[name] = saved
93
+ end
94
+
95
+ # logs the command then executes it locally.
96
+ # returns the command output as a string
97
+ def run_locally(cmd)
98
+ logger.trace "executing locally: #{cmd.inspect}" if logger
99
+ `#{cmd}`
100
+ end
101
+
102
+ # If a command is given, this will try to execute the given command, as
103
+ # described below. Otherwise, it will return a string for use in embedding in
104
+ # another command, for executing that command as described below.
105
+ #
106
+ # If :run_method is :sudo (or :use_sudo is true), this executes the given command
107
+ # via +sudo+. Otherwise is uses +run+. If :as is given as a key, it will be
108
+ # passed as the user to sudo as, if using sudo. If the :as key is not given,
109
+ # it will default to whatever the value of the :admin_runner variable is,
110
+ # which (by default) is unset.
111
+ #
112
+ # THUS, if you want to try to run something via sudo, and what to use the
113
+ # root user, you'd just to try_sudo('something'). If you wanted to try_sudo as
114
+ # someone else, you'd just do try_sudo('something', :as => "bob"). If you
115
+ # always wanted sudo to run as a particular user, you could do
116
+ # set(:admin_runner, "bob").
117
+ def try_sudo(*args)
118
+ options = args.last.is_a?(Hash) ? args.pop : {}
119
+ command = args.shift
120
+ raise ArgumentError, "too many arguments" if args.any?
121
+
122
+ as = options.fetch(:as, fetch(:admin_runner, nil))
123
+ via = fetch(:run_method, :sudo)
124
+ if command
125
+ invoke_command(command, :via => via, :as => as)
126
+ elsif via == :sudo
127
+ sudo(:as => as)
128
+ else
129
+ ""
130
+ end
131
+ end
132
+
133
+ # Same as sudo, but tries sudo with :as set to the value of the :runner
134
+ # variable (which defaults to "app").
135
+ def try_runner(*args)
136
+ options = args.last.is_a?(Hash) ? args.pop : {}
137
+ args << options.merge(:as => fetch(:runner, "app"))
138
+ try_sudo(*args)
139
+ end
140
+
141
+ # =========================================================================
142
+ # These are the tasks that are available to help with deploying web apps,
143
+ # and specifically, Rails applications. You can have cap give you a summary
144
+ # of them with `cap -T'.
145
+ # =========================================================================
146
+
147
+ namespace :deploy do
148
+ desc <<-DESC
149
+ Deploys your project. This calls both `update' and `restart'. Note that \
150
+ this will generally only work for applications that have already been deployed \
151
+ once. For a "cold" deploy, you'll want to take a look at the `deploy:cold' \
152
+ task, which handles the cold start specifically.
153
+ DESC
154
+ task :default do
155
+ update
156
+ # restart
157
+ end
158
+
159
+ [:start, :stop, :restart].each do |deprecated_task|
160
+ desc "#{deprecated_task.to_s} is deprecated. Please see "
161
+ task deprecated_task do
162
+
163
+ end
164
+ end
165
+
166
+
167
+
168
+ desc <<-DESC
169
+ Prepares one or more servers for deployment. Before you can use any \
170
+ of the Capistrano deployment tasks with your project, you will need to \
171
+ make sure all of your servers have been prepared with `cap deploy:setup'. When \
172
+ you add a new server to your cluster, you can easily run the setup task \
173
+ on just that server by specifying the HOSTS environment variable:
174
+
175
+ $ cap HOSTS=new.server.com deploy:setup
176
+
177
+ It is safe to run this task on servers that have already been set up; it \
178
+ will not destroy any deployed revisions or data.
179
+ DESC
180
+ task :setup, :except => { :no_release => true } do
181
+ dirs = [deploy_to, releases_path, shared_path]
182
+ dirs += shared_children.map { |d| File.join(shared_path, d) }
183
+ run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
184
+ end
185
+
186
+ desc <<-DESC
187
+ Copies your project and updates the symlink. It does this in a \
188
+ transaction, so that if either `update_code' or `symlink' fail, all \
189
+ changes made to the remote servers will be rolled back, leaving your \
190
+ system in the same state it was in before `update' was invoked. Usually, \
191
+ you will want to call `deploy' instead of `update', but `update' can be \
192
+ handy if you want to deploy, but not immediately restart your application.
193
+ DESC
194
+ task :update do
195
+ transaction do
196
+ update_code
197
+ symlink
198
+ end
199
+ end
200
+
201
+ desc <<-DESC
202
+ Copies your project to the remote servers. This is the first stage \
203
+ of any deployment; moving your updated code and assets to the deployment \
204
+ servers. You will rarely call this task directly, however; instead, you \
205
+ should call the `deploy' task (to do a complete deploy) or the `update' \
206
+ task (if you want to perform the `restart' task separately).
207
+
208
+ You will need to make sure you set the :scm variable to the source \
209
+ control software you are using (it defaults to :subversion), and the \
210
+ :deploy_via variable to the strategy you want to use to deploy (it \
211
+ defaults to :checkout).
212
+ DESC
213
+ task :update_code, :except => { :no_release => true } do
214
+ on_rollback { run "rm -rf #{release_path}; true" }
215
+ strategy.deploy!
216
+ finalize_update
217
+ end
218
+
219
+ desc <<-DESC
220
+ [internal] Touches up the released code. This is called by update_code \
221
+ after the basic deploy finishes. It assumes a Rails project was deployed, \
222
+ so if you are deploying something else, you may want to override this \
223
+ task with your own environment's requirements.
224
+
225
+ This task will make the release group-writable (if the :group_writable \
226
+ variable is set to true, which is the default). It will then set up \
227
+ symlinks to the shared directory for the log, system, and tmp/pids \
228
+ directories, and will lastly touch all assets in public/images, \
229
+ public/stylesheets, and public/javascripts so that the times are \
230
+ consistent (so that asset timestamping works). This touch process \
231
+ is only carried out if the :normalize_asset_timestamps variable is \
232
+ set to true, which is the default.
233
+ DESC
234
+ task :finalize_update, :except => { :no_release => true } do
235
+ run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
236
+ end
237
+
238
+ desc <<-DESC
239
+ Updates the symlink to the most recently deployed version. Capistrano works \
240
+ by putting each new release of your application in its own directory. When \
241
+ you deploy a new version, this task's job is to update the `current' symlink \
242
+ to point at the new version. You will rarely need to call this task \
243
+ directly; instead, use the `deploy' task (which performs a complete \
244
+ deploy, including `restart') or the 'update' task (which does everything \
245
+ except `restart').
246
+ DESC
247
+ task :symlink, :except => { :no_release => true } do
248
+ on_rollback do
249
+ if previous_release
250
+ run "rm -f #{current_path}; ln -s #{previous_release} #{current_path}; true"
251
+ else
252
+ logger.important "no previous release to rollback to, rollback of symlink skipped"
253
+ end
254
+ end
255
+
256
+ run "rm -f #{current_path} && ln -s #{latest_release} #{current_path}"
257
+ end
258
+
259
+ desc <<-DESC
260
+ Copy files to the currently deployed version. This is useful for updating \
261
+ files piecemeal, such as when you need to quickly deploy only a single \
262
+ file. Some files, such as updated templates, images, or stylesheets, \
263
+ might not require a full deploy, and especially in emergency situations \
264
+ it can be handy to just push the updates to production, quickly.
265
+
266
+ To use this task, specify the files and directories you want to copy as a \
267
+ comma-delimited list in the FILES environment variable. All directories \
268
+ will be processed recursively, with all files being pushed to the \
269
+ deployment servers.
270
+
271
+ $ cap deploy:upload FILES=templates,controller.rb
272
+
273
+ Dir globs are also supported:
274
+
275
+ $ cap deploy:upload FILES='config/apache/*.conf'
276
+ DESC
277
+ task :upload, :except => { :no_release => true } do
278
+ files = (ENV["FILES"] || "").split(",").map { |f| Dir[f.strip] }.flatten
279
+ abort "Please specify at least one file or directory to update (via the FILES environment variable)" if files.empty?
280
+
281
+ files.each { |file| top.upload(file, File.join(current_path, file)) }
282
+ end
283
+
284
+ namespace :rollback do
285
+ desc <<-DESC
286
+ [internal] Points the current symlink at the previous revision.
287
+ This is called by the rollback sequence, and should rarely (if
288
+ ever) need to be called directly.
289
+ DESC
290
+ task :revision, :except => { :no_release => true } do
291
+ if previous_release
292
+ run "rm #{current_path}; ln -s #{previous_release} #{current_path}"
293
+ else
294
+ abort "could not rollback the code because there is no prior release"
295
+ end
296
+ end
297
+
298
+ desc <<-DESC
299
+ [internal] Removes the most recently deployed release.
300
+ This is called by the rollback sequence, and should rarely
301
+ (if ever) need to be called directly.
302
+ DESC
303
+ task :cleanup, :except => { :no_release => true } do
304
+ run "if [ `readlink #{current_path}` != #{current_release} ]; then rm -rf #{current_release}; fi"
305
+ end
306
+
307
+ desc <<-DESC
308
+ Rolls back to the previously deployed version. The `current' symlink will \
309
+ be updated to point at the previously deployed version, and then the \
310
+ current release will be removed from the servers.
311
+ DESC
312
+ task :code, :except => { :no_release => true } do
313
+ revision
314
+ cleanup
315
+ end
316
+
317
+ desc <<-DESC
318
+ Rolls back to a previous version and restarts. This is handy if you ever \
319
+ discover that you've deployed a lemon; `cap rollback' and you're right \
320
+ back where you were, on the previously deployed version.
321
+ DESC
322
+ task :default do
323
+ revision
324
+ cleanup
325
+ end
326
+ end
327
+
328
+ desc <<-DESC
329
+ Clean up old releases. By default, the last 5 releases are kept on each \
330
+ server (though you can change this with the keep_releases variable). All \
331
+ other deployed revisions are removed from the servers. By default, this \
332
+ will use sudo to clean up the old releases, but if sudo is not available \
333
+ for your environment, set the :use_sudo variable to false instead.
334
+ DESC
335
+ task :cleanup, :except => { :no_release => true } do
336
+ count = fetch(:keep_releases, 5).to_i
337
+ if count >= releases.length
338
+ logger.important "no old releases to clean up"
339
+ else
340
+ logger.info "keeping #{count} of #{releases.length} deployed releases"
341
+
342
+ directories = (releases - releases.last(count)).map { |release|
343
+ File.join(releases_path, release) }.join(" ")
344
+
345
+ try_sudo "rm -rf #{directories}"
346
+ end
347
+ end
348
+
349
+ desc <<-DESC
350
+ Test deployment dependencies. Checks things like directory permissions, \
351
+ necessary utilities, and so forth, reporting on the things that appear to \
352
+ be incorrect or missing. This is good for making sure a deploy has a \
353
+ chance of working before you actually run `cap deploy'.
354
+
355
+ You can define your own dependencies, as well, using the `depend' method:
356
+
357
+ depend :remote, :gem, "tzinfo", ">=0.3.3"
358
+ depend :local, :command, "svn"
359
+ depend :remote, :directory, "/u/depot/files"
360
+ DESC
361
+ task :check, :except => { :no_release => true } do
362
+ dependencies = strategy.check!
363
+
364
+ other = fetch(:dependencies, {})
365
+ other.each do |location, types|
366
+ types.each do |type, calls|
367
+ if type == :gem
368
+ dependencies.send(location).command(fetch(:gem_command, "gem")).or("`gem' command could not be found. Try setting :gem_command")
369
+ end
370
+
371
+ calls.each do |args|
372
+ dependencies.send(location).send(type, *args)
373
+ end
374
+ end
375
+ end
376
+
377
+ if dependencies.pass?
378
+ puts "You appear to have all necessary dependencies installed"
379
+ else
380
+ puts "The following dependencies failed. Please check them and try again:"
381
+ dependencies.reject { |d| d.pass? }.each do |d|
382
+ puts "--> #{d.message}"
383
+ end
384
+ abort
385
+ end
386
+ end
387
+
388
+ desc <<-DESC
389
+ Deploys and starts a `cold' application. This is useful if you have not \
390
+ deployed your application before, or if your application is (for some \
391
+ other reason) not currently running. It will deploy the code, run any \
392
+ pending migrations, and then instead of invoking `deploy:restart', it will \
393
+ invoke `deploy:start' to fire up the application servers.
394
+ DESC
395
+ task :cold do
396
+ update
397
+ end
398
+
399
+ namespace :pending do
400
+ desc <<-DESC
401
+ Displays the `diff' since your last deploy. This is useful if you want \
402
+ to examine what changes are about to be deployed. Note that this might \
403
+ not be supported on all SCM's.
404
+ DESC
405
+ task :diff, :except => { :no_release => true } do
406
+ system(source.local.diff(current_revision))
407
+ end
408
+
409
+ desc <<-DESC
410
+ Displays the commits since your last deploy. This is good for a summary \
411
+ of the changes that have occurred since the last deploy. Note that this \
412
+ might not be supported on all SCM's.
413
+ DESC
414
+ task :default, :except => { :no_release => true } do
415
+ from = source.next_revision(current_revision)
416
+ system(source.local.log(from))
417
+ end
418
+ end
419
+
420
+ end
421
+
422
+ end # Capistrano::Configuration.instance(:must_exist).load do
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: leehambley-railsless-deploy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.12
5
+ platform: ruby
6
+ authors:
7
+ - Lee Hambley
8
+ autorequire: railsless-deploy
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Railsless deploy recipe for capistrano, replaces the default
17
+ email: lee.hambley@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/railsless-deploy.rb
26
+ has_rdoc: false
27
+ homepage: http://lee.hambley.name/
28
+ post_install_message:
29
+ rdoc_options: []
30
+
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: "0"
38
+ version:
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ requirements: []
46
+
47
+ rubyforge_project:
48
+ rubygems_version: 1.2.0
49
+ signing_key:
50
+ specification_version: 2
51
+ summary: Deployment recipe for Capistrano without the Railsisms,
52
+ test_files: []
53
+