Skip to content

Commit

Permalink
Dynamically load runner from config
Browse files Browse the repository at this point in the history
  • Loading branch information
defect committed Feb 15, 2016
1 parent 375a4de commit dea906e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
26 changes: 23 additions & 3 deletions support/ruby/consolr/lib/consolr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'optparse'
require 'yaml'

require 'consolr/runners/ipmitool'

module Consolr
class Console
Expand Down Expand Up @@ -90,14 +89,35 @@ def start options
abort("Please pass either the hostname OR the tag but not both.")
end

runners = @config_params.fetch('runners', []).map {|runner|
begin
require "consolr/runners/#{runner}"
Consolr::Runners.const_get(runner.capitalize).new @config_params.fetch(runner, {})
rescue NameError => e
puts "Could not load runner #{runner.capitalize}, skipping."
end
}.compact

# 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

# 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.")

runner = Consolr::Runners::Ipmitool.new @config_params['ipmitool']
# 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
if not runner.verify @node
abort("Cannot verify asset #{@node.hostname} (#{@node.tag})")
end

Expand Down
16 changes: 12 additions & 4 deletions support/ruby/consolr/lib/consolr/runners/ipmitool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ module Consolr
module Runners
class Ipmitool < Runner
def initialize config
@ipmitool = config['executable']
@ipmitool = if config.empty?
'/usr/bin/ipmitool'
else
config
end
end

def verify? node
def can_run? node
not (node.ipmi.address.empty? or node.ipmi.username.empty? or node.ipmi.password.empty?)
end

def verify node
Net::Ping::External.new(node.ipmi.address).ping?
end

Expand All @@ -16,11 +24,11 @@ def console node
end

def kick node
cmd 'sol dectivate', node
cmd 'sol deactivate', node
end

def identify node
cmd 'identify', node
cmd 'chassis identify', node
end

def sdr node
Expand Down

0 comments on commit dea906e

Please sign in to comment.