* Implement .new_from_string factory class method; * Rename private method #parse_from_string as class method .string_to_hash; * Remove argument kind detection logic in constructor.
53 lines
1.0 KiB
Ruby
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_from_string(execute 'env')
|
|
end
|
|
end
|
|
end
|
|
end
|