class RedZone::Zone

Represents a zone configuraton for a domain.

Takes a zone configuration in the form of a hash. this is usually read from a yaml file. The schema for the yaml file can be found in the resources

Attributes

altnames[R]

Returns a list of alternate names for machines. These are additional A/AAAA records to the same IP Address @return [Array<Machine>]

cnames[R]

Returns a list of domain-name aliases @return [Array<Record>]

default[R]

Returns the default machine for the domain when no subdomain is supplied @return [Machine]

machines[R]

Return the a list of machines @return [Array<Machine>]

mailservers[R]

Returns a list of mail servers @return [Array<Machine>]

name[R]

Returns the domain name of the zone @return [String]

nameservers[R]

Returns a list of name servers that service the zone @return [Array<Machine>]

records[R]

Returns a list of additional zone records @return [Array<Record>]

soa[R]

Return the SOA record @return [SOA]

wildcard[R]

Returns the wildcard entry - the catch-all machine for dns queries not matching any other subdomain entry. @return [Machine]

Public Class Methods

new(name,config) click to toggle source

Create a new Zone config @param [String] name Domain name (eg: example.com) @param [Hash] config Zone configuration

# File lib/redzone/zone.rb, line 62
def initialize(name,config)
  @name       = name
  @config     = config
  generate_machines()
  generate_aliases()
  generate_nameservers()
  generate_mailservers()
  generate_cnames()
  generate_records()
  @soa = generate_soa(@name)
  d  = get_machine('default')
  wc = get_machine('wildcard')
  if d
    @default = d.alias("@")
  end
  if wc
    @wildcard = wc.alias("*")
  end
end

Public Instance Methods

generate_arpa_list() click to toggle source

Generates a list of Arpas for this zone @return [Array<Arpa>]

# File lib/redzone/zone.rb, line 136
def generate_arpa_list
  arpas = []
  if @config.has_key?('arpa')
    @config['arpa'].each do |cfg|
      opts = symbolize(cfg)
      soa  = generate_soa(opts[:name])
      opts[:soa] = soa
      an = Arpa.new(opts)
      arpas << an
      @nameservers.each do |ns|
        an.add_record(Record.new(:name => '@', :type => 'NS', :data => ns.name + ".#{@name}." ))
      end
      @machines.each do |machine|
        an.add(machine,@name)
      end
    end
  end
  arpas
end
write(io) click to toggle source

Writes the entre zonefile to the target IO @param [IO] io Target IO stream

# File lib/redzone/zone.rb, line 109
def write(io)
  io << soa
  unless default.nil?
    write_machines(io,[default],"Default Machine")
  end
  write_machines(io,nameservers,"Name Servers")
  write_machines(io,mailservers,"Mail Servers")
  write_machines(io,machines,"Primary Machine names")
  write_machines(io,altnames,"Alternate Machine Names")
  unless wildcard.nil?
    write_machines(io,[wildcard],"Wildcard Machine")
  end
  unless cnames.empty?
    io << "; Canonical Names\n"
    write_records(io,cnames)
    io << "\n"
  end
  
  unless records.nil? or records.empty?
    io << "; Extra Records\n"
    write_records(io,records)
    io << "\n"
  end
end
write_machines(io,machines,comment=nil) click to toggle source

