Class: Hashicorptools::Host

Inherits:
Thor
  • Object
show all
Defined in:
lib/hashicorptools/host.rb

Instance Method Summary collapse

Instance Method Details

#consoleObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hashicorptools/host.rb', line 44

def console
  ec2 = Aws::EC2::Client.new(region: 'us-east-1')

  bastion_dns = dns_from_reservations(ec2.describe_instances(filters: filters('console', 'maintenance')))
  agra_console_dns = dns_from_reservations(ec2.describe_instances(filters: filters('console', 'agra')))


  if bastion_dns &&  agra_console_dns
    exec "ssh -t #{ssh_user_fragment}#{bastion_dns} 'ssh -t #{ssh_user_fragment}#{agra_console_dns}'"
  else
    puts "no instances found"
  end
end

#hostsObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hashicorptools/host.rb', line 8

def hosts
  ec2 = Aws::EC2::Client.new(region: 'us-east-1')

  resp = ec2.describe_instances(filters: filters) 
  resp.reservations.each do |reservation|
    reservation.instances.each do |instance|
      name = instance.tags.find{|t| t.key == 'Name'}.value
      puts "#{name} #{instance.public_dns_name}"
    end
  end
end

#ssh(role = '') ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hashicorptools/host.rb', line 24

def ssh(role = '')
  ec2 = Aws::EC2::Client.new(region: 'us-east-1')

  resp = ec2.describe_instances(filters: filters(role))
  if resp.reservations.any?
    instance = resp.reservations.first.instances.first
    dns = if instance.public_dns_name.present?
      instance.public_dns_name
    else
      instance.private_dns_name
    end

    exec "ssh #{ssh_user_fragment}#{dns}"
  else
    puts "no instances with #{role} role found"
  end
end