Module: Irgat::Execs

Included in:
Irgat
Defined in:
lib/irgat/execs.rb

Instance Method Summary collapse

Instance Method Details

#execute_command(command, command_description, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/irgat/execs.rb', line 6

def execute_command(command, command_description, options = {})
  @commands_launched ||= Array.new

  command_data     = Hash.new

  command_data[:info]     = command_description
  command_data[:exec]     = command.to_s
  command_data[:pre_time] = Time.now

  # No status is retuned, append at exit 
  Open3.popen3 "#{ command_data[:exec] }; echo \"_irgat_data_exit_command|\"$?" do |stdin, stdout, stderr|
    command_data[:errors] = stderr.readlines.join('')
    command_data[:output] = stdout.readlines.join('')
  end
  tmp = ''
  command_data[:output].each_line {|l|
    # exit status? 
    if l =~ /^_irgat_data_exit_command/
      command_data[:status] = l.split('|').last.to_i
    else
      tmp << l
    end
  }

  command_data[:output] = tmp

  #changes in output 
  #command_data[:output]   = %x[ #{command_data[:exec] } 2> /tmp/error_out ]
  #command_data[:status]   = $?
  # command_data[:errors]   = File.new("/tmp/error_out").read

  command_data[:pos_time] = Time.now
 

  # add to commands launched
  @commands_launched << { command_data[:info] => command_data[:status] }

  # exit?
  if command_data[:status] != 0 && options[:fatal_error]
    debug_mode = "\n"
    debug_mode << "\n [ Command Syntax ] => #{ command_data[:exec]   }"
    debug_mode << "\n [ Command Output ] => #{ command_data[:output] }"
    debug_mode << "\n [ Command Errors ] => #{ command_data[:errors] }"


    exit_with_error("Command '#{ command_data[:info] }' exit with a #{ command_data[:status] } status. Error is reported as FATAL, then, exit Irgat process. #{ debug_mode if @config[:debug_level] == 3 }", { :exit_level => 6 })
  end

  # Log the outputs || debug 1
  log("command", "* [#{ (command_data[:status] == 0 ? "   OK   " : " WARNING") }: #{Time.now.strftime("%H:%M:%S")}] -> #{ command_data[:info] }", 1) unless options[:debug] == 2 || options[:debug] == 3

  # Log warning level
  if command_data[:status] != 0
    log("warning", "  ** Command #{ command_data[:info] } exited with a #{ command_data[:status] } status.", 2)
  end

  # Log 3 level... commands launched and reports
  text_to_log =  "\n   *** [COMMAND LOG]"
  text_to_log << "\n        [ Command Syntax ] => #{ command_data[:exec]   }"
  text_to_log << "\n        [ Command Status ] => #{ command_data[:status]  }"
  text_to_log << "\n        [ Command Output ] => #{ command_data[:output] }"
  text_to_log << "\n        [ Command Errors ] => #{ command_data[:errors]   }"
  text_to_log << "\n        [ Command P.Time ] => #{ command_data[:pos_time] - command_data[:pre_time] }\n\n"

  log("text", text_to_log, 3)

  return command_data
end

#exit_with_error(msj, options = {}) ⇒ Object

Exit irgat process with error and optional exit status



77
78
79
80
81
82
83
84
85
86
# File 'lib/irgat/execs.rb', line 77

def exit_with_error(msj, options = {})
  log("error", msj, 1)

  # output unless irgat is not running a module
  output_log(self,
            { :main_msj => "[IRGAT] [#{ self.config_module[:subsystem_name].upcase }] [ERROR] #{ self.action } process in #{ @config[:server_name] }",
              :fatal => true }) unless self.class.to_s == "Irgat::Irgat"

  exit (options[:exit_level] ? options[:exit_level] : 1)
end