[mirrorbrain-commits] r8046 - in /trunk/mirrordoctor: mb/asn.py mb/mberr.py mirrordoctor.py

From: <poeml_at_mirrorbrain.org>
Date: Wed, 05 May 2010 16:04:29 -0000
Author: poeml
Date: Wed May  5 18:04:29 2010
New Revision: 8046

URL: http://svn.mirrorbrain.org/viewvc/mirrorbrain?rev=8046&view=rev
Log:
mb update:
- The command can now also update country & region info (from GeoIP).
- A --dry-run option has been added, to allow seeing the changes before applying them.
- An --all option has been added, which updates all metadata, same as when giving
  "-c -a -p --country --region" all at once
- The command now properly takes notice of hostnames that don't resolve in the
  DNS (so further action cannot be taken)

Modified:
    trunk/mirrordoctor/mb/asn.py
    trunk/mirrordoctor/mb/mberr.py
    trunk/mirrordoctor/mirrordoctor.py

Modified: trunk/mirrordoctor/mb/asn.py
URL: http://svn.mirrorbrain.org/viewvc/mirrorbrain/trunk/mirrordoctor/mb/asn.py?rev=8046&r1=8045&r2=8046&view=diff
==============================================================================
--- trunk/mirrordoctor/mb/asn.py (original)
+++ trunk/mirrordoctor/mb/asn.py Wed May  5 18:04:29 2010
_at_@ -2,6 +2,7 @@
 def iplookup(conn, s):
 
     from mb.util import IpAddress
+    import mb.mberr
 
 
     if s[0].isdigit():
_at_@ -14,17 +15,20 @@
         try:
             host, aliases, ips = socket.gethostbyname_ex(s)
         except socket.error, e:
-            print str(e)
-            return None
+            if e[0] == socket.EAI_NONAME:
+                raise mb.mberr.NameOrServiceNotKnown(s)
+            else:
+                print 'socket error msg:', str(e)
+                return None
 
 
         #print host, aliases, ips
         if len(ips) != 1:
             print >>sys.stderr, \
-                    'warning: %r resolves to a multiple IP addresses: %s' \
+                    '>>> warning: %r resolves to a multiple IP addresses: %s' \
                     % (s, ', '.join(ips))
-            print >>sys.stderr, '   see http://mirrorbrain.org/archive/mirrorbrain/0042.html why this could\n' \
-                                '   could be a problem, and what to do about it.\n'
+            print >>sys.stderr, '>>> see http://mirrorbrain.org/archive/mirrorbrain/0042.html why this could' \
+                                ' could be a problem, and what to do about it.\n'
         a = IpAddress(ips[0])
         
 

Modified: trunk/mirrordoctor/mb/mberr.py
URL: http://svn.mirrorbrain.org/viewvc/mirrorbrain/trunk/mirrordoctor/mb/mberr.py?rev=8046&r1=8045&r2=8046&view=diff
==============================================================================
--- trunk/mirrordoctor/mb/mberr.py (original)
+++ trunk/mirrordoctor/mb/mberr.py Wed May  5 18:04:29 2010
_at_@ -53,5 +53,11 @@
         self.url = url
         self.msg = msg
 
+class NameOrServiceNotKnown(Error):
+    """Raised when a hostname could not be looked up in the DNS"""
+
+    def __init__(self, msg):
+        Error.__init__(self, 'DNS lookup for hostname %r failed: Name or service not known' % (msg,))
+        self.msg = msg
 
 

Modified: trunk/mirrordoctor/mirrordoctor.py
URL: http://svn.mirrorbrain.org/viewvc/mirrorbrain/trunk/mirrordoctor/mirrordoctor.py?rev=8046&r1=8045&r2=8046&view=diff
==============================================================================
--- trunk/mirrordoctor/mirrordoctor.py (original)
+++ trunk/mirrordoctor/mirrordoctor.py Wed May  5 18:04:29 2010
_at_@ -359,12 +359,20 @@
 
     _at_cmdln.option('--all-mirrors', action='store_true',
                         help='update *all* mirrors (also disabled ones)')
