class Rubyipmi::BaseCommand

Attributes

cmd[R]
lastcall[R]
max_retry_count[R]
options[RW]
passfile[RW]
result[R]

Public Class Methods

new(commandname, opts = ObservableHash.new) click to toggle source
# File lib/rubyipmi/commands/basecommand.rb, line 32
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

Public Instance Methods

dump_command() click to toggle source
# File lib/rubyipmi/commands/basecommand.rb, line 28
def dump_command
  makecommand
end
find_fix(result) click to toggle source

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

# File lib/rubyipmi/commands/basecommand.rb, line 93
def find_fix(result)
  return unless 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
locate_command(commandname) click to toggle source
# File lib/rubyipmi/commands/basecommand.rb, line 39
def locate_command(commandname)
  location = `which #{commandname}`.strip
  unless $?.success?
    logger.error("#{commandname} command not found, is #{commandname} installed?") if logger
    raise "#{commandname} command not found, is #{commandname} installed?"
  end
  location
end
logger() click to toggle source
# File lib/rubyipmi/commands/basecommand.rb, line 12
def logger
  Rubyipmi.logger
end
makecommand() click to toggle source
# File lib/rubyipmi/commands/basecommand.rb, line 16
def makecommand
  # override in subclass
end
removepass() click to toggle source
# File lib/rubyipmi/commands/basecommand.rb, line 24
def removepass
  @passfile.unlink
end
run() click to toggle source
# File lib/rubyipmi/commands/basecommand.rb, line 58
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, 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
    process_status
  end
end
runcmd() click to toggle source

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

# File lib/rubyipmi/commands/basecommand.rb, line 50
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
setpass() click to toggle source
# File lib/rubyipmi/commands/basecommand.rb, line 20
def setpass
  @passfile = Tempfile.new('')
end
update(opts) click to toggle source
# File lib/rubyipmi/commands/basecommand.rb, line 105
def update(opts)
  @options.merge!(opts)
end
validate_status(exitstatus) click to toggle source

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

# File lib/rubyipmi/commands/basecommand.rb, line 110
def validate_status(exitstatus)
  raise "Error occurred" unless exitstatus.success?

  true
end