class Bakist::Remote
Attributes
connection[W]
exitstatus[R]
host[R]
key[R]
stderr[R]
stdout[R]
timeout[R]
user[R]
Public Class Methods
from_uri(uri, key = "~/.ssh/id_rsa")
click to toggle source
# File lib/bakist/remote.rb, line 12 def self.from_uri(uri, key = "~/.ssh/id_rsa") parsed = URI.parse("ssh://#{uri}") new(parsed.user || Etc.getlogin, parsed.host, key) end
new(user, host, key, options = {})
click to toggle source
# File lib/bakist/remote.rb, line 17 def initialize(user, host, key, options = {}) @user = user @host = host @key = File.expand_path(key) @timeout = options[:timeout] || 10000 end
Public Instance Methods
backtick(command)
click to toggle source
# File lib/bakist/remote.rb, line 24 def backtick(command) @stdout = "" @stderr = "" exec(command) @stdout end
system(command)
click to toggle source
# File lib/bakist/remote.rb, line 31 def system(command) @stdout = STDOUT @stderr = STDERR exec(command) exitstatus end
system!(*command)
click to toggle source
# File lib/bakist/remote.rb, line 38 def system!(*command) system(*command).tap do |status| raise RemoteError.new("#{command.join(" ")} exited #{status}") unless status == 0 end end
upload(from, to, opts = "--exclude .git")
click to toggle source
# File lib/bakist/remote.rb, line 44 def upload(from, to, opts = "--exclude .git") Kernel.system("rsync -e 'ssh -i #{key}' -avz --delete #{from} #{user}@#{host}:#{to} #{opts}") end
Private Instance Methods
connection()
click to toggle source
# File lib/bakist/remote.rb, line 49 def connection @connection ||= Net::SSH.start(host, user, :keys => [key], :timeout => timeout) end
exec(*command)
click to toggle source
# File lib/bakist/remote.rb, line 53 def exec(*command) connection.open_channel do |channel| channel.exec(*command) do |stream, success| raise RemoteError.new("Could not run #{command.join(" ")}") unless success stream.on_data { |_, data| stdout << data } stream.on_extended_data { |_, type, data| stderr << data } stream.on_request("exit-status") { |_, data| @exitstatus = data.read_long } end end connection.loop @exitstatus ||= 0 end