Class: Morpheus::Cli::License

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/commands/license.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Constructor Details

#initializeLicense

alias_subcommand :details, :get



14
15
16
# File 'lib/morpheus/cli/commands/license.rb', line 14

def initialize() 
  # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Instance Method Details

#apply(args) ⇒ Object



65
66
67
68
# File 'lib/morpheus/cli/commands/license.rb', line 65

def apply(args)
  print_error "#{yellow}DEPRECATION WARNING: `license apply` has been deprecated and replaced with `license install`. Please use `license install` instead.#{reset}\n"
  install(args)
end

#connect(opts) ⇒ Object



18
19
20
21
# File 'lib/morpheus/cli/commands/license.rb', line 18

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @license_interface = @api_client.license
end

#decode(args) ⇒ Object



111
112
113
114
# File 'lib/morpheus/cli/commands/license.rb', line 111

def decode(args)
  print_error "#{yellow}DEPRECATION WARNING: `license decode` has been deprecated and replaced with `license test`. Please use `license test` instead.#{reset}\n"
  test(args)
end

#format_limit(label, used, max, opts = {}) ⇒ Object

todo: use this to dry print_license_usage



367
368
369
370
371
372
373
374
375
# File 'lib/morpheus/cli/commands/license.rb', line 367

def format_limit(max, used)
  formatted_max = max.to_i > 0 ? format_number(max) : "Unlimited"
  if used
    formatted_used = format_number(used)
    "#{formatted_used} / #{formatted_max}"
  else
    "#{formatted_max}"
  end
end

#format_limit_bytes(max, used) ⇒ Object



258
259
260
261
262
263
264
265
266
# File 'lib/morpheus/cli/commands/license.rb', line 258

def format_limit_bytes(max, used)
  formatted_max = max.to_i > 0 ? format_bytes(max) : "Unlimited"
  if used
    formatted_used = format_bytes(used)
    "#{formatted_used} / #{formatted_max}"
  else
    "#{formatted_max}"
  end
end

#format_limit_type(license) ⇒ Object



239
240
241
242
243
244
245
246
# File 'lib/morpheus/cli/commands/license.rb', line 239

def format_limit_type(license)
  limit_type = license['limitType'] || 'workload'
  if limit_type == 'standard' || limit_type == 'metrics'
    'Standard'
  else
    limit_type.to_s.capitalize
  end
end

#format_product_tier(license) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/morpheus/cli/commands/license.rb', line 222

def format_product_tier(license)
  product_tier = license['productTier'] || 'capacity'
  if product_tier == 'capacity'
    'Capacity'
  elsif product_tier == 'essentials'
    'Essentials'
  elsif product_tier == 'pro'
    'Pro'
  elsif product_tier == 'enterprise'
    'Enterprise'
  elsif product_tier == 'msp'
    'Service Provider'
  else
    product_tier.to_s.capitalize
  end
end

#get(args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/morpheus/cli/commands/license.rb', line 27

def get(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:json, :yaml, :fields, :dry_run, :remote])
    opts.footer = "Get details about the currently installed license.\n" +
                  "This information includes license features and limits.\n" +
                  "The actual secret license key value will never be returned."
  end
  optparse.parse!(args)
  if args.count > 0
    raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
  end
  connect(options)
  
  # make api request
  @license_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @license_interface.dry.get()
    return 0, nil
  end
  json_response = @license_interface.get()
  render_response(json_response, options) do
    license = json_response['license']
    current_usage = json_response['currentUsage'] || {}
    print_h1 "License"
    if license.nil?
      print "#{yellow}No license currently installed#{reset}\n\n"
    else
      print_license_details(json_response)
      print_h2 "Current Usage", [], options
      print_license_usage(license, current_usage)
      print reset,"\n"
    end
  end
end

#handle(args) ⇒ Object



23
24
25
# File 'lib/morpheus/cli/commands/license.rb', line 23

def handle(args)
  handle_subcommand(args)
end

#install(args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/morpheus/cli/commands/license.rb', line 70

