Class: Dynamic53

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_53.rb,
lib/dynamic_53/version.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

DEFAULT_TTL =

1 hour

3600
VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zone, hostname, options = {}) ⇒ Dynamic53

Initializes an object for updating the Route 53 hostname with the machine’s current public IP address.



15
16
17
18
19
# File 'lib/dynamic_53.rb', line 15

def initialize(zone, hostname, options={})
  @zone = zone
  @hostname = hostname
  @options = options
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



10
11
12
# File 'lib/dynamic_53.rb', line 10

def hostname
  @hostname
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/dynamic_53.rb', line 11

def options
  @options
end

#zoneObject (readonly)

Returns the value of attribute zone.



9
10
11
# File 'lib/dynamic_53.rb', line 9

def zone
  @zone
end

Instance Method Details

#ip_addressObject

fetch the current machine’s public IP address.



31
32
33
34
35
36
37
38
# File 'lib/dynamic_53.rb', line 31

def ip_address
  @ip_address ||=
      begin
        ip_address = Net::HTTP.get("bot.whatismyipaddress.com", "/")
        $stdout.puts "Public IP Address: #{ip_address}" if options[:verbose]
        ip_address
      end
end

#record_setObject

Fetch a record set object for the given hostname in the Route 53 zone specified.



51
52
53
# File 'lib/dynamic_53.rb', line 51

def record_set
  @record_set ||= AWS::Route53::HostedZone.new(zone_id).rrsets[hostname, 'A']
end

#route_53_clientObject

A client for making Route 53 API requests.



56
57
58
# File 'lib/dynamic_53.rb', line 56

def route_53_client
  @route_53_client ||= AWS::Route53.new.client
end

#updateObject

perform the Route 53 update, returning the AWS SDK changelist object for the change.



22
23
24
25
26
27
28
# File 'lib/dynamic_53.rb', line 22

def update
  record_set.ttl = options[:ttl] || DEFAULT_TTL
  record_set.resource_records = [{:value => ip_address}]
  result = record_set.update
  $stdout.puts "Record updated" if options[:verbose]
  result
end

#zone_idObject

Fetches the AWS Route53 zone id for the specified zone.



41
42
43
44
45
46
47
48
# File 'lib/dynamic_53.rb', line 41

def zone_id
  route_53_client.
      list_hosted_zones[:hosted_zones].
      each.
      select { |z| z[:name] == zone }.
      map {|z| z[:id] }.
      first
end