Exemplo n.º 1
0
def test_collect_config_filenames_collects_given_files():
    config_paths = ('config.yaml', 'other.yaml')
    flexmock(module.os.path).should_receive('isdir').and_return(False)

    config_filenames = tuple(module.collect_config_filenames(config_paths))

    assert config_filenames == config_paths
Exemplo n.º 2
0
def main():  # pragma: no cover
    args = parse_arguments(*sys.argv[1:])

    logging.basicConfig(level=logging.INFO, format='%(message)s')

    config_filenames = tuple(collect.collect_config_filenames(args.config_paths))
    if len(config_filenames) == 0:
        logger.critical('No files to validate found')
        sys.exit(1)

    found_issues = False
    for config_filename in config_filenames:
        try:
            validate.parse_configuration(config_filename, validate.schema_filename())
        except (ValueError, OSError, validate.Validation_error) as error:
            logging.critical('{}: Error parsing configuration file'.format(config_filename))
            logging.critical(error)
            found_issues = True

    if found_issues:
        sys.exit(1)
    else:
        logger.info(
            'All given configuration files are valid: {}'.format(', '.join(config_filenames))
        )
Exemplo n.º 3
0
def test_collect_config_filenames_collects_given_files():
    config_paths = ('config.yaml', 'other.yaml')
    flexmock(module.os.path).should_receive('isdir').and_return(False)

    config_filenames = tuple(module.collect_config_filenames(config_paths))

    assert config_filenames == config_paths
Exemplo n.º 4
0
def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_sub_directories(
):
    config_paths = ('config.yaml', '/etc/borgmatic.d')
    mock_path = flexmock(module.os.path)
    mock_path.should_receive('exists').and_return(True)
    mock_path.should_receive('isdir').with_args('config.yaml').and_return(
        False)
    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(
        True)
    mock_path.should_receive('isdir').with_args(
        '/etc/borgmatic.d/foo.yaml').and_return(False)
    mock_path.should_receive('isdir').with_args(
        '/etc/borgmatic.d/bar').and_return(True)
    mock_path.should_receive('isdir').with_args(
        '/etc/borgmatic.d/baz.yaml').and_return(False)
    flexmock(module.os).should_receive('listdir')
    flexmock(sys.modules['builtins']).should_receive('sorted').and_return(
        ['foo.yaml', 'bar', 'baz.yaml'])

    config_filenames = tuple(module.collect_config_filenames(config_paths))

    assert config_filenames == (
        'config.yaml',
        '/etc/borgmatic.d/foo.yaml',
        '/etc/borgmatic.d/baz.yaml',
    )
Exemplo n.º 5
0
def main():  # pragma: no cover
    configure_signals()

    try:
        args = parse_arguments(*sys.argv[1:])
    except ValueError as error:
        logging.basicConfig(level=logging.CRITICAL, format='%(message)s')
        logger.critical(error)
        exit_with_help_link()

    logging.basicConfig(level=verbosity_to_log_level(args.verbosity), format='%(message)s')

    if args.version:
        print(pkg_resources.require('borgmatic')[0].version)
        sys.exit(0)

    config_filenames = tuple(collect.collect_config_filenames(args.config_paths))
    logger.debug('Ensuring legacy configuration is upgraded')
    convert.guard_configuration_upgraded(LEGACY_CONFIG_PATH, config_filenames)

    summary_logs = tuple(collect_configuration_run_summary_logs(config_filenames, args))

    logger.info('\nsummary:')
    [logger.handle(log) for log in summary_logs if log.levelno >= logger.getEffectiveLevel()]

    if any(log.levelno == logging.CRITICAL for log in summary_logs):
        exit_with_help_link()
Exemplo n.º 6
0
def main():  # pragma: no cover
    configure_signals()
    args = parse_arguments(*sys.argv[1:])
    logging.basicConfig(level=verbosity_to_log_level(args.verbosity),
                        format='%(message)s')

    if args.version:
        print(pkg_resources.require('borgmatic')[0].version)
        sys.exit(0)

    config_filenames = tuple(
        collect.collect_config_filenames(args.config_paths))
    logger.debug('Ensuring legacy configuration is upgraded')
    convert.guard_configuration_upgraded(LEGACY_CONFIG_PATH, config_filenames)

    summary_logs = tuple(
        collect_configuration_run_summary_logs(config_filenames, args))

    logger.info('\nsummary:')
    [
        logger.handle(log) for log in summary_logs
        if log.levelno >= logger.getEffectiveLevel()
    ]

    if any(log.levelno == logging.CRITICAL for log in summary_logs):
        logger.critical(
            '\nNeed some help? https://torsion.org/borgmatic/#issues')
        sys.exit(1)