def install(args)
  options = {}
   = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[key]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Install a new license key.\n" +
                  "This will potentially change the enabled features and capabilities of your appliance."
  end
  optparse.parse!(args)
  if args.count > 1
    raise_command_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
  end
  connect(options)
  begin
    if args[0]
      key = args[0]
    else
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'licenseKey', 'fieldLabel' => 'License Key', 'type' => 'text', 'required' => true}], options[:options])
      key = v_prompt['licenseKey'] || ''
    end
    @license_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @license_interface.dry.install(key)
      return 0
    end
    json_response = @license_interface.install(key)
    license = json_response['license']
    if options[:json]
      puts JSON.pretty_generate(json_response)
    else
      print_green_success "License installed!"
      get([] + (options[:remote] ? ["-r",options[:remote]] : []))
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return false
  end
end


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/morpheus/cli/commands/license.rb', line 268

def print_license_details(json_response)
  license = json_response['license']
  current_usage = json_response['currentUsage'] || {}
  description_cols = {
    "Account" => 'accountName',
    "Product Tier" => lambda {|it| format_product_tier(it) },
    "Start Date" => lambda {|it| format_local_dt(it['startDate']) },
    "End Date" => lambda {|it| 
      if it['endDate']
        format_local_dt(it['endDate']).to_s + ' (' + format_duration(Time.now, it['endDate']).to_s + ')' 
      else
        'Never'
      end
    },
    "Multi-Tenant" => lambda {|it| format_boolean it["multiTenant"] },
    "White Label" => lambda {|it| format_boolean it["whitelabel"] },
    "Stats Reporting" => lambda {|it| format_boolean it["reportStatus"] },
    "Hard Limit" => lambda {|it| format_boolean it["hardLimit"] },
    "Limit Type" => lambda {|it| format_limit_type(it) },
  }

  description_cols.delete("Multi-Tenant") if !license['multiTenant']
  description_cols.delete("White Label") if !license['whitelabel']

  if license['zoneTypes'] && license['zoneTypes'].size > 0
    description_cols["Included Clouds"] = lambda {|it| it['zoneTypes'].join(', ') }
  elsif license['zoneTypesExcluded'] && license['zoneTypesExcluded'].size > 0
    description_cols["Excluded Clouds"] = lambda {|it| it['zoneTypesExcluded'].join(', ') }
  end

  if license['taskTypes'] && license['taskTypes'].size > 0
    description_cols["Included Tasks"] = lambda {|it| it['taskTypes'].join(', ') }
  elsif license['taskTypesExcluded'] && license['taskTypesExcluded'].size > 0
    description_cols["Excluded Tasks"] = lambda {|it| it['taskTypesExcluded'].join(', ') }
  end

  print_description_list(description_cols, license)
end


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/morpheus/cli/commands/license.rb', line 307

