-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into example-updates
- Loading branch information
Showing
13 changed files
with
339 additions
and
73 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -157,6 +157,23 @@ In this case the `recursive: true` option mirrors the same options which are | |
available to [`Net::SCP`](https://github.com/net-ssh/net-scp) and | ||
[`Net::SFTP`](https://github.com/net-ssh/net-sftp). | ||
|
||
## Set the upload/download method (SCP or SFTP). | ||
|
||
SSHKit can use SCP or SFTP for file transfers. The default is SCP, but this can be changed to SFTP per host: | ||
|
||
```ruby | ||
host = SSHKit::Host.new('[email protected]') | ||
host.transfer_method = :sftp | ||
``` | ||
|
||
or globally: | ||
|
||
```ruby | ||
SSHKit::Backend::Netssh.configure do |ssh| | ||
ssh.transfer_method = :sftp | ||
end | ||
``` | ||
|
||
## Setting global SSH options | ||
|
||
Setting global SSH options, these will be overwritten by options set on the | ||
|
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
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
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,26 @@ | ||
require "net/scp" | ||
|
||
module SSHKit | ||
module Backend | ||
class Netssh < Abstract | ||
class ScpTransfer | ||
def initialize(ssh, summarizer) | ||
@ssh = ssh | ||
@summarizer = summarizer | ||
end | ||
|
||
def upload!(local, remote, options) | ||
ssh.scp.upload!(local, remote, options, &summarizer) | ||
end | ||
|
||
def download!(remote, local, options) | ||
ssh.scp.download!(remote, local, options, &summarizer) | ||
end | ||
|
||
private | ||
|
||
attr_reader :ssh, :summarizer | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require "net/sftp" | ||
|
||
module SSHKit | ||
module Backend | ||
class Netssh < Abstract | ||
class SftpTransfer | ||
def initialize(ssh, summarizer) | ||
@ssh = ssh | ||
@summarizer = summarizer | ||
end | ||
|
||
def upload!(local, remote, options) | ||
options = { progress: self }.merge(options || {}) | ||
ssh.sftp.connect! | ||
ssh.sftp.upload!(local, remote, options) | ||
ensure | ||
ssh.sftp.close_channel | ||
end | ||
|
||
def download!(remote, local, options) | ||
options = { progress: self }.merge(options || {}) | ||
destination = local ? local : StringIO.new.tap { |io| io.set_encoding('BINARY') } | ||
|
||
ssh.sftp.connect! | ||
ssh.sftp.download!(remote, destination, options) | ||
local ? true : destination.string | ||
ensure | ||
ssh.sftp.close_channel | ||
end | ||
|
||
def on_get(download, entry, offset, data) | ||
entry.size ||= download.sftp.file.open(entry.remote) { |file| file.stat.size } | ||
summarizer.call(nil, entry.remote, offset + data.bytesize, entry.size) | ||
end | ||
|
||
def on_put(_upload, file, offset, data) | ||
summarizer.call(nil, file.local, offset + data.bytesize, file.size) | ||
end | ||
|
||
private | ||
|
||
attr_reader :ssh, :summarizer | ||
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
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
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,83 @@ | ||
require 'securerandom' | ||
|
||
module SSHKit | ||
module Backend | ||
module NetsshTransferTests | ||
def setup | ||
super | ||
@output = String.new | ||
SSHKit.config.output_verbosity = :debug | ||
SSHKit.config.output = SSHKit::Formatter::SimpleText.new(@output) | ||
end | ||
|
||
def a_host | ||
VagrantWrapper.hosts['one'] | ||
end | ||
|
||
def test_upload_and_then_capture_file_contents | ||
actual_file_contents = "" | ||
file_name = File.join("/tmp", SecureRandom.uuid) | ||
File.open file_name, 'w+' do |f| | ||
f.write "Some Content\nWith a newline and trailing spaces \n " | ||
end | ||
Netssh.new(a_host) do | ||
upload!(file_name, file_name) | ||
actual_file_contents = capture(:cat, file_name, strip: false) | ||
end.run | ||
assert_equal "Some Content\nWith a newline and trailing spaces \n ", actual_file_contents | ||
end | ||
|
||
def test_upload_within | ||
file_name = SecureRandom.uuid | ||
file_contents = "Some Content" | ||
dir_name = SecureRandom.uuid | ||
actual_file_contents = "" | ||
Netssh.new(a_host) do |_host| | ||
within("/tmp") do | ||
execute :mkdir, "-p", dir_name | ||
within(dir_name) do | ||
upload!(StringIO.new(file_contents), file_name) | ||
end | ||
end | ||
actual_file_contents = capture(:cat, "/tmp/#{dir_name}/#{file_name}", strip: false) | ||
end.run | ||
assert_equal file_contents, actual_file_contents | ||
end | ||
|
||
def test_upload_string_io | ||
file_contents = "" | ||
Netssh.new(a_host) do |_host| | ||
file_name = File.join("/tmp", SecureRandom.uuid) | ||
upload!(StringIO.new('example_io'), file_name) | ||
file_contents = download!(file_name) | ||
end.run | ||
assert_equal "example_io", file_contents | ||
end | ||
|
||
def test_upload_large_file | ||
size = 25 | ||
fills = SecureRandom.random_bytes(1024*1024) | ||
file_name = "/tmp/file-#{size}.txt" | ||
File.open(file_name, 'wb') do |f| | ||
(size).times {f.write(fills) } | ||
end | ||
file_contents = "" | ||
Netssh.new(a_host) do | ||
upload!(file_name, file_name) | ||
file_contents = download!(file_name) | ||
end.run | ||
assert_equal File.open(file_name, 'rb').read, file_contents | ||
end | ||
|
||
def test_upload_via_pathname | ||
file_contents = "" | ||
Netssh.new(a_host) do |_host| | ||
file_name = Pathname.new(File.join("/tmp", SecureRandom.uuid)) | ||
upload!(StringIO.new('example_io'), file_name) | ||
file_contents = download!(file_name) | ||
end.run | ||
assert_equal "example_io", file_contents | ||
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
Oops, something went wrong.