2024-11-20 07:16:13 +01:00
|
|
|
# Duration to wait before taken modification into account (in minutes).
|
|
|
|
duration = 5
|
|
|
|
|
2024-07-03 16:03:59 +02:00
|
|
|
if ARGV.size != 2
|
|
|
|
puts "usage: #{PROGRAM_NAME} dnsmanagerd-bind9-dir powerdns-bind9-dir"
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
|
2024-11-20 07:16:13 +01:00
|
|
|
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
|
|
|
|
|
2024-07-03 16:03:59 +02:00
|
|
|
class Context
|
|
|
|
class_property dnsmanagerd_dir : String = ""
|
|
|
|
class_property powerdns_dir : String = ""
|
|
|
|
end
|
|
|
|
|
2024-07-04 14:10:14 +02:00
|
|
|
def copy_domain_files(domain : String) : Nil
|
2024-07-03 16:03:59 +02:00
|
|
|
src = "#{Context.dnsmanagerd_dir}/#{domain}"
|
|
|
|
dest = "#{Context.powerdns_dir}/#{domain}"
|
2024-11-20 07:16:13 +01:00
|
|
|
BrainlessLog.important "copying #{src} -> #{dest}"
|
2024-07-03 16:03:59 +02:00
|
|
|
i = File.info src
|
|
|
|
File.copy src, dest
|
|
|
|
rescue e : File::AccessDeniedError
|
2024-11-20 07:16:13 +01:00
|
|
|
BrainlessLog.important "You don't have enough rights: #{e}"
|
|
|
|
rescue e : File::Error
|
|
|
|
BrainlessLog.important "File error: #{e}"
|
2024-07-03 16:03:59 +02:00
|
|
|
end
|
|
|
|
|
2024-07-04 14:10:14 +02:00
|
|
|
def run_process(cmd : String, params : Array(String), env : Hash(String, String)) : Nil
|
|
|
|
unless Process.run(cmd, params, env,
|
2024-07-03 16:03:59 +02:00
|
|
|
true # clear environment
|
|
|
|
# input: Process::Redirect::Inherit,
|
|
|
|
# output: Process::Redirect::Inherit,
|
|
|
|
# error: Process::Redirect::Inherit
|
2024-07-04 14:10:14 +02:00
|
|
|
).success?
|
|
|
|
puts "cannot run #{cmd} #{params.join(' ')}"
|
2024-07-03 16:03:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-04 14:10:14 +02:00
|
|
|
def pdns_reload(domain : String) : Nil
|
2024-11-20 07:16:13 +01:00
|
|
|
BrainlessLog.important "reloading a domain: pdns_control bind-reload-now #{domain}"
|
2024-07-04 14:10:14 +02:00
|
|
|
run_process("pdns_control", [ "bind-reload-now", domain ], { "HOME" => "/" })
|
|
|
|
end
|
|
|
|
|
2024-07-03 16:03:59 +02:00
|
|
|
def update_domain(domain : String) : Nil
|
2024-11-20 07:16:13 +01:00
|
|
|
BrainlessLog.important "domain to reload: #{domain}"
|
2024-07-04 14:10:14 +02:00
|
|
|
copy_domain_files domain
|
2024-07-03 16:03:59 +02:00
|
|
|
pdns_reload domain
|
|
|
|
end
|
|
|
|
|
|
|
|
def pdns_add(domain : String) : Nil
|
2024-11-20 07:16:13 +01:00
|
|
|
BrainlessLog.important "adding a new domain: pdns_control bind-add-zone #{Context.powerdns_dir}/#{domain}"
|
2024-07-04 14:10:14 +02:00
|
|
|
run_process("pdns_control",
|
2024-07-03 16:03:59 +02:00
|
|
|
[ "bind-add-zone", domain, "#{Context.powerdns_dir}/#{domain}" ],
|
2024-07-04 14:10:14 +02:00
|
|
|
{ "HOME" => "/" })
|
2024-07-03 16:03:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_domain(domain : String) : Nil
|
2024-11-20 07:16:13 +01:00
|
|
|
BrainlessLog.important "domain to add: #{domain}"
|
2024-07-04 14:10:14 +02:00
|
|
|
copy_domain_files domain
|
2024-07-03 16:03:59 +02:00
|
|
|
pdns_add domain
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_file(path : String)
|
|
|
|
File.delete path
|
|
|
|
rescue e : File::AccessDeniedError
|
2024-11-20 07:16:13 +01:00
|
|
|
BrainlessLog.important "You don't have enough rights: #{e}"
|
2024-07-03 16:03:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def del_domain(domain : String) : Nil
|
2024-11-20 07:16:13 +01:00
|
|
|
BrainlessLog.important "domain to delete: #{domain}"
|
2024-07-03 16:03:59 +02:00
|
|
|
delete_file "#{Context.powerdns_dir}/#{domain}"
|
|
|
|
# TODO: pdns_control ???
|
|
|
|
end
|
|
|
|
|
|
|
|
Context.dnsmanagerd_dir = ARGV[0]
|
|
|
|
Context.powerdns_dir = ARGV[1]
|
|
|
|
|
|
|
|
dnsmanagerd_dir_content = Dir.children(Context.dnsmanagerd_dir).select { |d| ! d.ends_with? ".wip" }
|
|
|
|
powerdns_dir_content = Dir.children(Context.powerdns_dir)
|
|
|
|
|
|
|
|
both = dnsmanagerd_dir_content & powerdns_dir_content
|
|
|
|
both.each do |d|
|
|
|
|
i1 = File.info "#{Context.dnsmanagerd_dir}/#{d}"
|
|
|
|
i2 = File.info "#{Context.powerdns_dir}/#{d}"
|
|
|
|
|
|
|
|
if i1.modification_time > i2.modification_time
|
2024-07-04 14:10:14 +02:00
|
|
|
# Wait for a few minutes before changing anything, to avoid useless reloads.
|
2024-11-20 07:16:13 +01:00
|
|
|
if Time.local > i1.modification_time.shift minutes: duration
|
|
|
|
BrainlessLog.important "#{d}: updating"
|
2024-07-04 14:10:14 +02:00
|
|
|
update_domain d
|
|
|
|
else
|
2024-11-20 07:16:13 +01:00
|
|
|
BrainlessLog.unimportant "#{d}: has been modified less than #{duration} minute ago, do not update yet"
|
2024-07-04 14:10:14 +02:00
|
|
|
end
|
2024-07-03 16:03:59 +02:00
|
|
|
else
|
2024-11-20 07:16:13 +01:00
|
|
|
BrainlessLog.unimportant "#{d}: hasn't been modified"
|
2024-07-03 16:03:59 +02:00
|
|
|
end
|
|
|
|
end
|
2024-11-20 07:16:13 +01:00
|
|
|
BrainlessLog.flush
|
2024-07-03 16:03:59 +02:00
|
|
|
|
|
|
|
to_add = dnsmanagerd_dir_content - powerdns_dir_content
|
2024-07-04 14:10:14 +02:00
|
|
|
to_add.each { |d| add_domain d }
|
2024-07-03 16:03:59 +02:00
|
|
|
|
|
|
|
to_delete = powerdns_dir_content - dnsmanagerd_dir_content
|
2024-07-04 14:10:14 +02:00
|
|
|
to_delete.each { |d| del_domain d }
|