Class: Collins::CLI::Power

Inherits:
Object
  • Object
show all
Includes:
Mixins
Defined in:
lib/collins/cli/power.rb

Constant Summary collapse

PROG_NAME =
'collins power'
ALLOWABLE_POWER_ACTIONS =
['reboot','rebootsoft','reboothard','on','off','poweron','poweroff','identify']
DEFAULT_OPTIONS =
{
  :timeout => 120,
}

Constants included from Mixins

Mixins::COLORS, Mixins::ERROR, Mixins::SUCCESS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins

#api_call, #as_query?, #collins, #convert_to_query

Constructor Details

#initializePower

Returns a new instance of Power.



14
15
16
17
18
# File 'lib/collins/cli/power.rb', line 14

def initialize
  @options = DEFAULT_OPTIONS.clone
  @parsed, @validated = false, false
  @parser = nil
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/collins/cli/power.rb', line 12

def options
  @options
end

Instance Method Details

#parse!(argv = ARGV) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/collins/cli/power.rb', line 20

def parse!(argv = ARGV)
  @parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{PROG_NAME} [options]"
    opts.separator ""
    opts.on('-s','--status',"Show IPMI power status") {|v| @options[:mode] = :status }
    opts.on('-p','--power ACTION',String,"Perform IPMI power ACTION") {|v| @options[:mode] = :power ; @options[:power] = v.downcase }

    opts.separator ""
    opts.separator "General:"
    opts.on('-t','--tags TAG[,...]',Array,"Tags to work on, comma separated") {|v| @options[:tags] = v.map(&:to_sym)}
    opts.on('-C','--config CONFIG',String,'Use specific Collins config yaml for Collins::Client') {|v| @options[:config] = v}
    opts.on('-h','--help',"Help") {puts opts ; exit 0}

    opts.separator ""
    opts.separator "Examples:"
    opts.separator "  Reset some machines:"
    opts.separator "    #{PROG_NAME} -t 001234,003456,007895 -p reboot"
  end.parse!(argv)

  # convert what we allow to be specified to what collins::power allows
  @options[:power] = 'rebootsoft' if @options[:power] == 'reboot'

  if options[:tags].nil? or options[:tags].empty?
    # read tags from stdin. first field on the line is the tag
    input = ARGF.readlines
    @options[:tags] = input.map{|l| l.split(/\s+/)[0] rescue nil}.compact.uniq
  end
  @parsed = true
  self
end

#run!Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/collins/cli/power.rb', line 61

def run!
  success = true
  options[:tags].each do |t|
    case options[:mode]
    when :status
      res = api_call("checking power status",:power_status,t) {|status| status}
      success = false if !res
    when :power
      success &&= api_call("performing #{options[:power]}", :power!, t, options[:power])
    end
  end
  success
end

#validate!Object



51
52
53
54
55
56
57
58
59
# File 'lib/collins/cli/power.rb', line 51

def validate!
  raise "You need to tell me to do something!" if @options[:mode].nil?
  if options[:mode] == :power
    abort "Unknown power action #{options[:power]}, expecting one of #{ALLOWABLE_POWER_ACTIONS.join(',')}" unless ALLOWABLE_POWER_ACTIONS.include? options[:power]
    # TODO this arguably shouldnt be in validate. Maybe #parse!?
    @options[:power] = Collins::Power.normalize_action @options[:power]
  end
  self
end