Class: Galaxy::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/galaxy.rb

Constant Summary collapse

COMMANDS =
[:show, :install, :upgrade, :terminate, :start, :stop, :restart, :ssh, :reset_to_actual, :agent_show, :agent_add]
INITIAL_OPTIONS =
{
    :coordinator_url => ENV['GALAXY_COORDINATOR'] || 'http://localhost:64000',
    :ssh_command => "bash --login"
}

Class Method Summary collapse

Class Method Details

.execute(args) ⇒ Object



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
# File 'lib/galaxy.rb', line 543

def self.execute(args)
  begin
    (command, filter, options, is_agent, command_args) = parse_command_line(args)
    result = Commands.send(command, filter, options, command_args)
    if !is_agent  then
      slots = result.sort_by { |slot| [slot.ip, slot.binary || '', slot.config || '', slot.uuid] }
      puts '' if options[:debug]

      table = Table.new(['uuid', 'ip', 'status', 'binary', 'config', ''].map { |h| Colorize::colorize(h, :bright, :cyan) })
      slots.each { |slot| table << slot.columns(STDOUT.tty?) }
      puts table.render(STDOUT.tty?)
    else
      agents = result.sort_by { |agent| [agent.ip, agent.agent_id] }
      puts '' if options[:debug]

      table = Table.new(['id', 'ip', 'status', 'type', 'location'].map { |h| Colorize::colorize(h, :bright, :cyan) })
      agents.each { |agent| table << agent.columns(STDOUT.tty?) }
      puts table.render(STDOUT.tty?)
    end


    exit EXIT_CODES[:success]
  rescue Errno::ECONNREFUSED
    puts "Coordinator refused connection: #{options[:coordinator_url]}"
    exit EXIT_CODES[:general_error]
  rescue CommandError => e
    puts e.message
    if e.code == :invalid_usage
      puts ''
      parse_command_line([])
    end
    if options[:debug]
      puts ''
      puts "exit: #{e.code}"
    end
    exit EXIT_CODES[e.code || :general_error]
  end
end

.parse_command_line(args) ⇒ Object



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
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
# File 'lib/galaxy.rb', line 417

def self.parse_command_line(args)
  options = INITIAL_OPTIONS

  filter = Hash.new{[]}

  option_parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} [options] <command>"

    opts.separator ''
    opts.separator 'Options:'

    opts.on('-h', '--help', 'Display this screen') do
      puts opts
      exit EXIT_CODES[:success]
    end

    opts.on("-v", "--version", "Display the Galaxy version number and exit") do
      puts "Galaxy version #{GALAXY_VERSION}"
      exit EXIT_CODES[:success]
    end

    opts.on("--coordinator COORDINATOR", "Galaxy coordinator host (overrides GALAXY_COORDINATOR)") do |v|
      options[:coordinator_url] = v
    end

    opts.on('--debug', 'Enable debug messages') do
      options[:debug] = true
    end

    opts.separator ''
    opts.separator 'Filters:'

    opts.on("-b", "--binary BINARY", "Select slots with a given binary") do |arg|
      filter[:binary] <<= arg
    end

    opts.on("-c", "--config CONFIG", "Select slots with given configuration") do |arg|
      filter[:config] <<= arg
    end

    opts.on("-i", "--host HOST", "Select slots on the given hostname") do |arg|
      filter[:host] <<= arg
    end

    opts.on("-I", "--ip IP", "Select slots at the given IP address") do |arg|
      filter[:ip] <<= arg
    end

    opts.on("-u", "--uuid SLOT_UUID", "Select slots with given slot uuid") do |arg|
      filter[:uuid] <<= arg
    end

    opts.on("--count count", "Number of instances to install or agents to provision") do |arg|
      options[:count] = arg
    end

    opts.on("--availability-zone AVAILABILITY_ZONE", "Availability zone to agent provisioning") do |arg|
      options[:availability_zone] = arg
    end

    # todo find a better command line argument
    opts.on("-x", "--ssh-command SSH_COMMAND", "Command to execute with ssh") do |arg|
      options[:ssh_command] = arg
    end

    opts.on("-s", "--state STATE", "Select 'r{unning}', 's{topped}' or 'unknown' slots", [:running, :r, :stopped, :s, :unknown]) do |arg|
      case arg
        when :running, :r then
          filter[:state] <<= 'running'
        when :stopped, :s then
          filter[:state] <<= 'stopped'
        when :unknown then
          filter[:state] <<= 'unknown'
      end
    end

    opts.separator ''
    opts.separator <<-NOTES
Notes:
- A filter is required for all commands except for show
- Filters are evaluated as: set | host | ip | state | (binary & config)
- The HOST, BINARY, and CONFIG arguments are globs
- BINARY format is groupId:artifactId[:packaging[:classifier]]:version
- CONFIG format is @env:component[:pools]:version
- The default filter selects all hosts
NOTES
    opts.separator ''
    opts.separator 'Commands:'
    opts.separator "  #{COMMANDS.join("\n  ")}"
  end

  option_parser.parse!(args)

  puts options.map { |k, v| "#{k}=#{v}" }.join("\n") if options[:debug]
  puts filter.map { |k, v| v.map { |v1| "#{k}=#{URI.escape(v1)}" }.join('\n') }.join('\n') if options[:debug]

  if args.length == 0 then
    puts option_parser
    exit EXIT_CODES[:success]
  end

  args[0] = args[0].gsub('-', '_');
  command = args[0].to_sym

  is_agent = false
  if command == :agent then
    args = args.drop(1)
    if args.length == 0 then
      puts option_parser
      exit EXIT_CODES[:success]
    end
    command = "#{command}_#{args[0]}".to_sym
    is_agent = true
  end

  unless COMMANDS.include?(command)
    raise CommandError.new(:invalid_usage, "Unsupported command: #{command}")
  end

  if options[:coordinator_url].nil? || options[:coordinator_url].empty?
    raise CommandError.new(:invalid_usage, "You must set Galaxy coordinator host by passing --coordinator COORDINATOR or by setting the GALAXY_COORDINATOR environment variable.")
  end

  return [command, filter, options, is_agent, args.drop(1)]
end