Class: Rubber::Dns::Base

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

Direct Known Subclasses

Aws, Dyndns, Nettica, Zerigo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Base

Returns a new instance of Base.



8
9
10
# File 'lib/rubber/dns/base.rb', line 8

def initialize(env)
  @env = env   
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/rubber/dns/base.rb', line 6

def env
  @env
end

Instance Method Details

#create_host_record(opts = {}) ⇒ Object



37
38
39
# File 'lib/rubber/dns/base.rb', line 37

def create_host_record(opts = {})
  raise "create_host_record not implemented"
end

#destroy(host) ⇒ Object



26
27
28
29
30
31
# File 'lib/rubber/dns/base.rb', line 26

def destroy(host)
  if find_host_records(:host => host).size != 0
    puts "Destroying dynamic DNS record: #{host}"
    destroy_host_record(:host => host)
  end
end

#destroy_host_record(opts = {}) ⇒ Object



49
50
51
# File 'lib/rubber/dns/base.rb', line 49

def destroy_host_record(opts = {})
  raise "destroy_host_record not implemented"
end

#find_host_records(opts = {}) ⇒ Object



41
42
43
# File 'lib/rubber/dns/base.rb', line 41

def find_host_records(opts = {})
  raise "find_host_records not implemented"
end

#host_records_equal?(lhs_opts = {}, rhs_opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/rubber/dns/base.rb', line 53

def host_records_equal?(lhs_opts = {}, rhs_opts = {})
  lhs = setup_opts(lhs_opts)
  rhs = setup_opts(rhs_opts)
  [lhs, rhs].each {|h| h.delete(:id); h.delete(:priority) if h[:priority] == 0}
  lhs == rhs
end

#setup_opts(opts, required = []) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubber/dns/base.rb', line 60

def setup_opts(opts, required=[])
  default_opts = {:domain => env.domain || Rubber.config.domain,
                  :type => env['type'] || env.record_type || 'A',
                  :ttl => env.ttl || 300}
  
  if opts.delete(:no_defaults)
    actual_opts = Rubber::Util::symbolize_keys(opts)
  else
    actual_opts = default_opts.merge(Rubber::Util::symbolize_keys(opts))
  end

  if actual_opts.has_key?(:data) && actual_opts[:data].is_a?(Array) && actual_opts[:data].first.is_a?(Hash)
    actual_opts[:data] = actual_opts[:data].collect { |x| Rubber::Util.symbolize_keys(x) }
  end

  required.each do |r|
    raise "Missing required options: #{r}" unless actual_opts[r]
  end

  return actual_opts
end

#up_to_date(host, ip) ⇒ Object



33
34
35
# File 'lib/rubber/dns/base.rb', line 33

def up_to_date(host, ip)
  find_host_records(:host => host).any? {|host| host[:data].include?(ip) }
end

#update(host, ip) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rubber/dns/base.rb', line 12

def update(host, ip)
  if up_to_date(host, ip)
    puts "IP has not changed for #{host}, not updating dynamic DNS"
  else
    if find_host_records(:host => host).size == 0
      puts "Creating dynamic DNS: #{host} => #{ip}"
      create_host_record(:host => host, :data => [ip])
    else
      puts "Updating dynamic DNS: #{host} => #{ip}"
      update_host_record({:host => host}, {:host => host, :data => [ip]})
    end
  end
end

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



45
46
47
# File 'lib/rubber/dns/base.rb', line 45

def update_host_record(old_opts={}, new_opts={})
  raise "update_host_record not implemented"
end