class GenericCommand

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

Properties:

The protocol for scripts to log is as follows:

Constants

ERROR_CLOSE
ERROR_OPEN

Attributes

code[R]
command[R]
stderr[R]
stdout[R]

Public Class Methods

new(command, logger=nil, stdin=nil, timeout=nil) click to toggle source

Creates the new command: command: string with the command to be executed logger: proc that takes a message parameter and logs it

# File lib/CommandManager.rb, line 61
def initialize(command, logger=nil, stdin=nil, timeout=nil)
    @command = command
    @logger  = logger
    @stdin   = stdin
    @timeout = timeout
end
run(command, logger=nil, stdin=nil, timeout=nil) click to toggle source

Creates a command and runs it

# 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

Public Instance Methods

get_error_message() click to toggle source

Parses error message from stderr output

# File lib/CommandManager.rb, line 104
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) click to toggle source

Sends a log message to the logger proc

# File lib/CommandManager.rb, line 69
def log(message, all=true)
    @logger.call(message, all) if @logger
end
run() click to toggle source

Runs the command

# 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)
            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
to_xml() click to toggle source
# 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

Private Instance Methods

capture3_timeout(*cmd) click to toggle source

modified Open3.capture with terminator thread to deal with timeouts

# File lib/CommandManager.rb, line 134
def capture3_timeout(*cmd)
    if Hash === cmd.last
        opts = cmd.pop.dup
    else
        opts = {}
    end

    stdin_data = opts.delete(:stdin_data) || ''
    binmode = opts.delete(:binmode)

    Open3.popen3(*cmd, opts) {|i, o, e, t|
        if binmode
            i.binmode
            o.binmode
            e.binmode
        end

        terminator_e = nil
        mutex = Mutex.new

        out_reader = Thread.new { o.read }
        err_reader = Thread.new { e.read }
        terminator = Thread.new {
            if @timeout and @timeout>0
                begin
                    pid = Process.getpgid(t.pid) * -1
                rescue
                    pid = t.pid
                end

                if pid
                    begin
                        sleep @timeout

                        mutex.synchronize do
                            terminator_e = Timeout::Error
                        end
                    ensure
                    end

                    Process.kill('TERM', pid)
                end
            end
        }

        i.write stdin_data
        i.close

        # blocking wait for process termination
        t.value

        # if reader threads are not dead yet, kill them
        [out_reader, err_reader].each do |reader|
            next unless reader.status

            reader.join(0.1)
            reader.kill
        end

        mutex.lock
        terminator.kill
        raise terminator_e if terminator_e

        # return values
        [out_reader.value, err_reader.value, t.value]
    }
end
execute() click to toggle source

Low level command execution. This method has to be redefined for each kind of command execution. Returns an array with stdout, stderr and status of the command execution.

# File lib/CommandManager.rb, line 127
def execute
    puts "About to execute \"#{@command}\""
    ['', '', nil]
end