Module: Capistrano::Aws

Defined in:
lib/capistrano/aws.rb,
lib/capistrano/aws/client.rb,
lib/capistrano/aws/version.rb

Defined Under Namespace

Classes: AutoMapping

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.credentialsObject



11
12
13
# File 'lib/capistrano/aws/client.rb', line 11

def credentials
  @credentials ||= ::Aws::SharedCredentials.new(profile_name: fetch(:aws_profile)).credentials
end

.ec2Object



15
16
17
18
19
20
21
# File 'lib/capistrano/aws/client.rb', line 15

def ec2
  @ec2 ||= AutoMapping.new(
    fetch(:aws_regions).map do |region|
      ::Aws::EC2::Client.new(credentials: credentials, region: region)
    end
  )
end

.elbObject



23
24
25
26
27
28
29
# File 'lib/capistrano/aws/client.rb', line 23

def elb
  @elb ||= AutoMapping.new(
    fetch(:aws_regions).map do |region|
      ::Aws::ElasticLoadBalancingV2::Client.new(credentials: credentials, region: region)
    end
  )
end

Instance Method Details

#bluegreen(role, target_arn, &block) ⇒ Object



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

def bluegreen(role, target_arn, &block)
  servers = roles(role)
  servers.each_slice(servers.length / 2).each do |cluster|
    begin
      instance_ids = cluster.map{|x| x.properties.instance_id }
      deregister(target_arn, instance_ids)
      on(cluster, &block)
    ensure
      register(target_arn, instance_ids)
    end
  end
end

#deregister(target_arn, instance_ids) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/capistrano/aws.rb', line 38

def deregister(target_arn, instance_ids)
  params = {
    target_group_arn: target_arn,
    targets: instance_ids.map{|id| { id: id }}
  }

  elb.deregister_targets(params)
  loop do
    return if elb.describe_target_health(params).target_health_descriptions.all? { |t| t.target_health.state == 'unused' }
    sleep 5
  end
end

#instances(options = {}) ⇒ Object



8
9
10
# File 'lib/capistrano/aws.rb', line 8

def instances(options = {})
  ec2.describe_instances(filters: fetch(:ec2_filters)).reservations.instances
end

#register(target_arn, instance_ids) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/capistrano/aws.rb', line 25

def register(target_arn, instance_ids)
  params = {
    target_group_arn: target_arn,
    targets: instance_ids.map{|id| { id: id }}
  }

  elb.register_targets(params)
  loop do
    return if elb.describe_target_health(params).target_health_descriptions.all? { |t| t.target_health.state == 'healthy' }
    sleep 5
  end
end