7 use Math::Trig qw(deg2rad pip2 great_circle_distance);
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 statuscake => $cluster->{statuscake},
40 bandwidth => $cluster->{bandwidth},
41 ipv4 => $cluster->{ipv4},
42 ipv6 => $cluster->{ipv6}
45 $cluster->{servers} = [ $server ];
47 push @servers, $server;
50 $cluster->{name} = $name;
51 $cluster->{status} = "down";
54 # Initialise server details
55 foreach my $server (@servers)
57 $server->{status} = "up";
60 # If statuscake support is enabled then check which servers are up
61 if ($ENV{STATUSCAKE_USERNAME} && $ENV{STATUSCAKE_APIKEY})
63 my $ua = LWP::UserAgent->new;
66 $ua->agent("mkgeo/1.0");
67 $ua->default_header("Username", $ENV{STATUSCAKE_USERNAME});
68 $ua->default_header("API", $ENV{STATUSCAKE_APIKEY});
70 if (-f "statuscake.yml")
72 $cache = YAML::LoadFile("statuscake.yml");
79 my $response = $ua->get("https://app.statuscake.com/API/Tests/");
81 if ($response->is_success)
83 my $tests = decode_json($response->content);
85 foreach my $test (@$tests)
87 my $testid = $test->{TestID};
89 if ($test->{Status} eq "Up" && !$test->{Paused})
91 $cache->{$testid} = "up";
95 $cache->{$testid} = "down";
100 foreach my $server (@servers)
102 if (my $testids = $server->{statuscake})
104 $server->{status} = "up";
106 for my $testid (@$testids)
108 my $testresult = $cache->{$testid} || "down";
110 $server->{status} = "down" if $testresult eq "down";
115 $server->{status} = "down";
119 YAML::DumpFile("statuscake.yml", $cache);
122 # Mark a cluster as up if any servers are up
123 foreach my $server (@servers)
125 if ($server->{status} eq "up")
127 $server->{cluster}->{status} = "up";
131 $server->{cluster}->{bandwidth} = $server->{cluster}->{bandwidth} - $server->{bandwidth};
135 # Create target origins object
136 my $targetorigins = {};
138 # Initialise cluster details
139 while (my($name,$cluster) = each %$clusters)
141 $cluster->{bandwidth_limit} = $cluster->{bandwidth} * 1024 * 1024;
142 $cluster->{bandwidth_used} = 0;
144 $targetorigins->{$cluster->{name}} = {
145 code => $cluster->{name},
146 name => $cluster->{name},
147 lat => $cluster->{lat},
148 lon => $cluster->{lon},
155 # Scan origins and work out which clusters each can use
156 foreach my $origin (values %$origins)
158 foreach my $cluster (values %$clusters)
160 my $match = match_origin($cluster, $origin);
162 if ($cluster->{status} eq "up" && $match ne "denied")
164 my $priority = $match eq "preferred" ? 20 : 10;
165 my $distance = distance($origin->{lat}, $origin->{lon}, $cluster->{lat}, $cluster->{lon});
168 origin => $origin, cluster => $cluster,
169 priority => $priority, distance => $distance
175 # Allocate each country to a cluster
176 allocate_clusters(@mappings);
178 # If we failed to allocate every origin then loop, increasing
179 # the bandwidth for each cluster by a little and retrying until
180 # we manage to allocate everything
181 while (grep { !exists($_->{cluster}) } values %$origins)
183 # Clear any existing mappings of countries to clusters
184 foreach my $origin (values %$origins)
186 delete $origin->{cluster};
189 # Reset bandwidth usage for clusters and increase limits by 10%
190 foreach my $cluster (values %$clusters)
192 $cluster->{bandwidth_used} = 0;
193 $cluster->{bandwidth_limit} = $cluster->{bandwidth_limit} * 1.1;
196 # Try the allocate again
197 allocate_clusters(@mappings);
200 # Create JSON collection object
204 my $zonefile = IO::File->new("> data/${zone}") || die "$!";
205 my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!";
207 # Output details for each country
208 foreach my $origin (sort { $a->{name} cmp $b->{name} } values %$origins)
210 my $cluster = $origin->{cluster};
211 my $clon = $origin->{lon};
212 my $clat = $origin->{lat};
213 my $slon = $cluster->{lon};
214 my $slat = $cluster->{lat};
216 if ($clon > 0 && $slon < 0 && 360 + $slon - $clon < $clon - $slon)
220 elsif ($slon > 0 && $clon < 0 && 360 + $clon - $slon < $slon - $clon)
225 $zonefile->print("# $origin->{name}\n");
226 $zonefile->print("C\L$origin->{code}\E.${zone}:$cluster->{name}.${zone}:600\n");
231 type => "LineString",
232 coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ]
235 origin => $origin->{name},
236 server => $cluster->{name},
237 colour => $cluster->{colour}
241 $targetorigins->{$cluster->{name}}->{bandwidth} += $origin->{bandwidth};
244 # Header for default records
245 $zonefile->print("# Unknown origins\n");
247 # Output default records for IPs that can't be mapped to a country
248 foreach my $cluster (sort { $a->{name} cmp $b->{name} } values %$clusters)
250 my $name = $cluster->{name};
252 if (my $default = $cluster->{default})
254 output_server($zonefile, "${default}.${zone}", $cluster);
256 elsif (exists($cluster->{default}))
258 output_server($zonefile, "${zone}", $cluster);
262 # Header for underlying servers
263 $zonefile->print("# Servers\n");
265 # Output A records for each cluster
266 foreach my $cluster (sort { $a->{name} cmp $b->{name} } values %$clusters)
268 my $name = $cluster->{name};
270 output_server($zonefile, "${name}.${zone}", $cluster);
273 # Output the GeoJSON text
274 $jsonfile->print(encode_json(\@json));
276 # Close the output files
280 # Output the target details in origin format if required
281 YAML::DumpFile($targetoriginfile, $targetorigins) if $targetoriginfile;
286 # Match an origin against a cluster
294 if ($cluster->{preferred} &&
295 $cluster->{preferred}->{countries} &&
296 grep { $_ eq $origin->{country} } @{$cluster->{preferred}->{countries}})
298 $match = "preferred";
300 elsif ($cluster->{allowed} &&
301 $cluster->{allowed}->{countries} &&
302 grep { $_ eq $origin->{country} } @{$cluster->{allowed}->{countries}})
306 elsif ($cluster->{denied} &&
307 $cluster->{denied}->{countries} &&
308 grep { $_ eq $origin->{country} } @{$cluster->{denied}->{countries}})
312 elsif ($cluster->{preferred} &&
313 $cluster->{preferred}->{continents} &&
314 grep { $_ eq $origin->{continent} } @{$cluster->{preferred}->{continents}})
316 $match = "preferred";
318 elsif ($cluster->{allowed} &&
319 $cluster->{allowed}->{continents} &&
320 grep { $_ eq $origin->{continent} } @{$cluster->{allowed}->{continents}})
324 elsif ($cluster->{denied} &&
325 $cluster->{denied}->{continents} &&
326 grep { $_ eq $origin->{continent} } @{$cluster->{denied}->{continents}})
330 elsif ($cluster->{allowed})
343 # Compute the great circle distance between two points
347 my $lat1 = deg2rad(shift);
348 my $lon1 = deg2rad(shift);
349 my $lat2 = deg2rad(shift);
350 my $lon2 = deg2rad(shift);
352 return great_circle_distance($lon1, pip2 - $lat1, $lon2, pip2 - $lat2);
356 # Allocate each origin to a cluster
358 sub allocate_clusters
360 my @mappings = sort { compare_mappings($a, $b) } @_;
362 # Loop over the mappings, trying to assign each origin to the
363 # nearest cluster, but subject to the bandwidth limits
364 while (my $mapping = shift @mappings)
368 push @group, $mapping;
370 while (@mappings && compare_mappings($mapping, $mappings[0]) == 0)
372 push @group, shift @mappings;
375 for my $mapping (sort compare_bandwidth @group)
377 my $origin = $mapping->{origin};
378 my $cluster = $mapping->{cluster};
380 if (!exists($origin->{cluster}) &&
381 $cluster->{bandwidth_used} + $origin->{bandwidth} <= $cluster->{bandwidth_limit})
383 $origin->{cluster} = $cluster;
384 $cluster->{bandwidth_used} = $cluster->{bandwidth_used} + $origin->{bandwidth};
393 # Compare two mappings to decide which to use
400 return $b->{priority} <=> $a->{priority} ||
401 $a->{distance} <=> $b->{distance};
405 # Compare two mappings to decide which to try first
407 sub compare_bandwidth
409 my $a_used = ( $a->{cluster}->{bandwidth_used} * 100.0 ) / ( $a->{cluster}->{bandwidth_limit} * 1.0 );
410 my $b_used = ( $b->{cluster}->{bandwidth_used} * 100.0 ) / ( $b->{cluster}->{bandwidth_limit} * 1.0 );
412 return $a_used <=> $b_used;
416 # Output DNS records for a server
420 my $zonefile = shift;
424 foreach my $server (@{$cluster->{servers}})
426 if ($server->{status} eq "up")
428 $zonefile->print("+${name}:$server->{ipv4}:600\n");
432 # $zonefile->print("3${name}:$server->{ipv6}:600\n");