Writes the list of machines to the target IO @param [IO] io output IO Stream @param [Array<#records>] machines machines @param [String] comment (nil) Optional comment to be prefixed to the record section

# File lib/redzone/zone.rb, line 97
def write_machines(io,machines,comment=nil)
  unless machines.nil? or machines.empty?
    io << "; #{comment}\n" unless comment.nil?
    machines.each do |m|
      write_records(io,m.records)
    end
    io << "\n"
  end
end
write_records(io,records) click to toggle source

Writes a given set of Records to the target IO @param [IO] io output IO Stream @param [Array<Record>] records

# File lib/redzone/zone.rb, line 85
def write_records(io,records)
  unless records.nil? or records.empty?
    records.each do |r|
      io << r
    end
  end
end

Private Instance Methods

add_alias(name,machine) click to toggle source
# File lib/redzone/zone.rb, line 213
def add_alias(name,machine)
  unless @allnames.include?(name)
    @altnames << machine.alias(name)
    @allnames << name
  end
end
generate_aliases() click to toggle source
# File lib/redzone/zone.rb, line 197
def generate_aliases
  machines = []
  if @config.has_key?('aliases')
    sorted_each(@config['aliases']) do |name,ref|
    #@config['aliases'].each do |name,ref|
      m = get_machine(ref)
      if not m.nil?
        machines  << m.alias(name)
        @allnames << name
      else
        puts "ERROR: No such machine named #{ref}"
      end
    end
  end
  @altnames = machines
end
generate_cnames() click to toggle source
# File lib/redzone/zone.rb, line 253
def generate_cnames
  @cnames = []
  sorted_each(@config['cnames']) do |k,v|
  #@config['cnames'].each do |k,v|
    data = v
    # Assume absolute domain name
    # if value is not a machine name
    unless @allnames.include?(v)
      data = "#{v}."
    end
    @cnames << Record.new({
      :name => k,
      :type => 'CNAME',
      :data => data
    })
  end
  @cnames
end
generate_machines() click to toggle source
# File lib/redzone/zone.rb, line 178
def generate_machines
  @machines = []
  @allnames = []
  @machines_by_name = {}
  sorted_each(@config['machines']) do |n,c|
  #@config['machines'].each do |n,c|
    m = Machine.new(n,symbolize(c))
    @allnames << n
    @machines << m
    @machines_by_name[n] = m 
  end
end
generate_mailservers() click to toggle source
# File lib/redzone/zone.rb, line 219
def generate_mailservers
  machines = []
  if @config.has_key?('mailservers')
    sorted_each(@config['mailservers']) do |name,mx|
    #@config['mailservers'].each do |name,mx|
      ref = mx['machine']
      m  = get_machine(ref)
      if not m.nil?
        priority = mx['priority'] || 10
        machines << MailExchange.new(name,m,priority)
        add_alias(name,m)
      else
        puts "ERROR: No such machine named #{ref}"
      end
    end
  end
  @mailservers = machines
end
generate_nameservers() click to toggle source
# File lib/redzone/zone.rb, line 237
def generate_nameservers
  machines = []
  if @config.has_key?('nameservers')
    sorted_each(@config['nameservers']) do |name,ref|
    #@config['nameservers'].each do |name,ref|
      m = get_machine(ref)
      if not m.nil?
        machines << NameServer.new(name,m)
        add_alias(name,m)
      else
        puts "ERROR: No such machine named #{ref}"
      end
    end
  end
  @nameservers = machines
end
generate_records() click to toggle source
# File lib/redzone/zone.rb, line 271
def generate_records
  @records = []
  if @config.has_key?('records') and not @config['records'].nil?
    @config['records'].each do |record|
      @records << Record.new(symbolize(record))
    end
  end
  @records
end
generate_soa(name) click to toggle source
# File lib/redzone/zone.rb, line 171
def generate_soa(name)
  opts = symbolize(@config['lifetimes'])
  opts[:hostmaster] = @config['hostmaster'] if @config.has_key?('hostmaster')
  opts[:domain]     = name
  opts[:ns]         = @nameservers[0].name + ".#{@name}." unless @nameservers.empty?
  SOA.new(opts)
end
get_machine(name) click to toggle source
# File lib/redzone/zone.rb, line 190
def get_machine(name)
  if @machines_by_name.has_key?(name)
    @machines_by_name[name]
  else
    nil
  end
end
sorted_each(hash,&block) click to toggle source
# File lib/redzone/zone.rb, line 157
def sorted_each(hash,&block)
  unless hash.nil?
    keys = hash.keys.sort
    keys.each do |key|
      block.call(key,hash[key])
    end
  end
end
symbolize(hash) click to toggle source
# File lib/redzone/zone.rb, line 165
def symbolize(hash) 
  hash.inject({}) do |memo,(k,v)|
    memo[k.to_sym] = v
    memo
  end
end