]> git.openstreetmap.org Git - dns.git/blobdiff - bin/mkgeo
Update mkgeo to use LWP::UserAgent::Determined + set fair timeout
[dns.git] / bin / mkgeo
index 33a05b0e5086f11123bccd5e80914291e7a70da0..a732711af2b36173d1820e4c3f50db35a8108675 100755 (executable)
--- a/bin/mkgeo
+++ b/bin/mkgeo
@@ -6,7 +6,7 @@ use warnings;
 use IO::File;
 use Math::Trig qw(deg2rad pip2 great_circle_distance);
 use JSON::XS;
-use LWP::UserAgent;
+use LWP::UserAgent::Determined;
 use YAML;
 
 my $originfile = shift @ARGV;
@@ -75,7 +75,8 @@ foreach my $server (@servers)
 # If pingdom support is enabled then check which servers are up
 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
 {
-    my $ua = LWP::UserAgent->new;
+    my $ua = LWP::UserAgent::Determined->new;
+    $ua->timeout(15);
 
     $ua->default_header("App-Key", "2cohi62u5haxvqmypk3ljqqrze1jufrh");
     $ua->credentials("api.pingdom.com:443", "Pingdom API", $ENV{PINGDOM_USERNAME}, $ENV{PINGDOM_PASSWORD});
@@ -105,11 +106,22 @@ if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
     }
 }
 
+# Create target origins object
+my $targetorigins = {};
+
 # Initialise cluster details
 while (my($name,$cluster) = each %$clusters)
 {
     $cluster->{bandwidth_limit} = $cluster->{bandwidth} * 1024 * 1024;
     $cluster->{bandwidth_used} = 0;
+
+    $targetorigins->{$cluster->{name}} = {
+        code => $cluster->{name},
+        name => $cluster->{name},
+        lat => $cluster->{lat},
+        lon => $cluster->{lon},
+        bandwidth => 0
+    };
 }
 
 my @mappings = ();
@@ -162,9 +174,6 @@ while (grep { !exists($_->{cluster}) } values %$origins)
 # Create JSON collection object
 my @json;
 
-# Create target origins object
-my $targetorigins = {};
-
 # Open output files
 my $zonefile = IO::File->new("> data/${zone}") || die "$!";
 my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!";
@@ -203,17 +212,6 @@ foreach my $origin (values %$origins)
         }
     };
 
-    unless (exists($targetorigins->{$cluster->{name}}))
-    {
-        $targetorigins->{$cluster->{name}} = {
-            code => $cluster->{name},
-            name => $cluster->{name},
-            lat => $cluster->{lat},
-            lon => $cluster->{lon},
-            bandwidth => 0
-        };
-    }
-
     $targetorigins->{$cluster->{name}}->{bandwidth} += $origin->{bandwidth};
 }
 
@@ -225,11 +223,11 @@ while (my($name,$cluster) = each %$clusters)
 {
     if (my $default = $cluster->{default})
     {
-        $zonefile->print("C${default}.${zone}:${name}.${zone}:600\n");
+        output_server($zonefile, "${default}.${zone}", $cluster);
     }
     elsif (exists($cluster->{default}))
     {
-        $zonefile->print("C${zone}:${name}.${zone}:600\n");
+        output_server($zonefile, "${zone}", $cluster);
     }
 }
 
@@ -239,18 +237,7 @@ $zonefile->print("# Servers\n");
 # Output A records for each cluster
 while (my($name,$cluster) = each %$clusters)
 {
-    foreach my $server (@{$cluster->{servers}})
-    {
-        if ($server->{status} eq "up")
-        {
-            $zonefile->print("+${name}.${zone}:$server->{ipv4}:600\n");
-
-            if ($server->{ipv6})
-            {
-#                $zonefile->print("3${name}.${zone}:$server->{ipv6}:600\n");
-            }
-        }
-    }
+    output_server($zonefile, "${name}.${zone}", $cluster);
 }
 
 # Output the GeoJSON text
@@ -394,3 +381,28 @@ sub compare_bandwidth
 
     return $a_used <=> $b_used;
 }
+
+#
+# Output DNS records for a server
+#
+sub output_server
+{
+    my $zonefile = shift;
+    my $name = shift;
+    my $cluster = shift;
+
+    foreach my $server (@{$cluster->{servers}})
+    {
+        if ($server->{status} eq "up")
+        {
+            $zonefile->print("+${name}:$server->{ipv4}:3600\n");
+
+            if ($server->{ipv6})
+            {
+#                $zonefile->print("3${name}:$server->{ipv6}:3600\n");
+            }
+        }
+    }
+
+    return;
+}