Class: Rubyipmi::BaseCommand

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/rubyipmi/commands/basecommand.rb

Direct Known Subclasses

Freeipmi::BaseCommand, Ipmitool::BaseCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commandname, opts = ObservableHash.new) ⇒ BaseCommand

Returns a new instance of BaseCommand.



33
34
35
36
37
38
# File 'lib/rubyipmi/commands/basecommand.rb', line 33

def initialize(commandname, opts = ObservableHash.new)
  # This will locate the command path or raise an error if not found
  @cmdname = commandname
  @options = opts
  @options.add_observer(self)
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



9
10
11
# File 'lib/rubyipmi/commands/basecommand.rb', line 9

def cmd
  @cmd
end

#lastcallObject (readonly)

Returns the value of attribute lastcall.



11
12
13
# File 'lib/rubyipmi/commands/basecommand.rb', line 11

def lastcall
  @lastcall
end

#max_retry_countObject (readonly)

Returns the value of attribute max_retry_count.



9
10
11
# File 'lib/rubyipmi/commands/basecommand.rb', line 9

def max_retry_count
  @max_retry_count
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/rubyipmi/commands/basecommand.rb', line 10

def options
  @options
end

#passfileObject

Returns the value of attribute passfile.



10
11
12
# File 'lib/rubyipmi/commands/basecommand.rb', line 10

def passfile
  @passfile
end

#resultObject (readonly)

Returns the value of attribute result.



11
12
13
# File 'lib/rubyipmi/commands/basecommand.rb', line 11

def result
  @result
end

Instance Method Details

#dump_commandObject



29
30
31
# File 'lib/rubyipmi/commands/basecommand.rb', line 29

def dump_command
  makecommand
end

#find_fix(result) ⇒ Object

The findfix method acts like a recursive method and applies fixes defined in the errorcodes If a fix is found it is applied to the options hash, and then the last run command is retried until all the fixes are exhausted or a error not defined in the errorcodes is found this must be overrided in the subclass, as there are no generic errors that fit both providers



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rubyipmi/commands/basecommand.rb', line 95

def find_fix(result)
  if result
    # The errorcode code hash contains the fix
    begin
      fix = ErrorCodes.search(result)
      @options.merge_notify!(fix)
    rescue
      Rubyipmi.logger.debug("Could not find fix for error code: \n#{result}") if logger
      raise "Could not find fix for error code: \n#{result}"
    end
  end
end

#locate_command(commandname) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/rubyipmi/commands/basecommand.rb', line 40

def locate_command(commandname)
  location = `which #{commandname}`.strip
  if not $?.success?
    logger.error("#{commandname} command not found, is #{commandname} installed?") if logger
    raise "#{commandname} command not found, is #{commandname} installed?"
  end
  location
end

#loggerObject



13
14
15
# File 'lib/rubyipmi/commands/basecommand.rb', line 13

def logger
  Rubyipmi.logger
end

#makecommandObject



17
18
19
# File 'lib/rubyipmi/commands/basecommand.rb', line 17

def makecommand
  # override in subclass
end

#removepassObject



25
26
27
# File 'lib/rubyipmi/commands/basecommand.rb', line 25

def removepass
  @passfile.unlink
end

#runObject



59
60
61
62
63
64
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
# File 'lib/rubyipmi/commands/basecommand.rb', line 59

def run
  # we search for the command everytime just in case its removed during execution
  # we also don't want to add this to the initialize since mocking is difficult and we don't want to
  # throw errors upon object creation
  retrycount = 0
  process_status = false
  @cmd = locate_command(@cmdname)
  setpass
  @result = nil
  logger.debug(makecommand) if logger
  begin
    command = makecommand
    @lastcall = "#{command}"
    @result = `#{command} 2>&1`
    # sometimes the command tool does not return the correct result so we have to validate it with additional
    # code
    process_status = validate_status($?)
  rescue
    if retrycount < max_retry_count
      find_fix(@result)
      retrycount = retrycount.next
      retry
    else
      logger.error("Exhausted all auto fixes, cannot determine what the problem is") if logger
      raise "Exhausted all auto fixes, cannot determine what the problem is"
    end
  ensure
    removepass
    return process_status
  end
end

#runcmdObject

Use this function to run the command line tool, it will inherently use the options hash for all options That need to be specified on the command line



51
52
53
54
55
56
57
# File 'lib/rubyipmi/commands/basecommand.rb', line 51

def runcmd
  @success = false
  @success = run
  logger.debug(@lastcall.inspect) unless @lastcall.nil? if logger
  logger.debug(@result) unless @result.nil? if logger
  @success
end

#setpassObject



21
22
23
# File 'lib/rubyipmi/commands/basecommand.rb', line 21

def setpass
  @passfile = Tempfile.new('')
end

#update(opts) ⇒ Object



108
109
110
# File 'lib/rubyipmi/commands/basecommand.rb', line 108

def update(opts)
      @options.merge!(opts)
end

#validate_status(exitstatus) ⇒ Object

This method will check if the results are really valid as the exit code can be misleading and incorrect



113
114
115
116
117
118
119
120
# File 'lib/rubyipmi/commands/basecommand.rb', line 113

def validate_status(exitstatus)
  # override in child class if needed
  if ! exitstatus.success?
     raise "Error occurred"
  else
    return true
  end
end