Class: Rubber::Dns::Base

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

Direct Known Subclasses

Dyndns, Nettica, Zerigo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, provider_name) ⇒ Base

Returns a new instance of Base.



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

def initialize(env, provider_name)
  @env = env
  @provider_name = provider_name
  @provider_env = @env.dns_providers[provider_name]
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

#provider_envObject (readonly)

Returns the value of attribute provider_env.



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

def provider_env
  @provider_env
end

#provider_nameObject (readonly)

Returns the value of attribute provider_name.



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

def provider_name
  @provider_name
end

Instance Method Details

#create_host_record(opts = {}) ⇒ Object



39
40
41
# File 'lib/rubber/dns/base.rb', line 39

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

#destroy(host) ⇒ Object



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

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



51
52
53
# File 'lib/rubber/dns/base.rb', line 51

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

#find_host_records(opts = {}) ⇒ Object



43
44
45
# File 'lib/rubber/dns/base.rb', line 43

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

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

Returns:

  • (Boolean)


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

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



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

def setup_opts(opts, required=[])
  default_opts = {:domain => @env.domain,
                  :type => @provider_env['type'] || @provider_env.record_type || 'A',
                  :ttl => @provider_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

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

  return actual_opts
end

#up_to_date(host, ip) ⇒ Object



35
36
37
# File 'lib/rubber/dns/base.rb', line 35

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

#update(host, ip) ⇒ Object



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

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



47
48
49
# File 'lib/rubber/dns/base.rb', line 47

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