X-Git-Url: https://git.openstreetmap.org/dns.git/blobdiff_plain/f054b3702b5d3ac235c9259233244f7c4933cb5c..c7a0a974efe201eb2fdc5d4921230d689bcf2331:/bin/mkgeo diff --git a/bin/mkgeo b/bin/mkgeo index a47ea81..b2c1d4f 100755 --- a/bin/mkgeo +++ b/bin/mkgeo @@ -7,7 +7,6 @@ use IO::File; use Math::Trig qw(deg2rad pip2 great_circle_distance); use JSON::XS; use LWP::UserAgent; -use XML::Writer; use XML::TreeBuilder; use YAML; @@ -19,7 +18,8 @@ my $servers = YAML::LoadFile("src/${source}"); while (my($name,$server) = each %$servers) { $server->{name} = $name; - $server->{bandwidth} = $server->{bandwidth} * 1024 * 1024; + $server->{bandwidth_limit} = $server->{bandwidth} * 1024 * 1024; + $server->{bandwidth_used} = 0; if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD}) { @@ -107,39 +107,37 @@ foreach my $country ($countries->look_down("_tag" => "country")) # Discard the parsed country database $countries->delete; -# Loop over the mappings, trying to assign each country to the -# nearest server, but subject to the bandwidth limits; -foreach my $mapping (sort { $b->{priority} <=> $a->{priority} || $a->{distance} <=> $b->{distance} } @mappings) -{ - my $country = $mapping->{country}; - my $server = $mapping->{server}; +# Allocate each country to a server +allocate_servers(\@mappings); - if ($country->{bandwidth} <= $server->{bandwidth} && !exists($country->{server})) +# If we failed to allocate every country then loop, increasing +# the bandwidth for each server by a little and retrying until +# we manage to allocate everything +while (grep { !exists($_->{server}) } values %countries) +{ + # Clear any existing mappings of countries to servers + foreach my $country (values %countries) { - $country->{server} = $server; - $server->{bandwidth} = $server->{bandwidth} - $country->{bandwidth}; + delete $country->{server}; } -} -# Loop over the mappings again, assigning anything that is left -# as best we can, and allowing bandwidth limits to be exeeded -foreach my $mapping (sort { $b->{priority} <=> $a->{priority} || $a->{distance} <=> $b->{distance} } @mappings) -{ - my $country = $mapping->{country}; - my $server = $mapping->{server}; + # Reset bandwidth usage for servers and increase limits by 10% + foreach my $server (values %$servers) + { + $server->{bandwidth_used} = 0; + $server->{bandwidth_limit} = $server->{bandwidth_limit} * 1.1; + } - $country->{server} = $server unless exists($country->{server}); + # Try the allocate again + allocate_servers(\@mappings); } +# Create JSON collection object +my @json; + # Open output files my $zonefile = IO::File->new("> data/${zone}") || die "$!"; -my $kmlfile = IO::File->new("> kml/${zone}.kml") || die "$!"; -my $kmlwriter = XML::Writer->new(OUTPUT => $kmlfile, ENCODING => 'utf-8'); - -# Output the KML header -$kmlwriter->xmlDecl(); -$kmlwriter->startTag("kml", "xmlns" => "http://www.opengis.net/kml/2.2"); -$kmlwriter->startTag("Document"); +my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!"; # Output details for each country foreach my $country (values %countries) @@ -152,21 +150,28 @@ foreach my $country (values %countries) if ($clon > 0 && $slon < 0 && 360 + $slon - $clon < $clon - $slon) { - $clon = $clon - 360; + $slon = $slon + 360; } elsif ($slon > 0 && $clon < 0 && 360 + $clon - $slon < $slon - $clon) { - $slon = $slon - 360; + $clon = $clon + 360; } + $zonefile->print("# $country->{name}\n"); $zonefile->print("C\L$country->{code}\E.${zone}:$server->{name}.${zone}:600\n"); - $kmlwriter->startTag("Placemark"); - $kmlwriter->dataElement("name", $country->{name}); - $kmlwriter->startTag("LineString"); - $kmlwriter->dataElement("coordinates", "$clon,$clat $slon,$slat"); - $kmlwriter->endTag("LineString"); - $kmlwriter->endTag("Placemark"); + push @json, { + type => "Feature", + geometry => { + type => "LineString", + coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ] + }, + properties => { + country => $country->{name}, + server => $server->{name}, + colour => $server->{colour} + } + }; } # Output default records for IPs that can't be mapped to a country @@ -175,13 +180,11 @@ foreach my $server (grep { $servers->{$_}->{default} } keys %$servers) $zonefile->print("Cxx.${zone}:${server}.${zone}:600\n"); } -# End the KML file -$kmlwriter->endTag("Document"); -$kmlwriter->endTag("kml"); -$kmlwriter->end(); +# Output the GeoJSON text +$jsonfile->print(encode_json(\@json)); # Close the output files -$kmlfile->close(); +$jsonfile->close(); $zonefile->close(); exit 0; @@ -290,3 +293,28 @@ sub distance return great_circle_distance($lon1, pip2 - $lat1, $lon2, pip2 - $lat2); } + +# +# Allocate each country to a server +# +sub allocate_servers +{ + my $mappings = shift; + + # Loop over the mappings, trying to assign each country to the + # nearest server, but subject to the bandwidth limits + foreach my $mapping (sort { $b->{priority} <=> $a->{priority} || $a->{distance} <=> $b->{distance} } @$mappings) + { + my $country = $mapping->{country}; + my $server = $mapping->{server}; + + if (!exists($country->{server}) && + $server->{bandwidth_used} + $country->{bandwidth} <= $server->{bandwidth_limit}) + { + $country->{server} = $server; + $server->{bandwidth_used} = $server->{bandwidth_used} + $country->{bandwidth}; + } + } + + return; +}