Class: Wamupd::DNSUpdate

Inherits:
Object
  • Object
show all
Defined in:
lib/wamupd/dns_update.rb

Overview

Class to help with constructing DNS UPDATEs. Probably not useful except to me.

Constant Summary collapse

@@queue =
nil
@@outstanding =
[]

Class Method Summary collapse

Class Method Details

.async?Boolean

Is this update going to be asynchronous?

Returns:

  • (Boolean)


40
41
42
# File 'lib/wamupd/dns_update.rb', line 40

def self.async?
    return (not @@queue.nil?)
end

.outstandingObject

How many requests are outstanding?



35
36
37
# File 'lib/wamupd/dns_update.rb', line 35

def self.outstanding
    return @@outstanding
end

.publish(*args) ⇒ Object

Publish a single DNS record

Arguments: Same as Dnsruby::Update::add



100
101
102
# File 'lib/wamupd/dns_update.rb', line 100

def self.publish(*args)
    self.publish_all([args])
end

.publish_all(args) ⇒ Object

Publish a batch of DNS records

Arguments: An array of records to publish. Each record is either a Hash containing the keys :target, :type, :ttl, and :value, or an array of items which could be args to Dnsruby::Update::add



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/wamupd/dns_update.rb', line 50

def self.publish_all(args)
    sa = MainSettings.instance()
    resolver = sa.resolver
    update = Dnsruby::Update.new(sa.zone, "IN")
    shortest_ttl=86400
    args.each { |arg|
        if (arg.kind_of?(Hash))
            update.add(arg[:target], arg[:type], arg[:ttl], arg[:value])
            if (arg[:ttl] < shortest_ttl)
                shortest_ttl = arg[:ttl]
            end
        elsif (arg.kind_of?(Array))
            if (arg[2] < shortest_ttl)
                shortest_ttl = arg[2]
            end
            update.add(*arg)
        else
            raise ArgumentError.new("Could not parse arguments")
        end
    }
    opt = Dnsruby::RR::OPT.new
    lease_time = Dnsruby::RR::OPT::Option.new(2, [shortest_ttl].pack("N"))
    opt.klass="IN"
    opt.options=[lease_time]
    opt.ttl = 0
    update.add_additional(opt)
    update.header.rd = false
    queue_id = nil
    begin
        if (async?)
            queue_id = resolver.send_async(update, @@queue)
            @@outstanding << queue_id
        else
           resolver.send_message(update)
           queue_id = true
        end
    rescue Dnsruby::TsigNotSignedResponseError => e
        # Not really an error for UPDATE; we don't care if the reply is
        # signed!
        nil
    rescue Exception => e
        $stderr.puts "Registration failed: #{e.to_s}"
    end
    return queue_id
end

.queue=(v) ⇒ Object

Set the queue



30
31
32
# File 'lib/wamupd/dns_update.rb', line 30

def self.queue=(v)
    @@queue=v
end

.unpublish(*args) ⇒ Object

Unpublish a single DNS record

Arguments: Same as Dnsruby::Update::delete



150
151
152
# File 'lib/wamupd/dns_update.rb', line 150

def self.unpublish(*args)
    self.unpublish_all(args)
end

.unpublish_all(*args) ⇒ Object

Unpublish a batch of DNS records

Arguments: An array of records to unpublish. Each record is either a Hash containing keys :target, :type, and :value, or an array of items which could be args to Dnsruby::Update::delete



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/wamupd/dns_update.rb', line 110

def self.unpublish_all(*args)
    sa = MainSettings.instance()
    resolver = sa.resolver
    update = Dnsruby::Update.new(sa.zone, "IN")
    args.each { |arg|
        if (arg.kind_of?(Hash))
            if (arg.has_key?(:value))
                update.delete(arg[:target], arg[:type], arg[:value])
            else
                update.delete(arg[:target], arg[:type])
            end
        elsif (arg.kind_of?(Array))
            update.delete(*arg)
        else
            raise ArgumentError.new("Could not parse arguments")
        end
    }
    begin
        if (async?)
            queue_id = resolver.send_async(update, @@queue)
            @@outstanding << queue_id
        else
            resolver.send_message(update)
        end
    rescue Dnsruby::NXRRSet => e
        $stderr.puts "Could not remove record because it doesn't exist!"
    rescue Dnsruby::TsigNotSignedResponseError => e
        # Not really an error for UPDATE; we don't care if the reply is
        # signed!
        nil
    rescue Exception => e
        $stderr.puts "Unregistration failed: #{e.to_s}"
    end

end