From c9165dbdf96322d55ca543d4703e5b9caa95570f Mon Sep 17 00:00:00 2001 From: Samuel Reed Date: Mon, 3 Mar 2014 13:07:19 +0800 Subject: [PATCH 1/5] Move default options to load:defaults so they can be properly overridden. --- lib/capistrano/rsync.rb | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/capistrano/rsync.rb b/lib/capistrano/rsync.rb index 758d96b..a358eb0 100644 --- a/lib/capistrano/rsync.rb +++ b/lib/capistrano/rsync.rb @@ -5,23 +5,28 @@ # private API and internals of Capistrano::Rsync. If you think something should # be public for extending and hooking, please let me know! -set :rsync_options, [] -set :rsync_copy, "rsync --archive --acls --xattrs" - -# Stage is used on your local machine for rsyncing from. -set :rsync_stage, "tmp/deploy" - -# Cache is used on the server to copy files to from to the release directory. -# Saves you rsyncing your whole app folder each time. If you nil rsync_cache, -# Capistrano::Rsync will sync straight to the release path. -set :rsync_cache, "shared/deploy" - rsync_cache = lambda do cache = fetch(:rsync_cache) cache = deploy_to + "/" + cache if cache && cache !~ /^\// cache end +# Use cap3's load:defaults to set default vars so that they can be overridden. +namespace :load do + task :defaults do + set :rsync_options, [] + set :rsync_copy, "rsync --archive --acls --xattrs" + + # Stage is used on your local machine for rsyncing from. + set :rsync_stage, "tmp/deploy" + + # Cache is used on the server to copy files to from to the release directory. + # Saves you rsyncing your whole app folder each time. If you nil rsync_cache, + # Capistrano::Rsync will sync straight to the release path. + set :rsync_cache, "shared/deploy" + end +end + Rake::Task["deploy:check"].enhance ["rsync:hook_scm"] Rake::Task["deploy:updating"].enhance ["rsync:hook_scm"] From 2dc9008077ca4ecbc50fceb9ef8b89a2f8ac0a81 Mon Sep 17 00:00:00 2001 From: Samuel Reed Date: Mon, 3 Mar 2014 15:46:21 +0800 Subject: [PATCH 2/5] Move usage inside SSHKit so logging shows up as expected. --- README.md | 17 ++++++++++++----- lib/capistrano/rsync.rb | 36 ++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 38ba43c..0cefe65 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ require "capistrano/rsync" Set some `rsync_options` to your liking: ```ruby -set :rsync_options, %w[--recursive --delete --delete-excluded --exclude .git*] +set :rsync_options, %w[--recursive --delete --delete-excluded --exclude=.git*] ``` And after setting regular Capistrano options, deploy as usual! @@ -60,16 +60,23 @@ After that, Capistrano takes over and runs its usual tasks and symlinking. ### Exclude files from being deployed If you don't want to deploy everything you've committed to your repository, pass -some `--exclude` options to Rsync: +some `--exclude` options to Rsync (note the equals signs): ```ruby set :rsync_options, %w[ --recursive --delete --delete-excluded - --exclude .git* - --exclude /config/database.yml - --exclude /test/*** + --exclude=.git* + --exclude=/config/database.yml + --exclude=/test/*** + --include=/etc/folder + --exclude=/etc/* ] ``` +The `=` is required in order for patterns to be properly read. Note the order of the +`--include` and `--exclude` params; if you wish to exclude an entire directory except +a certain folder, because of an Rsync quirk you must specify the include first. + + ### Precompile assets before deploy Capistrano::Rsync runs `rsync:stage` before rsyncing. Hook to that like this: ```ruby diff --git a/lib/capistrano/rsync.rb b/lib/capistrano/rsync.rb index a358eb0..5e450c5 100644 --- a/lib/capistrano/rsync.rb +++ b/lib/capistrano/rsync.rb @@ -32,15 +32,17 @@ desc "Stage and rsync to the server (or its cache)." task :rsync => %w[rsync:stage] do - roles(:all).each do |role| - user = role.user + "@" if !role.user.nil? - - rsync = %w[rsync] - rsync.concat fetch(:rsync_options) - rsync << fetch(:rsync_stage) + "/" - rsync << "#{user}#{role.hostname}:#{rsync_cache.call || release_path}" + on roles(:all), in: :parallel do |host| + user = host.user + "@" if !host.user.nil? + + run_locally do + rsynccmd = [] + rsynccmd.concat(fetch(:rsync_options)) + rsynccmd << fetch(:rsync_stage) + "/" + rsynccmd << "#{user}#{host.hostname}:#{rsync_cache.call || release_path}" + execute :rsync, *rsynccmd + end - Kernel.system *rsync end end @@ -62,20 +64,18 @@ task :create_stage do next if File.directory?(fetch(:rsync_stage)) - clone = %W[git clone] - clone << fetch(:repo_url, ".") - clone << fetch(:rsync_stage) - Kernel.system *clone + run_locally do + execute :git, "clone #{fetch(:repo_url, '.')} #{fetch(:rsync_stage)}"; + end end desc "Stage the repository in a local directory." task :stage => %w[create_stage] do - Dir.chdir fetch(:rsync_stage) do - update = %W[git fetch --quiet --all --prune] - Kernel.system *update - - checkout = %W[git reset --hard origin/#{fetch(:branch)}] - Kernel.system *checkout + run_locally do + within fetch(:rsync_stage) do + execute :git, "fetch --quiet --all --prune" + execute :git, "reset --hard origin/#{fetch(:branch)}" + end end end From d9a7a7516dc48466af0a24e25c95a09132e21309 Mon Sep 17 00:00:00 2001 From: William Hurley Date: Mon, 2 Jun 2014 14:29:56 -0400 Subject: [PATCH 3/5] Resolving issue with set_current_revision not declared --- lib/capistrano/rsync.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/capistrano/rsync.rb b/lib/capistrano/rsync.rb index a358eb0..278757c 100644 --- a/lib/capistrano/rsync.rb +++ b/lib/capistrano/rsync.rb @@ -47,11 +47,11 @@ namespace :rsync do task :hook_scm do Rake::Task.define_task("#{scm}:check") do - invoke "rsync:check" + invoke "rsync:check" end Rake::Task.define_task("#{scm}:create_release") do - invoke "rsync:release" + invoke "rsync:release" end end @@ -91,4 +91,14 @@ # Matches the naming scheme of git tasks. # Plus was part of the public API in Capistrano::Rsync <= v0.2.1. task :create_release => %w[release] + + desc "Set the current revision" + task :set_current_revision do + run_locally do + within fetch(:rsync_stage) do + rev = capture(:git, 'rev-parse', 'HEAD') + set :current_revision, rev + end + end + end end From 0c763f45eb317ec85e908f3e17f90898b6911500 Mon Sep 17 00:00:00 2001 From: Alexander Gavrik Date: Thu, 9 Oct 2014 11:25:36 +1100 Subject: [PATCH 4/5] * Pass correct ssh port to the rsync * Use short git rev --- lib/capistrano/rsync.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/capistrano/rsync.rb b/lib/capistrano/rsync.rb index 278757c..e2e6874 100644 --- a/lib/capistrano/rsync.rb +++ b/lib/capistrano/rsync.rb @@ -34,13 +34,15 @@ task :rsync => %w[rsync:stage] do roles(:all).each do |role| user = role.user + "@" if !role.user.nil? + port = role.port rsync = %w[rsync] rsync.concat fetch(:rsync_options) rsync << fetch(:rsync_stage) + "/" + rsync << "-e \'ssh -p #{port}\'" if port rsync << "#{user}#{role.hostname}:#{rsync_cache.call || release_path}" - - Kernel.system *rsync +# Have to run system console to parse the command, otherwise port-part is parsed incorrectly + Kernel.system rsync.join(" ") end end @@ -96,7 +98,7 @@ task :set_current_revision do run_locally do within fetch(:rsync_stage) do - rev = capture(:git, 'rev-parse', 'HEAD') + rev = capture(:git, 'rev-parse', '--short', 'HEAD') set :current_revision, rev end end From 681e6f3c01ad03e0955647f20c7ac939c08410fd Mon Sep 17 00:00:00 2001 From: Alexander Gavrik Date: Thu, 9 Oct 2014 13:10:12 +1100 Subject: [PATCH 5/5] Add required configuration step to readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 38ba43c..19ad301 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,11 @@ Set some `rsync_options` to your liking: set :rsync_options, %w[--recursive --delete --delete-excluded --exclude .git*] ``` +Ensure to set `scm` to :rsync in your deploy.rb: +```ruby +set :scm, :rsync +``` + And after setting regular Capistrano options, deploy as usual! ``` cap deploy