initial prototype
This commit is contained in:
commit
5858499113
5 changed files with 635 additions and 0 deletions
110
sbin/symbiosis-mythic-dns
Executable file
110
sbin/symbiosis-mythic-dns
Executable 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;
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue