Class: GenericCommand

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

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) ⇒ GenericCommand

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



57
58
59
60
61
# File 'lib/CommandManager.rb', line 57

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

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#commandObject (readonly)

Returns the value of attribute command.



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

def command
  @command
end

#stderrObject (readonly)

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



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

def stdout
  @stdout
end

Class Method Details

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

Creates a command and runs it



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

def self.run(command, logger=nil, stdin=nil)
    cmd = self.new(command, logger, stdin)
    cmd.run
    cmd
end

Instance Method Details

#get_error_messageObject

Parses error message from stderr output



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

def get_error_message
    tmp=@stderr.scan(/^ERROR MESSAGE --8<------\n(.*?)ERROR MESSAGE ------>8--$/m)
    return "-" if !tmp[0]
    tmp[0].join(' ').strip
end

#log(message) ⇒ Object

Sends a log message to the logger proc



64
65
66
# File 'lib/CommandManager.rb', line 64

def log(message)
    @logger.call(message) if @logger
end

#runObject

Runs the command



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

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

    return @code
end