Skip to content

Instantly share code, notes, and snippets.

@agate
Created June 28, 2010 08:46
Show Gist options
  • Save agate/455599 to your computer and use it in GitHub Desktop.
Save agate/455599 to your computer and use it in GitHub Desktop.
diff --git a/lib/capistrano/recipes/deploy/strategy/copy.rb b/lib/capistrano/recipes/deploy/strategy/copy.rb
index 351d6e9..1ca84b2 100644
--- a/lib/capistrano/recipes/deploy/strategy/copy.rb
+++ b/lib/capistrano/recipes/deploy/strategy/copy.rb
@@ -101,7 +101,7 @@ module Capistrano
logger.trace "compressing #{destination} to #{filename}"
Dir.chdir(tmpdir) { system(compress(File.basename(destination), File.basename(filename)).join(" ")) }
- upload(filename, remote_filename)
+ upload(filename, remote_filename, { :via => copy_via })
run "cd #{configuration[:releases_path]} && #{decompress(remote_filename).join(" ")} && rm #{remote_filename}"
ensure
FileUtils.rm filename rescue nil
@@ -146,6 +146,12 @@ module Capistrano
@copy_strategy ||= configuration.fetch(:copy_strategy, :checkout)
end
+ # Returns the value of the :copy_via variable, defaulting to
+ # :sftp if it has not been set
+ def copy_via
+ @copy_via ||= configuration.fetch(:copy_via, :sftp)
+ end
+
# Should return the command(s) necessary to obtain the source code
# locally.
def command
diff --git a/test/deploy/strategy/copy_test.rb b/test/deploy/strategy/copy_test.rb
index 33ba696..df2f3ab 100644
--- a/test/deploy/strategy/copy_test.rb
+++ b/test/deploy/strategy/copy_test.rb
@@ -14,17 +14,17 @@ class DeployStrategyCopyTest < Test::Unit::TestCase
@config.stubs(:source).returns(@source)
@strategy = Capistrano::Deploy::Strategy::Copy.new(@config)
end
-
+
def test_deploy_with_defaults_should_use_remote_gtar
@config[:copy_remote_tar] = 'gtar'
-
+
Dir.expects(:tmpdir).returns("/temp/dir")
@source.expects(:checkout).with("154", "/temp/dir/1234567890").returns(:local_checkout)
@strategy.expects(:system).with(:local_checkout)
Dir.expects(:chdir).with("/temp/dir").yields
@strategy.expects(:system).with("tar czf 1234567890.tar.gz 1234567890")
- @strategy.expects(:upload).with("/temp/dir/1234567890.tar.gz", "/tmp/1234567890.tar.gz")
+ @strategy.expects(:upload).with("/temp/dir/1234567890.tar.gz", "/tmp/1234567890.tar.gz", :via => :sftp)
@strategy.expects(:run).with("cd /u/apps/test/releases && gtar xzf /tmp/1234567890.tar.gz && rm /tmp/1234567890.tar.gz")
mock_file = mock("file")
@@ -33,20 +33,20 @@ class DeployStrategyCopyTest < Test::Unit::TestCase
FileUtils.expects(:rm).with("/temp/dir/1234567890.tar.gz")
FileUtils.expects(:rm_rf).with("/temp/dir/1234567890")
-
+
@strategy.deploy!
end
-
+
def test_deploy_with_defaults_should_use_local_gtar
@config[:copy_local_tar] = 'gtar'
-
+
Dir.expects(:tmpdir).returns("/temp/dir")
@source.expects(:checkout).with("154", "/temp/dir/1234567890").returns(:local_checkout)
@strategy.expects(:system).with(:local_checkout)
Dir.expects(:chdir).with("/temp/dir").yields
@strategy.expects(:system).with("gtar czf 1234567890.tar.gz 1234567890")
- @strategy.expects(:upload).with("/temp/dir/1234567890.tar.gz", "/tmp/1234567890.tar.gz")
+ @strategy.expects(:upload).with("/temp/dir/1234567890.tar.gz", "/tmp/1234567890.tar.gz", :via => :sftp)
@strategy.expects(:run).with("cd /u/apps/test/releases && tar xzf /tmp/1234567890.tar.gz && rm /tmp/1234567890.tar.gz")
mock_file = mock("file")
@@ -55,9 +55,9 @@ class DeployStrategyCopyTest < Test::Unit::TestCase
FileUtils.expects(:rm).with("/temp/dir/1234567890.tar.gz")
FileUtils.expects(:rm_rf).with("/temp/dir/1234567890")
-
+
@strategy.deploy!
- end
+ end
def test_deploy_with_defaults_should_use_tar_gz_and_checkout
Dir.expects(:tmpdir).returns("/temp/dir")
@@ -110,7 +110,7 @@ class DeployStrategyCopyTest < Test::Unit::TestCase
@strategy.expects(:system).with(:local_checkout)
@strategy.expects(:system).with("zip -qr 1234567890.zip 1234567890")
- @strategy.expects(:upload).with("/temp/dir/1234567890.zip", "/tmp/1234567890.zip")
+ @strategy.expects(:upload).with("/temp/dir/1234567890.zip", "/tmp/1234567890.zip", :via => :sftp)
@strategy.expects(:run).with("cd /u/apps/test/releases && unzip -q /tmp/1234567890.zip && rm /tmp/1234567890.zip")
mock_file = mock("file")
@@ -131,7 +131,7 @@ class DeployStrategyCopyTest < Test::Unit::TestCase
@strategy.expects(:system).with(:local_checkout)
@strategy.expects(:system).with("tar cjf 1234567890.tar.bz2 1234567890")
- @strategy.expects(:upload).with("/temp/dir/1234567890.tar.bz2", "/tmp/1234567890.tar.bz2")
+ @strategy.expects(:upload).with("/temp/dir/1234567890.tar.bz2", "/tmp/1234567890.tar.bz2", :via => :sftp)
@strategy.expects(:run).with("cd /u/apps/test/releases && tar xjf /tmp/1234567890.tar.bz2 && rm /tmp/1234567890.tar.bz2")
mock_file = mock("file")
@@ -143,17 +143,17 @@ class DeployStrategyCopyTest < Test::Unit::TestCase
@strategy.deploy!
end
-
+
def test_deploy_with_unknown_compression_type_should_error
@config[:copy_compression] = :bogus
Dir.expects(:tmpdir).returns("/temp/dir")
@source.expects(:checkout).with("154", "/temp/dir/1234567890").returns(:local_checkout)
@strategy.stubs(:system)
File.stubs(:open)
-
+
assert_raises(ArgumentError) { @strategy.deploy! }
end
-
+
def test_deploy_with_custom_copy_dir_should_use_that_as_tmpdir
Dir.expects(:tmpdir).never
Dir.expects(:chdir).with("/other/path").yields
@@ -162,7 +162,7 @@ class DeployStrategyCopyTest < Test::Unit::TestCase
@strategy.expects(:system).with(:local_checkout)
@strategy.expects(:system).with("tar czf 1234567890.tar.gz 1234567890")
- @strategy.expects(:upload).with("/other/path/1234567890.tar.gz", "/tmp/1234567890.tar.gz")
+ @strategy.expects(:upload).with("/other/path/1234567890.tar.gz", "/tmp/1234567890.tar.gz", :via => :sftp)
@strategy.expects(:run).with("cd /u/apps/test/releases && tar xzf /tmp/1234567890.tar.gz && rm /tmp/1234567890.tar.gz")
mock_file = mock("file")
@@ -183,7 +183,7 @@ class DeployStrategyCopyTest < Test::Unit::TestCase
@strategy.expects(:system).with(:local_checkout)
@strategy.expects(:system).with("tar czf 1234567890.tar.gz 1234567890")
- @strategy.expects(:upload).with("/temp/dir/1234567890.tar.gz", "/somewhere/else/1234567890.tar.gz")
+ @strategy.expects(:upload).with("/temp/dir/1234567890.tar.gz", "/somewhere/else/1234567890.tar.gz", :via => :sftp)
@strategy.expects(:run).with("cd /u/apps/test/releases && tar xzf /somewhere/else/1234567890.tar.gz && rm /somewhere/else/1234567890.tar.gz")
mock_file = mock("file")
@@ -269,6 +269,46 @@ class DeployStrategyCopyTest < Test::Unit::TestCase
@strategy.deploy!
end
+ def test_deploy_with_defaults_should_use_sftp_to_upload
+ Dir.expects(:tmpdir).returns("/temp/dir")
+ @source.expects(:checkout).with("154", "/temp/dir/1234567890").returns(:local_checkout)
+ @strategy.expects(:system).with(:local_checkout)
+
+ Dir.expects(:chdir).with("/temp/dir").yields
+ @strategy.expects(:system).with("tar czf 1234567890.tar.gz 1234567890")
+ @strategy.expects(:upload).with("/temp/dir/1234567890.tar.gz", "/tmp/1234567890.tar.gz", :via => :sftp)
+ @strategy.expects(:run).with("cd /u/apps/test/releases && tar xzf /tmp/1234567890.tar.gz && rm /tmp/1234567890.tar.gz")
+
+ mock_file = mock("file")
+ mock_file.expects(:puts).with("154")
+ File.expects(:open).with("/temp/dir/1234567890/REVISION", "w").yields(mock_file)
+
+ FileUtils.expects(:rm).with("/temp/dir/1234567890.tar.gz")
+ FileUtils.expects(:rm_rf).with("/temp/dir/1234567890")
+ @strategy.deploy!
+ end
+
+ def test_deploy_with_copy_via_should_use_that_protocol_to_upload
+ @config[:copy_via] = :scp
+
+ Dir.expects(:tmpdir).returns("/temp/dir")
+ @source.expects(:checkout).with("154", "/temp/dir/1234567890").returns(:local_checkout)
+ @strategy.expects(:system).with(:local_checkout)
+
+ Dir.expects(:chdir).with("/temp/dir").yields
+ @strategy.expects(:system).with("tar czf 1234567890.tar.gz 1234567890")
+ @strategy.expects(:upload).with("/temp/dir/1234567890.tar.gz", "/tmp/1234567890.tar.gz", :via => :scp)
+ @strategy.expects(:run).with("cd /u/apps/test/releases && tar xzf /tmp/1234567890.tar.gz && rm /tmp/1234567890.tar.gz")
+
+ mock_file = mock("file")
+ mock_file.expects(:puts).with("154")
+ File.expects(:open).with("/temp/dir/1234567890/REVISION", "w").yields(mock_file)
+
+ FileUtils.expects(:rm).with("/temp/dir/1234567890.tar.gz")
+ FileUtils.expects(:rm_rf).with("/temp/dir/1234567890")
+ @strategy.deploy!
+ end
+
private
def prepare_directory_tree!(cache, exclude=false)
@@ -289,7 +329,7 @@ class DeployStrategyCopyTest < Test::Unit::TestCase
def prepare_standard_compress_and_copy!
Dir.expects(:chdir).with("/temp/dir").yields
@strategy.expects(:system).with("tar czf 1234567890.tar.gz 1234567890")
- @strategy.expects(:upload).with("/temp/dir/1234567890.tar.gz", "/tmp/1234567890.tar.gz")
+ @strategy.expects(:upload).with("/temp/dir/1234567890.tar.gz", "/tmp/1234567890.tar.gz", :via => :sftp)
@strategy.expects(:run).with("cd /u/apps/test/releases && tar xzf /tmp/1234567890.tar.gz && rm /tmp/1234567890.tar.gz")
mock_file = mock("file")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment