class ILO_SDK::Cli
cli for ilo-sdk
Public Instance Methods
console()
click to toggle source
# File lib/ilo-sdk/cli.rb, line 96 def console client_setup({}, true, true) puts "Connected to #{@client.host}" puts "HINT: The @client object is available to you\n\n" rescue puts "WARNING: Couldn't connect to #{@options['host'] || ENV['ILO_HOST'] || 'nil (host not set)'}\n\n" ensure require 'pry' Pry.config.prompt = proc { '> ' } Pry.plugins['stack_explorer'] && Pry.plugins['stack_explorer'].disable! Pry.plugins['byebug'] && Pry.plugins['byebug'].disable! Pry.start(ILO_SDK::Console.new(@client)) end
env()
click to toggle source
# File lib/ilo-sdk/cli.rb, line 82 def env data = {} ILO_SDK::ENV_VARS.each { |k| data[k] = ENV[k] } if @options['format'] == 'human' data.each do |key, value| value = "'#{value}'" if value && ! %w(true false).include?(value) printf "%-#{data.keys.max_by(&:length).length}s = %s\n", key, value || 'nil' end else output(parse_hash(data, true)) end end
login()
click to toggle source
# File lib/ilo-sdk/cli.rb, line 121 def login client_setup @client.response_handler(@client.rest_get('/redfish/v1/Sessions/')) puts 'Login Successful!' rescue StandardError => e fail_nice(e.message) end
rest(method, uri)
click to toggle source
# File lib/ilo-sdk/cli.rb, line 142 def rest(method, uri) client_setup('log_level' => :error) uri_copy = uri.dup uri_copy.prepend('/') unless uri_copy.start_with?('/') if @options['data'] begin data = { body: JSON.parse(@options['data']) } rescue JSON::ParserError => e fail_nice("Failed to parse data as JSON\n#{e.message}") end end data ||= {} response = @client.rest_api(method, uri_copy, data) if response.code.to_i.between?(200, 299) case @options['format'] when 'yaml' puts JSON.parse(response.body).to_yaml when 'json' puts JSON.pretty_generate(JSON.parse(response.body)) else # raw puts response.body end else fail_nice("Request failed: #{response.inspect}\nHeaders: #{response.to_hash}\nBody: #{response.body}") end rescue ILO_SDK::InvalidRequest => e fail_nice(e.message) end
version()
click to toggle source
# File lib/ilo-sdk/cli.rb, line 111 def version puts "Gem Version: #{ILO_SDK::VERSION}" client_setup({ 'log_level' => :error }, true) ver = @client.response_handler(@client.rest_get('/redfish/v1/'))['RedfishVersion'] puts "iLO Redfish API version: #{ver}" rescue StandardError, SystemExit puts 'iLO API version unknown' end
Private Instance Methods
client_setup(client_params = {}, quiet = false, throw_errors = false)
click to toggle source
# File lib/ilo-sdk/cli.rb, line 178 def client_setup(client_params = {}, quiet = false, throw_errors = false) client_params['ssl_enabled'] = true if @options['ssl_verify'] == true client_params['ssl_enabled'] = false if @options['ssl_verify'] == false client_params['host'] ||= @options['host'] if @options['host'] client_params['user'] ||= @options['user'] if @options['user'] client_params['password'] ||= @options['password'] if @options['password'] client_params['log_level'] ||= @options['log_level'].to_sym if @options['log_level'] @client = ILO_SDK::Client.new(client_params) rescue StandardError => e raise e if throw_errors fail_nice if quiet fail_nice "Failed to login to iLO at '#{client_params['host'] || ENV['ILO_HOST']}'. Message: #{e}" end
fail_nice(msg = nil)
click to toggle source
# File lib/ilo-sdk/cli.rb, line 173 def fail_nice(msg = nil) $stderr.puts "ERROR: #{msg}" if msg exit 1 end
output(data = {}, indent = 0)
click to toggle source
Print output in a given format.
# File lib/ilo-sdk/cli.rb, line 223 def output(data = {}, indent = 0) case @options['format'] when 'json' puts JSON.pretty_generate(data) when 'yaml' puts data.to_yaml else # rubocop:disable Metrics/BlockNesting if data.class == Hash data.each do |k, v| if v.class == Hash || v.class == Array puts "#{' ' * indent}#{k.nil? ? 'nil' : k}:" output(v, indent + 2) else puts "#{' ' * indent}#{k.nil? ? 'nil' : k}: #{v.nil? ? 'nil' : v}" end end elsif data.class == Array data.each do |d| if d.class == Hash || d.class == Array output(d, indent + 2) else puts "#{' ' * indent}#{d.nil? ? 'nil' : d}" end end puts "\nTotal: #{data.size}" if indent < 1 else puts "#{' ' * indent}#{data.nil? ? 'nil' : data}" end # rubocop:enable Metrics/BlockNesting end end
parse_hash(hash, convert_types = false)
click to toggle source
Parse options hash from input. Handles chaining and keywords such as true/false & nil Returns new hash with proper nesting and formatting
# File lib/ilo-sdk/cli.rb, line 194 def parse_hash(hash, convert_types = false) new_hash = {} hash.each do |k, v| if convert_types v = v.to_i if v && v.match(/^\d+$/) v = true if v == 'true' v = false if v == 'false' v = nil if v == 'nil' end if k =~ /\./ sub_hash = new_hash split = k.split('.') split.each do |sub_key| if sub_key == split.last sub_hash[sub_key] = v else sub_hash[sub_key] ||= {} sub_hash = sub_hash[sub_key] end end new_hash[split.first] ||= {} else new_hash[k] = v end end new_hash end