Project

General

Profile

« Previous | Next » 

Revision fba691ae

Added by Marc Dequènes almost 16 years ago

  • ID fba691ae268e2d90b38db4a6ee6587e6c75d8505

[evol] backup zone before replacing, reload zone, and rollback zone if reload failed

View differences:

lib/cyborghood/services/dns.rb
@config = Config.instance
@content = nil
@filename = @config.dns.master_zone_pattern.gsub("#ZONE#", @zone)
# TODO: should be checked at startup time
raise CyberError.new(:unrecoverable, "services/dns", "erroneous configuration: pattern is constant") if @filename == @config.dns.master_zone_pattern
# ensure we can find the needed programs (should be handled somewhere else)
ENV['PATH'] = (ENV['PATH'].split(":") + ["/sbin", "/usr/sbin", "/usr/local/sbin"]).uniq.join(":")
end
def read_zone
filename = @config.dns.master_zone_pattern.gsub("#ZONE#", @zone)
# TODO: should be checked at startup time
raise CyberError.new(:unrecoverable, "services/dns", "erroneous configuration: pattern is constant") if filename == @config.dns.master_zone_pattern
begin
@content = File.read(filename)
@content = File.read(@filename)
rescue
raise CyberError.new(:unrecoverable, "services/dns", "zone '#{@zone}' cannot be read (nonexistent or lack of permission)")
end
end
def serial
filename = @config.dns.master_zone_pattern.gsub("#ZONE#", @zone)
# TODO: should be checked at startup time
raise CyberError.new(:unrecoverable, "services/dns", "erroneous configuration: pattern is constant") if filename == @config.dns.master_zone_pattern
case @config.dns.nameserver || :bind
when :bind
output = []
begin
IO.popen("named-checkzone -i none '#{@zone}' #{filename}") do |fp|
IO.popen("named-checkzone -i none '#{@zone}' #{@filename}") do |fp|
output << fp.gets.chomp! until fp.eof?
end
rescue
......
end
def write_zone_from_file(new_zone_filename)
filename = @config.dns.master_zone_pattern.gsub("#ZONE#", @zone)
# TODO: should be checked at startup time
raise CyberError.new(:unrecoverable, "services/dns", "erroneous configuration: pattern is constant") if filename == @config.dns.master_zone_pattern
# create backup
FileUtils.cp(@filename, backup_zone_filename())
FileUtils.cp(new_zone_filename, @filename)
end
def replace_zone_with_backup
if File.exists?(backup_zone_filename())
FileUtils.cp(backup_zone_filename(), @filename)
else
raise CyberError.new(:unrecoverable, "services/dns", "no zone backup file found")
end
end
def reload_zone
system "sudo rndc reload '#{@zone}' >/dev/null"
end
private
FileUtils.cp(new_zone_filename, filename)
def backup_zone_filename
@filename + ".ch-backup"
end
end
end
postman
require 'cyborghood/mail'
require 'cyborghood/objects'
require 'cyborghood/services/dns'
require 'fileutils'
#Socket.gethostname
......
if current_serial.nil? or result.serial > current_serial
begin
srv_dns.write_zone_from_file(f.path)
f.close!
logger.info "zone changed"
if srv_dns.reload_zone
logger.info "zone reloaded"
else
logger.info "zone reload failed, replacing old content"
srv_dns.replace_zone_with_backup
result.message = "Internal error."
return result
end
rescue
logger.debug "Writing zone file failed"
raise

Also available in: Unified diff