7 use Math::Trig qw(deg2rad pip2 great_circle_distance);
9 use LWP::UserAgent::Determined;
12 my $originfile = shift @ARGV;
13 my $clusterfile = shift @ARGV;
14 my $zone = shift @ARGV;
15 my $targetoriginfile = shift @ARGV;
16 my $origins = YAML::LoadFile($originfile);
17 my $clusters = YAML::LoadFile($clusterfile);
20 # Initialise cluster details
21 while (my($name,$cluster) = each %$clusters)
23 if ($cluster->{servers})
25 $cluster->{bandwidth} = 0;
27 foreach my $server (@{$cluster->{servers}})
29 $server->{cluster} = $cluster;
30 $cluster->{bandwidth} = $cluster->{bandwidth} + $server->{bandwidth};
32 push @servers, $server;
39 pingdom => $cluster->{pingdom},
40 bandwidth => $cluster->{bandwidth},
41 ipv4 => $cluster->{ipv4},
42 ipv6 => $cluster->{ipv6}
45 $cluster->{servers} = [ $server ];
47 push @servers, $server;
50 $cluster->{name} = $name;
52 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
54 $cluster->{status} = "down";
58 $cluster->{status} = "up";
62 # Initialise server details
63 foreach my $server (@servers)
65 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
67 $server->{status} = "down";
71 $server->{status} = "up";
75 # If pingdom support is enabled then check which servers are up
76 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
78 my $ua = LWP::UserAgent::Determined->new;
81 $ua->default_header("App-Key", "2cohi62u5haxvqmypk3ljqqrze1jufrh");
82 $ua->credentials("api.pingdom.com:443", "Pingdom API", $ENV{PINGDOM_USERNAME}, $ENV{PINGDOM_PASSWORD});
84 foreach my $server (@servers)
86 if (my $checkid = $server->{pingdom})
88 my $response = $ua->get("https://api.pingdom.com/api/2.0/checks/${checkid}");
90 if ($response->is_success)
92 my $check = decode_json($response->content);
94 $server->{status} = $check->{check}->{status};
96 if ($server->{status} eq "up")
98 $server->{cluster}->{status} = "up";
102 $server->{cluster}->{bandwidth} = $server->{cluster}->{bandwidth} - $server->{bandwidth};
109 # Create target origins object
110 my $targetorigins = {};
112 # Initialise cluster details
113 while (my($name,$cluster) = each %$clusters)
115 $cluster->{bandwidth_limit} = $cluster->{bandwidth} * 1024 * 1024;
116 $cluster->{bandwidth_used} = 0;
118 $targetorigins->{$cluster->{name}} = {
119 code => $cluster->{name},
120 name => $cluster->{name},
121 lat => $cluster->{lat},
122 lon => $cluster->{lon},
129 # Scan origins and work out which clusters each can use
130 foreach my $origin (values %$origins)
132 foreach my $cluster (values %$clusters)
134 my $match = match_origin($cluster, $origin);
136 if ($cluster->{status} eq "up" && $match ne "denied")
138 my $priority = $match eq "preferred" ? 20 : 10;
139 my $distance = distance($origin->{lat}, $origin->{lon}, $cluster->{lat}, $cluster->{lon});
142 origin => $origin, cluster => $cluster,
143 priority => $priority, distance => $distance
149 # Allocate each country to a cluster
150 allocate_clusters(@mappings);
152 # If we failed to allocate every origin then loop, increasing
153 # the bandwidth for each cluster by a little and retrying until
154 # we manage to allocate everything
155 while (grep { !exists($_->{cluster}) } values %$origins)
157 # Clear any existing mappings of countries to clusters
158 foreach my $origin (values %$origins)
160 delete $origin->{cluster};
163 # Reset bandwidth usage for clusters and increase limits by 10%
164 foreach my $cluster (values %$clusters)
166 $cluster->{bandwidth_used} = 0;
167 $cluster->{bandwidth_limit} = $cluster->{bandwidth_limit} * 1.1;
170 # Try the allocate again
171 allocate_clusters(@mappings);
174 # Create JSON collection object
178 my $zonefile = IO::File->new("> data/${zone}") || die "$!";
179 my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!";
181 # Output details for each country
182 foreach my $origin (values %$origins)
184 my $cluster = $origin->{cluster};
185 my $clon = $origin->{lon};
186 my $clat = $origin->{lat};
187 my $slon = $cluster->{lon};
188 my $slat = $cluster->{lat};
190 if ($clon > 0 && $slon < 0 && 360 + $slon - $clon < $clon - $slon)
194 elsif ($slon > 0 && $clon < 0 && 360 + $clon - $slon < $slon - $clon)
199 $zonefile->print("# $origin->{name}\n");
200 $zonefile->print("C\L$origin->{code}\E.${zone}:$cluster->{name}.${zone}:600\n");
205 type => "LineString",
206 coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ]
209 origin => $origin->{name},
210 server => $cluster->{name},
211 colour => $cluster->{colour}
215 $targetorigins->{$cluster->{name}}->{bandwidth} += $origin->{bandwidth};
218 # Header for default records
219 $zonefile->print("# Unknown origins\n");
221 # Output default records for IPs that can't be mapped to a country
222 while (my($name,$cluster) = each %$clusters)
224 if (my $default = $cluster->{default})
226 output_server($zonefile, "${default}.${zone}", $cluster);
228 elsif (exists($cluster->{default}))
230 output_server($zonefile, "${zone}", $cluster);
234 # Header for underlying servers
235 $zonefile->print("# Servers\n");
237 # Output A records for each cluster
238 while (my($name,$cluster) = each %$clusters)
240 output_server($zonefile, "${name}.${zone}", $cluster);
243 # Output the GeoJSON text
244 $jsonfile->print(encode_json(\@json));
246 # Close the output files
250 # Output the target details in origin format if required
251 YAML::DumpFile($targetoriginfile, $targetorigins) if $targetoriginfile;
256 # Match an origin against a cluster
264 if ($cluster->{preferred} &&
265 $cluster->{preferred}->{countries} &&
266 grep { $_ eq $origin->{country} } @{$cluster->{preferred}->{countries}})
268 $match = "preferred";
270 elsif ($cluster->{preferred} &&
271 $cluster->{preferred}->{continents} &&
272 grep { $_ eq $origin->{continent} } @{$cluster->{preferred}->{continents}})
274 $match = "preferred";
276 elsif ($cluster->{allowed} &&
277 $cluster->{allowed}->{countries} &&
278 grep { $_ eq $origin->{country} } @{$cluster->{allowed}->{countries}})
282 elsif ($cluster->{allowed} &&
283 $cluster->{allowed}->{continents} &&
284 grep { $_ eq $origin->{continent} } @{$cluster->{allowed}->{continents}})
288 elsif ($cluster->{denied} &&
289 $cluster->{denied}->{countries} &&
290 grep { $_ eq $origin->{country} } @{$cluster->{preferred}->{countries}})
294 elsif ($cluster->{denied} &&
295 $cluster->{denied}->{continents} &&
296 grep { $_ eq $origin->{continent} } @{$cluster->{preferred}->{continents}})
300 elsif ($cluster->{allowed})
313 # Compute the great circle distance between two points
317 my $lat1 = deg2rad(shift);
318 my $lon1 = deg2rad(shift);
319 my $lat2 = deg2rad(shift);
320 my $lon2 = deg2rad(shift);
322 return great_circle_distance($lon1, pip2 - $lat1, $lon2, pip2 - $lat2);
326 # Allocate each origin to a cluster
328 sub allocate_clusters
330 my @mappings = sort { compare_mappings($a, $b) } @_;
332 # Loop over the mappings, trying to assign each origin to the
333 # nearest cluster, but subject to the bandwidth limits
334 while (my $mapping = shift @mappings)
338 push @group, $mapping;
340 while (@mappings && compare_mappings($mapping, $mappings[0]) == 0)
342 push @group, shift @mappings;
345 for my $mapping (sort compare_bandwidth @group)
347 my $origin = $mapping->{origin};
348 my $cluster = $mapping->{cluster};
350 if (!exists($origin->{cluster}) &&
351 $cluster->{bandwidth_used} + $origin->{bandwidth} <= $cluster->{bandwidth_limit})
353 $origin->{cluster} = $cluster;
354 $cluster->{bandwidth_used} = $cluster->{bandwidth_used} + $origin->{bandwidth};
363 # Compare two mappings to decide which to use
370 return $b->{priority} <=> $a->{priority} ||
371 $a->{distance} <=> $b->{distance};
375 # Compare two mappings to decide which to try first
377 sub compare_bandwidth
379 my $a_used = ( $a->{cluster}->{bandwidth_used} * 100.0 ) / ( $a->{cluster}->{bandwidth_limit} * 1.0 );
380 my $b_used = ( $b->{cluster}->{bandwidth_used} * 100.0 ) / ( $b->{cluster}->{bandwidth_limit} * 1.0 );
382 return $a_used <=> $b_used;
386 # Output DNS records for a server
390 my $zonefile = shift;
394 foreach my $server (@{$cluster->{servers}})
396 if ($server->{status} eq "up")
398 $zonefile->print("+${name}:$server->{ipv4}:3600\n");
402 # $zonefile->print("3${name}:$server->{ipv6}:3600\n");