Class: GenericCommand
- Inherits:
-
Object
- Object
- GenericCommand
- Defined in:
- lib/CommandManager.rb
Direct Known Subclasses
Constant Summary collapse
- ERROR_OPEN =
"ERROR MESSAGE --8<------"
- ERROR_CLOSE =
"ERROR MESSAGE ------>8--"
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
-
.run(command, logger = nil, stdin = nil, timeout = nil) ⇒ Object
Creates a command and runs it.
Instance Method Summary collapse
-
#get_error_message ⇒ Object
Parses error message from
stderr
output. -
#initialize(command, logger = nil, stdin = nil, timeout = nil) ⇒ GenericCommand
constructor
Creates the new command:
command
: string with the command to be executedlogger
: proc that takes a message parameter and logs it. -
#log(message, all = true) ⇒ Object
Sends a log message to the logger proc.
-
#run ⇒ Object
Runs the command.
- #to_xml ⇒ Object
Constructor Details
#initialize(command, logger = nil, stdin = nil, timeout = nil) ⇒ GenericCommand
Creates the new command: command
: string with the command to be executed logger
: proc that takes a message parameter and logs it
61 62 63 64 65 66 |
# File 'lib/CommandManager.rb', line 61 def initialize(command, logger=nil, stdin=nil, timeout=nil) @command = command @logger = logger @stdin = stdin @timeout = timeout end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
49 50 51 |
# File 'lib/CommandManager.rb', line 49 def code @code end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
49 50 51 |
# File 'lib/CommandManager.rb', line 49 def command @command end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
49 50 51 |
# File 'lib/CommandManager.rb', line 49 def stderr @stderr end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
49 50 51 |
# File 'lib/CommandManager.rb', line 49 def stdout @stdout end |
Class Method Details
.run(command, logger = nil, stdin = nil, timeout = nil) ⇒ Object
Creates a command and runs it
52 53 54 55 56 |
# File 'lib/CommandManager.rb', line 52 def self.run(command, logger=nil, stdin=nil, timeout=nil) cmd = self.new(command, logger, stdin, timeout) cmd.run cmd end |
Instance Method Details
#get_error_message ⇒ Object
Parses error message from stderr
output
104 105 106 107 108 |
# File 'lib/CommandManager.rb', line 104 def tmp=@stderr.scan(/^#{ERROR_OPEN}\n(.*?)#{ERROR_CLOSE}$/m) return "-" if !tmp[0] tmp[0].join(' ').strip end |
#log(message, all = true) ⇒ Object
Sends a log message to the logger proc
69 70 71 |
# File 'lib/CommandManager.rb', line 69 def log(, all=true) @logger.call(, all) if @logger end |
#run ⇒ Object
Runs the command
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/CommandManager.rb', line 74 def run begin @stdout, @stderr, status = execute if status && status.exited? @code = status.exitstatus else @code = 255 end if @code != 0 log("Command execution failed (exit code: #{@code}): #{command}") log(@stderr) end rescue Exception => e if e.is_a?(Timeout::Error) = "Timeout executing #{command}" else = "Internal error #{e}" end log() @stderr = ERROR_OPEN + "\n" + + "\n" + ERROR_CLOSE @code = 255 end return @code end |
#to_xml ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/CommandManager.rb', line 110 def to_xml stdout = @stdout.nil? ? '' : @stdout stderr = @stderr.nil? ? '' : @stderr '<EXECUTION_RESULT>' \ "<COMMAND>#{@command}</COMMAND>" \ "<STDOUT>#{Base64.encode64(stdout)}</STDOUT>" \ "<STDERR>#{Base64.encode64(stderr)}</STDERR>" \ "<CODE>#{@code}</CODE>" \ '</EXECUTION_RESULT>' end |