+    _at_cmdln.option('-A', '--all', action='store_true',
+                        help='update all metadata (same as "-c -a -p --country --region")')
+    _at_cmdln.option('--region', action='store_true',
+                        help='update the region setting with a fresh GeoIP lookup')
+    _at_cmdln.option('--country', action='store_true',
+                        help='update the country setting with a fresh GeoIP lookup')
     _at_cmdln.option('-p', '--prefix', action='store_true',
                         help='update the network prefix')
     _at_cmdln.option('-a', '--asn', action='store_true',
                         help='update the AS number')
     _at_cmdln.option('-c', '--coordinates', action='store_true',
                         help='update the geographical coordinates')
+    _at_cmdln.option('-n', '--dry-run', action='store_true',
+                        help='don\'t actually do anything, just show what would be done')
     def do_update(self, subcmd, opts, *args):
         """${cmd_name}: update mirrors network data in the database
 
_at_@ -380,10 +388,14 @@
         """
         from mb.asn import iplookup
         from mb.util import hostname_from_url
+        import mb.mberr
         from sqlobject.sqlbuilder import AND
 
-        if not (opts.asn or opts.prefix or opts.coordinates):
-            sys.exit('At least one of -c, -a or -p must be given as option.')
+        if opts.all:
+            opts.asn = opts.prefix = opts.coordinates = opts.country = opts.region = True
+
+        if not (opts.asn or opts.prefix or opts.coordinates or opts.country or opts.region):
+            sys.exit('At least one of -c, -a, -p, --country, --region must be given as option.')
 
         #r = mb.asn.iplookup(self.conn, ip)
 
_at_@ -407,29 +419,54 @@
                                  self.conn.Server.q.enabled))
 
         for mirror in mirrors:
-            #print mirror.identifier, 
             hostname = hostname_from_url(mirror.baseurl)
 
-            if opts.prefix or opts.asn:
+            #if opts.prefix or opts.asn:
+            try:
                 res = iplookup(self.conn, hostname)
-                #if res: print res
+            except mb.mberr.NameOrServiceNotKnown, e:
+                print '%s:' % mirror.identifier, e
+                #print '%s: without DNS lookup, no further lookups are possible' % mirror.identifier
+                continue
+
+
             if opts.prefix and res:
                 if mirror.prefix != res.prefix:
-                    print 'updating network prefix for %s (%s -> %s)' \
+                    print '%s: updating network prefix (%s -> %s)' \
                         % (mirror.identifier, mirror.prefix, res.prefix)
-                    mirror.prefix = res.prefix
+                    if not opts.dry_run:
+                        mirror.prefix = res.prefix
             if opts.asn and res:
                 if mirror.asn != res.asn:
-                    print 'updating autonomous system number for %s (%s -> %s)' \
+                    print '%s: updating autonomous system number (%s -> %s)' \
                         % (mirror.identifier, mirror.asn, res.asn)
-                    mirror.asn = res.asn
+                    if not opts.dry_run:
+                        mirror.asn = res.asn
 
             if opts.coordinates:
                 lat, lng = mb.geoip.lookup_coordinates(hostname)
                 if float(mirror.lat or 0) != lat or float(mirror.lng or 0) != lng:
-                    print 'updating geographical coordinates for %s (%s %s -> %s %s)' \
+                    print '%s: updating geographical coordinates (%s %s -> %s %s)' \
                         % (mirror.identifier, mirror.lat, mirror.lng, lat, lng)
-                    mirror.lat, mirror.lng = lat, lng
+                    if not opts.dry_run:
+                        mirror.lat, mirror.lng = lat, lng
+
+            if opts.region:
+                region = mb.geoip.lookup_region_code(hostname)
+                if mirror.region != region:
+                    print '%s: updating region (%s -> %s)' \
+                        % (mirror.identifier, mirror.region, region)
+                    if not opts.dry_run:
+                        mirror.region = region
+
+            if opts.country:
+                country = mb.geoip.lookup_country_code(hostname)
+                if mirror.country != country:
+                    print '%s: updating country (%s -> %s)' \
+                        % (mirror.identifier, mirror.country, country)
+                    if not opts.dry_run:
+                        mirror.country = country
+
 
 
     def do_test(self, subcmd, opts, identifier):




_______________________________________________
mirrorbrain-commits mailing list
Archive: http://mirrorbrain.org/archive/mirrorbrain-commits/

Note: To remove yourself from this list, send a mail with the content
 	unsubscribe
to the address mirrorbrain-commits-request_at_mirrorbrain.org
Received on Wed May 05 2010 - 16:04:31 GMT

This archive was generated by hypermail 2.3.0 : Mon Feb 20 2012 - 23:47:04 GMT