initial prototype

This commit is contained in:
Ben Charlton 2019-04-14 10:29:57 +01:00
commit 5858499113
5 changed files with 635 additions and 0 deletions

258
sbin/symbiosis-dns-generate Executable file
View file

@ -0,0 +1,258 @@
#!/usr/bin/ruby
#
# NAME
#
# symbiosis-dns-generate - Generate DNS snippet files for Symbiosis domains.
#
# USAGE
#
# symbiosis-dns-generate [ --sleep SEC | -s SEC ] [ --template TEMPLATE | -t TEMPLATE ]
# [ --force | -f ] [ --verbose | -v ]
# [ --help | -h ] [ DOMAIN ]
#
# SYNOPSIS
#
# --sleep SEC sleep for a random amount of time before doing
# anything, up to a maximum of SEC seconds.
#
# --template TEMPLATE Specify an alternative template file to read.
#
# --force Force the re-creation of all DNS data.
# --upload Force the upload of all DNS data.
# --help Show the help information for this script.
# --verbose Show debugging information.
#
# DETAILS
#
# This script is designed to iterate over the domains hosted upon a Symbiosis
# system, and create TinyDNS snippets for each one. This can then be uploaded
# to the Bytemark content DNS service.
#
# Domains can also be specified manually on the command line, in which case
# only those domains will be processed.
#
# AUTHOR
#
# Steve Kemp <steve@bytemark.co.uk>
# Adapted for Mythic Beasts by Ben Charlton <ben@spod.cx>
#
require 'getoptlong'
#
# Entry point to the code
#
force = false
help = false
$VERBOSE = false
#
# Do we need to re-upload the data?
#
upload=true
#
# The root directory -- '/' by default.
#
root = "/"
dns_template = nil
sleep_for = nil
opts = GetoptLong.new(
[ '--force', '-f', GetoptLong::NO_ARGUMENT ],
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--sleep', '-s', GetoptLong::REQUIRED_ARGUMENT ],
[ '--template', '-t', GetoptLong::REQUIRED_ARGUMENT ],
[ '--upload', '-u', GetoptLong::NO_ARGUMENT ],
[ '--verbose', '-v', GetoptLong::NO_ARGUMENT ]
)
opts.each do |opt, arg|
case opt
when '--help'
help = true
when '--verbose'
$VERBOSE = true
when '--template'
dns_template = arg
when '--sleep'
sleep_for = arg
when '--root'
root = arg
when '--force'
force = true
when '--upload'
upload = true
end
end
#
# CAUTION! Here be quality kode.
#
if help
# Open the file, stripping the shebang line
lines = File.open(__FILE__){|fh| fh.readlines}[2..-1]
lines.each do |line|
line.chomp!
break if line.empty?
puts line[2..-1].to_s
end
exit 0
end
def verbose(s)
puts s if $VERBOSE
end
require 'symbiosis/domains'
require 'symbiosis/domain'
require 'symbiosis/config_files/mythicdns'
#
# Set the default paths.
#
dns_template = File.join(root, "/etc/symbiosis/dns.d/mythicdns.template.erb") if dns_template.nil?
#
# Bail out if the template is missing
#
unless File.file?(dns_template)
verbose "Unable generate DNS data because the template #{dns_template.inspect} is missing."
exit 1
end
#
# Work out if we need to sleep.
#
unless sleep_for.nil?
sleep_for = (sleep_for =~ /(\d+)/ ? rand($1.to_i) : 0)
verbose "Sleeping for #{sleep_for}s before starting work"
sleep sleep_for
end
#
# Any arguments on the command line specify which domains to do.
#
domains_to_configure = ARGV
string_to_hash = []
#
# For each domain.
#
Symbiosis::Domains.each do |domain|
verbose "Domain: #{domain.name} "
next unless domains_to_configure.empty? or domains_to_configure.include?(domain.name)
begin
output = File.join(domain.config_dir, "dns", domain.name+".txt")
output_dir = File.dirname(output)
config = Symbiosis::ConfigFiles::Tinydns.new(output, "#")
config.domain = domain
config.template = dns_template
#
# Should the snippet be created?
#
do_create = false
if ( force )
verbose "\tForcing re-creation of snippet due to --force."
do_create = true
elsif config.exists?
if config.changed?
verbose "\tNot updating snippet, as it has been edited by hand."
elsif config.outdated?
verbose "\tRe-creating snippet as it is out of date."
do_create = true
else
verbose "\tDomain already present and up-to date."
end
else
verbose "\tConfiguring site for the first time"
do_create = true
end
#
#
# Check the TinyDNS syntax.. TODO!
#
if do_create
if config.ok?
verbose "\tWriting snippet to #{output}"
#
# Create directory with the same ownership as the parent
#
domain.create_dir(output_dir) unless File.exist?(output_dir)
#
# Write the snippet
#
config.write
#
# Make sure the ownership is correct.
#
File.chown(domain.uid, domain.gid, config.filename)
else
verbose "\tThe new DNS snippet is invalid -- no changes have been made."
end
end
#
# Rescue errors for this domain, but continue for others.
#
rescue StandardError => err
verbose "\tUnable to create DNS data for #{domain.name} because #{err.to_s}"
verbose "\t"+err.backtrace.join("\n\t")
end
end
begin
if upload
upload_script = "/usr/sbin/symbiosis-mythic-dns"
verbose "Uploading using #{upload_script}"
IO.popen("#{upload_script} 2>&1","r") do |io|
while !io.eof? do
verbose io.readline
end
end
unless 0 == $?
raise StandardError, "#{upload_script.inspect} failed."
end
else
verbose "No need to upload as no changes in the data have been detected."
end
rescue StandardError => err
warn "Unable to upload DNS data because #{err.to_s}"
verbose "\t"+err.backtrace.join("\n\t")
exit 1
end
#
# All done.
#
exit 0

