Class: Morpheus::Cli::PackagesCommand

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from LibraryHelper

#api_client, #find_container_type_by_id, #find_container_type_by_name, #find_container_type_by_name_or_id, #find_instance_type_by_id, #find_instance_type_by_name, #find_instance_type_by_name_or_id, #find_option_type_by_id, #find_option_type_by_name, #find_option_type_by_name_or_id, #find_option_type_list_by_id, #find_option_type_list_by_name, #find_option_type_list_by_name_or_id, #find_spec_template_by_id, #find_spec_template_by_name, #find_spec_template_by_name_or_id, #find_spec_template_type_by_id, #find_spec_template_type_by_name_or_code, #find_spec_template_type_by_name_or_code_id, #format_container_type_technology, #format_instance_type_technology, #get_all_spec_template_types, included, #load_balance_protocols_dropdown, #print_container_types_table, #print_instance_types_table, #print_resource_specs_table, #prompt_exposed_ports, #prompt_for_container_types, #prompt_for_option_types, #prompt_for_spec_templates

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

#initializePackagesCommand

Returns a new instance of PackagesCommand.



15
16
17
# File 'lib/morpheus/cli/commands/packages_command.rb', line 15

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

Instance Method Details

#build(args) ⇒ Object

generate a new .morpkg for a local source directory



639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
# File 'lib/morpheus/cli/commands/packages_command.rb', line 639

def build(args)
  full_command_string = "#{command_name} build #{args.join(' ')}".strip
  options = {}
  source_directory = nil
  outfile = nil
  do_overwrite = false
  do_mkdir = false
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[source] [target]")
    opts.on('--source FILE', String, "Source directory of the package being built.") do |val|
      source_directory = val
    end
    opts.on('--target FILE', String, "Destination filename for the .morpkg output file. Default is [code]-[version].morpkg") do |val|
      outfile = val
    end
    opts.on( '-p', '--mkdir', "Create missing directories for [target] if they do not exist." ) do
      do_mkdir = true
    end
    opts.on( '-f', '--force', "Overwrite existing [target] if it exists. Also creates missing directories." ) do
      do_overwrite = true
      do_mkdir = true
    end
   
    build_common_options(opts, options, [:options, :dry_run, :quiet])
    opts.footer = "Generate a new morpheus package file. \n" + 
                  "[source] is required. This is the source directory of the package.\n" +
                  "[target] is the output filename. The default is [code]-[version].morpkg"
  end
  optparse.parse!(args)

  if args.count < 1 || args.count > 2
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "wrong number of arguments, expected 1-2 and got #{args.count}\n#{optparse}"
    return 1
  end
  if args[0]
    source_directory = args[0]
  end
  if args[1]
    outfile = args[1]
  end
  if !source_directory
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "missing required argument [source]\n#{optparse}"
    return 1
  end
  
  # connect(options)
  begin  
    # validate source
    source_directory = File.expand_path(source_directory)
    if !File.exist?(source_directory)
      puts_error "#{Morpheus::Terminal.angry_prompt}[source] is invalid. Directory not found: #{source_directory}"
      return 1
    end
    if !File.directory?(source_directory)
      puts_error "#{Morpheus::Terminal.angry_prompt}[source] is invalid. Not a directory: #{source_directory}"
      return 1
    end
    # parse package source

    package_org = nil
    package_code = nil
    package_version = nil
    package_type = nil


    # validate package
    if !package_code.nil? || package_code.empty?
      puts_error "#{Morpheus::Terminal.angry_prompt}package data is invalid. Missing package code."
      return 1
    end
    if package_version.nil? || package_version.empty?
      puts_error "#{Morpheus::Terminal.angry_prompt}package data is invalid. Missing package version."
      return 1
    end
    # determine outfile
    if !outfile
      outfile = File.join(File.dirname(source_directory), "#{package_code}-#{package_version}.morpkg")
    else
      outfile = File.expand_path(outfile)
    end
    if Dir.exist?(outfile)
      puts_error "#{Morpheus::Terminal.angry_prompt}[target] is invalid. It is the name of an existing directory: #{outfile}"
      return 1
    end
    # always a .morpkg
    if outfile[-7..-1] != ".morpkg"
      outfile << ".morpkg"
    end
    destination_dir = File.dirname(outfile)
    if !Dir.exist?(destination_dir)
      if do_mkdir
        print cyan,"Creating local directory #{destination_dir}",reset,"\n"
        FileUtils.mkdir_p(destination_dir)
      else
        puts_error "#{Morpheus::Terminal.angry_prompt}[target] is invalid. Directory not found: #{destination_dir}  Use -p to create the missing directory."
        return 1
      end
    end
    if File.exist?(outfile)
      if do_overwrite
        # uhh need to be careful wih the passed filepath here..
        # don't delete, just overwrite.
        # File.delete(outfile)
      else
        puts_error "#{Morpheus::Terminal.angry_prompt}[target] is invalid. File already exists: #{outfile}", "Use -f to overwrite the existing file."
        return 1
      end
    end

    if options[:dry_run]
      print cyan + "Building morpheus package at #{source_directory} ..."
      print cyan + "DRY RUN" + reset + "\n"
      return 0
    end
    if !options[:quiet]
      print cyan + "Building morpheus package at #{source_directory} ... "
    end

    # build it
    #http_response, bad_body = @packages_interface.export(params, outfile)

    # FileUtils.chmod(0600, outfile)
    success = true
    error_msg = nil
    build_outfile = nil
    begin
      build_outfile = Morpheus::Morpkg.build_package(source_directory, outfile, options[:force])
      success = true
    rescue => ex
      error_msg = ex.message
    end
    if success
      if !options[:quiet]
        print green + "SUCCESS" + reset + "\n"
        print green + "Generated #{build_outfile}" + reset + "\n"
      end
      return 0
    else
      if !options[:quiet]
        print red + "ERROR" + reset + "\n"
        if error_msg
          print_error red + error_msg + reset + "\n"
        end
      end
      # F it, just remove a bad result
      # if File.exist?(outfile) && File.file?(outfile)
      #   Morpheus::Logging::DarkPrinter.puts "Deleting bad build file: #{outfile}" if Morpheus::Logging.debug?
      #   File.delete(outfile)
      # end
      if options[:debug]
        # puts_error error_msg
      end
      return 1
    end
    
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#connect(opts) ⇒ Object



