forked from vigetlabs/capistrano_rsync_with_remote_cache
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Mark Cornick
committed
Jan 23, 2008
1 parent
11654a1
commit 6cb9ef2
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
lib/capistrano/recipes/deploy/strategy/rsync_with_remote_cache.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters