2012-04-20 13:41:39 -04:00
|
|
|
require 'delegate'
|
|
|
|
|
2010-04-14 23:32:56 -04:00
|
|
|
module Fog
|
2010-05-02 22:43:03 -04:00
|
|
|
module SSH
|
2010-04-14 23:32:56 -04:00
|
|
|
|
2010-05-02 22:43:03 -04:00
|
|
|
def self.new(address, username, options = {})
|
|
|
|
if Fog.mocking?
|
|
|
|
Fog::SSH::Mock.new(address, username, options)
|
|
|
|
else
|
|
|
|
Fog::SSH::Real.new(address, username, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
2012-05-09 13:05:12 -04:00
|
|
|
hash[key] = []
|
2010-05-02 22:43:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-09 13:05:12 -04:00
|
|
|
def self.reset
|
|
|
|
@data= nil
|
|
|
|
end
|
|
|
|
|
2010-05-02 22:43:03 -04:00
|
|
|
def initialize(address, username, options)
|
|
|
|
@address = address
|
|
|
|
@username = username
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
2012-05-23 18:28:02 -04:00
|
|
|
def run(commands, &blk)
|
2012-05-09 13:05:12 -04:00
|
|
|
self.class.data[@address] << {:commands => commands, :username => @username, :options => @options}
|
2010-05-02 22:43:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(address, username, options)
|
2010-10-29 17:58:28 -04:00
|
|
|
require 'net/ssh'
|
2011-01-22 03:23:49 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2010-05-02 22:43:03 -04:00
|
|
|
@address = address
|
|
|
|
@username = username
|
2010-12-02 16:37:03 -05:00
|
|
|
@options = { :paranoid => false }.merge(options)
|
2010-05-02 22:43:03 -04:00
|
|
|
end
|
|
|
|
|
2012-05-23 18:28:02 -04:00
|
|
|
def run(commands, &blk)
|
2010-05-02 22:43:03 -04:00
|
|
|
commands = [*commands]
|
|
|
|
results = []
|
|
|
|
begin
|
|
|
|
Net::SSH.start(@address, @username, @options) do |ssh|
|
|
|
|
commands.each do |command|
|
2012-05-24 14:39:16 -04:00
|
|
|
result = Result.new(command)
|
2010-10-13 04:42:11 -04:00
|
|
|
ssh.open_channel do |ssh_channel|
|
|
|
|
ssh_channel.request_pty
|
2011-04-02 17:06:20 -04:00
|
|
|
ssh_channel.exec(command) do |channel, success|
|
2010-05-02 22:43:03 -04:00
|
|
|
unless success
|
|
|
|
raise "Could not execute command: #{command.inspect}"
|
|
|
|
end
|
|
|
|
|
2010-10-13 04:42:11 -04:00
|
|
|
channel.on_data do |ch, data|
|
2010-05-02 22:43:03 -04:00
|
|
|
result.stdout << data
|
2012-05-23 18:54:35 -04:00
|
|
|
yield [data, ''] if blk
|
2010-05-02 22:43:03 -04:00
|
|
|
end
|
|
|
|
|
2010-10-13 04:42:11 -04:00
|
|
|
channel.on_extended_data do |ch, type, data|
|
2010-05-02 22:43:03 -04:00
|
|
|
next unless type == 1
|
|
|
|
result.stderr << data
|
2012-05-23 18:54:35 -04:00
|
|
|
yield ['', data] if blk
|
2010-05-02 22:43:03 -04:00
|
|
|
end
|
|
|
|
|
2010-10-13 04:42:11 -04:00
|
|
|
channel.on_request('exit-status') do |ch, data|
|
2010-05-02 22:43:03 -04:00
|
|
|
result.status = data.read_long
|
|
|
|
end
|
|
|
|
|
2010-10-13 04:42:11 -04:00
|
|
|
channel.on_request('exit-signal') do |ch, data|
|
2010-05-02 22:43:03 -04:00
|
|
|
result.status = 255
|
|
|
|
end
|
2010-04-19 00:42:08 -04:00
|
|
|
end
|
|
|
|
end
|
2010-05-02 22:43:03 -04:00
|
|
|
ssh.loop
|
2010-09-15 20:31:45 -04:00
|
|
|
results << result
|
2010-04-19 00:42:08 -04:00
|
|
|
end
|
2010-04-14 23:32:56 -04:00
|
|
|
end
|
2010-05-02 22:43:03 -04:00
|
|
|
rescue Net::SSH::HostKeyMismatch => exception
|
|
|
|
exception.remember_host!
|
|
|
|
sleep 0.2
|
|
|
|
retry
|
2010-04-14 23:32:56 -04:00
|
|
|
end
|
2010-05-02 22:43:03 -04:00
|
|
|
results
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Result
|
|
|
|
|
|
|
|
attr_accessor :command, :stderr, :stdout, :status
|
|
|
|
|
2010-09-15 20:31:45 -04:00
|
|
|
def display_stdout
|
2011-06-15 02:10:54 -04:00
|
|
|
data = stdout.split("\r\n")
|
|
|
|
if data.is_a?(String)
|
|
|
|
Formatador.display_line(data)
|
|
|
|
elsif data.is_a?(Array)
|
|
|
|
Formatador.display_lines(data)
|
|
|
|
end
|
2010-09-15 20:31:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_stderr
|
|
|
|
Formatador.display_line(stderr.split("\r\n"))
|
|
|
|
end
|
|
|
|
|
2012-05-24 14:39:16 -04:00
|
|
|
def initialize(command)
|
2010-05-02 22:43:03 -04:00
|
|
|
@command = command
|
2012-05-24 14:39:16 -04:00
|
|
|
@stderr = ''
|
|
|
|
@stdout = ''
|
2010-04-14 23:32:56 -04:00
|
|
|
end
|
2010-05-02 22:43:03 -04:00
|
|
|
|
2010-04-14 23:32:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|