19
20
21
22
# File 'lib/morpheus/cli/commands/packages_command.rb', line 19

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @packages_interface = @api_client.packages
end

#export(args) ⇒ Object

download a new .morpkg for remote instanceType(s)



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/morpheus/cli/commands/packages_command.rb', line 441

def export(args)
  full_command_string = "#{command_name} export #{args.join(' ')}".strip
  options = {}
  params = {}
  instance_type_codes = nil
  outfile = nil
  do_overwrite = false
  do_mkdir = false
  do_unzip = false
  do_open = false
  open_prog = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[instance-type]")
    opts.on('--file FILE', String, "Destination filepath for the downloaded .morpkg file.") do |val|
      outfile = val
    end
    opts.on('--package-version VALUE', String, "Version number for package.") do |val|
      params['version'] = val
    end
    opts.on('--organization NAME', String, "Organization for package.") do |val|
      params['organization'] = val
    end
    opts.on('--code VALUE', String, "Code for package. Default comes from instance type.") do |val|
      params['code'] = val
    end
    opts.on('--name VALUE', String, "Name for package. Default comes from the instance type name") do |val|
      params['name'] = val
    end
    opts.on('--description VALUE', String, "Description of package.") do |val|
      params['description'] = val
    end
    opts.on('--instance-types LIST', String, "Can be used to export multiple instance types as a single package.") do |val|
      instance_type_codes = []
      val.split(',').collect do |it|
        if !it.strip.empty?
          instance_type_codes << it.strip
        end
      end
    end
    opts.on('--all', String, "Export entire library instead of specific instance type(s).") do
      params['all'] = true
    end
    opts.on('--all-system', String, "Export all system instance types instead of specific instance type(s).") do
      params['all'] = true
      params['systemOnly'] = true
    end
    opts.on('--all-custom', String, "Export all custom instance types instead of specific instance type(s).") do
      params['all'] = true
      params['customOnly'] = true
    end
    opts.on( '-p', '--mkdir', "Create missing directories for [filename] if they do not exist." ) do
      do_mkdir = true
    end
    opts.on( '-f', '--force', "Overwrite existing [filename] if it exists. Also creates missing directories." ) do
      do_overwrite = true
      do_mkdir = true
    end
    opts.on( '--unzip', "Unzip the package to a directory with the same name." ) do
      do_unzip = true
    end
    opts.on( '--open [PROG]', String, "Unzip the package and open the expanded directory with the specified program." ) do |val|
      do_unzip = true
      do_open = true
      if !val.to_s.empty?
        open_prog = val.to_s
      else
        if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
          open_prog = "start"
        elsif RbConfig::CONFIG['host_os'] =~ /darwin/
          open_prog = "open"
        elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
          open_prog = "xdg-open"
        end
      end
    end
    build_common_options(opts, options, [:options, :dry_run, :quiet])
    opts.footer = "Export one or many instance types as a morpheus package (.morpkg) file.\n" + 
                  "[instance-type] is required. This is the instance type code." +
                  "--instance-types can be export multiple instance types at once. This is a list of instance type codes."
  end
  optparse.parse!(args)

  if args.count != 1 && !instance_type_codes && !params['all']
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} export expects 1 argument and received #{args.count} #{args.join(' ')}\n#{optparse}"
    return 1
  end
  connect(options)
  begin
    # construct payload
    if !params['all']
      if args[0] && !instance_type_codes
        instance_type_codes = [args[0]]
      end
      params['instanceType'] = instance_type_codes
    end
    # determine outfile
    if !outfile
      # use a default location
      do_mkdir = true
      if instance_type_codes && instance_type_codes.size == 1
        outfile = File.join(Morpheus::Cli.home_directory, "tmp", "morpheus-packages", "#{instance_type_codes[0]}.morpkg")
      else
        outfile = File.join(Morpheus::Cli.home_directory, "tmp", "morpheus-packages", "download.morpkg")
      end
    end
    outfile = File.expand_path(outfile)
    if Dir.exist?(outfile)
      puts_error "#{Morpheus::Terminal.angry_prompt}--file is invalid. It is the name of an existing directory: #{outfile}"
      return 1
    end
    # always a .morpkg
    if outfile[-7..-1] != ".morpkg"
      outfile << ".morpkg"
    end
    destination_dir = File.dirname(outfile)
    if !Dir.exist?(destination_dir)
      if do_mkdir
        print cyan,"Creating local directory #{destination_dir}",reset,"\n"
        FileUtils.mkdir_p(destination_dir)
      else
        puts_error "#{Morpheus::Terminal.angry_prompt}[filename] is invalid. Directory not found: #{destination_dir}  Use -p to create the missing directory."
        return 1
      end
    end
    if File.exist?(outfile)
      if do_overwrite
        # uhh need to be careful wih the passed filepath here..
        # don't delete, just overwrite.
        # File.delete(outfile)
      else
        print_error Morpheus::Terminal.angry_prompt
        puts_error "[filename] is invalid. File already exists: #{outfile}", "Use -f to overwrite the existing file."
        # puts_error optparse
        return 1
      end
    end

    # merge -O options into normally parsed options
    params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
    @packages_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @packages_interface.dry.export(params, outfile), full_command_string
      return 0
    end
    if !options[:quiet]
      print cyan + "Downloading morpheus package file #{outfile} ... "
    end

    http_response, bad_body = @packages_interface.export(params, outfile)

    # FileUtils.chmod(0600, outfile)
    success = http_response.code.to_i == 200
    if success
      if !options[:quiet]
        print green + "SUCCESS" + reset + "\n"
      end

      if do_unzip
        package_dir = File.join(File.dirname(outfile), File.basename(outfile).sub(/\.morpkg\Z/, ''))
        if File.exist?(package_dir)
          print cyan,"Deleting existing directory #{package_dir}",reset,"\n"
          FileUtils.rm_rf(package_dir)
        end
        print cyan,"Unzipping to #{package_dir}",reset,"\n"
        system("unzip '#{outfile}' -d '#{package_dir}' > /dev/null 2>&1")
        if do_open
          system("#{open_prog} '#{package_dir}'")
        end
      end

      return 0
    else
      if !options[:quiet]
        print red + "ERROR" + reset + " HTTP #{http_response.code}" + "\n"
        if bad_body
          puts red + bad_body
        end
        #response_body = (http_response.body.kind_of?(Net::ReadAdapter) ? "" : http_response.body)
      end
      # F it, just remove a bad result
      if File.exist?(outfile) && File.file?(outfile)
        Morpheus::Logging::DarkPrinter.puts "Deleting bad file download: #{outfile}" if Morpheus::Logging.debug?
        File.delete(outfile)
      end
      if options[:debug]
        puts_error http_response.inspect
      end
      return 1
    end
    
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#get(args) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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
# File 'lib/morpheus/cli/commands/packages_command.rb', line 154