110
sbin/symbiosis-mythic-dns Executable file
View file

@ -0,0 +1,110 @@
#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;
use Getopt::Std;
our ($opt_v, $opt_f);
getopts('vf');
my $domaindir = "/srv";
my $url = 'https://dnsapi.mythic-beasts.com/';
sub upload_dns($$$) {
my ($domain, $dnsfile, $password) = @_;
my $mech = WWW::Mechanize->new( autocheck => 0 );
my $response = $mech->post($url,
{ domain => $domain, password => $password, command => 'LIST' }
);
if (!$response->is_success()) {
warn $mech->content() ;
my $status = $response->status_line;
warn "status = $status\n";
return 0
}
my %existing;
foreach my $line (split /\n/, $mech->content()) {
$line =~ s/\s+$//;
$existing{$line} = 1;
}
my $update = 0;
open F, $dnsfile || die "Can't open $dnsfile";
my $commands = [ domain => $domain, password => $password ];
foreach my $record (<F>) {
chomp $record;
next if $record =~ m/^\s*\#/;
next if $record =~ m/^$/;
if (exists $existing{$record}) {
delete $existing{$record};
} else {
print "ADD $record\n" if ($opt_v);
push @$commands, ("command", "ADD $record");
$update++;
}
}
foreach my $record (keys %existing) {
push @$commands, ("command", "DELETE $record");
print "DELETE $record\n" if ($opt_v);
$update++;
}
if ($update) {
my $response = $mech->post($url,
$commands
);
return 1 if $response->is_success();
warn $mech->content() ;
my $status = $response->status_line;
warn "status = $status\n";
return undef;
}
return 1;
}
opendir(my $dh, $domaindir) || die "can't opendir $domaindir: $!";
while (my $d = readdir($dh)) {
my $target = "$domaindir/$d";
my $passwordfile = "$target/config/dns/mbpassword";
my $lastfile = "$target/config/dns/.lastuploaded";
my $dnsfile = "$domaindir/$d/config/dns/$d.txt";
# Does this look like a valid domain?
if (-d $target && -f $passwordfile) {
print "$d\n" if ($opt_v);
# ALWAYS restrict the password file.
chmod 0600, $passwordfile;
open F, $passwordfile;
my $password = <F>;
close F;
chomp($password);
# Check when the last successful upload was
my $laststamp = 0;
if (-e $lastfile) {
$laststamp = (stat($lastfile))[9];
}
my $tstamp = (stat($dnsfile))[9];
print "last uploaded $laststamp, last generated $tstamp\n" if ($opt_v);
# and upload if generated file is newer (or forced)
if ( ($opt_f) || ($tstamp > $laststamp)) {
print "Uploading...\n" if ($opt_v);
my $success = upload_dns($d, $dnsfile, $password);
if ($success) {
# only update lastfile on success
open F, ">", $lastfile;
close F;
utime(undef, undef, $lastfile);
}
}
}
}
closedir $dh;