Class: Capistrano::Ec2RoleTag::AwsEc2Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/ec2_role_tag/aws_ec2_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AwsEc2Loader

Returns a new instance of AwsEc2Loader.



6
7
8
9
10
11
12
# File 'lib/capistrano/ec2_role_tag/aws_ec2_loader.rb', line 6

def initialize(options = {})
  if options[:region].nil?
    @ec2 = Aws::EC2::Client.new(region: 'us-west-2')
  else
    @ec2 = Aws::EC2::Client.new(region: options[:region])
  end
end

Instance Method Details

#fetch(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/capistrano/ec2_role_tag/aws_ec2_loader.rb', line 13

def fetch(options = {})
  apps = @ec2.describe_instances(
    {filters: [
      {name: "tag:Stage", values: [options[:stage].to_s]},
      {name: "tag:Role", values: [options[:role].to_s]},
      {name: "instance-state-name", values: ["running"]}
    ]})
  return [] if apps.reservations.empty?
  return [] if apps.reservations[0].instances.empty?
  return apps.reservations[0].instances.map{|instance| instance.private_dns_name }
end