Migration: things are getting pretty serious.

This commit is contained in:
Philippe PITTOLI 2024-06-10 03:29:26 +02:00
parent 2509e8af15
commit 8119bf7f57
7 changed files with 79 additions and 20 deletions

View File

@ -107,7 +107,7 @@ print-response-messages-without-comments:
MIGRATION_FILE_INIT ?= /tmp/dnsmanager-migration-init.txt MIGRATION_FILE_INIT ?= /tmp/dnsmanager-migration-init.txt
MIGRATION_FILE_INTERMEDIARY ?= /tmp/dnsmanager-migration-intermediary.txt MIGRATION_FILE_INTERMEDIARY ?= /tmp/dnsmanager-migration-intermediary.txt
MIGRATION_FILE_TARGET ?= /tmp/dnsmanager-migration-target.txt MIGRATION_FILE_TARGET ?= /tmp/dnsmanager-migration-target.txt
migrate: $(MIGRATION_FILE_TARGET):
# Format entries: one per line. # Format entries: one per line.
./bin/format.awk < $(MIGRATION_FILE_INIT) |\ ./bin/format.awk < $(MIGRATION_FILE_INIT) |\
./bin/fix-last-element.awk |\ ./bin/fix-last-element.awk |\
@ -115,6 +115,8 @@ migrate:
sed 's/ \+activated \+=> \+0, \+//' |\ sed 's/ \+activated \+=> \+0, \+//' |\
sed 's/domain \+=> \+/"domain": /' |\ sed 's/domain \+=> \+/"domain": /' |\
sed 's/ \+login \+=> \+/"login": /' |\ sed 's/ \+login \+=> \+/"login": /' |\
# Put double-quotes around logins composed of digits. \
sed 's/"login": \([0-9]\+\)/"login": "\1"/' |\
sed 's/", \+}/" }/' |\ sed 's/", \+}/" }/' |\
# Fix @ in logins. \ # Fix @ in logins. \
sed 's/\\@/@/' |\ sed 's/\\@/@/' |\
@ -125,6 +127,24 @@ migrate:
grep -v '\\' > $(MIGRATION_FILE_INTERMEDIARY) grep -v '\\' > $(MIGRATION_FILE_INTERMEDIARY)
./bin/migration-final.awk < $(MIGRATION_FILE_INTERMEDIARY) | sort -n > $(MIGRATION_FILE_TARGET) ./bin/migration-final.awk < $(MIGRATION_FILE_INTERMEDIARY) | sort -n > $(MIGRATION_FILE_TARGET)
MIGRATION_FILE_AUTHD ?= /tmp/authd-migration-user-db.txt
$(MIGRATION_FILE_AUTHD):; cat $(MIGRATION_FILE_TARGET) | awk '{ print $$2 }' > $(MIGRATION_FILE_AUTHD)
migration-file-authd: $(MIGRATION_FILE_AUTHD)
run-migration-client:; ./bin/dnsmanager-client admin migration-script $(MIGRATION_FILE_TARGET) $(LOGIN)
migration-files: $(MIGRATION_FILE_TARGET) $(MIGRATION_FILE_AUTHD)
migration: migration-files run-migration-client
doc:
crystal docs src/main.cr src/client.cr lib/authd/src/client.cr
HTTPD_ACCESS_LOGS ?= /tmp/access-dodb-docs.log
HTTPD_ADDR ?= 127.0.0.1
HTTPD_PORT ?= 9001
DIR ?= docs
serve-doc:
darkhttpd $(DIR) --addr $(HTTPD_ADDR) --port $(HTTPD_PORT) --log $(HTTPD_ACCESS_LOGS)
wipe-db: wipe-db:
rm -r $(DBDIR) rm -r $(DBDIR)

View File

@ -1,5 +1,9 @@
#!/usr/bin/gawk -f #!/usr/bin/gawk -f
BEGIN {
OFS = "\t"
}
/^\[/ || /^\]/ { /^\[/ || /^\]/ {
next next
} }

