lib/evrone/common/spawn/ssh.rb in evrone-common-spawn-0.0.3 vs lib/evrone/common/spawn/ssh.rb in evrone-common-spawn-0.0.4
- old
+ new
@@ -7,22 +7,21 @@
class SSH
class << self
def open(host, user, options = {}, &block)
::Net::SSH.start(host, user, {
- forward_agent: true,
paranoid: false
}.merge(options)) do |ssh|
yield new(ssh)
end
end
end
- attr_reader :host, :user, :options
+ attr_reader :host, :user, :options, :connection
def initialize(ssh)
- @ssh = ssh
+ @connection = ssh
end
def spawn(*args, &block)
env = args.first.is_a?(Hash) ? args.shift : {}
options = args.last.is_a?(Hash) ? args.pop : {}
@@ -30,11 +29,12 @@
exit_code = nil
timeout = Spawn::Timeout.new options.delete(:timeout)
read_timeout = Spawn::ReadTimeout.new options.delete(:read_timeout)
- channel = spawn_channel env, command, read_timeout, &block
+ command = build_command(env, command, options)
+ channel = spawn_channel command, read_timeout, &block
channel.on_request("exit-status") do |_,data|
exit_code = data.read_long
end
@@ -43,12 +43,25 @@
compute_exit_code command, exit_code, timeout, read_timeout
end
private
+ def build_command(env, command, options)
+ cmd = command
+ unless env.empty?
+ e = env.map{|k,v| "#{k}=#{v}" }.join(" ")
+ cmd = "env #{e} #{cmd}"
+ end
+ if options.key?(:chdir)
+ e = "cd #{options[:chdir]}"
+ cmd = "#{e} ; #{cmd}"
+ end
+ cmd
+ end
+
def pool(channel, timeout, read_timeout)
- @ssh.loop Spawn.pool_interval do
+ @connection.loop Spawn.pool_interval do
if read_timeout.happened? || timeout.happened?
false
else
channel.active?
end
@@ -64,19 +77,19 @@
else
exit_code || -1 # nil exit_code means that the process is killed
end
end
- def spawn_channel(env, command, read_timeout, &block)
+ def spawn_channel(command, read_timeout, &block)
- @ssh.open_channel do |channel|
+ @connection.open_channel do |channel|
read_timeout.reset
- env.each do |k, v|
- channel.env k, v do |_, success|
- yield "FAILED: couldn't execute command (ssh.channel.env)\n" if block_given?
- end
- end
+ #env.each do |k, v|
+ # channel.env k, v do |_, success|
+ # yield "FAILED: couldn't execute command (ssh.channel.env)\n" if block_given?
+ # end
+ #end
channel.exec command do |_, success|
unless success
yield "FAILED: couldn't execute command (ssh.channel.exec)\n" if block_given?