Module: Ec2Ssh::Cli::Aws

Defined in:
lib/ec2-ssh/aws.rb

Instance Method Summary collapse

Instance Method Details

#aws_init(profile, region) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/ec2-ssh/aws.rb', line 2

def aws_init(profile, region)
  ENV['AWS_PROFILE'] = profile
  Aws.config.update({region: region})

  @region = region
  @as = Aws::AutoScaling::Client.new()
  @ec2 = Aws::EC2::Client.new()

  say "Currently running user is: #{Aws::IAM::CurrentUser.new.arn}"
end

#get_auto_scale_groupsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ec2-ssh/aws.rb', line 13

def get_auto_scale_groups
  say "Fetching AutoScale groups from #{@region} please wait..."
    @as_groups = @as.describe_auto_scaling_groups.auto_scaling_groups

    as_group_names = @as_groups.inject([]) {|acc, asg| acc << asg.auto_scaling_group_name; acc }

    as_selection = {}
    as_group_names.each_with_index.inject(as_selection) {|acc, pair|
      element, index = pair
      as_selection[index] = element
      acc
    } 
    
    say "AutoScale Group in #{options[:region]}:\n"
    as_selection.each {|k,v| say "#{k}: #{v}"}

    selected_as = ask("Which server group do you want to ssh to?", color = :yellow)
    
    get_instances('aws:autoscaling:groupName', as_selection[selected_as.to_i])
end

#get_instances(tag_key, tag_value) ⇒ Object



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
# File 'lib/ec2-ssh/aws.rb', line 34

def get_instances(tag_key, tag_value)
  @all_servers = []

  response = @ec2.describe_instances({
    filters: [
    {
      name: 'instance-state-code',
      values: ['16']

    },{
      name: 'tag-key',
      values: ["#{tag_key}"]
    },{
      name: 'tag-value',
      values: ["#{tag_value}"]
    }
  ]
  })

  if !response.reservations.empty?
    response.reservations.each {|r| r.instances.inject(@all_servers){|acc, k| acc << k.public_ip_address; acc}}
  else
    say "could not find any instances with the tag #{tag_key}: #{tag_value} on #{@region}"
  end

  say "All servers: #{@all_servers.size}"
end