View File

@ -58,6 +58,18 @@ class DNSManager::Client < IPC
], read ], read
end end
# Migration: add a domain for a user.
def migration(login : String, domain : String)
request = Request::Migration.new login, domain
send_now request
parse_message [ Response::DomainAdded,
Response::DomainAlreadyExists,
Response::InvalidDomainName,
Response::UnacceptableDomain,
Response::UnknownUser
], read
end
# Remove a domain. # Remove a domain.
def user_domain_del(domain : String) def user_domain_del(domain : String)
request = Request::DeleteDomain.new domain request = Request::DeleteDomain.new domain

View File

@ -96,11 +96,36 @@ class Actions
filename = Context.args.not_nil!.[0] filename = Context.args.not_nil!.[0]
File.each_line(filename) do |line| File.each_line(filename) do |line|
data = line.split "\t" data = line.split "\t"
_ = data.shift
login = data.shift login = data.shift
nb_domains = data.size
begin begin
Baguette::Log.info "migrating login #{login}, domains: #{data.join(',')}" str = data.join(", ").to_s
# TODO i = 0
#pp! @dnsmanagerd.migration data.each do |domain|
STDOUT.write "migrating '#{i}/#{nb_domains}' domains for '#{login}': #{ "%.50s" % domain }\r".to_slice
STDOUT.write ((" " * 100) + "\r").to_slice
i += 1
response = @dnsmanagerd.migration login, domain
case response
when DNSManager::Response::DomainAdded
# Do nothing.
else
case response
when DNSManager::Response::DomainAlreadyExists
Baguette::Log.error "error: domain name '#{domain}' already exists"
when DNSManager::Response::InvalidDomainName
Baguette::Log.error "error: invalid domain name #{domain}"
when DNSManager::Response::UnacceptableDomain
Baguette::Log.error "error: unacceptable domain name '#{domain}'"
when DNSManager::Response::UnknownUser
Baguette::Log.error "error: unknown user #{login}"
break
else
Baguette::Log.error "error: unknown error"
end
end
end
rescue e rescue e
puts "error for generate_zonefile: #{e.message}" puts "error for generate_zonefile: #{e.message}"
end end

View File

@ -13,9 +13,7 @@ class DNSManager::Request
user = dnsmanagerd.get_logged_user event user = dnsmanagerd.get_logged_user event
return Response::ErrorUserNotLogged.new unless user return Response::ErrorUserNotLogged.new unless user
accepted_domains = dnsmanagerd.configuration.accepted_domains.not_nil! dnsmanagerd.storage.new_domain user.uid, @domain
template_directory = dnsmanagerd.configuration.template_directory
dnsmanagerd.storage.new_domain accepted_domains, template_directory, user.uid, @domain
end end
end end
DNSManager.requests << NewDomain DNSManager.requests << NewDomain

View File

@ -111,10 +111,10 @@ class DNSManager::Storage
Response::GeneratedZone.new domain, (String.new io.buffer, io.pos) Response::GeneratedZone.new domain, (String.new io.buffer, io.pos)
end end
def new_domain(accepted_domains : Array(String), def new_domain(user_id : UserDataID, domain : String) : IPC::JSON
template_directory : String,
user_id : UserDataID, accepted_domains = dnsmanagerd.configuration.accepted_domains.not_nil!
domain : String) : IPC::JSON template_directory = dnsmanagerd.configuration.template_directory
# Prevent future very confusing errors. # Prevent future very confusing errors.
domain = domain.downcase domain = domain.downcase

View File

