Permalink
Browse files

added basic scp support

  • Loading branch information...
1 parent ad40758 commit 8ccbd4917dba487329c2df50dc11f3a88902e6fa Allan committed with Wesley Beary Feb 24, 2011
Showing with 76 additions and 0 deletions.
  1. +8 −0 lib/fog/compute/models/aws/server.rb
  2. +1 −0 lib/fog/core.rb
  3. +67 −0 lib/fog/core/scp.rb
View
8 lib/fog/compute/models/aws/server.rb
@@ -186,6 +186,14 @@ def ssh(commands)
Fog::SSH.new(ip_address, username, options).run(commands)
end
+ def scp(local_path, remote_path)
+ requires :ip_address, :username
+
+ options = {}
+ options[:key_data] = [private_key] if private_key
+ Fog::SCP.new(ip_address, username, options).upload(local_path, remote_path)
+ end
+
def start
requires :id
connection.start_instances(id)
View
1 lib/fog/core.rb
@@ -27,5 +27,6 @@
require 'fog/core/provider'
require 'fog/core/service'
require 'fog/core/ssh'
+require 'fog/core/scp'
require 'fog/core/time'
require 'fog/core/wait_for'
View
67 lib/fog/core/scp.rb
@@ -0,0 +1,67 @@
+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
+
+ def self.reset_data(keys=Mock.data.keys)
+ Mock.reset_data(keys)
+ end
+
+ class Mock
+
+ def self.data
+ @data ||= Hash.new do |hash, key|
+ hash[key] = {}
+ end
+ end
+
+ def initialize(address, username, options)
+ @address = address
+ @username = username
+ @options = options
+ end
+
+ def run(commands)
+ Fog::Mock.not_implemented
+ end
+
+ 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
+
+ @address = address
+ @username = username
+ @options = { :paranoid => false }.merge(options)
+ end
+
+ def upload(local_path, remote_path)
+ begin
+ Net::SCP.start(@address, @username, @options) do |scp|
+ scp.upload!(local_path, remote_path) do |ch, name, sent, total|
+ # TODO: handle progress display?
+ end
+ end
+ rescue Exception => error
+ raise error
+ end
+ end
+
+ end
+
+ end
+end

0 comments on commit 8ccbd49

Please sign in to comment.