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

feature #863: uses scripts_common for error_messages, added more rescue if ssh command fails.

This commit is contained in:
Ruben S. Montero 2011-11-25 00:30:58 +01:00
parent 1a15293903
commit 800ee164cf
2 changed files with 29 additions and 22 deletions

View File

@ -43,11 +43,18 @@ module OpenNebula
# This function is used to pass error message to the mad
def self.error_message(message)
STDERR.puts "ERROR MESSAGE --8<------"
STDERR.puts message
STDERR.puts "ERROR MESSAGE ------>8--"
STDERR.puts format_error_message(message)
end
#This function formats an error message for OpenNebula Copyright e
def self.format_error_message(message)
error_str = "ERROR MESSAGE --8<------\n"
error_str << message
error_str << "\nERROR MESSAGE ------>8--"
return error_str
end
# Executes a command, if it fails returns error message and exits
# If a second parameter is present it is used as the error message when
# the command fails

View File

@ -16,7 +16,7 @@
require 'CommandManager'
require 'open3'
require 'scripts_common'
class SshStream
attr_reader :stream_out, :stream_err, :stdin
@ -53,7 +53,10 @@ class SshStream
end
def close
@stdin.puts "\nexit"
begin
@stdin.puts "\nexit"
rescue #rescue from EPIPE if ssh command exited already
end
@stdin.close if not @stdin.closed?
@stdout.close if not @stdout.closed?
@ -93,15 +96,11 @@ class SshStream
rc.each { |fd|
begin
c = fd.read_nonblock(50)
rescue EOFError => e
next if !c
rescue #rescue from EOF if ssh command finishes and closes fds
next
end
if !c
done = true
break
end
if fd == @stdout
@out << c
done_out = true if @out.slice!("#{EOF_OUT}\n")
@ -111,11 +110,14 @@ class SshStream
tmp = @err.scan(/^#{SSH_RC_STR}(\d+)$/)
if tmp[0]
@err << "\n"
@err << "ERROR MESSAGE --8<------\n"
@err << "Error connecting to #{@host}\n"
@err << "ERROR MESSAGE ------>8--\n"
return tmp[0][0].to_i
message = "Error connecting to #{@host}"
code = tmp[0][0].to_i
@err << OpenNebula.format_error_message(message)
done_out = true
done_err = true
break
end
tmp = @err.scan(/^#{RC_STR}(\d*) #{EOF_ERR}\n/)
@ -182,7 +184,7 @@ if $0 == __FILE__
ssh.exec("date | tee /tmp/test.javi")
code=ssh.wait_for_command

puts "Code: #{code}"
puts "output: #{ssh.out}"
@ -206,10 +208,8 @@ if $0 == __FILE__
puts "output: #{ssh.out}"
puts "output err: #{ssh.err}"
ssh.close
cssh = SshStreamCommand.new('no_host',lambda { |e| STDOUT.puts "error: #{e}" }, nil)
cssh.run('whoami')
end