hetzner: Remove unnecessary host groupings
Each host is already updated individually so there's no point in grouping the hosts.
This commit is contained in:
parent
5ae0fd3024
commit
216c9c6010
1 changed files with 85 additions and 92 deletions
177
ddclient.in
177
ddclient.in
|
@ -6253,114 +6253,107 @@ EoEXAMPLE
|
||||||
######################################################################
|
######################################################################
|
||||||
sub nic_hetzner_update {
|
sub nic_hetzner_update {
|
||||||
debug("\nnic_hetzner_update -------------------");
|
debug("\nnic_hetzner_update -------------------");
|
||||||
my %groups = group_hosts_by(\@_, [qw(ssh login password server wildcard mx backupmx zone wantipv4 wantipv6)]);
|
for my $domain (@_) {
|
||||||
for my $sig (keys %groups) {
|
my $headers = "Auth-API-Token: $config{$domain}{'password'}\n";
|
||||||
my @hosts = @{$groups{$sig}};
|
|
||||||
my $hosts = join(',', @hosts);
|
|
||||||
my $key = $hosts[0];
|
|
||||||
|
|
||||||
my $headers = "Auth-API-Token: $config{$key}{'password'}\n";
|
|
||||||
$headers .= "Content-Type: application/json";
|
$headers .= "Content-Type: application/json";
|
||||||
|
|
||||||
for my $domain (@hosts) {
|
(my $hostname = $domain) =~ s/\.$config{$domain}{zone}$//;
|
||||||
(my $hostname = $domain) =~ s/\.$config{$key}{zone}$//;
|
my $ipv4 = delete $config{$domain}{'wantipv4'};
|
||||||
my $ipv4 = delete $config{$domain}{'wantipv4'};
|
my $ipv6 = delete $config{$domain}{'wantipv6'};
|
||||||
my $ipv6 = delete $config{$domain}{'wantipv6'};
|
|
||||||
|
|
||||||
info("getting Hetzner Zone ID for %s", $domain);
|
info("getting Hetzner Zone ID for %s", $domain);
|
||||||
|
|
||||||
# Get zone ID
|
# Get zone ID
|
||||||
my $url = "https://$config{$key}{'server'}/zones?name=" . $config{$key}{'zone'};
|
my $url = "https://$config{$domain}{'server'}/zones?name=" . $config{$domain}{'zone'};
|
||||||
|
|
||||||
my $reply = geturl(proxy => opt('proxy'),
|
my $reply = geturl(proxy => opt('proxy'),
|
||||||
url => $url,
|
url => $url,
|
||||||
headers => $headers
|
headers => $headers
|
||||||
);
|
);
|
||||||
|
unless ($reply && header_ok($domain, $reply)) {
|
||||||
|
failed("updating %s: Could not connect to %s.", $domain, $config{$domain}{'server'});
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Strip header
|
||||||
|
$reply =~ qr/{(?:[^{}]*|(?R))*}/mp;
|
||||||
|
my $response = eval {decode_json(${^MATCH})};
|
||||||
|
unless ($response && $response->{zones}) {
|
||||||
|
failed("updating %s: invalid json or result.", $domain);
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Pull the ID out of the json, messy
|
||||||
|
my ($zone_id) = map {$_->{name} eq $config{$domain}{'zone'} ? $_->{id} : ()} @{$response->{zones}};
|
||||||
|
unless ($zone_id) {
|
||||||
|
failed("updating %s: No zone ID found.", $config{$domain}{'zone'});
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
info("Zone ID is %s", $zone_id);
|
||||||
|
|
||||||
|
# IPv4 and IPv6 handling are similar enough to do in a loop...
|
||||||
|
for my $ip ($ipv4, $ipv6) {
|
||||||
|
next if (!$ip);
|
||||||
|
my $ipv = ($ip eq ($ipv6 // '')) ? '6' : '4';
|
||||||
|
my $type = ($ip eq ($ipv6 // '')) ? 'AAAA' : 'A';
|
||||||
|
|
||||||
|
info("updating %s: setting IPv$ipv address to %s", $domain, $ip);
|
||||||
|
$config{$domain}{"status-ipv$ipv"} = 'failed';
|
||||||
|
|
||||||
|
# Get DNS 'A' or 'AAAA' record ID
|
||||||
|
$url = "https://$config{$domain}{'server'}/records?zone_id=$zone_id";
|
||||||
|
$reply = geturl(proxy => opt('proxy'),
|
||||||
|
url => $url,
|
||||||
|
headers => $headers
|
||||||
|
);
|
||||||
unless ($reply && header_ok($domain, $reply)) {
|
unless ($reply && header_ok($domain, $reply)) {
|
||||||
failed("updating %s: Could not connect to %s.", $domain, $config{$key}{'server'});
|
failed("updating %s: Could not connect to %s.", $domain, $config{$domain}{'server'});
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Strip header
|
# Strip header
|
||||||
$reply =~ qr/{(?:[^{}]*|(?R))*}/mp;
|
$reply =~ qr/{(?:[^{}]*|(?R))*}/mp;
|
||||||
my $response = eval {decode_json(${^MATCH})};
|
$response = eval {decode_json(${^MATCH})};
|
||||||
unless ($response && $response->{zones}) {
|
unless ($response && $response->{records}) {
|
||||||
failed("updating %s: invalid json or result.", $domain);
|
failed("updating %s: invalid json or result.", $domain);
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Pull the ID out of the json, messy
|
# Pull the ID out of the json, messy
|
||||||
my ($zone_id) = map {$_->{name} eq $config{$key}{'zone'} ? $_->{id} : ()} @{$response->{zones}};
|
my ($dns_rec_id) = map { ($_->{name} eq $hostname && $_->{type} eq $type) ? $_->{id} : ()} @{$response->{records}};
|
||||||
unless ($zone_id) {
|
|
||||||
failed("updating %s: No zone ID found.", $config{$key}{'zone'});
|
# Set domain
|
||||||
|
my $http_method="";
|
||||||
|
if ($dns_rec_id)
|
||||||
|
{
|
||||||
|
debug("updating %s: DNS '$type' record ID: $dns_rec_id", $domain);
|
||||||
|
$url = "https://$config{$domain}{'server'}/records/$dns_rec_id";
|
||||||
|
$http_method = "PUT";
|
||||||
|
} else {
|
||||||
|
debug("creating %s: DNS '$type'", $domain);
|
||||||
|
$url = "https://$config{$domain}{'server'}/records";
|
||||||
|
$http_method = "POST";
|
||||||
|
}
|
||||||
|
my $data = "{\"zone_id\":\"$zone_id\", \"name\": \"$hostname\", \"value\": \"$ip\", \"type\": \"$type\", \"ttl\": $config{$domain}{'ttl'}}";
|
||||||
|
|
||||||
|
$reply = geturl(proxy => opt('proxy'),
|
||||||
|
url => $url,
|
||||||
|
headers => $headers,
|
||||||
|
method => $http_method,
|
||||||
|
data => $data
|
||||||
|
);
|
||||||
|
unless ($reply && header_ok($domain, $reply)) {
|
||||||
|
failed("updating %s: Could not connect to %s.", $domain, $config{$domain}{'server'});
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
info("Zone ID is %s", $zone_id);
|
# Strip header
|
||||||
|
$reply =~ qr/{(?:[^{}]*|(?R))*}/mp;
|
||||||
# IPv4 and IPv6 handling are similar enough to do in a loop...
|
$response = eval {decode_json(${^MATCH})};
|
||||||
for my $ip ($ipv4, $ipv6) {
|
if ($response && $response->{record}) {
|
||||||
next if (!$ip);
|
success("updating %s: IPv$ipv address set to %s", $domain, $ip);
|
||||||
my $ipv = ($ip eq ($ipv6 // '')) ? '6' : '4';
|
$config{$domain}{"ipv$ipv"} = $ip;
|
||||||
my $type = ($ip eq ($ipv6 // '')) ? 'AAAA' : 'A';
|
$config{$domain}{'mtime'} = $now;
|
||||||
|
$config{$domain}{"status-ipv$ipv"} = 'good';
|
||||||
info("updating %s: setting IPv$ipv address to %s", $domain, $ip);
|
} else {
|
||||||
$config{$domain}{"status-ipv$ipv"} = 'failed';
|
failed("updating %s: invalid json or result.", $domain);
|
||||||
|
|
||||||
# Get DNS 'A' or 'AAAA' record ID
|
|
||||||
$url = "https://$config{$key}{'server'}/records?zone_id=$zone_id";
|
|
||||||
$reply = geturl(proxy => opt('proxy'),
|
|
||||||
url => $url,
|
|
||||||
headers => $headers
|
|
||||||
);
|
|
||||||
unless ($reply && header_ok($domain, $reply)) {
|
|
||||||
failed("updating %s: Could not connect to %s.", $domain, $config{$key}{'server'});
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
# Strip header
|
|
||||||
$reply =~ qr/{(?:[^{}]*|(?R))*}/mp;
|
|
||||||
$response = eval {decode_json(${^MATCH})};
|
|
||||||
unless ($response && $response->{records}) {
|
|
||||||
failed("updating %s: invalid json or result.", $domain);
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
# Pull the ID out of the json, messy
|
|
||||||
my ($dns_rec_id) = map { ($_->{name} eq $hostname && $_->{type} eq $type) ? $_->{id} : ()} @{$response->{records}};
|
|
||||||
|
|
||||||
# Set domain
|
|
||||||
my $http_method="";
|
|
||||||
if ($dns_rec_id)
|
|
||||||
{
|
|
||||||
debug("updating %s: DNS '$type' record ID: $dns_rec_id", $domain);
|
|
||||||
$url = "https://$config{$key}{'server'}/records/$dns_rec_id";
|
|
||||||
$http_method = "PUT";
|
|
||||||
} else {
|
|
||||||
debug("creating %s: DNS '$type'", $domain);
|
|
||||||
$url = "https://$config{$key}{'server'}/records";
|
|
||||||
$http_method = "POST";
|
|
||||||
}
|
|
||||||
my $data = "{\"zone_id\":\"$zone_id\", \"name\": \"$hostname\", \"value\": \"$ip\", \"type\": \"$type\", \"ttl\": $config{$domain}{'ttl'}}";
|
|
||||||
|
|
||||||
$reply = geturl(proxy => opt('proxy'),
|
|
||||||
url => $url,
|
|
||||||
headers => $headers,
|
|
||||||
method => $http_method,
|
|
||||||
data => $data
|
|
||||||
);
|
|
||||||
unless ($reply && header_ok($domain, $reply)) {
|
|
||||||
failed("updating %s: Could not connect to %s.", $domain, $config{$domain}{'server'});
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
# Strip header
|
|
||||||
$reply =~ qr/{(?:[^{}]*|(?R))*}/mp;
|
|
||||||
$response = eval {decode_json(${^MATCH})};
|
|
||||||
if ($response && $response->{record}) {
|
|
||||||
success("updating %s: IPv$ipv address set to %s", $domain, $ip);
|
|
||||||
$config{$domain}{"ipv$ipv"} = $ip;
|
|
||||||
$config{$domain}{'mtime'} = $now;
|
|
||||||
$config{$domain}{"status-ipv$ipv"} = 'good';
|
|
||||||
} else {
|
|
||||||
failed("updating %s: invalid json or result.", $domain);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue