1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

feature #863: Update remotes in sshtream. Checks if the stream is alive before using it

This commit is contained in:
Ruben S. Montero 2011-11-26 01:12:24 +01:00
parent 053efd8854
commit 23e33e3200
3 changed files with 50 additions and 23 deletions

View File

@ -42,6 +42,10 @@ class SshStream
defined?(@stdin)
end
def alive?
@alive == true
end
def open
@stdin, @stdout, @stderr=Open3::popen3("#{SSH_CMD} #{@host} bash -s ; echo #{SSH_RC_STR} $? 1>&2")
@ -50,6 +54,8 @@ class SshStream
@out = ""
@err = ""
@alive = true
end
def close
@ -61,14 +67,18 @@ class SshStream
@stdin.close if not @stdin.closed?
@stdout.close if not @stdout.closed?
@stderr.close if not @stderr.closed?
@alive = false
end
def exec(command)
return if ! @alive
@out = ""
@err = ""
begin
cmd="(#{command}); #{EOF_CMD}\n"
cmd="(#{command}); #{EOF_CMD}"
sliced=cmd.scan(/.{1,100}/)
@ -77,7 +87,7 @@ class SshStream
@stdin.flush
end
@stdin.puts
@stdin.write "\n"
@stdin.flush
rescue
@ -90,12 +100,12 @@ class SshStream
code = -1
while not (done_out and done_err)
while not (done_out and done_err ) and @alive
rc, rw, re= IO.select([@stdout, @stderr],[],[])
rc.each { |fd|
begin
c = fd.read_nonblock(50)
c = fd.read_nonblock(100)
next if !c
rescue #rescue from EOF if ssh command finishes and closes fds
next
@ -115,8 +125,7 @@ class SshStream
@err << OpenNebula.format_error_message(message)
done_out = true
done_err = true
@alive = false
break
end
@ -145,18 +154,29 @@ class SshStream
end
class SshStreamCommand < GenericCommand
def initialize(host, logger=nil, stdin=nil)
@host=host
super('true', logger, stdin)
class SshStreamCommand < RemotesCommand
def initialize(host, remote_dir, logger=nil, stdin=nil)
super('true', host, logger, stdin)
@remote_dir = remote_dir
@stream = SshStream.new(host)
@stream = SshStream.new(host)
@stream.open
end
def run(command, stdin=nil)
def run(command, stdin=nil, base_cmd = nil)
@stream.open unless @stream.opened?
if base_cmd #Check if base command is on remote host
chk_cmd = "if [ ! -x \"#{base_cmd.match(/\S*/)[0]}\" ]; \
then exit #{MAGIC_RC} 1>&2; \
fi"
if @stream.exec_and_wait(chk_cmd) == MAGIC_RC
RemotesCommand.update_remotes(@host, @remote_dir, @logger)
end
end
@stream.exec(command)
@stream.stdin.write(stdin) if stdin
@ -184,7 +204,7 @@ if $0 == __FILE__
ssh.exec("date | tee /tmp/test.javi")
code=ssh.wait_for_command

puts "Code: #{code}"
puts "output: #{ssh.out}"
@ -210,6 +230,9 @@ if $0 == __FILE__
ssh.close
cssh = SshStreamCommand.new('no_host',lambda { |e| STDOUT.puts "error: #{e}" }, nil)
cssh = SshStreamCommand.new('no_host',
'/tmp',
lambda { |e| STDOUT.puts "error: #{e}" },
nil)
cssh.run('whoami')
end
end

View File

@ -74,7 +74,10 @@ class ExecDriver < VirtualMachineDriver
dfile = remote_dfile
end
ssh = SshStreamCommand.new(host, log_method(id))
ssh = SshStreamCommand.new(host,
@remote_scripts_base_path,
log_method(id))
vnm = VirtualNetworkDriver.new(data.elements['NET_DRV'].text,
:local_actions => @options[:local_actions],
:message => data,

View File

@ -49,21 +49,22 @@ class VirtualNetworkDriver
:stdin => nil,
}.merge(ops)
command = action_command_line(aname, @vm_encoded)
cmd = action_command_line(aname, @vm_encoded)
if action_is_local?(aname)
execution = LocalCommand.run(command, log_method(id))
execution = LocalCommand.run(cmd, log_method(id))
else
if options[:stdin]
command = "cat << EOT | #{command}"
stdin = "#{options[:stdin]}\nEOT\n"
cmdin = "cat << EOT | #{cmd}"
stdin = "#{options[:stdin]}\nEOT\n"
else
stdin = nil
cmdin = cmd
stdin = nil
end
execution = @ssh_stream.run(command, stdin)
execution = @ssh_stream.run(cmdin, stdin, cmd)
end
result, info = get_info_from_execution(execution)
end
end
end