Class: RedZone::Zone

Inherits:
Object
  • Object
show all
Defined in:
lib/redzone/zone.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config) ⇒ Zone

Create a new Zone config

Parameters:

  • name (String)

    Domain name (eg: example.com)

  • config (Hash)

    Zone configuration



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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

Instance Attribute Details

#altnamesArray<Machine> (readonly)

Returns a list of alternate names for machines. These are additional A/AAAA records to the same IP Address

Returns:



44
45
46
# File 'lib/redzone/zone.rb', line 44

def altnames
  @altnames
end

#cnamesArray<Record> (readonly)

Returns a list of domain-name aliases

Returns:



48
49
50
# File 'lib/redzone/zone.rb', line 48

def cnames
  @cnames
end

#defaultMachine (readonly)

Returns the default machine for the domain when no subdomain is supplied

Returns:



30
31
32
# File 'lib/redzone/zone.rb', line 30

def default
  @default
end

#machinesArray<Machine> (readonly)

Return the a list of machines

Returns:



22
23
24
# File 'lib/redzone/zone.rb', line 22

def machines
  @machines
end

#mailserversArray<Machine> (readonly)

Returns a list of mail servers

Returns:



38
39
40
# File 'lib/redzone/zone.rb', line 38

def mailservers
  @mailservers
end

#nameString (readonly)

Returns the domain name of the zone

Returns:

  • (String)


18
19
20
# File 'lib/redzone/zone.rb', line 18

def name
  @name
end

#nameserversArray<Machine> (readonly)

Returns a list of name servers that service the zone

Returns:



34
35
36
# File 'lib/redzone/zone.rb', line 34

def nameservers
  @nameservers
end

#recordsArray<Record> (readonly)

Returns a list of additional zone records

Returns:



57
58
59
# File 'lib/redzone/zone.rb', line 57

def records
  @records
end

#soaSOA (readonly)

Return the SOA record

Returns:



26
27
28
# File 'lib/redzone/zone.rb', line 26

def soa
  @soa
end

#wildcardMachine (readonly)

Returns the wildcard entry - the catch-all machine for dns queries not matching any other subdomain entry.

Returns:



53
54
55
# File 'lib/redzone/zone.rb', line 53

def wildcard
  @wildcard
end

Instance Method Details

#generate_arpa_listArray<Arpa>

Generates a list of Arpas for this zone

Returns:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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) ⇒ Object

Writes the entre zonefile to the target IO

Parameters:

  • io (IO)

    Target IO stream



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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) ⇒ Object

Writes the list of machines to the target IO

Parameters:

  • io (IO)

    output IO Stream

  • machines (Array<#records>)

    machines

  • comment (String) (defaults to: nil)

    (nil) Optional comment to be prefixed to the record section



97
98
99
100
101
102
103
104
105
# 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) ⇒ Object

Writes a given set of Records to the target IO

Parameters:

  • io (IO)

    output IO Stream

  • records (Array<Record>)


85
86
87
88
89
90
91
# 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