2011-02-24 14:40:15 -05:00
|
|
|
module Fog
|
|
|
|
module SCP
|
|
|
|
|
|
|
|
def self.new(address, username, options = {})
|
|
|
|
if Fog.mocking?
|
|
|
|
Fog::SCP::Mock.new(address, username, options)
|
|
|
|
else
|
|
|
|
Fog::SCP::Real.new(address, username, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
2012-05-31 01:38:39 -04:00
|
|
|
hash[key] = []
|
2011-02-24 14:40:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(address, username, options)
|
|
|
|
@address = address
|
|
|
|
@username = username
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
2011-06-13 13:53:08 -04:00
|
|
|
def upload(local_path, remote_path, upload_options = {})
|
2012-05-31 01:38:39 -04:00
|
|
|
self.class.data[@address] << { :username => @username,
|
|
|
|
:options => @options,
|
|
|
|
:local_path => local_path,
|
|
|
|
:remote_path => remote_path,
|
|
|
|
:upload_options => upload_options }
|
2011-02-24 14:40:15 -05:00
|
|
|
end
|
|
|
|
|
2011-12-09 08:14:53 -05:00
|
|
|
def download(remote_path, local_path, download_options = {})
|
2012-05-31 01:38:39 -04:00
|
|
|
self.class.data[@address] << { :username => @username,
|
|
|
|
:options => @options,
|
|
|
|
:remote_path => remote_path,
|
|
|
|
:local_path => local_path,
|
|
|
|
:download_options => download_options }
|
2011-12-09 08:14:53 -05:00
|
|
|
end
|
|
|
|
|
2011-02-24 14:40:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(address, username, options)
|
|
|
|
require 'net/scp'
|
|
|
|
|
|
|
|
key_manager = Net::SSH::Authentication::KeyManager.new(nil, options)
|
|
|
|
|
|
|
|
unless options[:key_data] || options[:keys] || options[:password] || key_manager.agent
|
|
|
|
raise ArgumentError.new(':key_data, :keys, :password or a loaded ssh-agent is required to initialize SSH')
|
|
|
|
end
|
|
|
|
|
2012-07-10 07:30:30 -04:00
|
|
|
if options[:key_data] || options[:keys]
|
|
|
|
options[:keys_only] = true
|
|
|
|
#Explicitly set these so net-ssh doesn't add the default keys
|
|
|
|
#as seen at https://github.com/net-ssh/net-ssh/blob/master/lib/net/ssh/authentication/session.rb#L131-146
|
|
|
|
options[:keys] = [] unless options[:keys]
|
|
|
|
options[:key_data] = [] unless options[:key_data]
|
|
|
|
end
|
|
|
|
|
2011-02-24 14:40:15 -05:00
|
|
|
@address = address
|
|
|
|
@username = username
|
|
|
|
@options = { :paranoid => false }.merge(options)
|
|
|
|
end
|
|
|
|
|
2011-12-09 08:14:53 -05:00
|
|
|
def upload(local_path, remote_path, upload_options = {})
|
|
|
|
begin
|
|
|
|
Net::SCP.start(@address, @username, @options) do |scp|
|
|
|
|
scp.upload!(local_path, remote_path, upload_options) do |ch, name, sent, total|
|
|
|
|
# TODO: handle progress display?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Exception => error
|
|
|
|
raise error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def download(remote_path, local_path, download_options = {})
|
2011-02-24 14:40:15 -05:00
|
|
|
begin
|
|
|
|
Net::SCP.start(@address, @username, @options) do |scp|
|
2011-12-09 08:14:53 -05:00
|
|
|
scp.download!(remote_path, local_path, download_options) do |ch, name, sent, total|
|
2011-02-24 14:40:15 -05:00
|
|
|
# TODO: handle progress display?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Exception => error
|
|
|
|
raise error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|