Class: Pec2::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/pec2/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = [], options = {}, config = {}) ⇒ CLI

Returns a new instance of CLI.



12
13
14
15
16
17
# File 'lib/pec2/cli.rb', line 12

def initialize(args = [], options = {}, config = {})
  super(args, options, config)
  @global_options = config[:shell].base.options
  @core = Ec2.new(profile: @global_options[:profile])
  @logger = Logger.new(STDOUT)
end

Instance Method Details

#run_commandObject



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
# File 'lib/pec2/cli.rb', line 28

def run_command
  addresses = @core.instances_hash(options[:tag]).map do |instance|
    if options[:resolve] == 'private_ip_address'
      instance.private_ip_address
    elsif options[:resolve] == 'public_ip_address'
      instance.public_ip_address
    elsif options[:resolve] == 'name_tag'
      instance.tags.select{|tag| tag["key"] == "Name" }.first["value"]
    end
  end

  if addresses.empty?
    @logger.info(%Q{no host tag #{options[:tag]}.})
    exit
  end

  @logger.info(%Q{connection size #{addresses.size}.})
  @logger.info(%Q{listing connection to #{addresses.join(', ')}.})

  pssh = Pssh.new(options, addresses, addresses.size)

  file_command = options[:file] ? File.read(options[:file]) : nil
  command = options[:command] || file_command
  interactive = command ? false : true

  if interactive
    while true
      command = ask(">:")
      pssh.exec_pssh_command(command)
    end
  else
    ret = pssh.exec_pssh_command(command)
    unless ret
      exit 1
    end
  end
end

#versionObject



67
68
69
# File 'lib/pec2/cli.rb', line 67

def version
  puts VERSION
end