X-Git-Url: https://git.openstreetmap.org./dns.git/blobdiff_plain/f940aa881391ea62e9622c7068255c612238743d..fce2906221cf3e9cd6a33b6fa755c56a502488c1:/bin/mkgeo?ds=sidebyside diff --git a/bin/mkgeo b/bin/mkgeo index 2e0ac00..d942ee0 100755 --- a/bin/mkgeo +++ b/bin/mkgeo @@ -14,6 +14,7 @@ use YAML; my $originfile = shift @ARGV; my $clusterfile = shift @ARGV; my $zone = shift @ARGV; +my $jszone = shift @ARGV; my $targetoriginfile = shift @ARGV; my $origins = YAML::LoadFile($originfile); my $clusters = YAML::LoadFile($clusterfile); @@ -205,60 +206,70 @@ my @json; # Open output files my $zonefile = IO::File->new("> data/${zone}") || die "$!"; +my $jszonefile = IO::File->new("> include/${jszone}.js") || die "$!"; my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!"; -# Output details for each country -foreach my $origin (sort { $a->{name} cmp $b->{name} } values %$origins) -{ - my $cluster = $origin->{cluster}; - my $clon = $origin->{lon}; - my $clat = $origin->{lat}; - my $slon = $cluster->{lon}; - my $slat = $cluster->{lat}; +# Output headers +$jszonefile->print("var \U${jszone}\E_RECORDS = [\n"); - if ($clon > 0 && $slon < 0 && 360 + $slon - $clon < $clon - $slon) - { - $slon = $slon + 360; - } - elsif ($slon > 0 && $clon < 0 && 360 + $clon - $slon < $slon - $clon) +# Skip outputting server map if gdnsd is doing that +if (!defined($gdnsname)) +{ + # Output details for each country + foreach my $origin (sort { $a->{name} cmp $b->{name} } values %$origins) { - $clon = $clon + 360; - } + my $cluster = $origin->{cluster}; + my $clon = $origin->{lon}; + my $clat = $origin->{lat}; + my $slon = $cluster->{lon}; + my $slat = $cluster->{lat}; - $zonefile->print("# $origin->{name}\n"); - $zonefile->print("C\L$origin->{code}\E.${zone}:$cluster->{name}.${zone}:600\n"); - - push @json, { - type => "Feature", - geometry => { - type => "LineString", - coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ] - }, - properties => { - origin => $origin->{name}, - server => $cluster->{name}, - colour => $cluster->{colour} + if ($clon > 0 && $slon < 0 && 360 + $slon - $clon < $clon - $slon) + { + $slon = $slon + 360; + } + elsif ($slon > 0 && $clon < 0 && 360 + $clon - $slon < $slon - $clon) + { + $clon = $clon + 360; } - }; - $targetorigins->{$cluster->{name}}->{bandwidth} += $origin->{bandwidth}; -} + $zonefile->print("# $origin->{name}\n"); + $zonefile->print("C\L$origin->{code}\E.${zone}:$cluster->{name}.${zone}:600\n"); + + $jszonefile->print(" CNAME(\"\L$origin->{code}\E.${jszone}\", \"$cluster->{name}.${zone}.\", TTL(\"10m\")),\n"); + + push @json, { + type => "Feature", + geometry => { + type => "LineString", + coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ] + }, + properties => { + origin => $origin->{name}, + server => $cluster->{name}, + colour => $cluster->{colour} + } + }; -# Header for default records -$zonefile->print("# Unknown origins\n"); + $targetorigins->{$cluster->{name}}->{bandwidth} += $origin->{bandwidth}; + } -# Output default records for IPs that can't be mapped to a country -foreach my $cluster (sort { $a->{name} cmp $b->{name} } values %$clusters) -{ - my $name = $cluster->{name}; + # Header for default records + $zonefile->print("# Unknown origins\n"); - if (my $default = $cluster->{default}) - { - output_server($zonefile, "${default}.${zone}", $cluster); - } - elsif (exists($cluster->{default})) + # Output default records for IPs that can't be mapped to a country + foreach my $cluster (sort { $a->{name} cmp $b->{name} } values %$clusters) { - output_server($zonefile, "${zone}", $cluster); + my $name = $cluster->{name}; + + if (my $default = $cluster->{default}) + { + output_server($zonefile, $jszonefile, "${default}.${zone}", "${default}.${jszone}", $cluster); + } + elsif (exists($cluster->{default})) + { + output_server($zonefile, $jszonefile, "${zone}", "${jszone}", $cluster); + } } } @@ -270,19 +281,23 @@ foreach my $cluster (sort { $a->{name} cmp $b->{name} } values %$clusters) { my $name = $cluster->{name}; - output_server($zonefile, "${name}.${zone}", $cluster); + output_server($zonefile, $jszonefile, "${name}.${zone}", "${name}.${jszone}", $cluster); if (@{$cluster->{servers}} > 1) { - output_server($zonefile, "${name}-%02d.${zone}", $cluster); + output_server($zonefile, $jszonefile, "${name}-%02d.${zone}", "${name}-%02d.${jszone}", $cluster); } } # Output the GeoJSON text $jsonfile->print(encode_json(\@json)); +# Output footers +$jszonefile->print("];\n"); + # Close the output files $jsonfile->close(); +$jszonefile->close(); $zonefile->close(); # Output gdnsd configuration @@ -294,7 +309,7 @@ if (defined($gdnsname)) my $continent = ""; $gdnsmapfile->print("${gdnsname} => {\n"); - $gdnsmapfile->print(" geoip2_db => /var/lib/GeoIP/GeoLite2-Country.mmdb\n"); + $gdnsmapfile->print(" geoip2_db => /usr/share/GeoIP/GeoLite2-Country.mmdb\n"); $gdnsmapfile->print(" datacenters => [" . join(",", sort(keys(%$clusters))) . "]\n"); $gdnsmapfile->print(" map => {\n"); $gdnsmapfile->print(" default => [" . join(",", sort(map { $_->{name} } grep { $_->{default} } values(%$clusters))) . "]\n"); @@ -331,8 +346,6 @@ if (defined($gdnsname)) { my $name = $cluster->{name}; - $gdnsresourcefile->print(" ${name} => ${name}.${zone}.\n"); - if (@{$cluster->{servers}} > 1) { $gdnsweightedfile->print("${name} => {\n"); @@ -343,12 +356,18 @@ if (defined($gdnsname)) { my $number = sprintf("%02d", $index + 1); my $bandwidth = $server->{bandwidth}; - + $gdnsweightedfile->print(" ${name}-${number} = [ ${name}-${number}.${zone}., ${bandwidth} ]\n"); } } $gdnsweightedfile->print("}\n"); + + $gdnsresourcefile->print(" ${name} => %weighted!${name}\n"); + } + else + { + $gdnsresourcefile->print(" ${name} => ${name}.${zone}.\n"); } } @@ -513,7 +532,9 @@ sub compare_bandwidth sub output_server { my $zonefile = shift; + my $jszonefile = shift; my $name = shift; + my $jsname = shift; my $cluster = shift; while (my($index,$server) = each @{$cluster->{servers}}) @@ -521,10 +542,16 @@ sub output_server if ($server->{status} eq "up") { $zonefile->printf("+${name}:$server->{ipv4}:600\n", $index + 1); + $jszonefile->printf(" A(\"${jsname}\", \"$server->{ipv4}\", TTL(\"10m\")),\n", $index + 1); if ($server->{ipv6}) { -# $zonefile->printf("3${name}:$server->{ipv6}:600\n", $index + 1); + my $ipv6 = $server->{ipv6}; + + $ipv6 =~ s/([0-9a-f]{4})(?=.)/$1:/ig; + + $zonefile->printf("3${name}:$server->{ipv6}:600\n", $index + 1); + $jszonefile->printf(" AAAA(\"${jsname}\", \"${ipv6}\", TTL(\"10m\")),\n", $index + 1); } } }