Skip to content

Commit

Permalink
extracted rsync class, only using ssh keys that exist
Browse files Browse the repository at this point in the history
  • Loading branch information
pitluga committed May 21, 2011
1 parent 090a373 commit 25d7cc8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Rakefile
@@ -1,3 +1,11 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.test_files = FileList['test/*_test.rb']
end

task :default => :test

desc "clean"
task :clean do
rm_f Dir.glob("*.gem")
Expand Down
10 changes: 9 additions & 1 deletion lib/supply_drop.rb
@@ -1,3 +1,5 @@
require 'supply_drop/rsync'

Capistrano::Configuration.instance.load do
namespace :puppet do
set :puppet_target, '/home/vagrant/supply_drop'
Expand All @@ -14,7 +16,13 @@
desc "pushes the current puppet configuration to the server"
task :update_code, :except => { :nopuppet => true } do
find_servers_for_task(current_task).each do |server|
rsync_cmd = "rsync -az --delete --exclude=.git -e 'ssh -i #{ssh_options[:keys]}' . #{server.user || user}@#{server.host}:#{puppet_target}/"
rsync_cmd = Rsync.command(
".",
"#{server.user || user}@#{server.host}:#{puppet_target}/",
:delete => true,
:excludes => ['.git'],
:ssh => { :keys => ssh_options[:keys] }
)
logger.debug rsync_cmd
system rsync_cmd
end
Expand Down
26 changes: 26 additions & 0 deletions lib/supply_drop/rsync.rb
@@ -0,0 +1,26 @@
class Rsync
class << self
def command(from, to, options={})
flags = ['-az']
flags << '--delete' if options[:delete]
flags << excludes(options[:excludes]) if options.has_key?(:excludes)
flags << ssh_options(options[:ssh]) if options.has_key?(:ssh)

"rsync #{flags.compact.join(' ')} #{from} #{to}"
end

def excludes(patterns)
[patterns].flatten.map { |p| "--exclude=#{p}" }
end

def ssh_options(options)
mapped_options = options.map do |key, value|
if key == :keys
[value].flatten.select { |k| File.exist?(k) }.map { |k| "-i #{k}" }
end
end

%[-e "ssh #{mapped_options.join(' ')}"]
end
end
end
26 changes: 26 additions & 0 deletions test/rsync_test.rb
@@ -0,0 +1,26 @@
require 'test/unit'
require File.expand_path('../../lib/supply_drop/rsync', __FILE__)

class RsyncTest < Test::Unit::TestCase

def test_build_simple_command
command = Rsync.command('bar', 'foo')
assert_equal 'rsync -az bar foo', command
end

def test_allows_passing_delete
command = Rsync.command('bar', 'foo', :delete => true)
assert_equal 'rsync -az --delete bar foo', command
end

def test_allows_specifying_an_exclude
command = Rsync.command('bar', 'foo', :excludes => '.git')
assert_equal 'rsync -az --exclude=.git bar foo', command
end

def test_ssh_options_keys_only_lists_existing_files
command = Rsync.command('.', 'foo', :ssh => { :keys => [__FILE__, "#{__FILE__}dadijofs"] })
assert_match /-i #{__FILE__}/, command
end

end

0 comments on commit 25d7cc8

Please sign in to comment.