[mirrorbrain-commits] [opensuse-svn] r6451 - in trunk/tools/download-redirector-v2/mirrordoctor: . mb

From: Novell Forge SVN <noreply_at_novell.com>
Date: Fri, 13 Feb 2009 00:17:31 -0700 (MST)
Author: poeml
Date: 2009-02-13 00:17:27 -0700 (Fri, 13 Feb 2009)
New Revision: 6451

Added:
   trunk/tools/download-redirector-v2/mirrordoctor/mb/asn.py
Modified:
   trunk/tools/download-redirector-v2/mirrordoctor/mb/conn.py
   trunk/tools/download-redirector-v2/mirrordoctor/mb/util.py
   trunk/tools/download-redirector-v2/mirrordoctor/mirrordoctor.py
Log:
Two new mirrordoctor tools added:
- "mb iplookup" looks up network prefix and AS
- "mb update" can set/update this data across the database (all mirrors)


Added: trunk/tools/download-redirector-v2/mirrordoctor/mb/asn.py
===================================================================
--- trunk/tools/download-redirector-v2/mirrordoctor/mb/asn.py	                        (rev 0)
+++ trunk/tools/download-redirector-v2/mirrordoctor/mb/asn.py	2009-02-13 07:17:27 UTC (rev 6451)
_at_@ -0,0 +1,34 @@
+
+def iplookup(conn, s):
+
+    from mb.util import IpAddress
+
+
+    if s[0].isdigit():
+        a = IpAddress(s)
+
+    else:
+        import sys, socket
+        # note the difference between socket.gethostbyname 
+        # and socket.gethostbyname_ex
+        host, aliases, ips = socket.gethostbyname_ex(s)
+
+        #print host, aliases, ips
+        if len(ips) != 1:
+            print >>sys.stderr, \
+                    'warning: %r resolves to a multiple IP addresses: %s' \
+                    % (s, ', '.join(ips))
+        a = IpAddress(ips[0])
+        
+
+    query = """SELECT pfx, asn \
+                   FROM pfx2asn \
+                   WHERE pfx >>= ip4r('%s') \
+                   ORDER BY ip4r_size(pfx) \
+                   LIMIT 1""" % a.ip
+
+    res = conn.Pfx2asn._connection.queryAll(query)
+    if len(res) != 1:
+        return a
+    (a.prefix, a.asn) = res[0]
+    return a

Modified: trunk/tools/download-redirector-v2/mirrordoctor/mb/conn.py
===================================================================
--- trunk/tools/download-redirector-v2/mirrordoctor/mb/conn.py	2009-02-13 07:14:44 UTC (rev 6450)
+++ trunk/tools/download-redirector-v2/mirrordoctor/mb/conn.py	2009-02-13 07:17:27 UTC (rev 6451)
_at_@ -144,12 +144,19 @@
         self.Country = Country
 
         class Region(SQLObject):
-            """the countries table"""
+            """the regions table"""
             class sqlmeta:
                 fromDatabase = True
                 defaultOrder = 'code'
         self.Region = Region
 
+        class Pfx2asn(SQLObject):
+            """the pfx2asn table"""
+            class sqlmeta:
+                fromDatabase = True
+                defaultOrder = 'asn'
+        self.Pfx2asn = Pfx2asn
+
         if debug:
             self.Server._connection.debug = True
 

Modified: trunk/tools/download-redirector-v2/mirrordoctor/mb/util.py
===================================================================
--- trunk/tools/download-redirector-v2/mirrordoctor/mb/util.py	2009-02-13 07:14:44 UTC (rev 6450)
+++ trunk/tools/download-redirector-v2/mirrordoctor/mb/util.py	2009-02-13 07:17:27 UTC (rev 6451)
_at_@ -13,6 +13,16 @@
         return self.name
 
 
+class IpAddress:
+    """represent an IP address, or rather some data associated with it"""
+    def __init__(self, ip):
+        self.ip = ip
+        self.asn = None
+        self.prefix = None
+    def __str__(self):
+        return '%s (%s AS%s)' % (self.ip, self.prefix, self.asn)
+
+
 def b64_md5(path):
     import base64, md5
     return base64.standard_b64encode(md5.md5(path).digest())[:-2]
