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} = $server->{bandwidth} * 1024 * 1024;
23 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
25 $server->{status} = "down";
29 $server->{status} = "up";
33 # If pingdom support is enabled then check which servers are up
34 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
36 my $ua = LWP::UserAgent->new;
38 $ua->default_header("App-Key", "2cohi62u5haxvqmypk3ljqqrze1jufrh");
39 $ua->credentials("api.pingdom.com:443", "Pingdom API", $ENV{PINGDOM_USERNAME}, $ENV{PINGDOM_PASSWORD});
41 foreach my $server (values %$servers)
43 if (my $checkid = $server->{pingdom})
45 my $response = $ua->get("https://api.pingdom.com/api/2.0/checks/${checkid}");
47 if ($response->is_success)
49 my $check = decode_json($response->content);
51 $server->{status} = $check->{check}->{status};
60 # Create a parser for the country database
61 my $countries = XML::TreeBuilder->new;
63 # Parse the country database
64 $countries->parsefile("lib/countries.xml");
66 # Load the per-country bandwidth details
67 my $bandwidth = YAML::LoadFile("bandwidth/${source}.yml");
69 # Fill in country table and work out which servers each can use
70 foreach my $country ($countries->look_down("_tag" => "country"))
72 my $code = $country->look_down("_tag" => "countryCode")->as_text;
73 my $name = $country->look_down("_tag" => "countryName")->as_text;
74 my $population = $country->look_down("_tag" => "population")->as_text;
75 my $bandwidth = $bandwidth->{$code} || 0;
76 my $continent = $country->look_down("_tag" => "continent")->as_text;
77 my $west = $country->look_down("_tag" => "west")->as_text;
78 my $north = $country->look_down("_tag" => "north")->as_text;
79 my $east = $country->look_down("_tag" => "east")->as_text;
80 my $south = $country->look_down("_tag" => "south")->as_text;
81 my $lat = centre_lat( $south, $north );
82 my $lon = centre_lon( $west, $east );
85 code => $code, name => $name, continent => $continent,
86 bandwidth => $bandwidth, lat => $lat, lon => $lon
89 foreach my $server (values %$servers)
91 my $match = match_country($server, $code, $continent);
93 if ($server->{status} eq "up" && $match ne "denied")
95 my $priority = $match eq "preferred" ? 20 : 10;
96 my $distance = distance($lat, $lon, $server->{lat}, $server->{lon});
99 country => $countries{$code}, server => $server,
100 priority => $priority, distance => $distance
106 # Discard the parsed country database
109 # Loop over the mappings, trying to assign each country to the
110 # nearest server, but subject to the bandwidth limits;
111 foreach my $mapping (sort { $b->{priority} <=> $a->{priority} || $a->{distance} <=> $b->{distance} } @mappings)
113 my $country = $mapping->{country};
114 my $server = $mapping->{server};
116 if ($country->{bandwidth} <= $server->{bandwidth} && !exists($country->{server}))
118 $country->{server} = $server;
119 $server->{bandwidth} = $server->{bandwidth} - $country->{bandwidth};
123 # Loop over the mappings again, assigning anything that is left
124 # as best we can, and allowing bandwidth limits to be exeeded
125 foreach my $mapping (sort { $b->{priority} <=> $a->{priority} || $a->{distance} <=> $b->{distance} } @mappings)
127 my $country = $mapping->{country};
128 my $server = $mapping->{server};
130 $country->{server} = $server unless exists($country->{server});
133 # Create JSON collection object
137 my $zonefile = IO::File->new("> data/${zone}") || die "$!";
138 my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!";
140 # Output details for each country
141 foreach my $country (values %countries)
143 my $server = $country->{server};
144 my $clon = $country->{lon};
145 my $clat = $country->{lat};
146 my $slon = $server->{lon};
147 my $slat = $server->{lat};
149 if ($clon > 0 && $slon < 0 && 360 + $slon - $clon < $clon - $slon)
153 elsif ($slon > 0 && $clon < 0 && 360 + $clon - $slon < $slon - $clon)
158 $zonefile->print("C\L$country->{code}\E.${zone}:$server->{name}.${zone}:600\n");
163 type => "LineString",
164 coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ]
167 country => $country->{name}
172 # Output default records for IPs that can't be mapped to a country
173 foreach my $server (grep { $servers->{$_}->{default} } keys %$servers)
175 $zonefile->print("Cxx.${zone}:${server}.${zone}:600\n");
178 # Output the GeoJSON text
179 $jsonfile->print(encode_json(\@json));
181 # Close the output files
188 # Find the centre value between two latitudes
195 return ( $south + $north ) / 2;
199 # Find the centre value between two longitudes
209 $lon = ( $west + $east ) / 2;
213 $lon = ( $west + $east + 360 ) / 2;
216 $lon = $lon - 360 if $lon > 180;
222 # Match a country against a server
228 my $continent = shift;
231 if ($server->{preferred} &&
232 $server->{preferred}->{countries} &&
233 grep { $_ eq $country } @{$server->{preferred}->{countries}})
235 $match = "preferred";
237 elsif ($server->{preferred} &&
238 $server->{preferred}->{continents} &&
239 grep { $_ eq $continent } @{$server->{preferred}->{continents}})
241 $match = "preferred";
243 elsif ($server->{allowed} &&
244 $server->{allowed}->{countries} &&
245 grep { $_ eq $country } @{$server->{allowed}->{countries}})
249 elsif ($server->{allowed} &&
250 $server->{allowed}->{continents} &&
251 grep { $_ eq $continent } @{$server->{allowed}->{continents}})
255 elsif ($server->{denied} &&
256 $server->{denied}->{countries} &&
257 grep { $_ eq $country } @{$server->{preferred}->{countries}})
261 elsif ($server->{denied} &&
262 $server->{denied}->{continents} &&
263 grep { $_ eq $continent } @{$server->{preferred}->{continents}})
267 elsif ($server->{allowed})
280 # Compute the great circle distance between two points
284 my $lat1 = deg2rad(shift);
285 my $lon1 = deg2rad(shift);
286 my $lat2 = deg2rad(shift);
287 my $lon2 = deg2rad(shift);
289 return great_circle_distance($lon1, pip2 - $lat1, $lon2, pip2 - $lat2);