Class: ProxyAPI::BMC

Inherits:
Resource show all
Defined in:
lib/proxy_api/bmc.rb

Constant Summary collapse

SUPPORTED_BOOT_DEVICES =
%w[disk cdrom pxe bios]

Instance Attribute Summary

Attributes inherited from Resource

#url

Instance Method Summary collapse

Methods included from Foreman::TelemetryHelper

#telemetry_duration_histogram, #telemetry_increment_counter, #telemetry_observe_histogram, #telemetry_set_gauge

Constructor Details

#initialize(args) ⇒ BMC

Returns a new instance of BMC.


5
6
7
8
9
# File 'lib/proxy_api/bmc.rb', line 5

def initialize(args)
  @target = args[:host_ip] || '127.0.0.1'
  @url = args[:url] + "/bmc"
  super args
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/proxy_api/bmc.rb', line 111

def method_missing(method, *args, &block)
  if method.to_s.starts_with?('power_', 'boot_', 'identify_', 'lan_')
    margs = args.first
    farg  = method.to_s.split('_')
    # method must contain 2 parts, ie: power_on, boot_disk
    raise NoMethodError unless farg.length == 2

    case farg.first
    when 'power'
      margs[:action] = farg.last
      power(margs)
    when 'boot'
      margs[:function] = 'bootdevice'
      margs[:device]   = farg.last
      boot(margs)
    when 'identify'
      margs[:action] = farg.last
      identify(margs)
    when 'lan'
      margs[:action] = farg.last
      lan(margs)
    end
  else
    super
  end
end

Instance Method Details

#boot(args) ⇒ Object

Perform a boot operation on the bmc device


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/proxy_api/bmc.rb', line 26

def boot(args)
  # valid additional arguments args[:reboot] = true|false, args[:persistent] = true|false
  #  put "/bmc/:host/chassis/config/?:function?/?:action?" do
  case args[:function]
  when "bootdevice"
    if SUPPORTED_BOOT_DEVICES.include?(args[:device])
      parse put(args, bmc_url_for('config', "#{args[:function]}/#{args[:device]}"))
    else
      raise NoMethodError
    end
  else
    raise NoMethodError
  end
rescue NoMethodError => e
  raise e
rescue => e
  raise ProxyException.new(url, e, N_("Unable to perform boot BMC operation"))
end

#identify(args) ⇒ Object

perform an identify operation on the bmc device


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/proxy_api/bmc.rb', line 67

def identify(args)
  # get "/bmc/:host/chassis/identify/:action"
  # put "/bmc/:host/chassis/identify/:action"
  case args[:action]
  when "status"
    parse get(bmc_url_for('identify', args[:action]), args)
  when "on", "off"
    parse put(args, bmc_url_for('identify', args[:action]))
  else
    raise NoMethodError
  end
rescue NoMethodError => e
  raise e
rescue => e
  raise ProxyException.new(url, e, N_("Unable to perform identify BMC operation"))
end

#lan(args) ⇒ Object

perform a lan get operation on the bmc device


85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/proxy_api/bmc.rb', line 85

def lan(args)
  # get "/bmc/:host/lan/:action"
  case args[:action]
  when "ip", "netmask", "mac", "gateway"
    response = parse(get(bmc_url_for('lan', args[:action]), args))
    response.is_a?(Hash) ? response['result'] : response
  else
    raise NoMethodError
  end
rescue NoMethodError => e
  raise e
rescue => e
  raise ProxyException.new(url, e, N_("Unable to perform lan BMC operation"))
end

#power(args) ⇒ Object

Perform a power operation on the bmc device


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/proxy_api/bmc.rb', line 46

def power(args)
  # get "/bmc/:host/chassis/power/:action"
  # put "/bmc/:host/chassis/power/:action"
  case args[:action]
  when "on?", "off?", "status"
    args[:action].chop! if args[:action].include?('?')
    response = parse(get(bmc_url_for('power', args[:action]), args))
    response.is_a?(Hash) ? response['result'] : response
  when "on", "off", "cycle", "soft"
    res = parse put(args, bmc_url_for('power', args[:action]))
    res && (res['result'] == true || res['result'] == "#{@target}: ok\n")
  else
    raise NoMethodError
  end
rescue NoMethodError => e
  raise e
rescue => e
  raise ProxyException.new(url, e, N_("Unable to perform power BMC operation"))
end

#providersObject

gets a list of supported providers


12
13
14
15
16
# File 'lib/proxy_api/bmc.rb', line 12

def providers
  parse get("providers")
rescue => e
  raise ProxyException.new(url, e, N_("Unable to get BMC providers"))
end

#providers_installedObject

gets a list of supported providers installed on the proxy


19
20
21
22
23
# File 'lib/proxy_api/bmc.rb', line 19

def providers_installed
  parse get("providers/installed")
rescue => e
  raise ProxyException.new(url, e, N_("Unable to get installed BMC providers"))
end