From 23e33e32003ea08cda38c66bc3df1be112c23afc Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 26 Nov 2011 01:12:24 +0100 Subject: [PATCH] feature #863: Update remotes in sshtream. Checks if the stream is alive before using it --- src/mad/ruby/ssh_stream.rb | 53 +++++++++++++++++++++++--------- src/vmm_mad/exec/one_vmm_exec.rb | 5 ++- src/vnm_mad/one_vnm.rb | 15 ++++----- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/mad/ruby/ssh_stream.rb b/src/mad/ruby/ssh_stream.rb index b0de36b72f..067227beeb 100644 --- a/src/mad/ruby/ssh_stream.rb +++ b/src/mad/ruby/ssh_stream.rb @@ -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 \ No newline at end of file diff --git a/src/vmm_mad/exec/one_vmm_exec.rb b/src/vmm_mad/exec/one_vmm_exec.rb index 659c37767c..c79d789410 100755 --- a/src/vmm_mad/exec/one_vmm_exec.rb +++ b/src/vmm_mad/exec/one_vmm_exec.rb @@ -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, diff --git a/src/vnm_mad/one_vnm.rb b/src/vnm_mad/one_vnm.rb index a5f069b1c5..aba0339611 100644 --- a/src/vnm_mad/one_vnm.rb +++ b/src/vnm_mad/one_vnm.rb @@ -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 \ No newline at end of file