Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby -w
- module MyBIND
- ZONEFMT =<<EOS
- zone "%s" {
- type slave;
- masters { %s};
- file "%s";
- };
- EOS
- TYPE_RE = /type\s+slave\s*;/
- MASTERS_LIST_RE = /masters\s+\{([^}]+)\}\s*;/
- FILENAME_RE = /file\s+"([^"]+)"\s*;/
- ZONENAME_RE = /^zone\s+"([^"]+)"/
- ZONE_RE = /^zone\s+"[^"]+"\s*\{\s*
- .*?
- ^\s*\}\s*;\s*$/mx
- #ZONE_RE = /^zone\s+"([^"]+)"\s*\{\s*
- # (?:(?:#{TYPE_RE}|#{FILENAME_RE}|#{MASTERS_LIST_RE})\s*)+
- # \s*\}\s*;\s*$/mx
- # reads a named.conf.slave file
- # and builds an array with information about
- # zone name, masters list and filename
- def self.readconf(conffilename)
- result = []
- File.read(conffilename).scan(ZONE_RE) do |m|
- if TYPE_RE.match(m)
- zonename = ZONENAME_RE.match(m)[1]
- masters_list = MASTERS_LIST_RE.match(m)[1].split(";").collect{|e| e.strip}
- filename = FILENAME_RE.match(m)[1]
- result << [zonename, masters_list, filename]
- end
- end
- result
- end
- # builds a conf string from a named.conf.slave hash
- # with information about zone name, masters list and filename
- def self.writeconf(hash)
- hash.keys.sort.inject("") do |conf, zonename|
- conf << ZONEFMT % [ zonename,
- hash[zonename][:masters].sort.reverse.join("; "),
- hash[zonename][:file] ]
- end
- end
- # merges two named.conf.slave arrays into a hash.
- def self.mergeconf(a, b)
- zones = Hash.new {|h, k| h[k] = {:masters => []} }
- (a | b).each do |r|
- zones[r[0]][:masters] |= r[1]
- zones[r[0]][:file] = r[2]
- end
- zones
- end
- end # module MyBIND
- if __FILE__ == $0
- def usage
- warn "Usage:
- #{File.basename $0} slave_file_1 slave_file_2"
- exit 1
- end
- usage if ARGV.size != 2
- filenames = ARGV
- confs = filenames.inject([]) {|m, filename| m << MyBIND.readconf(filename)}
- mergedconf = MyBIND.mergeconf(*confs)
- print MyBIND.writeconf(mergedconf)
- end
Add Comment
Please, Sign In to add comment