Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

integrate capistrano_rsync_with_remote_cache, eliminating the need to…

… install it separately via gem

git-svn-id: svn://localhost/lab/trunk/plugins/viget_deployment@503 e959a6d6-1924-0410-92b3-a6fa492f4c66
  • Loading branch information...
commit 6cb9ef2b00faef21438afd223eb6f76a50238bbe 1 parent 11654a1
Mark Cornick authored
View
93 lib/capistrano/recipes/deploy/strategy/rsync_with_remote_cache.rb
@@ -0,0 +1,93 @@
+require 'capistrano/recipes/deploy/strategy/remote'
+
+module Capistrano
+ module Deploy
+ module Strategy
+ class RsyncWithRemoteCache < Remote
+
+ # The deployment method itself, in three major steps.
+ def deploy!
+
+ # Step 1: Update the local cache.
+ system(command)
+ File.open(File.join(local_cache, "REVISION"), "w") { |f| f.puts(revision) }
+
+ # Step 2: Update the remote cache.
+ logger.trace "copying local cache to remote"
+ find_servers(:roles => :app, :except => { :no_release => true }).each do |server|
+ system("rsync #{rsync_options} #{local_cache}/ #{rsync_host(server)}:#{repository_cache}/")
+ end
+
+ # Step 3: Copy the remote cache into place.
+ run("rsync -a --delete #{repository_cache}/ #{configuration[:release_path]}/ && #{mark}")
+ end
+
+ # Defines commands that should be checked for by deploy:check. These include the SCM command
+ # on the local end, and rsync on both ends. Note that the SCM command is not needed on the
+ # remote end.
+ def check!
+ super.check do |d|
+ d.local.command(source.command)
+ d.local.command('rsync')
+ d.remote.command('rsync')
+ end
+ end
+
+ private
+
+ # Path to the remote cache. We use a variable name and default that are compatible with
+ # the stock remote_cache strategy, for easy migration.
+ def repository_cache
+ File.join(shared_path, configuration[:repository_cache] || "cached-copy")
+ end
+
+ # Path to the local cache. If not specified in the Capfile, we use an arbitrary default.
+ def local_cache
+ configuration[:local_cache] || ".rsync_cache"
+ end
+
+ # Options to use for rsync in step 2. If not specified in the Capfile, we use the default
+ # from prior versions.
+ def rsync_options
+ configuration[:rsync_options] || "-az --delete"
+ end
+
+ # Returns the host used in the rsync command, prefixed with user@ if user is specified in Capfile.
+ def rsync_host(server)
+ if configuration[:user]
+ "#{configuration[:user]}@#{server.host}"
+ else
+ server.host
+ end
+ end
+
+ # Command to get source from SCM on the local side. If the local_cache directory exists,
+ # we check to see if it's an SCM checkout that matches the currently configured repository.
+ # If it matches, we update it. If it doesn't match (either it's for another repository or
+ # not a checkout at all), we remove the directory and recreate it with a fresh SCM checkout.
+ # If the directory doesn't exist, we create it with a fresh SCM checkout.
+ # TODO: punt in some sensible way if local_cache exists but is a regular file.
+ # TODO: `svn info` restricts this to svn only, which is not a problem for us at Viget, but
+ # may be a problem elsewhere. Try to find a less svn-specific way of doing this.
+ def command
+ if (File.exists?(local_cache) && File.directory?(local_cache))
+ unless (`svn info #{local_cache} | sed -n 's/URL: //p'`.strip != configuration[:repository])
+ logger.trace "updating local cache to revision #{revision}"
+ cmd = source.sync(revision, local_cache)
+ else
+ logger.trace "repository has changed; removing old local cache"
+ system("rm -rf #{local_cache}")
+ end
+ end
+ unless (File.exists?(local_cache) || File.directory?(local_cache))
+ logger.trace "creating local cache with revision #{revision}"
+ File.delete(local_cache) if File.exists?(local_cache)
+ Dir.mkdir(File.dirname(local_cache)) unless File.directory?(File.dirname(local_cache))
+ cmd = source.checkout(revision, local_cache)
+ end
+ cmd
+ end
+ end
+ end
+ end
+end
View
3  recipes/viget_deployment_recipes.rb
@@ -1,3 +1,6 @@
+# make sure we can find rsync_with_remote_cache
+$: << File.expand_path("vendor/plugins/viget_deployment/lib")
+
require 'rubygems'
require 'yaml'
require 'erb'
Please sign in to comment.
Something went wrong with that request. Please try again.