Class: Automan::Beanstalk::Router

Inherits:
Automan::Base show all
Defined in:
lib/automan/beanstalk/router.rb

Constant Summary

Constants included from Mixins::AwsCaller

Mixins::AwsCaller::S3_PROTO

Instance Attribute Summary collapse

Attributes inherited from Automan::Base

#logger, #wait

Attributes included from Mixins::AwsCaller

#as, #cfn, #eb, #ec, #ec2, #elb, #r53, #rds, #s3

Instance Method Summary collapse

Methods inherited from Automan::Base

add_option, #log_options, #wait_until

Methods included from Mixins::AwsCaller

#account, #configure_aws, #looks_like_s3_path?, #parse_s3_path, #s3_object_exists?, #s3_read

Constructor Details

#initialize(options = {}) ⇒ Router

Returns a new instance of Router.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/automan/beanstalk/router.rb', line 13

def initialize(options={})
  super
  @wait = Wait.new({
    attempts: 10,
    delay:    60,   # 10 x 60s == 10m
    debug:    true,
    logger:   @logger,

    # rescue from InvalidChangeBatch
    # just because we've found the elb cname doesn't
    # mean it is immediately available as a route53 alias
    rescuer:  WaitRescuer.new(AWS::Route53::Errors::InvalidChangeBatch)
  })
end

Instance Attribute Details

#attempts_madeObject

Returns the value of attribute attempts_made.



11
12
13
# File 'lib/automan/beanstalk/router.rb', line 11

def attempts_made
  @attempts_made
end

Instance Method Details

#elb_cname_from_beanstalk_environment(env_name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/automan/beanstalk/router.rb', line 28

def elb_cname_from_beanstalk_environment(env_name)
  opts = {
    environment_name: env_name
  }
  response = eb.describe_environment_resources opts

  unless response.successful?
    raise RequestFailedError, "describe_environment_resources failed: #{response.error}"
  end

  balancers = response.data[:environment_resources][:load_balancers]
  if balancers.empty?
    return nil
  end

  balancers.first[:name]
end

#runObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/automan/beanstalk/router.rb', line 75

def run
  log_options

  wait.until do |attempt|

    self.attempts_made = attempt

    logger.info "waiting for elb cname..."
    elb_name = elb_cname_from_beanstalk_environment environment_name

    if elb_name.nil?
      false
    else

      elb_data = elb.load_balancers[elb_name]

      update_dns_alias(
        hosted_zone_name,
        hostname,
        elb_data
      )
      true
    end

  end
end

#update_dns_alias(zone_name, alias_name, elb_data) ⇒ Object

be sure alias_name ends in period



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/automan/beanstalk/router.rb', line 47

def update_dns_alias(zone_name, alias_name, elb_data)
  zone = r53.hosted_zones.select {|z| z.name == zone_name}.first
  rrset = zone.rrsets[alias_name, 'A']

  target = elb_data.dns_name.downcase

  if rrset.exists?
    if rrset.alias_target[:dns_name] == target + '.'
      logger.info "record exists: #{alias_name} -> #{rrset.alias_target[:dns_name]}"
      return
    else
      logger.info "removing old record #{alias_name} -> #{rrset.alias_target[:dns_name]}"
      rrset.delete
    end
  end

  logger.info "creating record #{alias_name} -> #{target}"
  zone.rrsets.create(
    alias_name,
    'A',
    alias_target: {
      hosted_zone_id: elb_data.canonical_hosted_zone_name_id,
      dns_name:       target,
      evaluate_target_health: false
    }
  )
end