Class: Bind9mgr::NamedConf

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

Overview

You can specify bind_location. If you do so then .add_zone method will generate zones with right filenames. If .file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(named_conf_file_name = '') ⇒ NamedConf

Returns a new instance of NamedConf.



12
13
14
15
16
# File 'lib/named_conf.rb', line 12

def initialize( named_conf_file_name = '' )
  @file = named_conf_file_name
  @bind_location = File.dirname(named_conf_file_name) if file.length > 1
  load
end

Instance Attribute Details

#bind_locationObject

BIND_PATH = ‘/etc/bind’

BIND_DB_PATH = BIND_PATH + '/master'


9
10
11
# File 'lib/named_conf.rb', line 9

def bind_location
  @bind_location
end

#fileObject

BIND_PATH = ‘/etc/bind’

BIND_DB_PATH = BIND_PATH + '/master'


9
10
11
# File 'lib/named_conf.rb', line 9

def file
  @file
end

#main_nsObject

BIND_PATH = ‘/etc/bind’

BIND_DB_PATH = BIND_PATH + '/master'


9
10
11
# File 'lib/named_conf.rb', line 9

def main_ns
  @main_ns
end

#main_server_ipObject

BIND_PATH = ‘/etc/bind’

BIND_DB_PATH = BIND_PATH + '/master'


9
10
11
# File 'lib/named_conf.rb', line 9

def main_server_ip
  @main_server_ip
end

#secondary_nsObject

BIND_PATH = ‘/etc/bind’

BIND_DB_PATH = BIND_PATH + '/master'


9
10
11
# File 'lib/named_conf.rb', line 9

def secondary_ns
  @secondary_ns
end

#support_emailObject

BIND_PATH = ‘/etc/bind’

BIND_DB_PATH = BIND_PATH + '/master'


9
10
11
# File 'lib/named_conf.rb', line 9

def support_email
  @support_email
end

#zonesObject (readonly)

Returns the value of attribute zones.



10
11
12
# File 'lib/named_conf.rb', line 10

def zones
  @zones
end

Instance Method Details

#add_zone(zone_or_name, file_name = nil) ⇒ Object

Raises:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/named_conf.rb', line 99

def add_zone( zone_or_name, file_name = nil )
  if zone_or_name.kind_of?( Zone )
    raise ArgumentError, "file_name should be nil if instance of Zone supplied" unless file_name.nil?
    zone = zone_or_name
  elsif zone_or_name.kind_of?( String )
    raise ArgumentError, "Main ns not secified" unless @main_ns
    # TODO what to do with sec. ns? # raise ArgumentError, "Secondary ns not secified" unless @secondary_ns
    raise ArgumentError, "Support email not secified" unless @support_email
    raise ArgumentError, "Main server ip not secified" unless @main_server_ip
    
    zone = Zone.new( zone_or_name,
                     file_name || gen_zone_file_name(zone_or_name), 
                     :main_ns => @main_ns,
                     :secondary_ns => @secondary_ns,
                     :support_email => @support_email,
                     :main_server_ip => @main_server_ip,
                     :mail_server_ip => @mail_server_ip)
    zone.add_default_rrs
  else
    raise( RuntimeError, "BindZone or String instance expected, but #{zone_or_name.inspect} got")
  end

  raise InvalidZoneError, zone.inspect unless zone.valid?

  del_zone! zone.origin
  @zones.push zone

  zone
end

#del_zone!(origin_or_name) ⇒ Object

We should remove zone enties and delete db file immidiately.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/named_conf.rb', line 134

def del_zone!( origin_or_name )
  founded = @zones.select{ |z| z.origin == origin_or_name || z.name == origin_or_name }
  founded.each do |z| 
    z.load
    File.delete( z.file ) if File.exists? z.file
  end
  # TODO refactor!
  if founded.count > 0
    @zones.delete_if{ |z| z.origin == origin_or_name || z.name == origin_or_name }
  end
  
  # TODO unsafe code: other zone entries can be updated!
  write_conf_file
end

#find(origin_or_name) ⇒ Object



129
130
131
# File 'lib/named_conf.rb', line 129

def find( origin_or_name )
  @zones.select{ |z| z.origin == origin_or_name || z.name == origin_or_name }
end

#gen_conf_contentObject



63
64
65
66
67
68
69
70
71
# File 'lib/named_conf.rb', line 63

def gen_conf_content
  cont = '# File is under automatic control. Edit with caution.' 
  if @zones.size > 0
    @zones.uniq.each do |zone|
      cont << zone.gen_zone_entry << "\n"
    end
  end
  cont
end

#loadObject

Tries to load data from named conf. Do nothing if file missing.



30
31
32
33
34
# File 'lib/named_conf.rb', line 30

def load
  init_zones
  parse File.read( file ) if File.exists?(file)
  zones
end

#load!Object

Tries to load data from named conf. Raises exception if file missing.



24
25
26
27
# File 'lib/named_conf.rb', line 24

def load!
  init_zones
  parse File.read( @file ) if file.length > 0
end

#load_one(zone_name) ⇒ Object

Load just one zone and return it

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
# File 'lib/named_conf.rb', line 37

def load_one( zone_name )
  raise ArgumentError, "Zone name expected to be a string" unless zone_name.kind_of? String
  load
  if zone = zones.find{ |z| z.name == zone_name }
    zone.load
  end
  zone
end

#load_with_zonesObject



46
47
48
49
# File 'lib/named_conf.rb', line 46

def load_with_zones
  load
  zones.each{ |z| z.load}
end

#parse(content) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/named_conf.rb', line 52

def parse content
  content.scan(/(zone "(.*?)" \{.*?file\s+"(.*?)".*?\};\n)/m) do |zcontent, zone, file|
    @zones.push Zone.new( zone, file, 
                          :main_ns => @main_ns,
                          :secondary_ns => @secondary_ns,
                          :support_email => @support_email,
                          :main_server_ip => @main_server_ip )
  end
  @zones
end

#write_allObject



94
95
96
97
# File 'lib/named_conf.rb', line 94

def write_all
  write_conf_file
  write_zones
end

#write_conf_fileObject

Raises:

  • (ArgumentError)


73
74
75
76
# File 'lib/named_conf.rb', line 73

def write_conf_file
  raise ArgumentError, "Conf file not specified" unless @file.kind_of? String
  File.open( @file, 'w' ){|f| f.write( gen_conf_content )}
end

#write_zonesObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/named_conf.rb', line 78

def write_zones
  @zones.uniq.each do |z| 
    z.file ||= gen_zone_file_name( z.origin );
    zones_subdir = File.dirname( z.file )
    Dir.mkdir( zones_subdir ) unless File.exists?( zones_subdir )

    # OPTIMIZE this
    z.options[:main_ns]        ||= @main_ns
    z.options[:secondary_ns]   ||= @secondary_ns
    z.options[:support_email]  ||= @support_email
    z.options[:main_server_ip] ||= @main_server_ip

    z.write_db_file
  end if @zones.size > 0
end