def get(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[code]")
    opts.on( nil, '--versions', "Display Available Versions" ) do
      options[:show_versions] = true
    end
    opts.on( nil, '--objects', "Display Installed Package Objects" ) do
      options[:show_objects] = true
    end
    build_common_options(opts, options, [:query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Get details about a package.\n" + 
                  "[code] is required. This is the package code."
  end
  optparse.parse!(args)
  connect(options)
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} get expects 1 argument and received #{args.count} #{args.join(' ')}\n#{optparse}"
    return 1
  end
  begin
    params.merge!(parse_list_options(options))
    params['code'] = args[0]
    @packages_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @packages_interface.dry.info(params)
      return 0
    end
    json_response = @packages_interface.info(params)
    if options[:json]
      puts as_json(json_response, options, "package")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "package")
      return 0
    elsif options[:csv]
      puts records_as_csv(json_response["package"], options)
    else
      installed_package = json_response["package"]
      available_package = json_response["availablePackage"]

      if installed_package.nil? && available_package.nil?
        print yellow,"No package found for code '#{params['code']}'",reset,"\n"
        return 1
      end

      print_h1 "Package Info"
    
      # merge installed and available package info
      package_record = installed_package || available_package
      #package_record['versions'] = available_package['versions'] if available_package
      print cyan
      description_cols = {
        # "Organization" => 'organization',
        "Code" => 'code',
        "Name" => 'name',
        "Description" => 'description',
        # "Type" => lambda {|it| (it['packageType'] || it['type']).to_s },
        "Latest Version" => lambda {|it| 
          if available_package && available_package['versions']
            latest_version = available_package['versions'].find {|v| v['latestVersion'] }
            if latest_version.nil?
              sorted_versions = available_package['versions'].sort {|x,y|  x['dateCreated'] <=> y['dateCreated'] }
              latest_version = sorted_versions.first()
            end
            latest_version ? latest_version['version'] : ""
          else
            ""
          end
        },
        "Installed Version" => lambda {|it| 
          installed_package ? installed_package['version'] : ""
        },
        "Status" => lambda {|it| 
          installed_package ? format_package_status(installed_package['status']) : format_package_status(nil)
         },
      }
      if installed_package.nil?
        description_cols.delete("Installed Version")
      end

      print_description_list(description_cols, package_record)
      # print reset, "\n"
      if options[:show_versions]
        print_h2 "Available Versions"
        if available_package.nil?
          print yellow,"No marketplace package found for code '#{params['code']}'",reset,"\n"
        else
          if available_package['versions'].nil? || available_package['versions'].empty?
            print yellow,"No available versions found",reset,"\n"
          else
            # api is sorting these with latestVersion first, right?
            sorted_versions = available_package['versions'] || []
            #sorted_versions = available_package['versions'].sort {|x,y|  x['dateCreated'] <=> y['dateCreated'] }
            rows = sorted_versions.collect {|package_version|
              {
                "PACKAGE VERSION": package_version['version'],
                "MORPHEUS VERSION": package_version['minApplianceVersion'],
                "PUBLISH DATE": format_local_dt(package_version['created'] || package_version['dateCreated'])
              }
            }
            columns = ["PACKAGE VERSION", "MORPHEUS VERSION", "PUBLISH DATE"]
            print cyan
            print as_pretty_table(rows, columns)
            # print reset, "\n"
          end
        end
      end
      if options[:show_objects]
        print_h2 "Package Objects"
        if installed_package.nil?
          print cyan,"No objects to show",reset,"\n"
        else
          if installed_package['objects'].nil? || installed_package['objects'].empty?
            print yellow,"No objects to show",reset,"\n"
          else
            rows = installed_package['objects'].collect {|it|
              {
                type: it['refType'],
                id: it['refId'],
                name: it['displayName']
              }
            }
            columns = [:type, :id, :name]
            print cyan
            print as_pretty_table(rows, columns)
            # print reset, "\n"
          end
        end
      end
      
      print reset, "\n"
      return 0
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#handle(args) ⇒ Object



24
25
26
# File 'lib/morpheus/cli/commands/packages_command.rb', line 24

def handle(args)
  handle_subcommand(args)
end

#install(args) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
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
# File 'lib/morpheus/cli/commands/packages_command.rb', line 296

def install(args)
  full_command_string = "#{command_name} install #{args.join(' ')}".strip
  options = {}
  params = {}
  open_prog = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[code]")
    opts.on('-v','--version VERSION', "Package Version number") do |val|
      params['version'] = val
    end
    # opts.on('--package-version VALUE', String, "Version number for package.") do |val|
    #   params['version'] = val
    # end
    opts.on('--organization NAME', String, "Package Organization.  Default is morpheus.") do |val|
      params['organization'] = val
    end
    opts.on( '-f', '--force', "Force Install." ) do
      params['force'] = true
    end
    build_common_options(opts, options, [:options, :json, :dry_run, :remote, :quiet])
    opts.footer = "Install a morpheus package from the marketplace.\n" + 
                  "[code] is required. This is the package code."
  end
  optparse.parse!(args)
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} install expects 1 argument and received #{args.count} #{args.join(' ')}\n#{optparse}"
    return 1
  end
  connect(options)
  begin
    params['code'] = args[0]
    @packages_interface.setopts(options)
    if options[:dry_run]
      #print cyan,bold, "  - Uploading #{local_file_path} to #{bucket_id}:#{destination} DRY RUN", reset, "\n"
      # print_h1 "DRY RUN"
      print_dry_run @packages_interface.dry.install(params)
      print "\n"
      return 0
    end
    
    json_response = @packages_interface.install(params)
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      package_str = params['code'] || ""
      installed_package = json_response['package']
      if installed_package && installed_package['code']
        package_str = installed_package['code']
      end
      if installed_package && installed_package['version']
        package_str = "#{package_str} #{installed_package['version']}"
      end
      print_green_success "Installed package #{package_str}"
    end
    
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#install_file(args) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/morpheus/cli/commands/packages_command.rb', line 358

