aboutsummaryrefslogtreecommitdiff
path: root/ca/rpkigui-import-routes
blob: aeecea6e1a6526d9b62fb878ff65556676c84372 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/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)