Class: Rubber::Dns::Dyndns

Inherits:
Base
  • Object
show all
Defined in:
lib/rubber/dns/dyndns.rb

Instance Attribute Summary

Attributes inherited from Base

#env, #provider_env, #provider_name

Instance Method Summary collapse

Methods inherited from Base

#destroy, #host_records_equal?, #setup_opts, #update

Constructor Details

#initialize(env) ⇒ Dyndns

Returns a new instance of Dyndns


6
7
8
9
10
11
# File 'lib/rubber/dns/dyndns.rb', line 6

def initialize(env)
  super(env, 'dyndns')
  @user, @pass = provider_env.user, provider_env.password
  @update_url = provider_env.update_url || 'https://members.dyndns.org/nic/update?hostname=%host%&myip=%ip%'
  @update_url = @update_url.gsub(/%([^%]+)%/, '#{\1}')
end

Instance Method Details

#create_host_record(opts = {}) ⇒ Object


40
41
42
# File 'lib/rubber/dns/dyndns.rb', line 40

def create_host_record(opts={})
  puts "WARNING: No create record available for dyndns, you need to do so manually"
end

#destroy_host_record(opts = {}) ⇒ Object


44
45
46
# File 'lib/rubber/dns/dyndns.rb', line 44

def destroy_host_record(opts={})
  puts "WARNING: No destroy record available for dyndns, you need to do so manually"
end

#find_host_records(opts = {}) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubber/dns/dyndns.rb', line 26

def find_host_records(opts={})
  opts = setup_opts(opts, [:host, :domain])
  hostname = "#{opts[:host]}.#{opts[:domain]}"
  begin
    Resolv::DNS.open(:nameserver => [nameserver], :search => [], :ndots => 1) do |dns|
      r = dns.getresource(hostname, Resolv::DNS::Resource::IN::A)
      result = [{:host =>opts[:host], :data => r.address}]
    end
  rescue
    puts "Rescue #{e} #{e.message}"
    raise "Domain needs to exist in dyndns as an A record before record can be updated"
  end
end

#nameserverObject


13
14
15
# File 'lib/rubber/dns/dyndns.rb', line 13

def nameserver
  "ns1.mydyndns.org"
end

#up_to_date(host, ip) ⇒ Object


17
18
19
20
21
22
23
24
# File 'lib/rubber/dns/dyndns.rb', line 17

def up_to_date(host, ip)
  # This queries dns server directly instead of using hosts file
  current_ip = nil
  Resolv::DNS.open(:nameserver => [nameserver], :search => [], :ndots => 1) do |dns|
    current_ip = dns.getaddress("#{host}.#{provider_env.domain}").to_s rescue nil
  end
  return ip == current_ip
end

#update_host_record(old_opts = {}, new_opts = {}) ⇒ Object


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
# File 'lib/rubber/dns/dyndns.rb', line 48

def update_host_record(old_opts={}, new_opts={})
  old_opts = setup_opts(old_opts, [:host, :domain])

  host = "#{old_opts[:host]}.#{old_opts[:domain]}"
  ip = new_opts[:data]
  update_url = eval('%Q{' + @update_url + '}')
  # puts update_url
  # This header is required by dyndns.org
  headers = {
   "User-Agent" => "Capistrano - Rubber - 0.1"
  }

  uri = URI.parse(update_url)
  http = Net::HTTP.new(uri.host, uri.port)
  # switch on SSL
  http.use_ssl = true if uri.scheme == "https"
  # suppress verification warning
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  req = Net::HTTP::Get.new(update_url.gsub(/.*:\/\/[^\/]*/, ''), headers)
  # authentication details
  req.basic_auth @user, @pass
  resp = http.request(req)
  # print out the response for the update
  puts "DynDNS Update result: #{resp.body}"
end