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



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

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.



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

def code
  @code
end

#commandObject (readonly)

Returns the value of attribute command.



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

def command
  @command
end

#stderrObject (readonly)

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



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

def stdout
  @stdout
end

Class Method Details

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

Creates a command and runs it



52
53
54
55
56
# 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

Instance Method Details

#get_error_messageObject

Parses error message from stderr output



133
134
135
136
137
# 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) ⇒ Object



73
74
75
76
77
78
79
80
# 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) ⇒ Object

Sends a log message to the logger proc



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

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

#runObject

Runs the command



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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