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 = {})
|
2010-04-14 23:32:56 -04:00
|
|
|
unless options[:keys] || options[:password]
|
|
|
|
raise ArgumentError.new(':keys or :password are required to initialize SSH')
|
|
|
|
end
|
2010-05-02 22:43:03 -04:00
|
|
|
if Fog.mocking?
|
|
|
|
Fog::SSH::Mock.new(address, username, options)
|
|
|
|
else
|
|
|
|
Fog::SSH::Real.new(address, username, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset_data(keys=Mock.data.keys)
|
|
|
|
Mock.reset_data(keys)
|
2010-04-14 23:32:56 -04:00
|
|
|
end
|
|
|
|
|
2010-05-02 22:43:03 -04:00
|
|
|
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)
|
2010-05-26 20:38:23 -04:00
|
|
|
Fog::Mock.not_implemented
|
2010-05-02 22:43:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(address, username, options)
|
|
|
|
@address = address
|
|
|
|
@username = username
|
|
|
|
@options = options.merge!(:paranoid => false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(commands)
|
|
|
|
commands = [*commands]
|
|
|
|
results = []
|
|
|
|
begin
|
|
|
|
Net::SSH.start(@address, @username, @options) do |ssh|
|
|
|
|
commands.each do |command|
|
|
|
|
ssh.open_channel do |channel|
|
|
|
|
sudoable_command = command.sub(/^sudo/, %{sudo -p 'fog sudo password:'})
|
|
|
|
escaped_command = sudoable_command.sub(/'/, %{'"'"'})
|
|
|
|
channel.request_pty
|
|
|
|
result = Result.new(command)
|
|
|
|
channel.exec(%{bash -lc '#{escaped_command}'}) do |channel, success|
|
|
|
|
unless success
|
|
|
|
raise "Could not execute command: #{command.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
channel.on_data do |channel, data|
|
|
|
|
result.stdout << data
|
|
|
|
end
|
|
|
|
|
|
|
|
channel.on_extended_data do |channel, type, data|
|
|
|
|
next unless type == 1
|
|
|
|
result.stderr << data
|
|
|
|
end
|
|
|
|
|
|
|
|
channel.on_request('exit-status') do |channel, data|
|
|
|
|
result.status = data.read_long
|
|
|
|
end
|
|
|
|
|
|
|
|
channel.on_request('exit-signal') do |channel, data|
|
|
|
|
result.status = 255
|
|
|
|
end
|
2010-04-19 00:42:08 -04:00
|
|
|
end
|
2010-05-02 22:43:03 -04:00
|
|
|
results << result
|
2010-04-19 00:42:08 -04:00
|
|
|
end
|
2010-05-02 22:43:03 -04:00
|
|
|
ssh.loop
|
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
|
|
|
|
|
|
|
|
def initialize(command)
|
|
|
|
@command = command
|
|
|
|
@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
|