7 use Math::Trig qw(deg2rad pip2 great_circle_distance);
 
  13 my $source = shift @ARGV;
 
  14 my $zone = shift @ARGV;
 
  15 my $servers = YAML::LoadFile("src/${source}");
 
  17 # Initialise server details
 
  18 while (my($name,$server) = each %$servers)
 
  20     $server->{name} = $name;
 
  21     $server->{bandwidth_limit} = $server->{bandwidth} * 1024 * 1024;
 
  22     $server->{bandwidth_used} = 0;
 
  24     if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
 
  26         $server->{status} = "down";
 
  30         $server->{status} = "up";
 
  34 # If pingdom support is enabled then check which servers are up
 
  35 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
 
  37     my $ua = LWP::UserAgent->new;
 
  39     $ua->default_header("App-Key", "2cohi62u5haxvqmypk3ljqqrze1jufrh");
 
  40     $ua->credentials("api.pingdom.com:443", "Pingdom API", $ENV{PINGDOM_USERNAME}, $ENV{PINGDOM_PASSWORD});
 
  42     foreach my $server (values %$servers)
 
  44         if (my $checkid = $server->{pingdom})
 
  46             my $response = $ua->get("https://api.pingdom.com/api/2.0/checks/${checkid}");
 
  48             if ($response->is_success)
 
  50                 my $check = decode_json($response->content);
 
  52                 $server->{status} = $check->{check}->{status};
 
  61 # Create a parser for the country database
 
  62 my $countries = XML::TreeBuilder->new;
 
  64 # Parse the country database
 
  65 $countries->parsefile("lib/countries.xml");
 
  67 # Load the per-country bandwidth details
 
  68 my $bandwidth = YAML::LoadFile("bandwidth/${source}.yml");
 
  70 # Fill in country table and work out which servers each can use
 
  71 foreach my $country ($countries->look_down("_tag" => "country"))
 
  73     my $code = $country->look_down("_tag" => "countryCode")->as_text;
 
  74     my $name = $country->look_down("_tag" => "countryName")->as_text;
 
  75     my $population = $country->look_down("_tag" => "population")->as_text;
 
  76     my $bandwidth = $bandwidth->{$code} || 0;
 
  77     my $continent = $country->look_down("_tag" => "continent")->as_text;
 
  78     my $west = $country->look_down("_tag" => "west")->as_text;
 
  79     my $north = $country->look_down("_tag" => "north")->as_text;
 
  80     my $east = $country->look_down("_tag" => "east")->as_text;
 
  81     my $south = $country->look_down("_tag" => "south")->as_text;
 
  82     my $lat = centre_lat( $south, $north );
 
  83     my $lon = centre_lon( $west, $east );
 
  86         code => $code, name => $name, continent => $continent,
 
  87         bandwidth => $bandwidth, lat => $lat, lon => $lon
 
  90     foreach my $server (values %$servers)
 
  92         my $match = match_country($server, $code, $continent);
 
  94         if ($server->{status} eq "up" && $match ne "denied")
 
  96             my $priority = $match eq "preferred" ? 20 : 10;
 
  97             my $distance = distance($lat, $lon, $server->{lat}, $server->{lon});
 
 100                 country => $countries{$code}, server => $server,
 
 101                 priority => $priority, distance => $distance
 
 107 # Discard the parsed country database
 
 110 # Allocate each country to a server
 
 111 allocate_servers(\@mappings);
 
 113 # If we failed to allocate every country then loop, increasing
 
 114 # the bandwidth for each server by a little and retrying until
 
 115 # we manage to allocate everything
 
 116 while (grep { !exists($_->{server}) } values %countries)
 
 118     # Clear any existing mappings of countries to servers
 
 119     foreach my $country (values %countries)
 
 121         delete $country->{server};
 
 124     # Reset bandwidth usage for servers and increase limits by 10%
 
 125     foreach my $server (values %$servers)
 
 127         $server->{bandwidth_used} = 0;
 
 128         $server->{bandwidth_limit} = $server->{bandwidth_limit} * 1.1;
 
 131     # Try the allocate again
 
 132     allocate_servers(\@mappings);
 
 135 # Create JSON collection object
 
 139 my $zonefile = IO::File->new("> data/${zone}") || die "$!";
 
 140 my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!";
 
 142 # Output details for each country
 
 143 foreach my $country (values %countries)
 
 145     my $server = $country->{server};
 
 146     my $clon = $country->{lon};
 
 147     my $clat = $country->{lat};
 
 148     my $slon = $server->{lon};
 
 149     my $slat = $server->{lat};
 
 151     if ($clon > 0 && $slon < 0 && 360 + $slon - $clon < $clon - $slon)
 
 155     elsif ($slon > 0 && $clon < 0 && 360 + $clon - $slon < $slon - $clon)
 
 160     $zonefile->print("# $country->{name}\n");
 
 161     $zonefile->print("C\L$country->{code}\E.${zone}:$server->{name}.${zone}:600\n");
 
 166             type => "LineString",
 
 167             coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ]
 
 170             country => $country->{name},
 
 171             server => $server->{name},
 
 172             colour => $server->{colour}
 
 177 # Output default records for IPs that can't be mapped to a country
 
 178 foreach my $server (grep { $servers->{$_}->{default} } keys %$servers)
 
 180     $zonefile->print("Cxx.${zone}:${server}.${zone}:600\n");
 
 183 # Output the GeoJSON text
 
 184 $jsonfile->print(encode_json(\@json));
 
 186 # Close the output files
 
 193 # Find the centre value between two latitudes
 
 200     return ( $south + $north ) / 2;
 
 204 # Find the centre value between two longitudes
 
 214         $lon = ( $west + $east ) / 2;
 
 218         $lon = ( $west + $east + 360 ) / 2;
 
 221     $lon = $lon - 360 if $lon > 180;
 
 227 # Match a country against a server
 
 233     my $continent = shift;
 
 236     if ($server->{preferred} &&
 
 237         $server->{preferred}->{countries} &&
 
 238         grep { $_ eq $country } @{$server->{preferred}->{countries}})
 
 240         $match = "preferred";
 
 242     elsif ($server->{preferred} &&
 
 243            $server->{preferred}->{continents} &&
 
 244            grep { $_ eq $continent } @{$server->{preferred}->{continents}})
 
 246         $match = "preferred";
 
 248     elsif ($server->{allowed} &&
 
 249            $server->{allowed}->{countries} &&
 
 250            grep { $_ eq $country } @{$server->{allowed}->{countries}})
 
 254     elsif ($server->{allowed} &&
 
 255            $server->{allowed}->{continents} &&
 
 256            grep { $_ eq $continent } @{$server->{allowed}->{continents}})
 
 260     elsif ($server->{denied} &&
 
 261         $server->{denied}->{countries} &&
 
 262         grep { $_ eq $country } @{$server->{preferred}->{countries}})
 
 266     elsif ($server->{denied} &&
 
 267            $server->{denied}->{continents} &&
 
 268            grep { $_ eq $continent } @{$server->{preferred}->{continents}})
 
 272     elsif ($server->{allowed})
 
 285 # Compute the great circle distance between two points
 
 289     my $lat1 = deg2rad(shift);
 
 290     my $lon1 = deg2rad(shift);
 
 291     my $lat2 = deg2rad(shift);
 
 292     my $lon2 = deg2rad(shift);
 
 294     return great_circle_distance($lon1, pip2 - $lat1, $lon2, pip2 - $lat2);
 
 298 # Allocate each country to a server
 
 302     my $mappings = shift;
 
 304     # Loop over the mappings, trying to assign each country to the
 
 305     # nearest server, but subject to the bandwidth limits
 
 306     foreach my $mapping (sort {  $b->{priority} <=> $a->{priority} || $a->{distance} <=> $b->{distance} } @$mappings)
 
 308         my $country = $mapping->{country};
 
 309         my $server = $mapping->{server};
 
 311         if (!exists($country->{server}) &&
 
 312             $server->{bandwidth_used} + $country->{bandwidth} <= $server->{bandwidth_limit})
 
 314             $country->{server} = $server;
 
 315             $server->{bandwidth_used} = $server->{bandwidth_used} + $country->{bandwidth};