Migration: change old NS entries, better logging system for powerdns sync tool.
This commit is contained in:
		
							parent
							
								
									40ee42301f
								
							
						
					
					
						commit
						6a149a3e7c
					
				
					 2 changed files with 53 additions and 14 deletions
				
			
		| 
						 | 
					@ -51,7 +51,12 @@ $(OLDBINDDIR):
 | 
				
			||||||
# A nice rule for the command-line.
 | 
					# A nice rule for the command-line.
 | 
				
			||||||
migration-file: $(MIGRATION_FILE_TARGET)
 | 
					migration-file: $(MIGRATION_FILE_TARGET)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
migration: build-client migration-file run-migration-client run-client-genzones copy-old-zones
 | 
					fix-old-zones-ns:
 | 
				
			||||||
 | 
						@echo "fix old zones NS entries"
 | 
				
			||||||
 | 
						sed -i "s/ns0.arn-fai.net/ns0.karchnu.fr/g"        $(OLDBINDDIR)/*
 | 
				
			||||||
 | 
						sed -i "s/alsace.tetaneutral.net/ns1.karchnu.fr/g" $(OLDBINDDIR)/*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					migration: build-client migration-file fix-old-zones-ns run-migration-client run-client-genzones copy-old-zones
 | 
				
			||||||
	@echo "next as root: make powerdns-add-zones"
 | 
						@echo "next as root: make powerdns-add-zones"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# POWERDNS RULES
 | 
					# POWERDNS RULES
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,8 +1,40 @@
 | 
				
			||||||
 | 
					# Duration to wait before taken modification into account (in minutes).
 | 
				
			||||||
 | 
					duration = 5
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if ARGV.size != 2
 | 
					if ARGV.size != 2
 | 
				
			||||||
	puts "usage: #{PROGRAM_NAME} dnsmanagerd-bind9-dir powerdns-bind9-dir"
 | 
						puts "usage: #{PROGRAM_NAME} dnsmanagerd-bind9-dir powerdns-bind9-dir"
 | 
				
			||||||
	exit 0
 | 
						exit 0
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class BrainlessLog
 | 
				
			||||||
 | 
						class_property last_text_size : Int32 = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						# In case the lastly `unimportant` written string is to be kept on-screen.
 | 
				
			||||||
 | 
						def self.important(str : String)
 | 
				
			||||||
 | 
							unimportant "" # Clear the line and last_text_size = 0
 | 
				
			||||||
 | 
							STDOUT.puts str
 | 
				
			||||||
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						# Log something that isn't important, meaning text that will be
 | 
				
			||||||
 | 
						# rewritten in-place since we don't really want to read it.
 | 
				
			||||||
 | 
						# WARNING: user may want to keep the lastly written string, `keep` it.
 | 
				
			||||||
 | 
						def self.unimportant(str : String)
 | 
				
			||||||
 | 
							STDOUT.write ("\r" + (" " * self.last_text_size)).to_slice # Clear the line.
 | 
				
			||||||
 | 
							self.last_text_size = str.size
 | 
				
			||||||
 | 
							STDOUT.write ("\r" + str).to_slice
 | 
				
			||||||
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						# In case the last `unimportant` string is to be kept on-screen.
 | 
				
			||||||
 | 
						def self.keep
 | 
				
			||||||
 | 
							STDOUT.puts "" unless self.last_text_size == 0
 | 
				
			||||||
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						# In case the last `unimportant` string should be removed.
 | 
				
			||||||
 | 
						def self.flush
 | 
				
			||||||
 | 
							unimportant "" if self.last_text_size > 0
 | 
				
			||||||
 | 
						end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Context
 | 
					class Context
 | 
				
			||||||
	class_property dnsmanagerd_dir : String = ""
 | 
						class_property dnsmanagerd_dir : String = ""
 | 
				
			||||||
	class_property powerdns_dir    : String = ""
 | 
						class_property powerdns_dir    : String = ""
 | 
				
			||||||
| 
						 | 
					@ -11,11 +43,13 @@ end
 | 
				
			||||||
def copy_domain_files(domain : String) : Nil
 | 
					def copy_domain_files(domain : String) : Nil
 | 
				
			||||||
	src  = "#{Context.dnsmanagerd_dir}/#{domain}"
 | 
						src  = "#{Context.dnsmanagerd_dir}/#{domain}"
 | 
				
			||||||
	dest = "#{Context.powerdns_dir}/#{domain}"
 | 
						dest = "#{Context.powerdns_dir}/#{domain}"
 | 
				
			||||||
	puts "copying #{src} -> #{dest}"
 | 
						BrainlessLog.important "copying #{src} -> #{dest}"
 | 
				
			||||||
	i = File.info src
 | 
						i = File.info src
 | 
				
			||||||
	File.copy src, dest
 | 
						File.copy src, dest
 | 
				
			||||||
rescue e : File::AccessDeniedError
 | 
					rescue e : File::AccessDeniedError
 | 
				
			||||||
	puts "You don't have enough rights: #{e}"
 | 
						BrainlessLog.important "You don't have enough rights: #{e}"
 | 
				
			||||||
 | 
					rescue e : File::Error
 | 
				
			||||||
 | 
						BrainlessLog.important "File error: #{e}"
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def run_process(cmd : String, params : Array(String), env : Hash(String, String)) : Nil
 | 
					def run_process(cmd : String, params : Array(String), env : Hash(String, String)) : Nil
 | 
				
			||||||
| 
						 | 
					@ -30,25 +64,25 @@ def run_process(cmd : String, params : Array(String), env : Hash(String, String)
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def pdns_reload(domain : String) : Nil
 | 
					def pdns_reload(domain : String) : Nil
 | 
				
			||||||
	puts "reloading a domain: pdns_control bind-reload-now #{domain}"
 | 
						BrainlessLog.important "reloading a domain: pdns_control bind-reload-now #{domain}"
 | 
				
			||||||
	run_process("pdns_control", [ "bind-reload-now", domain ], { "HOME" => "/" })
 | 
						run_process("pdns_control", [ "bind-reload-now", domain ], { "HOME" => "/" })
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def update_domain(domain : String) : Nil
 | 
					def update_domain(domain : String) : Nil
 | 
				
			||||||
	puts "domain to reload: #{domain}"
 | 
						BrainlessLog.important "domain to reload: #{domain}"
 | 
				
			||||||
	copy_domain_files domain
 | 
						copy_domain_files domain
 | 
				
			||||||
	pdns_reload domain
 | 
						pdns_reload domain
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def pdns_add(domain : String) : Nil
 | 
					def pdns_add(domain : String) : Nil
 | 
				
			||||||
	puts "adding a new domain: pdns_control bind-add-zone #{Context.powerdns_dir}/#{domain}"
 | 
						BrainlessLog.important "adding a new domain: pdns_control bind-add-zone #{Context.powerdns_dir}/#{domain}"
 | 
				
			||||||
	run_process("pdns_control",
 | 
						run_process("pdns_control",
 | 
				
			||||||
			[ "bind-add-zone", domain, "#{Context.powerdns_dir}/#{domain}" ],
 | 
								[ "bind-add-zone", domain, "#{Context.powerdns_dir}/#{domain}" ],
 | 
				
			||||||
			{ "HOME" => "/" })
 | 
								{ "HOME" => "/" })
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def add_domain(domain : String) : Nil
 | 
					def add_domain(domain : String) : Nil
 | 
				
			||||||
	puts "domain to add: #{domain}"
 | 
						BrainlessLog.important "domain to add: #{domain}"
 | 
				
			||||||
	copy_domain_files domain
 | 
						copy_domain_files domain
 | 
				
			||||||
	pdns_add domain
 | 
						pdns_add domain
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					@ -56,11 +90,11 @@ end
 | 
				
			||||||
def delete_file(path : String)
 | 
					def delete_file(path : String)
 | 
				
			||||||
	File.delete path
 | 
						File.delete path
 | 
				
			||||||
rescue e : File::AccessDeniedError
 | 
					rescue e : File::AccessDeniedError
 | 
				
			||||||
	puts "You don't have enough rights: #{e}"
 | 
						BrainlessLog.important "You don't have enough rights: #{e}"
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def del_domain(domain : String) : Nil
 | 
					def del_domain(domain : String) : Nil
 | 
				
			||||||
	puts "domain to delete: #{domain}"
 | 
						BrainlessLog.important "domain to delete: #{domain}"
 | 
				
			||||||
	delete_file "#{Context.powerdns_dir}/#{domain}"
 | 
						delete_file "#{Context.powerdns_dir}/#{domain}"
 | 
				
			||||||
	# TODO: pdns_control ???
 | 
						# TODO: pdns_control ???
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					@ -77,18 +111,18 @@ both.each do |d|
 | 
				
			||||||
	i2 = File.info "#{Context.powerdns_dir}/#{d}"
 | 
						i2 = File.info "#{Context.powerdns_dir}/#{d}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if i1.modification_time > i2.modification_time
 | 
						if i1.modification_time > i2.modification_time
 | 
				
			||||||
		puts "has been modified: #{d}"
 | 
					 | 
				
			||||||
		# Wait for a few minutes before changing anything, to avoid useless reloads.
 | 
							# Wait for a few minutes before changing anything, to avoid useless reloads.
 | 
				
			||||||
		if Time.local > i1.modification_time.shift minutes: 5
 | 
							if Time.local > i1.modification_time.shift minutes: duration
 | 
				
			||||||
			puts "file was modified more than 5 minutes ago"
 | 
								BrainlessLog.important "#{d}: updating"
 | 
				
			||||||
			update_domain d
 | 
								update_domain d
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
			puts "file has been modified less than 5 minutes ago: do not update yet"
 | 
								BrainlessLog.unimportant "#{d}: has been modified less than #{duration} minute ago, do not update yet"
 | 
				
			||||||
		end
 | 
							end
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
		puts "hasn't been modified: #{d}"
 | 
							BrainlessLog.unimportant "#{d}: hasn't been modified"
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					BrainlessLog.flush
 | 
				
			||||||
 | 
					
 | 
				
			||||||
to_add = dnsmanagerd_dir_content - powerdns_dir_content
 | 
					to_add = dnsmanagerd_dir_content - powerdns_dir_content
 | 
				
			||||||
to_add.each { |d| add_domain d }
 | 
					to_add.each { |d| add_domain d }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue