-
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.
Allow SFTP to be used for upload!/download! instead of SCP
- Loading branch information
1 parent
8981ad8
commit 5d98e4a
Showing
7 changed files
with
135 additions
and
7 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 |
---|---|---|
|
@@ -162,6 +162,23 @@ end | |
In this case the `recursive: true` option mirrors the same options which are | ||
available to [`Net::{SCP,SFTP}`](http://net-ssh.github.io/net-scp/). | ||
|
||
## 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, &: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