def install_file(args)
  full_command_string = "#{command_name} install-file #{args.join(' ')}".strip
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[morpkg-file]")
    # opts.on('--url URL', String, "Use a remote URL as the source .morpkg instead of uploading a file.") do |val|
    #   params['url'] = val
    # end
    opts.on( '-f', '--force', "Force Install." ) do
      params['force'] = true
    end
    build_common_options(opts, options, [:options, :dry_run, :quiet])
    opts.footer = "Install a morpheus package with a .morpkg file directly.\n" + 
                  "[morpkg-file] is required. This is the local filepath of a .morpkg to be uploaded and installed."
  end
  optparse.parse!(args)

  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "expects 1 argument and received #{args.count} #{args.join(' ')}\n#{optparse}"
    return 1
  end

  # validate local file path
  local_file_path = File.expand_path(args[0].squeeze('/'))
  if local_file_path == "" || local_file_path == "/" || local_file_path == "."
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "bad argument: [morpkg-file]\nFile '#{local_file_path}' is invalid.\n#{optparse}"
    return 1
  end
  if !File.exist?(local_file_path)
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "bad argument: [morpkg-file]\nFile '#{local_file_path}' was not found.\n"
    return 1
  end
  if !File.file?(local_file_path)
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "bad argument: [morpkg-file]\nFile '#{local_file_path}' is not a file.\n"
    return 1
  end

  connect(options)
  begin
    @packages_interface.setopts(options)
    if options[:dry_run]
      #print cyan,bold, "  - Uploading #{local_file_path} to #{bucket_id}:#{destination} DRY RUN", reset, "\n"
      # print_h1 "DRY RUN"
      print_dry_run @packages_interface.dry.install_file(local_file_path, params)
      print "\n"
      return 0
    end
    
    json_response = @packages_interface.install_file(local_file_path, params)
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      package_str = params['code'] || ""
      installed_package = json_response['package']
      if installed_package && installed_package['code']
        package_str = installed_package['code']
      end
      if installed_package && installed_package['version']
        package_str = "#{package_str} #{installed_package['version']}"
      end
      print_green_success "Installed package #{package_str}"
    end
    
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#list(args) ⇒ Object



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/morpheus/cli/commands/packages_command.rb', line 28

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "List installed packages."
  end
  optparse.parse!(args)
  connect(options)
  begin
    params.merge!(parse_list_options(options))
    @packages_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @packages_interface.dry.list(params)
      return 0
    end
    json_response = @packages_interface.list(params)
    if options[:json]
      puts as_json(json_response, options, "packages")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "packages")
      return 0
    elsif options[:csv]
      puts records_as_csv(json_response["packages"], options)
    else
      installed_packages = json_response["packages"]
      title = "Installed Packages"
      subtitles = []
      subtitles += parse_list_subtitles(options)
      print_h1 title, subtitles
      if installed_packages.empty?
        print cyan,"No installed packages found",reset,"\n"
      else
        rows = installed_packages.collect {|package|
          {
            code: package['code'],
            name: package['name'],
            version: package['version'],
            description: package['description'],
          }
        }
        columns = [:code, :name, {:description => {:max_width => 50}}, :version]
        # custom pretty table columns ...
        if options[:include_fields]
          columns = options[:include_fields]
        end
        print cyan
        print as_pretty_table(rows, columns, options)
        print reset
        print_results_pagination(json_response)
      end
      print reset,"\n"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#remove(args) ⇒ Object



