lib/consolr.rb in consolr-1.0.2 vs lib/consolr.rb in consolr-1.1.0

- old
+ new

@@ -1,13 +1,13 @@ $LOAD_PATH.unshift('./lib/') require 'consolr/version' require 'collins_auth' -require 'net/ping' require 'optparse' require 'yaml' + module Consolr class Console attr_reader :ipmitool_exec, :dangerous_assets, :dangerous_status, :dangerous_actions, :options, :opt_parser @@ -17,11 +17,11 @@ '/etc/consolr.yml', '/etc/consolr.yaml', '/var/db/consolr.yml', '/var/db/consolr.yaml'].compact.find do |conf| File.readable?(File.expand_path(conf, __FILE__)) and File.size(File.expand_path(conf, __FILE__)) > 0 end - config_params = begin + @config_params = begin YAML.load(File.open(File.expand_path(config_file, __FILE__))) rescue TypeError => e puts "-------" puts "Failed to load Configuration File ... " puts "Looks like a configuration file doesn't exist." @@ -34,37 +34,25 @@ puts "Looks like the configuration file is not correctly formatted" puts "Please check if your file conforms to YAML spec" puts "------" exit 1 end - - begin - @ipmitool_exec = config_params['ipmitool'] # ipmitool absolute path - rescue Exception => e - puts e - puts "-------" - puts "Ensure that the ipmitool's path (#{@ipmitool_exec}) is given in the consolr.yml file and is correct" - puts "-------" - exit 1 - end - + # # Will be ignored for dangerous actions, no matter what, even with --force begin - @dangerous_assets = config_params['dangerous_assets'] + @dangerous_assets = @config_params['dangerous_assets'] rescue Exception => e puts e puts "-------" puts "Dangerous Assets -- #{dangrous}" puts "Please make sure dangerous_assets exists and is valid." puts "Supply them in a comma separated list." - puts "-------" - exit 1 end # Dangerous actions wont be run in these status, override with --force begin - @dangerous_status = config_params['dangerous_status'] + @dangerous_status = @config_params['dangerous_status'] rescue Exception => e puts e puts "-------" puts "Dangerous Status -- #{@dangeous_status}" puts "Please specify the statuses which are dangerorous, during which dangerous shouldn't be run." @@ -76,12 +64,10 @@ end def start options abort("Please pass either the asset tag or hostname") if options[:tag].nil? and options[:hostname].nil? - abort("Cannot find #{@ipmitool_exec}") unless File.exist?(@ipmitool_exec) - dangerous_body = "Dangerous actions: #{dangerous_actions.join(', ')}\n"\ "Dangerous status: #{dangerous_status.join(', ')} (override with --force)\n"\ "Dangerous assets: #{dangerous_assets.join(', ')} (ignored no matter what, even with --force)" if options[:dangerous] @@ -101,16 +87,41 @@ if options[:tag] and options[:hostname] abort("Please pass either the hostname OR the tag but not both.") end + if options[:runner].nil? + runners = load_runners(@config_params.fetch('runners', [])) + # Default to the ipmitool runner for backwards compatibility + if runners.empty? + require 'consolr/runners/ipmitool' + runners = [Consolr::Runners::Ipmitool.new(@config_params.fetch('ipmitool', {}))] + end + else + runners = load_runners([options[:runner]]) + if runners.empty? + abort('Specified runner could not be loaded. Aborting.') + end + end + # match assets like vm-67f5eh, zt-*, etc. nodes = options[:tag] ? (collins.find :tag => options[:tag]) : (collins.find :hostname => options[:hostname]) @node = nodes.length == 1 ? nodes.first : abort("Found #{nodes.length} assets, aborting.") - abort("Cannot ping IP #{@node.ipmi.address} (#{@node.tag})") unless Net::Ping::External.new(@node.ipmi.address).ping? + # select the first runner that support the node + runner = runners.select {|runner| + runner.can_run? @node + }.first + if runner.nil? + abort("No runners available for node #{@node.hostname} (#{@node.tag})") + end + + if not runner.verify @node + abort("Cannot verify asset #{@node.hostname} (#{@node.tag})") + end + selected_dangerous_actions = dangerous_actions.select { |o| options[o] } if dangerous_assets.include?(@node.tag) and selected_dangerous_actions.any? abort "Asset #{@node.tag} is a crucial asset. Can't ever execute dangerous actions on this asset.\n#{dangerous_body}" end @@ -119,42 +130,49 @@ end case when options[:console] puts '--> Opening SOL session (type ~~. to quit)' - puts ipmitool_cmd('sol activate') + puts runner.console @node when options[:kick] - puts ipmitool_cmd('sol deactivate') + puts runner.kick @node when options[:identify] - puts ipmitool_cmd('chassis identify') + puts runner.identify @node when options[:sdr] - puts ipmitool_cmd('sdr elist all') + puts runner.sdr @node when options[:log] == 'list' - puts ipmitool_cmd('sel list') + puts runner.log_list @node when options[:log] == 'clear' - puts ipmitool_cmd('sel clear') + puts runner.log_clear @node when options[:on] - puts ipmitool_cmd('power on') + puts runner.on @node when options[:off] - puts ipmitool_cmd('power off') + puts runner.off @node + when options[:soft_off] + puts runner.soft_off @node when options[:reboot] - puts ipmitool_cmd('power cycle') + puts runner.reboot @node + when options[:soft_reboot] + puts runner.soft_reboot @node + when options[:status] + puts runner.status @node else begin - raise OptionParser::MissingArgument, "specify an action" - rescue OptionParser::MissingArgument => e - puts e + puts "specify an action" exit 1 end end end private - - def ipmitool_cmd action - system("#{@ipmitool_exec} -I lanplus -H #{@node.ipmi.address} -U #{@node.ipmi.username} -P #{@node.ipmi.password} #{action}") - return $?.exitstatus == 0 ? "SUCCESS" : "FAILED" + def load_runners runners + runners.map {|runner| + begin + require "consolr/runners/#{runner}" + Consolr::Runners.const_get(runner.capitalize).new @config_params.fetch(runner, {}) + rescue NameError, LoadError => e + puts "Could not load runner #{runner.capitalize}, skipping." + end + }.compact end - end - end