7 use Math::Trig qw(deg2rad pip2 great_circle_distance);
13 my $source = shift @ARGV;
14 my $zone = shift @ARGV;
15 my $clusters = YAML::LoadFile("src/${source}");
18 # Initialise cluster details
19 while (my($name,$cluster) = each %$clusters)
21 if ($cluster->{servers})
23 $cluster->{bandwidth} = 0;
25 foreach my $server (@{$cluster->{servers}})
27 $server->{cluster} = $cluster;
28 $cluster->{bandwidth} = $cluster->{bandwidth} + $server->{bandwidth};
30 push @servers, $server;
37 pingdom => $cluster->{pingdom},
38 bandwidth => $cluster->{bandwidth},
39 ipv4 => $cluster->{ipv4},
40 ipv6 => $cluster->{ipv6}
43 $cluster->{servers} = [ $server ];
45 push @servers, $server;
48 $cluster->{name} = $name;
50 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
52 $cluster->{status} = "down";
56 $cluster->{status} = "up";
60 # Initialise server details
61 foreach my $server (@servers)
63 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
65 $server->{status} = "down";
69 $server->{status} = "up";
73 # If pingdom support is enabled then check which servers are up
74 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
76 my $ua = LWP::UserAgent->new;
78 $ua->default_header("App-Key", "2cohi62u5haxvqmypk3ljqqrze1jufrh");
79 $ua->credentials("api.pingdom.com:443", "Pingdom API", $ENV{PINGDOM_USERNAME}, $ENV{PINGDOM_PASSWORD});
81 foreach my $server (@servers)
83 if (my $checkid = $server->{pingdom})
85 my $response = $ua->get("https://api.pingdom.com/api/2.0/checks/${checkid}");
87 if ($response->is_success)
89 my $check = decode_json($response->content);
91 $server->{status} = $check->{check}->{status};
93 if ($server->{status} eq "up")
95 $server->{cluster}->{status} = "up";
99 $server->{cluster}->{bandwidth} = $server->{cluster}->{bandwidth} - $server->{bandwidth};
106 # Initialise cluster details
107 while (my($name,$cluster) = each %$clusters)
109 $cluster->{bandwidth_limit} = $cluster->{bandwidth} * 1024 * 1024;
110 $cluster->{bandwidth_used} = 0;
116 # Create a parser for the country database
117 my $countries = XML::TreeBuilder->new;
119 # Parse the country database
120 $countries->parsefile("lib/countries.xml");
122 # Load the per-country bandwidth details
123 my $bandwidth = YAML::LoadFile("bandwidth/${source}.yml");
125 # Fill in country table and work out which clusters each can use
126 foreach my $country ($countries->look_down("_tag" => "country"))
128 my $code = $country->look_down("_tag" => "countryCode")->as_text;
129 my $name = $country->look_down("_tag" => "countryName")->as_text;
130 my $population = $country->look_down("_tag" => "population")->as_text;
131 my $bandwidth = $bandwidth->{$code} || 0;
132 my $continent = $country->look_down("_tag" => "continent")->as_text;
133 my $west = $country->look_down("_tag" => "west")->as_text;
134 my $north = $country->look_down("_tag" => "north")->as_text;
135 my $east = $country->look_down("_tag" => "east")->as_text;
136 my $south = $country->look_down("_tag" => "south")->as_text;
137 my $lat = centre_lat( $south, $north );
138 my $lon = centre_lon( $west, $east );
140 $countries{$code} = {
141 code => $code, name => $name, continent => $continent,
142 bandwidth => $bandwidth, lat => $lat, lon => $lon
145 foreach my $cluster (values %$clusters)
147 my $match = match_country($cluster, $code, $continent);
149 if ($cluster->{status} eq "up" && $match ne "denied")
151 my $priority = $match eq "preferred" ? 20 : 10;
152 my $distance = distance($lat, $lon, $cluster->{lat}, $cluster->{lon});
155 country => $countries{$code}, cluster => $cluster,
156 priority => $priority, distance => $distance
162 # Discard the parsed country database
165 # Allocate each country to a cluster
166 allocate_clusters(\@mappings);
168 # If we failed to allocate every country then loop, increasing
169 # the bandwidth for each cluster by a little and retrying until
170 # we manage to allocate everything
171 while (grep { !exists($_->{cluster}) } values %countries)
173 # Clear any existing mappings of countries to clusters
174 foreach my $country (values %countries)
176 delete $country->{cluster};
179 # Reset bandwidth usage for clusters and increase limits by 10%
180 foreach my $cluster (values %$clusters)
182 $cluster->{bandwidth_used} = 0;
183 $cluster->{bandwidth_limit} = $cluster->{bandwidth_limit} * 1.1;
186 # Try the allocate again
187 allocate_clusters(\@mappings);
190 # Create JSON collection object
194 my $zonefile = IO::File->new("> data/${zone}") || die "$!";
195 my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!";
197 # Output details for each country
198 foreach my $country (values %countries)
200 my $cluster = $country->{cluster};
201 my $clon = $country->{lon};
202 my $clat = $country->{lat};
203 my $slon = $cluster->{lon};
204 my $slat = $cluster->{lat};
206 if ($clon > 0 && $slon < 0 && 360 + $slon - $clon < $clon - $slon)
210 elsif ($slon > 0 && $clon < 0 && 360 + $clon - $slon < $slon - $clon)
215 $zonefile->print("# $country->{name}\n");
216 $zonefile->print("C\L$country->{code}\E.${zone}:$cluster->{name}.${zone}:600\n");
221 type => "LineString",
222 coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ]
225 country => $country->{name},
226 server => $cluster->{name},
227 colour => $cluster->{colour}
232 # Output default records for IPs that can't be mapped to a country
233 foreach my $cluster (grep { $clusters->{$_}->{default} } keys %$clusters)
235 $zonefile->print("# Unknown countries\n");
236 $zonefile->print("Cxx.${zone}:${cluster}.${zone}:600\n");
239 $zonefile->print("# Servers\n");
241 # Output A records for each cluster
242 while (my($name,$cluster) = each %$clusters)
244 foreach my $server (@{$cluster->{servers}})
246 if ($server->{status} eq "up")
248 $zonefile->print("+${name}.${zone}:$server->{ipv4}:600\n");
252 # $zonefile->print("3${name}.${zone}:$server->{ipv6}:600\n");
258 # Output the GeoJSON text
259 $jsonfile->print(encode_json(\@json));
261 # Close the output files
268 # Find the centre value between two latitudes
275 return ( $south + $north ) / 2;
279 # Find the centre value between two longitudes
289 $lon = ( $west + $east ) / 2;
293 $lon = ( $west + $east + 360 ) / 2;
296 $lon = $lon - 360 if $lon > 180;
302 # Match a country against a cluster
308 my $continent = shift;
311 if ($cluster->{preferred} &&
312 $cluster->{preferred}->{countries} &&
313 grep { $_ eq $country } @{$cluster->{preferred}->{countries}})
315 $match = "preferred";
317 elsif ($cluster->{preferred} &&
318 $cluster->{preferred}->{continents} &&
319 grep { $_ eq $continent } @{$cluster->{preferred}->{continents}})
321 $match = "preferred";
323 elsif ($cluster->{allowed} &&
324 $cluster->{allowed}->{countries} &&
325 grep { $_ eq $country } @{$cluster->{allowed}->{countries}})
329 elsif ($cluster->{allowed} &&
330 $cluster->{allowed}->{continents} &&
331 grep { $_ eq $continent } @{$cluster->{allowed}->{continents}})
335 elsif ($cluster->{denied} &&
336 $cluster->{denied}->{countries} &&
337 grep { $_ eq $country } @{$cluster->{preferred}->{countries}})
341 elsif ($cluster->{denied} &&
342 $cluster->{denied}->{continents} &&
343 grep { $_ eq $continent } @{$cluster->{preferred}->{continents}})
347 elsif ($cluster->{allowed})
360 # Compute the great circle distance between two points
364 my $lat1 = deg2rad(shift);
365 my $lon1 = deg2rad(shift);
366 my $lat2 = deg2rad(shift);
367 my $lon2 = deg2rad(shift);
369 return great_circle_distance($lon1, pip2 - $lat1, $lon2, pip2 - $lat2);
373 # Allocate each country to a cluster
375 sub allocate_clusters
377 my $mappings = shift;
379 # Loop over the mappings, trying to assign each country to the
380 # nearest cluster, but subject to the bandwidth limits
381 foreach my $mapping (sort { $b->{priority} <=> $a->{priority} || $a->{distance} <=> $b->{distance} } @$mappings)
383 my $country = $mapping->{country};
384 my $cluster = $mapping->{cluster};
386 if (!exists($country->{cluster}) &&
387 $cluster->{bandwidth_used} + $country->{bandwidth} <= $cluster->{bandwidth_limit})
389 $country->{cluster} = $cluster;
390 $cluster->{bandwidth_used} = $cluster->{bandwidth_used} + $country->{bandwidth};