@ -90,7 +90,7 @@ class DNSManager::Storage::Zone
def to_s(io : IO) def to_s(io : IO)
io << "(#{ "%4d" % @rrid }) " io << "(#{ "%4d" % @rrid }) "
io << "#{ "%30s" % @name} #{ "%6d" % @ttl} #{ "%10s" % @rrtype } #{ "%30s" % @target}\n" io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} #{ "%.10s" % @rrtype } #{ "%.30s" % @target}\n"
end end
def to_bind9(io : IO) def to_bind9(io : IO)
@ -226,7 +226,7 @@ class DNSManager::Storage::Zone
def to_s(io : IO) def to_s(io : IO)
io << "(#{ "%4d" % @rrid }) " io << "(#{ "%4d" % @rrid }) "
io << "#{ "%30s" % @name} #{ "%6d" % @ttl} #{ "%10s" % @rrtype } #{quoted_string @target}\n" io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} #{ "%.10s" % @rrtype } #{quoted_string @target}\n"
end end
def to_bind9(io : IO) def to_bind9(io : IO)
@ -465,7 +465,7 @@ class DNSManager::Storage::Zone
def to_s(io : IO) def to_s(io : IO)
io << "(#{ "%4d" % @rrid }) " io << "(#{ "%4d" % @rrid }) "
io << "#{ "%30s" % @name} #{ "%6d" % @ttl} SPF " io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} SPF "
io << '"' io << '"'
@mechanisms.each do |m| @mechanisms.each do |m|
io << m io << m
@ -556,7 +556,7 @@ class DNSManager::Storage::Zone
def to_s(io : IO) def to_s(io : IO)
io << "(#{ "%4d" % @rrid }) " io << "(#{ "%4d" % @rrid }) "
io << "#{ "%30s" % @name} #{ "%6d" % @ttl} DKIM #{split_line dkim.to_s}\n" io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} DKIM #{split_line dkim.to_s}\n"
end end
def to_bind9(io : IO) def to_bind9(io : IO)
@ -714,7 +714,7 @@ class DNSManager::Storage::Zone
def to_s(io : IO) def to_s(io : IO)
io << "(#{ "%4d" % @rrid }) " io << "(#{ "%4d" % @rrid }) "
io << "#{ "%30s" % @name} #{ "%6d" % @ttl} DMARC #{split_line dmarc.to_s}\n" io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} DMARC #{split_line dmarc.to_s}\n"
end end
def to_bind9(io : IO) def to_bind9(io : IO)
@ -755,8 +755,8 @@ class DNSManager::Storage::Zone
def to_s(io : IO) def to_s(io : IO)
io << "(#{ "%4d" % @rrid }) " io << "(#{ "%4d" % @rrid }) "
io << "#{ "%30s" % @name} #{ "%6d" % @ttl} CAA " io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} CAA "
io << "#{ "%3s" % @caa.flag} #{ "%15s" % @caa.tag} #{@caa.value}\n" io << "#{ "%.3s" % @caa.flag} #{ "%.15s" % @caa.tag} #{@caa.value}\n"
end end
def to_bind9(io : IO) def to_bind9(io : IO)
@ -789,7 +789,7 @@ class DNSManager::Storage::Zone
def to_s(io : IO) def to_s(io : IO)
io << "(#{ "%4d" % @rrid }) " io << "(#{ "%4d" % @rrid }) "
io << "#{ "%30s" % @name} #{ "%6d" % @ttl} MX #{ "%3d" % @priority} #{ "%30s" % @target}\n" io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} MX #{ "%3d" % @priority} #{ "%.30s" % @target}\n"
end end
def to_bind9(io : IO) def to_bind9(io : IO)
@ -850,7 +850,7 @@ class DNSManager::Storage::Zone
io << "#{ "%3d" % @priority} " io << "#{ "%3d" % @priority} "
io << "#{ "%3d" % @weight} " io << "#{ "%3d" % @weight} "
io << "#{ "%5d" % @port} " io << "#{ "%5d" % @port} "
io << "#{ "%30s" % @target}\n" io << "#{ "%.30s" % @target}\n"
end end
def to_bind9(io : IO) def to_bind9(io : IO)