Class: GenericCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/CommandManager.rb

Direct Known Subclasses

LocalCommand, SSHCommand

Constant Summary collapse

ERROR_OPEN =
"ERROR MESSAGE --8<------"
ERROR_CLOSE =
"ERROR MESSAGE ------>8--"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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



60
61
62
63
64
65
# File 'lib/CommandManager.rb', line 60

def initialize(command, logger=nil, stdin=nil, timeout=nil)
    @command = command
    @logger  = logger
    @stdin   = stdin
    @timeout = timeout
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



48
49
50
# File 'lib/CommandManager.rb', line 48

def code
  @code
end

#commandObject (readonly)

Returns the value of attribute command.



48
49
50
# File 'lib/CommandManager.rb', line 48

def command
  @command
end

#stderrObject (readonly)

Returns the value of attribute stderr.



48
49
50
# File 'lib/CommandManager.rb', line 48

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



48
49
50
# File 'lib/CommandManager.rb', line 48

def stdout
  @stdout
end

Class Method Details

.run(command, logger = nil, stdin = nil, timeout = nil) ⇒ Object

Creates a command and runs it



51
52
53
54
55
# File 'lib/CommandManager.rb', line 51

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_messageObject

Parses error message from stderr output



103
104
105
106
107
# File 'lib/CommandManager.rb', line 103

def get_error_message
    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



68
69
70
# File 'lib/CommandManager.rb', line 68

def log(message, all=true)
    @logger.call(message, all) if @logger
end

#runObject

Runs the command



73
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
# File 'lib/CommandManager.rb', line 73

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)
            error_message = "Timeout executing #{command}"
        else
            error_message = "Internal error #{e}"
        end

        log(error_message)
        @stderr = ERROR_OPEN + "\n" + error_message + "\n" + ERROR_CLOSE
        @code = 255
    end

    return @code
end