class Deputy53::Agent

Handles creation and delegation

Public Class Methods

new(target) click to toggle source
# File lib/deputy53/agent.rb, line 10
def initialize(target)
  @target = target
  self
end

Public Instance Methods

caller_reference() click to toggle source
# File lib/deputy53/agent.rb, line 16
def caller_reference
  @caller_reference ||= "#{subdomain}@#{Time.now.to_i}"
end
child() click to toggle source
# File lib/deputy53/agent.rb, line 46
def child
  @child ||= Zone.new create subdomain
end
create(name) click to toggle source
# File lib/deputy53/agent.rb, line 56
def create(name)
  return route53.id(name) if route53.zone? name

  route53
    .api
    .create_hosted_zone(
      name: name,
      caller_reference: caller_reference
    ).hosted_zone
    .id
end
delegate() click to toggle source
# File lib/deputy53/agent.rb, line 69
def delegate
  return true if parent.delegation(subdomain).sort == child.name_servers.sort
  wait_for_change route53.api.change_resource_record_sets(payload).change_info
end
domain() click to toggle source
# File lib/deputy53/agent.rb, line 21
def domain
  @domain ||= @target.split('.').last(2).join('.') << '.'
end
parent() click to toggle source
# File lib/deputy53/agent.rb, line 51
def parent
  @parent ||= Zone.new create domain
end
payload() click to toggle source
# File lib/deputy53/agent.rb, line 95
def payload
  {
    hosted_zone_id: parent.id,
    change_batch: {
      changes: [
        action: 'CREATE',
        resource_record_set: {
          name: subdomain,
          type: 'NS',
          ttl: 300,
          resource_records: child.name_servers.map { |ns| { value: ns } }
        }
      ]
    }
  }
end
prefix() click to toggle source
# File lib/deputy53/agent.rb, line 26
def prefix
  @prefix ||= @target.split('.').slice(0..-3).join('.')
end
route53() click to toggle source
# File lib/deputy53/agent.rb, line 41
def route53
  @route53 ||= route53!
end
route53!() click to toggle source
# File lib/deputy53/agent.rb, line 36
def route53!
  @route53 = Route53.new
end
subdomain() click to toggle source
# File lib/deputy53/agent.rb, line 31
def subdomain
  @subdomain ||= "#{prefix}.#{domain}"
end
wait_for_change(change) click to toggle source
# File lib/deputy53/agent.rb, line 75
def wait_for_change(change)
  ExponentialBackoff.new(0.5, 8.0).tap do |backoff|
    while change.status == 'PENDING'
      route53.api.get_change(id: change.id).change_info.tap do |info|
        backoff.next_interval
        message = "#{info.id} is #{info.status}"
        if info.status == 'PENDING'
          STDERR.puts "#{message} (recheck in #{backoff.current_interval}s)"
          sleep backoff.current_interval
        else
          STDERR.puts message
        end
        change = info
      end
    end
  end
  true if change.status == 'INSYNC'
end