aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rpkid/portal-gui/scripts/rpkigui-import-routes.py89
1 files changed, 55 insertions, 34 deletions
diff --git a/rpkid/portal-gui/scripts/rpkigui-import-routes.py b/rpkid/portal-gui/scripts/rpkigui-import-routes.py
index 77c4a569..ad273a39 100644
--- a/rpkid/portal-gui/scripts/rpkigui-import-routes.py
+++ b/rpkid/portal-gui/scripts/rpkigui-import-routes.py
@@ -1,4 +1,3 @@
-# $Id$
# Copyright (C) 2012 SPARTA, Inc. a Parsons Company
#
# Permission to use, copy, modify, and distribute this software for any
@@ -12,7 +11,8 @@
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
-#
+
+__version__ = '$Id'
import itertools
import _mysql_exceptions
@@ -29,14 +29,26 @@ from rpki.resource_set import resource_range_ipv4, resource_range_ipv6
import rpki.gui.app.timestamp
# globals
-DEBUG=False
-VERBOSE=False
-BGPDUMP='bgpdump'
+DEBUG = False
+VERBOSE = False
+BGPDUMP = 'bgpdump'
PREFIXES = {}
+
class InvalidPrefix(Exception):
pass
+
+def debug(s):
+ if DEBUG:
+ print s
+
+
+def log(s):
+ if VERBOSE:
+ print s
+
+
def parse_text(f):
ip_re = re.compile(r'^[0-9a-fA-F:.]+/\d{1,3}$')
last_prefix = None
@@ -59,15 +71,17 @@ def parse_text(f):
# if this is the same prefix as the last entry, we don't need
# to validate it again.
if prefix != last_prefix:
- # validate the prefix since the "sh ip bgp" output is sometimes corrupt
- # by no space between the prefix and the next hop IP address.
+ # validate the prefix since the "sh ip bgp" output is sometimes
+ # corrupt by no space between the prefix and the next hop IP
+ # address.
if not ip_re.match(prefix):
net, bits = prefix.split('/')
if len(bits) > 2 and int(bits[0]) <= 3:
- print 'mask for %s looks fishy...' % prefix,
+ s = ['mask for %s looks fishy...' % prefix]
prefix = '%s/%s' % (net, bits[0:2])
- print 'assuming it should be %s' % prefix
+ s.append('assuming it should be %s' % prefix)
+ log(' '.join(s))
if not ip_re.match(prefix):
raise InvalidPrefix(prefix)
last_prefix = prefix
@@ -83,13 +97,14 @@ def parse_text(f):
PREFIXES[prefix] = asns
asns.add(int(origin_as))
- except InvalidPrefix, e:
- print >>sys.stderr, 'skipping bad entry: ' + row,
- print >>sys.stderr, e
+ except InvalidPrefix:
+ log('skipping bad entry: ' + row)
+
def parse_mrt(f):
# filter input through bgpdump
- pipe = subprocess.Popen([BGPDUMP, '-m', '-v', '-'], stdin=f, stdout=subprocess.PIPE)
+ pipe = subprocess.Popen([BGPDUMP, '-m', '-v', '-'], stdin=f,
+ stdout=subprocess.PIPE)
last_prefix = None
last_as = None
@@ -118,6 +133,7 @@ def parse_mrt(f):
if pipe.returncode:
raise ProgException('bgpdump exited with code %d' % pipe.returncode)
+
def commit():
"Write the PREFIXES dict into the appropriate database table."
# auto-detect the IP version
@@ -146,7 +162,7 @@ def commit():
pass
debug('Creating staging table...')
- cursor.execute('CREATE TABLE %(table)s_new LIKE %(table)s' % { 'table': table })
+ cursor.execute('CREATE TABLE %(table)s_new LIKE %(table)s' % {'table': table})
debug('Disabling autocommit...')
cursor.execute('SET autocommit=0')
@@ -169,26 +185,29 @@ def commit():
pass
debug('Swapping staging table with live table...')
- cursor.execute('RENAME TABLE %(table)s TO %(table)s_old, %(table)s_new TO %(table)s' % { 'table': table })
+ cursor.execute('RENAME TABLE %(table)s TO %(table)s_old, %(table)s_new TO %(table)s' % {'table': table})
transaction.commit_unless_managed()
debug('Updating timestamp metadata...')
rpki.gui.app.timestamp.update('bgp_v4_import' if ip_version == 4 else 'bgp_v6_import')
-def debug(s):
- if DEBUG: print s
-def log(s):
- if VERBOSE: print s
+class ProgException(Exception):
+ pass
+
+
+class BadArgument(ProgException):
+ pass
-class ProgException(Exception): pass
-class BadArgument(ProgException): pass
+class UnknownInputType(ProgException):
+ pass
-class UnknownInputType(ProgException): pass
-class PipeFailed(ProgException): pass
+class PipeFailed(ProgException):
+ pass
+
if __name__ == '__main__':
parser = optparse.OptionParser(usage='%prog [options] PATH',
@@ -197,21 +216,22 @@ from routeviews.org into the RPKI Web Portal database. If the
input file is a bzip2 compressed file, it will be decompressed
automatically.""")
parser.add_option('-t', '--type', dest='filetype', metavar='TYPE',
- help='Specify the input file type (auto, text, mrt) [Default: %default]')
+ help='Specify the input file type (auto, text, mrt) [Default: %default]')
parser.add_option('-d', '--debug', dest='debug', action='store_true',
- help='Enabling debugging output [Default: %default]')
+ help='Enabling debugging output [Default: %default]')
parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
- help='Enable verbose output [Default: %default]')
+ help='Enable verbose output [Default: %default]')
parser.add_option('-u', '--bunzip2', dest='bunzip', metavar='PROG',
- help='Specify bunzip2 program to use')
+ help='Specify bunzip2 program to use')
parser.add_option('-b', '--bgpdump', dest='bgpdump', metavar='PROG',
- help='Specify path to bgdump binary')
+ help='Specify path to bgdump binary')
parser.set_defaults(debug=False, verbose=False, filetype='auto')
options, args = parser.parse_args()
DEBUG = options.debug
VERBOSE = options.verbose
- if options.bgpdump: BGPDUMP=os.path.expanduser(options.bgpdump)
+ if options.bgpdump:
+ BGPDUMP = os.path.expanduser(options.bgpdump)
try:
if len(args) != 1:
@@ -219,8 +239,8 @@ automatically.""")
filename = args[0]
if options.filetype == 'auto':
- # try to determine input type from filename, based on the default filenames from
- # archive.routeviews.org
+ # try to determine input type from filename, based on the default
+ # filenames from archive.routeviews.org
bname = os.path.basename(filename)
if bname.startswith('oix-full-snapshot-latest'):
filetype = 'text'
@@ -236,14 +256,15 @@ automatically.""")
if filename.endswith('.bz2'):
bunzip = 'bunzip2' if not options.bunzip else os.path.expanduser(options.bunzip)
debug('Decompressing input file on the fly...')
- pipe = subprocess.Popen([bunzip, '--stdout', filename], stdout=subprocess.PIPE)
+ pipe = subprocess.Popen([bunzip, '--stdout', filename],
+ stdout=subprocess.PIPE)
input_file = pipe.stdout
else:
input_file = open(filename)
try:
log('Reading data...')
- dispatch = { 'text': parse_text, 'mrt': parse_mrt }
+ dispatch = {'text': parse_text, 'mrt': parse_mrt}
dispatch[filetype](input_file)
except KeyError:
raise UnknownInputType('"%s" is an unknown input file type' % filetype)
@@ -262,5 +283,5 @@ automatically.""")
sys.exit(0)
except ProgException, e:
- print 'Error:', e
+ print >>sys.stderr, 'Error:', e
sys.exit(1)