_at_@ -28,6 +38,14 @@
     return 'data:image/%s;base64,%s' % (ext, data)
 
 
+def hostname_from_url(url):
+    import urlparse
+    h = urlparse.urlparse(url)[1]
+    if ':' in h:
+        h = h.split(':')[0]
+    return h
+
+
 def dgst(file):
     import md5
     BUFSIZE = 1024*1024

Modified: trunk/tools/download-redirector-v2/mirrordoctor/mirrordoctor.py
===================================================================
--- trunk/tools/download-redirector-v2/mirrordoctor/mirrordoctor.py	2009-02-13 07:14:44 UTC (rev 6450)
+++ trunk/tools/download-redirector-v2/mirrordoctor/mirrordoctor.py	2009-02-13 07:17:27 UTC (rev 6451)
_at_@ -225,6 +225,86 @@
         print mb.conn.server_show_template % mb.conn.server2dict(mirror)
 
 
+    _at_cmdln.option('-p', '--prefix', action='store_true',
+                        help='print the network prefix')
+    _at_cmdln.option('-a', '--asn', action='store_true',
+                        help='print the AS number')
+    def do_iplookup(self, subcmd, opts, ip):
+        """${cmd_name}: lookup stuff about an IP address
+
+        Requires a pfx2asn table to be present, which can be used to look
+        up the AS (autonomous system) number and the closest network prefix
+        that an IP is contained in.
+        Such a table is probably used in conjunction with mod_asn.
+        (Get it. It is worth it ;-)
+
+        ${cmd_usage}
+        ${cmd_option_list}
+        """
+        import mb.asn
+
+        r = mb.asn.iplookup(self.conn, ip)
+
+        if opts.asn:
+            print r.asn
+        elif opts.prefix:
+            print r.prefix
+        else:
+            print '%s (AS%s)' % (r.prefix, r.asn)
+
+
+    _at_cmdln.option('--all-mirrors', action='store_true',
+                        help='update *all* mirrors (also disabled ones)')
+    _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')
+    def do_update(self, subcmd, opts, *args):
+        """${cmd_name}: update mirrors in the database
+
+        Requires a pfx2asn table to be present, which can be used to look
+        up the AS (autonomous system) number and the closest network prefix
+        that an IP is contained in.
+        Such a table is probably used in conjunction with mod_asn.
+
+        ${cmd_usage}
+        ${cmd_option_list}
+        """
+        from mb.asn import iplookup
+        from mb.util import hostname_from_url
+        from sqlobject.sqlbuilder import AND
+
+        #r = mb.asn.iplookup(self.conn, ip)
+
+        #if opts.asn:
+        #    print r.asn
+        #elif opts.prefix:
+        #    print r.prefix
+        #else:
+        #    print '%s (AS%s)' % (r.prefix, r.asn)
+
+        mirrors = []
+        for arg in args:
+            mirrors.append(lookup_mirror(self, arg))
+
+        if not args:
+            if opts.all_mirrors:
+                mirrors = self.conn.Server.select()
+            else:
+                mirrors = self.conn.Server.select(
+                             AND(self.conn.Server.q.statusBaseurl, 
+                                 self.conn.Server.q.enabled))
+
+        for mirror in mirrors:
+            hostname = hostname_from_url(mirror.baseurl)
+            res = iplookup(self.conn, hostname)
+            print mirror.identifier, res
+            if opts.prefix:
+                mirror.prefix = res.prefix
+            if opts.asn:
+                mirror.asn = res.asn
+
+
     def do_test(self, subcmd, opts, identifier):
         """${cmd_name}: test if a mirror is working
 

_______________________________________________
Opensuse-svn mailing list
Opensuse-svn_at_forge.novell.com
http://forge.novell.com/mailman/listinfo/opensuse-svn


_______________________________________________
mirrorbrain-commits mailing list

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 Fri Feb 13 2009 - 07:17:56 GMT

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