436
437
438
# File 'lib/morpheus/cli/commands/packages_command.rb', line 436

def remove(args)
  raise "not yet implemented"
end

#search(args) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/morpheus/cli/commands/packages_command.rb', line 89

def search(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Search the marketplace for available packages."
  end
  optparse.parse!(args)
  connect(options)
  begin
    if args[0]
      options[:phrase] = args[0]
      # params['phrase'] = args[0]
    end
    params.merge!(parse_list_options(options))
    @packages_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @packages_interface.dry.search(params)
      return 0
    end
    json_response = @packages_interface.search(params)
    if options[:json]
      puts as_json(json_response, options, "packages")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "packages")
      return 0
    elsif options[:csv]
      puts records_as_csv(json_response["packages"], options)
    else
      available_packages = json_response["packages"]
      title = "Available Packages"
      subtitles = []
      subtitles += parse_list_subtitles(options)
      print_h1 title, subtitles
      if available_packages.empty?
        print cyan,"No packages found",reset,"\n"
      else
        rows = available_packages.collect {|package|
          {
            code: package['code'],
            name: package['name'],
            description: package['description'],
            versions: (package['versions'] || []).collect {|v| v['version'] }.join(', ')
          }
        }
        columns = [:code, :name, {:description => {:max_width => 50}}, {:versions => {:max_width => 30}}]
        # custom pretty table columns ...
        if options[:include_fields]
          columns = options[:include_fields]
        end
        print cyan
        print as_pretty_table(rows, columns, options)
        print reset
        print_results_pagination(json_response)
      end
      print reset,"\n"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update(args) ⇒ Object



432
433
434
# File 'lib/morpheus/cli/commands/packages_command.rb', line 432

def update(args)
  raise "not yet implemented"
end