lib/vos/drivers/ssh.rb in vos-0.3.12 vs lib/vos/drivers/ssh.rb in vos-0.3.13
- old
+ new
@@ -1,35 +1,35 @@
require 'net/ssh'
require 'net/sftp'
module Vos
module Drivers
- class Ssh < Abstract
+ class Ssh < Abstract
DEFAULT_OPTIONS = {
config: true
}
-
+
def initialize options = {}
- super
+ super
raise ":host not provided!" unless options[:host]
@options = DEFAULT_OPTIONS.merge options
-
+
# config_options = Net::SSH.configuration_for(options[:host])
# options = DEFAULT_OPTIONS.merge(config_options).merge options
# raise ":user not provided (provide explicitly or in .ssh/config)!" unless options[:user]
end
-
-
- #
+
+
+ #
# Establishing SSH channel
- #
+ #
def open &block
if block
if @ssh
block.call self
else
- begin
+ begin
open
block.call self
ensure
close
end
@@ -42,94 +42,94 @@
@ssh = Net::SSH.start(host, nil, opt)
@sftp = @ssh.sftp.connect
end
end
- end
-
- def close
+ end
+
+ def close
if @ssh
@ssh.close
# @sftp.close not needed
@ssh, @sftp = nil
end
end
- #
+ #
# Vfs
- #
+ #
include SshVfsStorage
alias_method :open_fs, :open
-
-
- #
+
+
+ #
# Shell
- #
+ #
def exec command
# somehow net-ssh doesn't executes ~/.profile, so we need to execute it manually
# command = ". ~/.profile && #{command}"
stdout, stderr, code, signal = hacked_exec! ssh, command
return code, stdout, stderr
- end
-
+ end
+
def bash command
# somehow net-ssh doesn't executes ~/.profile, so we need to execute it manually
# command = ". ~/.profile && #{command}"
stdout_and_stderr, stderr, code, signal = hacked_exec! ssh, command, true
return code, stdout_and_stderr
end
-
-
- #
+
+
+ #
# Miscellaneous
- #
+ #
def to_s; options[:host] end
def host; options[:host] end
-
+
protected
attr_accessor :ssh, :sftp
-
+
def fix_path path
path.sub(/^\~/, home)
end
-
+
def home
unless @home
command = 'cd ~; pwd'
code, stdout, stderr = exec command
raise "can't execute '#{command}'!" unless code == 0
- @home = stdout.gsub("\n", '')
+ @home = stdout.gsub("\n", '')
end
@home
end
-
+
# taken from here http://stackoverflow.com/questions/3386233/how-to-get-exit-status-with-rubys-netssh-library/3386375#3386375
def hacked_exec!(ssh, command, merge_stdout_and_stderr = false, &block)
stdout_data = ""
stderr_data = ""
exit_code = nil
exit_signal = nil
-
+
channel = ssh.open_channel do |channel|
channel.exec(command) do |ch, success|
raise "could not execute command: #{command.inspect}" unless success
channel.on_data{|ch2, data| stdout_data << data}
- channel.on_extended_data do |ch2, type, data|
+ channel.on_extended_data do |ch2, type, data|
stdout_data << data if merge_stdout_and_stderr
stderr_data << data
end
channel.on_request("exit-status"){|ch,data| exit_code = data.read_long}
channel.on_request("exit-signal"){|ch, data| exit_signal = data.read_long}
- end
- end
-
- channel.wait
+ end
+ end
+
+ channel.wait
[stdout_data, stderr_data, exit_code, exit_signal]
- end
+ end
end
end
end
\ No newline at end of file