#!/usr/bin/env python # Copyright (C) 2012, 2013 SPARTA, Inc. a Parsons Company # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND SPARTA DISCLAIMS ALL WARRANTIES WITH # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY # AND FITNESS. IN NO EVENT SHALL SPARTA BE LIABLE FOR ANY SPECIAL, DIRECT, # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM # 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. """This tool is used to import the IPv4/6 BGP table dumps from routeviews.org into the RPKI Web Portal database. If the input file is a bzip2 compressed file, it will be decompressed automatically.""" __version__ = '$Id$' import optparse import logging import time import random import signal import errno import atexit import fcntl import sys import os import rpki.config # configure django ORM from rpki.gui.script_util import setup setup() from rpki.gui.routeview.util import import_routeviews_dump default_url = 'http://archive.routeviews.org/oix-route-views/oix-full-snapshot-latest.dat.bz2' class BadArgument(Exception): pass def timed_out(*ignored): logging.error('timed out') sys.exit(1) if __name__ == '__main__': myname = 'rpkigui-import-routes' cfg = rpki.config.argparser(section=myname, doc=__doc__) cfg.add_logging_arguments() args = cfg.argparser.parse_args() cfg.configure_logging(ident=myname, args=args) jitter = cfg.getint('jitter', section=myname, default=600) if jitter > 0: try: delay = random.SystemRandom().randint(0, jitter) except NotImplementedError: delay = random.randint(0, jitter) logging.info('jitter active, delaying startup for %d seconds', delay) time.sleep(delay) lockfile = cfg.get('lockfile', section=myname, default='/tmp/rpkigui-import-routes.lock') try: lock = os.open(lockfile, os.O_RDONLY | os.O_CREAT | os.O_NONBLOCK, 0666) fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) except (IOError, OSError), e: if e.errno == errno.EAGAIN: logging.info('lock held by another process') sys.exit(0) else: logging.exception(e) sys.exit(1) filename = cfg.get('filename', section=myname, default=default_url) filetype = cfg.get('filetype', section=myname, default='text') download_dir = cfg.get('download-directory', section=myname, default='/var/tmp') timeout = cfg.getint('timeout', section=myname, default=5400) try: if timeout > 0: signal.signal(signal.SIGALRM, timed_out) signal.setitimer(signal.ITIMER_REAL, timeout) import_routeviews_dump(filename=filename, filetype=filetype, download_dir=download_dir) if timeout > 0: signal.setitimer(signal.ITIMER_REAL, 0) except Exception as e: logging.exception(e) sys.exit(1)