]> git.openstreetmap.org Git - dns.git/blob - bin/mkgeo
Fix names of provo servers
[dns.git] / bin / mkgeo
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use IO::File;
7 use Math::Trig qw(deg2rad pip2 great_circle_distance);
8 use JSON::XS;
9 use LWP::UserAgent;
10 use XML::TreeBuilder;
11 use YAML;
12
13 my $source = shift @ARGV;
14 my $zone = shift @ARGV;
15 my $clusters = YAML::LoadFile("src/${source}");
16 my $servers = {};
17
18 # Initialise cluster details
19 while (my($name,$cluster) = each %$clusters)
20 {
21     if ($cluster->{servers})
22     {
23         $cluster->{bandwidth} = 0;
24
25         while (my($name,$server) = each %{$cluster->{servers}})
26         {
27             $server->{cluster} = $cluster;
28             $cluster->{bandwidth} = $cluster->{bandwidth} + $server->{bandwidth};
29
30             $servers->{$name} = $server;
31         }
32     }
33     else
34     {
35         $servers->{$name} = {
36             cluster => $cluster,
37             pingdom => $cluster->{pingdom},
38             bandwidth => $cluster->{bandwidth}
39         };
40
41         $cluster->{servers} = {
42             $name => $servers->{$name}
43         };
44     }
45
46     $cluster->{name} = $name;
47
48     if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
49     {
50         $cluster->{status} = "down";
51     }
52     else
53     {
54         $cluster->{status} = "up";
55     }
56 }
57
58 # Initialise server details
59 while (my($name,$server) = each %$servers)
60 {
61     $server->{name} = $name;
62
63     if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
64     {
65         $server->{status} = "down";
66     }
67     else
68     {
69         $server->{status} = "up";
70     }
71 }
72
73 # If pingdom support is enabled then check which servers are up
74 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
75 {
76     my $ua = LWP::UserAgent->new;
77
78     $ua->default_header("App-Key", "2cohi62u5haxvqmypk3ljqqrze1jufrh");
79     $ua->credentials("api.pingdom.com:443", "Pingdom API", $ENV{PINGDOM_USERNAME}, $ENV{PINGDOM_PASSWORD});
80
81     foreach my $server (values %$servers)
82     {
83         if (my $checkid = $server->{pingdom})
84         {
85             my $response = $ua->get("https://api.pingdom.com/api/2.0/checks/${checkid}");
86
87             if ($response->is_success)
88             {
89                 my $check = decode_json($response->content);
90
91                 $server->{status} = $check->{check}->{status};
92
93                 if ($server->{status} eq "up")
94                 {
95                     $server->{cluster}->{status} = "up";
96                 }
97                 else
98                 {
99                     $server->{cluster}->{bandwidth} = $server->{cluster}->{bandwidth} - $server->{bandwidth};
100                 }
101             }
102         }
103     }
104 }
105
106 # Initialise cluster details
107 while (my($name,$cluster) = each %$clusters)
108 {
109     $cluster->{bandwidth_limit} = $cluster->{bandwidth} * 1024 * 1024;
110     $cluster->{bandwidth_used} = 0;
111 }
112
113 my %countries = ();
114 my @mappings = ();
115
116 # Create a parser for the country database
117 my $countries = XML::TreeBuilder->new;
118
119 # Parse the country database
120 $countries->parsefile("lib/countries.xml");
121
122 # Load the per-country bandwidth details
123 my $bandwidth = YAML::LoadFile("bandwidth/${source}.yml");
124
125 # Fill in country table and work out which clusters each can use
126 foreach my $country ($countries->look_down("_tag" => "country"))
127 {
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 );
139
140     $countries{$code} = {
141         code => $code, name => $name, continent => $continent,
142         bandwidth => $bandwidth, lat => $lat, lon => $lon
143     };
144
145     foreach my $cluster (values %$clusters)
146     {
147         my $match = match_country($cluster, $code, $continent);
148
149         if ($cluster->{status} eq "up" && $match ne "denied")
150         {
151             my $priority = $match eq "preferred" ? 20 : 10;
152             my $distance = distance($lat, $lon, $cluster->{lat}, $cluster->{lon});
153
154             push @mappings, {
155                 country => $countries{$code}, cluster => $cluster,
156                 priority => $priority, distance => $distance
157             };
158         }
159     }
160 }
161
162 # Discard the parsed country database
163 $countries->delete;
164
165 # Allocate each country to a cluster
166 allocate_clusters(\@mappings);
167
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)
172 {
173     # Clear any existing mappings of countries to clusters
174     foreach my $country (values %countries)
175     {
176         delete $country->{cluster};
177     }
178
179     # Reset bandwidth usage for clusters and increase limits by 10%
180     foreach my $cluster (values %$clusters)
181     {
182         $cluster->{bandwidth_used} = 0;
183         $cluster->{bandwidth_limit} = $cluster->{bandwidth_limit} * 1.1;
184     }
185
186     # Try the allocate again
187     allocate_clusters(\@mappings);
188 }
189
190 # Create JSON collection object
191 my @json;
192
193 # Open output files
194 my $zonefile = IO::File->new("> data/${zone}") || die "$!";
195 my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!";
196
197 # Output details for each country
198 foreach my $country (values %countries)
199 {
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};
205
206     if ($clon > 0 && $slon < 0 && 360 + $slon - $clon < $clon - $slon)
207     {
208         $slon = $slon + 360;
209     }
210     elsif ($slon > 0 && $clon < 0 && 360 + $clon - $slon < $slon - $clon)
211     {
212         $clon = $clon + 360;
213     }
214
215     $zonefile->print("# $country->{name}\n");
216
217     while (my($name,$server) = each %{$cluster->{servers}})
218     {
219         if ($server->{status} eq "up")
220         {
221             $zonefile->print("C\L$country->{code}\E.${zone}:$server->{name}.${zone}:600\n");
222         }
223     }
224
225     push @json, {
226         type => "Feature",
227         geometry => {
228             type => "LineString",
229             coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ]
230         },
231         properties => {
232             country => $country->{name},
233             server => $cluster->{name},
234             colour => $cluster->{colour}
235         }
236     };
237 }
238
239 # Output default records for IPs that can't be mapped to a country
240 foreach my $cluster (grep { $_->{default} } values %$clusters)
241 {
242     $zonefile->print("# Unknown countries\n");
243
244     while (my($name,$server) = each %{$cluster->{servers}})
245     {
246         $zonefile->print("Cxx.${zone}:${name}.${zone}:600\n");
247     }
248 }
249
250 # Output the GeoJSON text
251 $jsonfile->print(encode_json(\@json));
252
253 # Close the output files
254 $jsonfile->close();
255 $zonefile->close();
256
257 exit 0;
258
259 #
260 # Find the centre value between two latitudes
261 #
262 sub centre_lat
263 {
264     my $south = shift;
265     my $north = shift;
266
267     return ( $south + $north ) / 2;
268 }
269
270 #
271 # Find the centre value between two longitudes
272 #
273 sub centre_lon
274 {
275     my $west = shift;
276     my $east = shift;
277     my $lon;
278
279     if ($west < $east)
280     {
281         $lon = ( $west + $east ) / 2;
282     }
283     else
284     {
285         $lon = ( $west + $east + 360 ) / 2;
286     }
287
288     $lon = $lon - 360 if $lon > 180;
289
290     return $lon
291 }
292
293 #
294 # Match a country against a cluster
295 #
296 sub match_country
297 {
298     my $cluster = shift;
299     my $country = shift;
300     my $continent = shift;
301     my $match;
302
303     if ($cluster->{preferred} &&
304         $cluster->{preferred}->{countries} &&
305         grep { $_ eq $country } @{$cluster->{preferred}->{countries}})
306     {
307         $match = "preferred";
308     }
309     elsif ($cluster->{preferred} &&
310            $cluster->{preferred}->{continents} &&
311            grep { $_ eq $continent } @{$cluster->{preferred}->{continents}})
312     {
313         $match = "preferred";
314     }
315     elsif ($cluster->{allowed} &&
316            $cluster->{allowed}->{countries} &&
317            grep { $_ eq $country } @{$cluster->{allowed}->{countries}})
318     {
319         $match = "allowed";
320     }
321     elsif ($cluster->{allowed} &&
322            $cluster->{allowed}->{continents} &&
323            grep { $_ eq $continent } @{$cluster->{allowed}->{continents}})
324     {
325         $match = "allowed";
326     }
327     elsif ($cluster->{denied} &&
328            $cluster->{denied}->{countries} &&
329            grep { $_ eq $country } @{$cluster->{preferred}->{countries}})
330     {
331         $match = "denied";
332     }
333     elsif ($cluster->{denied} &&
334            $cluster->{denied}->{continents} &&
335            grep { $_ eq $continent } @{$cluster->{preferred}->{continents}})
336     {
337         $match = "denied";
338     }
339     elsif ($cluster->{allowed})
340     {
341         $match = "denied";
342     }
343     else
344     {
345         $match = "allowed";
346     }
347
348     return $match;
349 }
350
351 #
352 # Compute the great circle distance between two points
353 #
354 sub distance
355 {
356     my $lat1 = deg2rad(shift);
357     my $lon1 = deg2rad(shift);
358     my $lat2 = deg2rad(shift);
359     my $lon2 = deg2rad(shift);
360
361     return great_circle_distance($lon1, pip2 - $lat1, $lon2, pip2 - $lat2);
362 }
363
364 #
365 # Allocate each country to a cluster
366 #
367 sub allocate_clusters
368 {
369     my $mappings = shift;
370
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)
374     {
375         my $country = $mapping->{country};
376         my $cluster = $mapping->{cluster};
377
378         if (!exists($country->{cluster}) &&
379             $cluster->{bandwidth_used} + $country->{bandwidth} <= $cluster->{bandwidth_limit})
380         {
381             $country->{cluster} = $cluster;
382             $cluster->{bandwidth_used} = $cluster->{bandwidth_used} + $country->{bandwidth};
383         }
384     }
385
386     return;
387 }