Thibault Jouan f45d0b5a20 Modify Remote::FS interface:
* Accept SFTP session in FS constructor;
* Modify Remote to build the session required by FS;
* Change TestEnvHelpers#build_remote in order to provided a test double
  as the remote FS.
2014-03-05 00:07:59 +00:00

50 lines
1.0 KiB
Ruby

module Producer
module Core
class Remote
attr_reader :hostname
def initialize(hostname)
@hostname = hostname
end
def session
@session ||= Net::SSH.start(@hostname, user_name)
end
def config
@config ||= Net::SSH::Config.for(@hostname)
end
def user_name
config[:user] || Etc.getlogin
end
def fs
@fs ||= Remote::FS.new(session.sftp.connect)
end
def execute(command)
output = ''
session.open_channel do |channel|
channel.exec command do |ch, success|
ch.on_data do |c, data|
output << data
end
ch.on_request 'exit-status' do |c, data|
exit_status = data.read_long
raise RemoteCommandExecutionError if exit_status != 0
end
end
end
session.loop
output
end
def environment
Environment.new_from_string(execute 'env')
end
end
end
end