Class: ILO_SDK::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/ilo-sdk/cli.rb

Overview

cli for ilo-sdk

Defined Under Namespace

Classes: Runner

Instance Method Summary collapse

Instance Method Details

#consoleObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# 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

#envObject



82
83
84
85
86
87
88
89
90
91
92
93
# 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

#loginObject



121
122
123
124
125
126
127
# File 'lib/ilo-sdk/cli.rb', line 121

def 
  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) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 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

#versionObject



111
112
113
114
115
116
117
118
# 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