Class: GenericCommand

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

Overview

Generic command executor that holds the code shared by all the command executors.

Properties:

  • code: integer holding the exit code. Read-only

  • stdout: string of the standard output. Read-only

  • stderr: string of the standard error. Read-only

  • command: command to execute. Read-only

The protocol for scripts to log is as follows:

  • Log messages will be sent to STDOUT

  • The script will return 0 if it succeded or any other value if there was a failure

  • In case of failure the cause of the error will be written to STDERR.

Direct Known Subclasses

LocalCommand, SSHCommand

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



53
54
55
56
57
58
# File 'lib/CommandManager.rb', line 53

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.



41
42
43
# File 'lib/CommandManager.rb', line 41

def code
  @code
end

#commandObject (readonly)

Returns the value of attribute command.



41
42
43
# File 'lib/CommandManager.rb', line 41

def command
  @command
end

#stderrObject (readonly)

Returns the value of attribute stderr.



41
42
43
# File 'lib/CommandManager.rb', line 41

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



41
42
43
# File 'lib/CommandManager.rb', line 41

def stdout
  @stdout
end

Class Method Details

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

Creates a command and runs it



44
45
46
47
48
# File 'lib/CommandManager.rb', line 44

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



97
98
99
100
101
# File 'lib/CommandManager.rb', line 97

def get_error_message
    return '-' if @stderr.empty?

    @stderr.tr("\n",' ').strip
end

#log(message, all = true) ⇒ Object

Sends a log message to the logger proc



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

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

#runObject

Runs the command



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/CommandManager.rb', line 66

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_message
        @code   = 255
    end

    return @code
end

#to_xmlObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/CommandManager.rb', line 103

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