Exemplo n.º 7
0
def main():  # pragma: no cover
    try:
        configure_signals()
        args = parse_arguments(*sys.argv[1:])
        logging.basicConfig(level=verbosity_to_log_level(args.verbosity),
                            format='%(message)s')

        config_filenames = tuple(
            collect.collect_config_filenames(args.config_paths))
        logger.debug('Ensuring legacy configuration is upgraded')
        convert.guard_configuration_upgraded(LEGACY_CONFIG_PATH,
                                             config_filenames)

        if len(config_filenames) == 0:
            raise ValueError(
                'Error: No configuration files found in: {}'.format(' '.join(
                    args.config_paths)))

        for config_filename in config_filenames:
            run_configuration(config_filename, args)
    except (ValueError, OSError, CalledProcessError) as error:
        print(error, file=sys.stderr)
        print(file=sys.stderr)
        print('Need some help? https://torsion.org/borgmatic/#issues',
              file=sys.stderr)
        sys.exit(1)
Exemplo n.º 8
0
def test_collect_config_filenames_skips_etc_borgmatic_dot_d_if_it_does_not_exist():
    config_paths = ('config.yaml', '/etc/borgmatic.d')
    mock_path = flexmock(module.os.path)
    mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
    mock_path.should_receive('exists').with_args('/etc/borgmatic.d').and_return(False)
    mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)

    config_filenames = tuple(module.collect_config_filenames(config_paths))

    assert config_filenames == ('config.yaml',)
Exemplo n.º 9
0
def test_collect_config_filenames_includes_other_directory_if_it_does_not_exist():
    config_paths = ('config.yaml', '/my/directory')
    mock_path = flexmock(module.os.path)
    mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
    mock_path.should_receive('exists').with_args('/my/directory').and_return(False)
    mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
    mock_path.should_receive('isdir').with_args('/my/directory').and_return(True)

    config_filenames = tuple(module.collect_config_filenames(config_paths))

    assert config_filenames == config_paths
Exemplo n.º 10
0
def test_collect_config_filenames_includes_other_directory_if_it_does_not_exist():
    config_paths = ('config.yaml', '/my/directory')
    mock_path = flexmock(module.os.path)
    mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
    mock_path.should_receive('exists').with_args('/my/directory').and_return(False)
    mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
    mock_path.should_receive('isdir').with_args('/my/directory').and_return(True)

    config_filenames = tuple(module.collect_config_filenames(config_paths))

    assert config_filenames == config_paths
Exemplo n.º 11
0
def test_collect_config_filenames_skips_non_canonical_etc_borgmatic_dot_d_if_it_does_not_exist():
    config_paths = ('config.yaml', '/etc/../etc/borgmatic.d')
    mock_path = flexmock(module.os.path)
    mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
    mock_path.should_receive('exists').with_args('/etc/../etc/borgmatic.d').and_return(False)
    mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
    mock_path.should_receive('isdir').with_args('/etc/../etc/borgmatic.d').and_return(True)

    config_filenames = tuple(module.collect_config_filenames(config_paths))

    assert config_filenames == ('config.yaml',)
