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 while (my($name,$server) = each %{$cluster->{servers}})
27 $server->{cluster} = $cluster;
28 $cluster->{bandwidth} = $cluster->{bandwidth} + $server->{bandwidth};
30 $servers->{$name} = $server;
37 pingdom => $cluster->{pingdom},
38 bandwidth => $cluster->{bandwidth}
41 $cluster->{servers} = {
42 $name => $servers->{$name}
46 $cluster->{name} = $name;
48 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
50 $cluster->{status} = "down";
54 $cluster->{status} = "up";
58 # Initialise server details
59 while (my($name,$server) = each %$servers)
61 $server->{name} = $name;
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 (values %$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");
217 while (my($name,$server) = each %{$cluster->{servers}})
219 if ($server->{status} eq "up")
221 $zonefile->print("C\L$country->{code}\E.${zone}:$server->{name}.${zone}:600\n");
228 type => "LineString",
229 coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ]
232 country => $country->{name},
233 server => $cluster->{name},
234 colour => $cluster->{colour}
239 # Output default records for IPs that can't be mapped to a country
240 foreach my $cluster (grep { $_->{default} } values %$clusters)
242 $zonefile->print("# Unknown countries\n");
244 while (my($name,$server) = each %{$cluster->{servers}})
246 $zonefile->print("Cxx.${zone}:${name}.${zone}:600\n");
250 # Output the GeoJSON text
251 $jsonfile->print(encode_json(\@json));
253 # Close the output files
260 # Find the centre value between two latitudes
267 return ( $south + $north ) / 2;
271 # Find the centre value between two longitudes
281 $lon = ( $west + $east ) / 2;
285 $lon = ( $west + $east + 360 ) / 2;
288 $lon = $lon - 360 if $lon > 180;
294 # Match a country against a cluster
300 my $continent = shift;
303 if ($cluster->{preferred} &&
304 $cluster->{preferred}->{countries} &&
305 grep { $_ eq $country } @{$cluster->{preferred}->{countries}})
307 $match = "preferred";
309 elsif ($cluster->{preferred} &&
310 $cluster->{preferred}->{continents} &&
311 grep { $_ eq $continent } @{$cluster->{preferred}->{continents}})
313 $match = "preferred";
315 elsif ($cluster->{allowed} &&
316 $cluster->{allowed}->{countries} &&
317 grep { $_ eq $country } @{$cluster->{allowed}->{countries}})
321 elsif ($cluster->{allowed} &&
322 $cluster->{allowed}->{continents} &&
323 grep { $_ eq $continent } @{$cluster->{allowed}->{continents}})
327 elsif ($cluster->{denied} &&
328 $cluster->{denied}->{countries} &&
329 grep { $_ eq $country } @{$cluster->{preferred}->{countries}})
333 elsif ($cluster->{denied} &&
334 $cluster->{denied}->{continents} &&
335 grep { $_ eq $continent } @{$cluster->{preferred}->{continents}})
339 elsif ($cluster->{allowed})
352 # Compute the great circle distance between two points
356 my $lat1 = deg2rad(shift);
357 my $lon1 = deg2rad(shift);
358 my $lat2 = deg2rad(shift);
359 my $lon2 = deg2rad(shift);
361 return great_circle_distance($lon1, pip2 - $lat1, $lon2, pip2 - $lat2);
365 # Allocate each country to a cluster
367 sub allocate_clusters
369 my $mappings = shift;
371 # Loop over the mappings, trying to assign each country to the
372 # nearest cluster, but subject to the bandwidth limits
373 foreach my $mapping (sort { $b->{priority} <=> $a->{priority} || $a->{distance} <=> $b->{distance} } @$mappings)
375 my $country = $mapping->{country};
376 my $cluster = $mapping->{cluster};
378 if (!exists($country->{cluster}) &&
379 $cluster->{bandwidth_used} + $country->{bandwidth} <= $cluster->{bandwidth_limit})
381 $country->{cluster} = $cluster;
382 $cluster->{bandwidth_used} = $cluster->{bandwidth_used} + $country->{bandwidth};