Thibault Jouan 85a47df547 Use SSH user name configured for a given target:
When a user is configured for the targeted host name, use it for SSH
authentification instead of the name of current user logged in.

* Modify Remote#user_name to use configured user name.
2013-09-28 20:07:36 +00:00

53 lines
1.0 KiB
Ruby

module Producer
module Core
class Remote
require 'etc'
require 'net/ssh'
attr_accessor :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(self)
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(execute 'env')
end
end
end
end