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 133
def get_error_message
    tmp=@stderr.scan(/^#{ERROR_OPEN}\n(.*?)#{ERROR_CLOSE}$/m)
    return "-" if !tmp[0]
    tmp[0].join(' ').strip
end
kill(pid) click to toggle source
# File lib/CommandManager.rb, line 73
def kill(pid)
    # executed processes now have its own process group to be able
    # to kill all children
    pgid = Process.getpgid(pid)

    # Kill all processes belonging to process group
    Process.kill("HUP", pgid * -1)
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 83
def run
    std = nil
    process = Proc.new do
        std = execute

        # Close standard IO descriptors
        if @stdin
            std[0] << @stdin
            std[0].flush
        end
        std[0].close if !std[0].closed?

        @stdout=std[1].read
        std[1].close if !std[1].closed?

        @stderr=std[2].read
        std[2].close if !std[2].closed?

        @code=get_exit_code(@stderr)

        if @code!=0
            log("Command execution fail: #{command}")
            log(@stderr)
        end
    end

    begin
        if @timeout
            Timeout.timeout(@timeout, nil, &process)
        else
            process.call
        end
    rescue Timeout::Error
        error_message = "Timeout executing #{command}"
        log(error_message)

        @stderr = ERROR_OPEN + "\n" + error_message + "\n" + ERROR_CLOSE

        3.times {|n| std[n].close if !std[n].closed? }

        pid = std[-1].pid
        self.kill(pid)

        @code = 255
    end

    return @code
end

Private Instance Methods

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 stdin, stdout and stderr handlers of the command execution.

# File lib/CommandManager.rb, line 151
def execute
    puts "About to execute \"#{@command}\""
    [StringIO.new, StringIO.new, StringIO.new]
end
get_exit_code(str) click to toggle source

Gets exit code from STDERR

# File lib/CommandManager.rb, line 142
def get_exit_code(str)
    tmp=str.scan(/^ExitCode: (\d*)$/)
    return nil if !tmp[0]
    tmp[0][0].to_i
end