Exemplo n.º 12
0
def main():  # pragma: no cover
    configure_signals()

    try:
        arguments = parse_arguments(*sys.argv[1:])
    except ValueError as error:
        configure_logging(logging.CRITICAL)
        logger.critical(error)
        exit_with_help_link()
    except SystemExit as error:
        if error.code == 0:
            raise error
        configure_logging(logging.CRITICAL)
        logger.critical('Error parsing arguments: {}'.format(' '.join(
            sys.argv)))
        exit_with_help_link()

    global_arguments = arguments['global']
    if global_arguments.version:
        print(pkg_resources.require('borgmatic')[0].version)
        sys.exit(0)

    config_filenames = tuple(
        collect.collect_config_filenames(global_arguments.config_paths))
    configs, parse_logs = load_configurations(config_filenames)

    colorama.init(
        autoreset=True,
        strip=not should_do_markup(global_arguments.no_color, configs))
    configure_logging(
        verbosity_to_log_level(global_arguments.verbosity),
        verbosity_to_log_level(global_arguments.syslog_verbosity),
    )

    logger.debug('Ensuring legacy configuration is upgraded')
    convert.guard_configuration_upgraded(LEGACY_CONFIG_PATH, config_filenames)

    summary_logs = list(
        collect_configuration_run_summary_logs(configs, arguments))

    logger.info('')
    logger.info('summary:')
    [
        logger.handle(log) for log in parse_logs + summary_logs
        if log.levelno >= logger.getEffectiveLevel()
    ]

    if any(log.levelno == logging.CRITICAL for log in summary_logs):
        exit_with_help_link()
Exemplo n.º 13
0
def test_collect_config_filenames_skips_permission_denied_directories():
    config_paths = ('config.yaml', '/etc/borgmatic.d')
    mock_path = flexmock(module.os.path)
    mock_path.should_receive('exists').and_return(True)
    mock_path.should_receive('isdir').with_args('config.yaml').and_return(
        False)
    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(
        True)
    flexmock(module.os).should_receive('access').and_return(False)
    flexmock(module.os).should_receive('listdir')
    flexmock(sys.modules['builtins']).should_receive('sorted').and_return(
        ['config.yaml'])

    config_filenames = tuple(module.collect_config_filenames(config_paths))

    assert config_filenames == ('config.yaml', )
Exemplo n.º 14
0
def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_non_yaml_filenames():
    config_paths = ('/etc/borgmatic.d',)
    mock_path = flexmock(module.os.path)
    mock_path.should_receive('exists').and_return(True)
    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar.yaml~').and_return(False)
    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.txt').and_return(False)
    flexmock(module.os).should_receive('listdir')
    flexmock(sys.modules['builtins']).should_receive('sorted').and_return(
        ['foo.yaml', 'bar.yaml~', 'baz.txt']
    )

    config_filenames = tuple(module.collect_config_filenames(config_paths))

    assert config_filenames == ('/etc/borgmatic.d/foo.yaml',)
Exemplo n.º 15
0
def main():  # pragma: no cover
    try:
        args = parse_arguments(*sys.argv[1:])
        config_filenames = tuple(
            collect.collect_config_filenames(args.config_paths))
        convert.guard_configuration_upgraded(LEGACY_CONFIG_PATH,
                                             config_filenames)

        if len(config_filenames) == 0:
            raise ValueError(
                'Error: No configuration files found in: {}'.format(' '.join(
                    args.config_paths)))

        for config_filename in config_filenames:
            config = validate.parse_configuration(config_filename,
                                                  validate.schema_filename())
            (location, storage, retention,
             consistency) = (config.get(section_name, {})
                             for section_name in ('location', 'storage',
                                                  'retention', 'consistency'))
            remote_path = location.get('remote_path')

            create.initialize(storage)

            for repository in location['repositories']:
                if args.prune:
                    prune.prune_archives(args.verbosity,
                                         repository,
                                         retention,
                                         remote_path=remote_path)
                if args.create:
                    create.create_archive(
                        args.verbosity,
                        repository,
                        location,
                        storage,
                    )
                if args.check:
                    check.check_archives(args.verbosity,
                                         repository,
                                         consistency,
                                         remote_path=remote_path)
    except (ValueError, OSError, CalledProcessError) as error:
        print(error, file=sys.stderr)
        sys.exit(1)
Exemplo n.º 16
0
def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_sub_directories():
    config_paths = ('config.yaml', '/etc/borgmatic.d')
    mock_path = flexmock(module.os.path)
    mock_path.should_receive('exists').and_return(True)
    mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar').and_return(True)
    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.yaml').and_return(False)
    flexmock(module.os).should_receive('listdir').and_return(['foo.yaml', 'bar', 'baz.yaml'])

    config_filenames = tuple(module.collect_config_filenames(config_paths))

    assert config_filenames == (
        'config.yaml',
        '/etc/borgmatic.d/foo.yaml',
        '/etc/borgmatic.d/baz.yaml',
    )