def print_license_usage(license, current_usage)
  unlimited_label = "Unlimited"
  # unlimited_label = "∞"
  if license['limitType'] == 'standard'
    # new standard metrics limit
    used_managed_servers = current_usage['managedServers']
    used_discovered_servers = current_usage['discoveredServers']
    used_hosts = current_usage['hosts']
    used_mvm = current_usage['mvm']
    used_mvm_sockets = current_usage['mvmSockets']
    used_iac = current_usage['iac']
    used_xaas = current_usage['xaas']
    used_executions = current_usage['executions']
    used_distributed_workers = current_usage['distributedWorkers']
    used_discovered_objects = current_usage['discoveredObjects']


    max_managed_servers = license["maxManagedServers"]
    max_discovered_servers = license['maxDiscoveredServers']
    max_hosts = license['maxHosts']
    max_mvm = license['maxMvm']
    max_mvm_sockets = license['maxMvmSockets']
    max_iac = license['maxIac']
    max_xaas = license['maxXaas']
    max_executions = license['maxExecutions']
    max_distributed_workers = license['maxDistributedWorkers']
    max_discovered_objects = license['maxDiscoveredObjects']
    label_width = 20
    chart_opts = {max_bars: 20, unlimited_label: '0%', percent_sigdig: 0}
    out = ""
    out << cyan + "Managed Servers".rjust(label_width, ' ') + ": " + generate_usage_bar(used_managed_servers, max_managed_servers, chart_opts) + cyan + used_managed_servers.to_s.rjust(8, ' ') + " / " + (max_managed_servers ? max_managed_servers.to_s : unlimited_label).to_s.ljust(15, ' ') + "\n"
    out << cyan + "Discovered Servers".rjust(label_width, ' ') + ": " + generate_usage_bar(used_discovered_servers, max_discovered_servers, chart_opts) + cyan + used_discovered_servers.to_s.rjust(8, ' ') + " / " + (max_discovered_servers ? max_discovered_servers.to_s : unlimited_label).to_s.ljust(15, ' ') + "\n"
    out << cyan + "Hosts".rjust(label_width, ' ') + ": " + generate_usage_bar(used_hosts, max_hosts, chart_opts) + cyan + used_hosts.to_s.rjust(8, ' ') + " / " + (max_hosts ? max_hosts.to_s : unlimited_label).to_s.ljust(15, ' ') + "\n"
    out << cyan + "HPE VM Hosts".rjust(label_width, ' ') + ": " + generate_usage_bar(used_mvm, max_mvm, chart_opts) + cyan + used_mvm.to_s.rjust(8, ' ') + " / " + (max_mvm ? max_mvm.to_s : unlimited_label).to_s.ljust(15, ' ') + "\n"
    out << cyan + "HPE VM Sockets".rjust(label_width, ' ') + ": " + generate_usage_bar(used_mvm_sockets, max_mvm_sockets, chart_opts) + cyan + used_mvm_sockets.to_s.rjust(8, ' ') + " / " + (max_mvm_sockets ? max_mvm_sockets.to_s : unlimited_label).to_s.ljust(15, ' ') + "\n"
    out << cyan + "Iac Deployments".rjust(label_width, ' ') + ": " + generate_usage_bar(used_iac, max_iac, chart_opts) + cyan + used_iac.to_s.rjust(8, ' ') + " / " + (max_iac ? max_iac.to_s : unlimited_label).to_s.ljust(15, ' ') + "\n"
    out << cyan + "Xaas Instances".rjust(label_width, ' ') + ": " + generate_usage_bar(used_xaas, max_xaas, chart_opts) + cyan + used_xaas.to_s.rjust(8, ' ') + " / " + (max_xaas ? max_xaas.to_s : unlimited_label).to_s.ljust(15, ' ') + "\n"
    out << cyan + "Executions".rjust(label_width, ' ') + ": " + generate_usage_bar(used_executions, max_executions, chart_opts) + cyan + used_executions.to_s.rjust(8, ' ') + " / " + (max_executions ? max_executions.to_s : unlimited_label).to_s.ljust(15, ' ') + "\n"
    out << cyan + "Distributed Workers".rjust(label_width, ' ') + ": " + generate_usage_bar(used_distributed_workers, max_distributed_workers, chart_opts) + cyan + used_distributed_workers.to_s.rjust(8, ' ') + " / " + (max_distributed_workers ? max_distributed_workers.to_s : unlimited_label).to_s.ljust(15, ' ') + "\n"
    #out << cyan + "Discovered Objects".rjust(label_width, ' ') + ": " + generate_usage_bar(used_discovered_objects, max_discovered_objects, chart_opts) + cyan + used_discovered_objects.to_s.rjust(8, ' ') + " / " + (max_discovered_objects.to_i > 0 ? max_discovered_objects.to_s : unlimited_label).to_s.ljust(15, ' ') + "\n"
    print out
  else
    # old workloads limit
    used_memory = current_usage['memory']
    used_storage = current_usage['storage']
    used_workloads = current_usage['workloads']
    max_memory = license['maxMemory'].to_i
    max_storage = license['maxStorage'].to_i
    max_workloads = license['maxInstances'].to_i
    label_width = 15
    chart_opts = {max_bars: 20, unlimited_label: '0%', percent_sigdig: 0}
    out = ""
    out << cyan + "Workloads".rjust(label_width, ' ') + ": " + generate_usage_bar(used_workloads, max_workloads, chart_opts) + cyan + used_workloads.to_s.rjust(15, ' ') + " / " + (max_workloads.to_i > 0 ? max_workloads.to_s : unlimited_label).to_s.ljust(15, ' ') + "\n"
    out << cyan + "Memory".rjust(label_width, ' ') + ": " + generate_usage_bar(used_memory, max_memory, chart_opts) + cyan + Filesize.from("#{used_memory} B").pretty.strip.rjust(15, ' ') + " / " + (max_memory.to_i > 0 ? Filesize.from("#{max_memory} B").pretty : unlimited_label).strip.ljust(15, ' ') + "\n"
    out << cyan + "Storage".rjust(label_width, ' ') + ": " + generate_usage_bar(used_storage, max_storage, chart_opts) + cyan + Filesize.from("#{used_storage} B").pretty.strip.rjust(15, ' ') + " / " + (max_storage.to_i > 0 ? Filesize.from("#{max_storage} B").pretty : unlimited_label).strip.ljust(15, ' ') + "\n"
    print out
  end
