Class: Commander

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

Defined Under Namespace

Modules: LoggerBuffer, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Commander

Returns a new instance of Commander.

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
# File 'lib/commander.rb', line 54

def initialize( logger )
  raise ArgumentError.new("A reference to a valid logger must be provided.") if logger.nil? || !logger.respond_to?(:info) || !logger.respond_to?(:error)
  @log = logger
  @log.extend( Commander::LoggerBuffer )

  @exit_status = 0
  @commands = []
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



52
53
54
# File 'lib/commander.rb', line 52

def commands
  @commands
end

#exit_statusObject

Returns the value of attribute exit_status.



52
53
54
# File 'lib/commander.rb', line 52

def exit_status
  @exit_status
end

#logObject

Returns the value of attribute log.



52
53
54
# File 'lib/commander.rb', line 52

def log
  @log
end

Instance Method Details

#errors?Boolean

returns true if logging facility has recorded warnings

Returns:

  • (Boolean)


111
112
113
114
# File 'lib/commander.rb', line 111

def errors?
  return true if @log.buffer.join().match(/WARN|ERROR|FATAL|UNKNOWN/i)
  return false
end

#run(cmd, override = false) ⇒ Object

run a single command set override to true in order to suppress warnings from STDERR



65
66
67
68
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
94
95
96
97
98
99
100
101
# File 'lib/commander.rb', line 65

def run( cmd, override = false )
  @log.info "Command: #{ cmd }"

  # switched to popen4 for support of exitstatus and ease-of-use
  begin
    pid, stdin, stdout, stderr = Open4::popen4 cmd    
    ignored, status = Process::waitpid2 pid
  
    stdout_array = stdout.readlines
    stderr_array  = stderr.readlines
  rescue Exception => err
    # set status.exitstatus to 1 due to failure
    status = Object.new()
    status.instance_eval do
      def exitstatus
        1
      end
    end

    stdout_array = []
    stderr_array = [ err ]
  end

  stdout_array.each do | line |
    @log.info line
  end
  
  stderr_array.each do | line |
    if override
      @log.info line
    else
      @log.error line
    end
  end

  @exit_status = status.exitstatus
end

#run_commandsObject

run commands in commands array sequentially



104
105
106
107
108
# File 'lib/commander.rb', line 104

def run_commands()
  @commands.each do | command |
    self.run( command )
  end
end