Exemplo n.º 17
0
def main():  # pragma: no cover
    try:
        configure_signals()
        args = parse_arguments(*sys.argv[1:])
        logging.basicConfig(level=verbosity_to_log_level(args.verbosity), format='%(message)s')

        config_filenames = tuple(collect.collect_config_filenames(args.config_paths))
        logger.debug('Ensuring legacy configuration is upgraded')
        convert.guard_configuration_upgraded(LEGACY_CONFIG_PATH, config_filenames)

        if len(config_filenames) == 0:
            raise ValueError('Error: No configuration files found in: {}'.format(' '.join(args.config_paths)))

        for config_filename in config_filenames:
            run_configuration(config_filename, args)
    except (ValueError, OSError, CalledProcessError) as error:
        print(error, file=sys.stderr)
        sys.exit(1)
Exemplo n.º 18
0
def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_non_yaml_filenames(
):
    config_paths = ('/etc/borgmatic.d', )
    mock_path = flexmock(module.os.path)
    mock_path.should_receive('exists').and_return(True)
    mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(
        True)
    mock_path.should_receive('isdir').with_args(
        '/etc/borgmatic.d/foo.yaml').and_return(False)
    mock_path.should_receive('isdir').with_args(
        '/etc/borgmatic.d/bar.yaml~').and_return(False)
    mock_path.should_receive('isdir').with_args(
        '/etc/borgmatic.d/baz.txt').and_return(False)
    flexmock(module.os).should_receive('listdir').and_return(
        ['foo.yaml', 'bar.yaml~', 'baz.txt'])

    config_filenames = tuple(module.collect_config_filenames(config_paths))

    assert config_filenames == ('/etc/borgmatic.d/foo.yaml', )
Exemplo n.º 19
0
def main():  # pragma: no cover
    configure_signals()

    try:
        arguments = parse_arguments(*sys.argv[1:])
    except ValueError as error:
        configure_logging(logging.CRITICAL)
        logger.critical(error)
        exit_with_help_link()
    except SystemExit as error:
        if error.code == 0:
            raise error
        configure_logging(logging.CRITICAL)
        logger.critical('Error parsing arguments: {}'.format(' '.join(
            sys.argv)))
        exit_with_help_link()

    global_arguments = arguments['global']
    if global_arguments.version:
        print(pkg_resources.require('borgmatic')[0].version)
        sys.exit(0)

    config_filenames = tuple(
        collect.collect_config_filenames(global_arguments.config_paths))
    configs, parse_logs = load_configurations(config_filenames,
                                              global_arguments.overrides)

    any_json_flags = any(
        getattr(sub_arguments, 'json', False)
        for sub_arguments in arguments.values())
    colorama.init(
        autoreset=True,
        strip=not should_do_markup(global_arguments.no_color or any_json_flags,
                                   configs),
    )
    try:
        configure_logging(
            verbosity_to_log_level(global_arguments.verbosity),
            verbosity_to_log_level(global_arguments.syslog_verbosity),
            verbosity_to_log_level(global_arguments.log_file_verbosity),
            verbosity_to_log_level(global_arguments.monitoring_verbosity),
            global_arguments.log_file,
        )
    except (FileNotFoundError, PermissionError) as error:
        configure_logging(logging.CRITICAL)
        logger.critical('Error configuring logging: {}'.format(error))
        exit_with_help_link()

    logger.debug('Ensuring legacy configuration is upgraded')
    convert.guard_configuration_upgraded(LEGACY_CONFIG_PATH, config_filenames)

    summary_logs = parse_logs + list(
        collect_configuration_run_summary_logs(configs, arguments))
    summary_logs_max_level = max(log.levelno for log in summary_logs)

    for message in ('', 'summary:'):
        log_record(
            levelno=summary_logs_max_level,
            levelname=logging.getLevelName(summary_logs_max_level),
            msg=message,
        )

    for log in summary_logs:
        logger.handle(log)

    if summary_logs_max_level >= logging.CRITICAL:
        exit_with_help_link()