lib/rubyipmi.rb in rubyipmi-0.10.0 vs lib/rubyipmi.rb in rubyipmi-0.11.0

- old
+ new

@@ -14,20 +14,19 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 # USA # - require 'rubyipmi/ipmitool/connection' require 'rubyipmi/freeipmi/connection' require 'logger' class NullLogger < Logger - def initialize(*args) + def initialize(*_args) end - def add(*args, &block) + def add(*_args, &_block) end end module Rubyipmi PRIV_TYPES = ['CALLBACK', 'USER', 'OPERATOR', 'ADMINISTRATOR'] @@ -49,11 +48,11 @@ # this is an read only method that only creates a real logger if the log_level is set # if the log_level is not setup it creates a null logger which logs nothing def self.logger # by default the log will be set to info unless @logger - if @log_level and @log_level >= 0 + if @log_level && @log_level >= 0 @logger = Logger.new('/tmp/rubyipmi.log') @logger.progname = 'Rubyipmi' @logger.level = @log_level else @logger = NullLogger.new @@ -71,11 +70,11 @@ end # The connect method will create a connection object based the provider type passed in # If provider is left blank the function will use the first available provider # When the driver is set to auto, rubyipmi will try and figure out which driver to use by common error messages. We will most likely be using # the lan20 driver, but in order to support a wide use case we default to auto. - def self.connect(user, pass, host, provider='any', opts={:driver => 'lan20', :timeout => 'default'}) + def self.connect(user, pass, host, provider = 'any', opts = {:driver => 'lan20', :timeout => 'default'}) # use this variable to reduce cmd calls installed = false # if the user supplied nil, we want to fix this automatically opts = {:driver => 'lan20', :timeout => 'default'} if opts.nil? @@ -86,20 +85,20 @@ end # allow the user to specify an options hash instead of the provider # in the future I would stop using the provider and use the opts hash instead to get the provider # This allows us to be a little more flexible if the user is doesn't supply us what we need. - if provider.is_a?(Hash) + if provider.kind_of?(Hash) opts = provider provider = opts[:provider] ||= 'any' end # Verify options just in case user passed in a incomplete hash - opts[:driver] ||= 'lan20' + opts[:driver] ||= 'lan20' opts[:timeout] ||= 'default' - if opts[:privilege] and not supported_privilege_type?(opts[:privilege]) + if opts[:privilege] && !supported_privilege_type?(opts[:privilege]) logger.error("Invalid privilege type :#{opts[:privilege]}, must be one of: #{PRIV_TYPES.join("\n")}") if logger raise "Invalid privilege type :#{opts[:privilege]}, must be one of: #{PRIV_TYPES.join("\n")}" end # use the first available provider @@ -122,15 +121,15 @@ logger.debug("You must specify a valid driver: #{valid_drivers.join(',')}") if logger raise "You must specify a valid driver: #{valid_drivers.join(',')}" end # If the provider is available create a connection object - if installed or is_provider_installed?(provider) + if installed || is_provider_installed?(provider) if provider == "freeipmi" Rubyipmi::Freeipmi::Connection.new(user, pass, host, opts) elsif provider == "ipmitool" - Rubyipmi::Ipmitool::Connection.new(user,pass,host, opts) + Rubyipmi::Ipmitool::Connection.new(user, pass, host, opts) else logger.error("Incorrect provider given, must use one of #{valid_providers.join(', ')}") if logger raise "Incorrect provider given, must use one of #{valid_providers.join(', ')}" end else @@ -140,35 +139,33 @@ end end # returns boolean true if privilege type is valid def self.supported_privilege_type?(type) - PRIV_TYPES.include?(type) + PRIV_TYPES.include?(type) end # method used to find the command which also makes it easier to mock with def self.locate_command(commandname) location = `which #{commandname}`.strip - if not $?.success? - location = nil - end + location = nil unless $?.success? location end # Return true or false if the provider is available def self.is_provider_installed?(provider) case provider - when "freeipmi" - cmdpath = locate_command('ipmipower') - when "ipmitool" - cmdpath = locate_command('ipmitool') - else - logger.error("Invalid BMC provider type #{provider}") if logger - false + when "freeipmi" + cmdpath = locate_command('ipmipower') + when "ipmitool" + cmdpath = locate_command('ipmitool') + else + logger.error("Invalid BMC provider type #{provider}") if logger + false end # return false if command was not found - return ! cmdpath.nil? + !cmdpath.nil? end def self.providers ["freeipmi", "ipmitool"] end @@ -179,19 +176,17 @@ end def self.providers_installed available = [] providers.each do |prov| - if is_provider_installed?(prov) - available << prov - end + available << prov if is_provider_installed?(prov) end - return available + available end # gets data from the bmc device and puts in a hash for diagnostics - def self.get_diag(user, pass, host, opts={:driver => 'lan20', :timeout => 'default'}) + def self.get_diag(user, pass, host, opts = {:driver => 'lan20', :timeout => 'default'}) data = {} if Rubyipmi.is_provider_installed?('freeipmi') freeconn = Rubyipmi.connect(user, pass, host, 'freeipmi', opts) if freeconn puts "Retrieving freeipmi data" @@ -203,9 +198,9 @@ if ipmiconn puts "Retrieving ipmitool data" data[:ipmitool] = ipmiconn.get_diag end end - File.open('/tmp/rubyipmi_diag_data.txt', 'w') {|f| f.write(data)} + File.open('/tmp/rubyipmi_diag_data.txt', 'w') { |f| f.write(data) } puts "Created file /tmp/rubyipmi_diag_data.txt" end end