end

#test(args) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
170
171
172
173
174
# File 'lib/morpheus/cli/commands/license.rb', line 116

def test(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[key]")
    build_common_options(opts, options, [:json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Test a license key.\n" +
                  "This is a way to decode and view a license key before installing it."
  end
  optparse.parse!(args)
  if args.count > 1
    raise_command_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
  end
  connect(options)
  key = nil
  if args[0]
    key = args[0]
  else
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'licenseKey', 'fieldLabel' => 'License Key', 'type' => 'text', 'required' => true}], options[:options])
    key = v_prompt['licenseKey'] || ''
  end
  begin
    @license_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @license_interface.dry.test(key)
      return
    end
    json_response = @license_interface.test(key)
    license = json_response['license']
    current_usage = json_response['currentUsage'] || {}
    exit_code, err = 0, nil
    if license.nil?
      err = "Unable to decode license."
      exit_code = 1
      #return err, exit_code
    end
    render_result = render_with_format(json_response, options)
    if render_result
      return exit_code, err
    end
    if options[:quiet]
      return exit_code, err
    end
    if exit_code != 0
      print_error red, err.to_s, reset, "\n"
      return exit_code, err
    end
    
    # all good
    print_h1 "License"
    print_license_details(json_response)
    print_h2 "License Usage", [], options
    print_license_usage(license, current_usage)
    print reset,"\n"
    return exit_code, err
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#uninstall(args) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/morpheus/cli/commands/license.rb', line 176

def uninstall(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[key]")
    build_common_options(opts, options, [:auto_confirm, :json, :yaml, :dry_run, :remote])
    opts.footer = "Uninstall the current license key.\n" +
                  "This clears out the current license key from the appliance.\n" +
                  "The function of the remote appliance will be restricted without a license installed.\n" +
                  "Be careful using this."
  end
  optparse.parse!(args)
  if args.count > 0
    raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
  end
  connect(options)
  begin
    @license_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @license_interface.dry.uninstall(params)
      return
    end
    
    unless options[:quiet]
      print cyan,"#{bold}WARNING!#{reset}#{cyan} You are about to uninstall your license key.",reset,"\n"
      print yellow, "Be careful using this. Make sure you have a copy of your key somewhere if you intend to use it again.",reset, "\n"
      print "\n"
    end
    
    unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you want to uninstall the license key for remote #{@appliance_name} - #{@appliance_url}?")
      return 9, "command aborted"
    end

    json_response = @license_interface.uninstall(params)
    render_result = render_with_format(json_response, options)
    return 0 if render_result
    return 0 if options[:quiet]
    print_green_success "License uninstalled!"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end