|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (C) 2016 The OpenTimestamps developers |
| 3 | +# |
| 4 | +# This file is part of the OpenTimestamps Server. |
| 5 | +# |
| 6 | +# It is subject to the license terms in the LICENSE file found in the top-level |
| 7 | +# directory of this distribution. |
| 8 | +# |
| 9 | +# No part of the OpenTimestamps Server, including this file, may be copied, |
| 10 | +# modified, propagated, or distributed except according to the terms contained |
| 11 | +# in the LICENSE file. |
| 12 | + |
| 13 | +import argparse |
| 14 | +import logging.handlers |
| 15 | +import os |
| 16 | +import sys |
| 17 | +import otsserver.calendar |
| 18 | +import otsserver.backup |
| 19 | + |
| 20 | +parser = argparse.ArgumentParser(description="OpenTimestamps Backup Server") |
| 21 | + |
| 22 | +parser.add_argument("-q", "--quiet", action="count", default=0, |
| 23 | + help="Be more quiet.") |
| 24 | +parser.add_argument("-v", "--verbose", action="count", default=0, |
| 25 | + help="Be more verbose. Both -v and -q may be used multiple times.") |
| 26 | +parser.add_argument("-p", "--path", type=str, |
| 27 | + dest='base_path', |
| 28 | + default='~/.otsd/backups', |
| 29 | + help="Location of the calendar (default: '%(default)s')") |
| 30 | + |
| 31 | +parser.add_argument('-c', '--calendar', metavar='calendar', action='append', type=str, |
| 32 | + default=[], help='Add a calendar URL to the ones to backup.') |
| 33 | + |
| 34 | +parser.add_argument("--debug-file", type=str, |
| 35 | + dest='debug_file', |
| 36 | + default='~/.otsd/backups/debug.log', |
| 37 | + help="Location of the debug log") |
| 38 | +parser.add_argument("--debug-file-max-size", type=int, |
| 39 | + dest='debug_file_max_size', |
| 40 | + default=10000000, |
| 41 | + help="Max size of the debug log (default: %(default)d bytes) ") |
| 42 | + |
| 43 | +parser.add_argument("--rpc-port", type=int, |
| 44 | + default=14799, |
| 45 | + help="RPC port (default: %(default)d)") |
| 46 | +parser.add_argument("--rpc-address", type=str, |
| 47 | + default='localhost', |
| 48 | + help="RPC address (default: %(default)s)") |
| 49 | + |
| 50 | + |
| 51 | +args = parser.parse_args() |
| 52 | +args.parser = parser |
| 53 | + |
| 54 | +base_path = os.path.expanduser(args.base_path) |
| 55 | +os.makedirs(base_path, exist_ok=True) |
| 56 | +db_dir = base_path + '/db' |
| 57 | +os.makedirs(db_dir, exist_ok=True) |
| 58 | +print("db dir is %s" % db_dir) |
| 59 | + |
| 60 | +debugfile = os.path.expanduser(args.debug_file) |
| 61 | +handler = logging.handlers.RotatingFileHandler(filename=debugfile, maxBytes=args.debug_file_max_size) |
| 62 | +fmt = logging.Formatter("%(asctime)-15s %(message)s") |
| 63 | +handler.setFormatter(fmt) |
| 64 | +logger = logging.getLogger('') |
| 65 | +logger.addHandler(handler) |
| 66 | +ch = logging.StreamHandler(sys.stdout) |
| 67 | +logger.addHandler(ch) |
| 68 | + |
| 69 | +args.verbosity = args.verbose - args.quiet |
| 70 | + |
| 71 | +if args.verbosity == 0: |
| 72 | + logging.root.setLevel(logging.INFO) |
| 73 | +elif args.verbosity > 0: |
| 74 | + logging.root.setLevel(logging.DEBUG) |
| 75 | +elif args.verbosity == -1: |
| 76 | + logging.root.setLevel(logging.WARNING) |
| 77 | +elif args.verbosity < -1: |
| 78 | + logging.root.setLevel(logging.ERROR) |
| 79 | + |
| 80 | + |
| 81 | +db = otsserver.calendar.LevelDbCalendar(db_dir) |
| 82 | +calendar = otsserver.backup.BackupCalendar(db) |
| 83 | +server = otsserver.backup.BackupServer((args.rpc_address, args.rpc_port), calendar) |
| 84 | + |
| 85 | +for calendar_url in args.calendar: |
| 86 | + print("Starting calendar checker for %s" % calendar_url) |
| 87 | + ask_thread = otsserver.backup.AskBackup(db, calendar_url, base_path) |
| 88 | + ask_thread.start() |
| 89 | + |
| 90 | +try: |
| 91 | + print("Starting at %s:%s" % (args.rpc_address, args.rpc_port)) |
| 92 | + server.serve_forever() |
| 93 | +except KeyboardInterrupt: |
| 94 | + sys.exit(0) |
| 95 | + |
| 96 | +# vim:syntax=python filetype=python |
0 commit comments