class Morpheus::Cli::License

Public Class Methods

new() click to toggle source

alias_subcommand :details, :get

# File lib/morpheus/cli/commands/license.rb, line 14
def initialize() 
  # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Public Instance Methods

apply(args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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(max, used) click to toggle source
# File lib/morpheus/cli/commands/license.rb, line 248
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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/morpheus/cli/commands/license.rb, line 23
def handle(args)
  handle_subcommand(args)
end
install(args) click to toggle source
# File lib/morpheus/cli/commands/license.rb, line 70
def install(args)
  options = {}
  account_name = 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
print_license_details(json_response) click to toggle source
print_license_usage(license, current_usage) click to toggle source
test(args) click to toggle source
# 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) click to toggle source
# 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