Exemplo n.º 1
0
def build_config_from_dicts(global_conf=None, main_conf=None,
                            test_conf=None,
                            config_name=None):
    """
    Utility method, generate a barman.config.Config object

    It has  a minimal configuration and a single server called "main".
    All options can be override using the optional arguments

    :param dict[str,str|None]|None global_conf: using this dictionary
        it is possible to override or add new values to the [barman] section
    :param dict[str,str|None]|None main_conf: using this dictionary
        it is possible to override/add new values to the [main] section
    :return barman.config.Config: a barman configuration object
    """
    # base barman section
    base_barman = {
        'barman_home': '/some/barman/home',
        'barman_user': '******',
        'log_file': '%(barman_home)s/log/barman.log',
        'archiver': True
    }
    # base main section
    base_main = {
        'description': '" Text with quotes "',
        'ssh_command': 'ssh -c "arcfour" -p 22 [email protected]',
        'conninfo': 'host=pg01.nowhere user=postgres port=5432'
    }
    # base test section
    base_test = {
        'description': '" Text with quotes "',
        'ssh_command': 'ssh -c "arcfour" -p 22 [email protected]',
        'conninfo': 'host=pg02.nowhere user=postgres port=5433'
    }
    # update map values of the two sections
    if global_conf is not None:
        base_barman.update(global_conf)
    if main_conf is not None:
        base_main.update(main_conf)
    if test_conf is not None:
        base_test.update(test_conf)

    # writing the StringIO obj with the barman and main sections
    config_file = StringIO()
    config_file.write('\n[barman]\n')
    for key in base_barman.keys():
        config_file.write('%s = %s\n' % (key, base_barman[key]))

    config_file.write('[main]\n')
    for key in base_main.keys():
        config_file.write('%s = %s\n' % (key, base_main[key]))

    config_file.write('[test]\n')
    for key in base_test.keys():
        config_file.write('%s = %s\n' % (key, base_main[key]))

    config_file.seek(0)
    config = Config(config_file)
    config.config_file = config_name or 'build_config_from_dicts'
    return config
Exemplo n.º 2
0
def build_config_from_dicts(global_conf=None, main_conf=None,
                            test_conf=None,
                            config_name=None):
    """
    Utility method, generate a barman.config.Config object

    It has  a minimal configuration and a single server called "main".
    All options can be override using the optional arguments

    :param dict[str,str|None]|None global_conf: using this dictionary
        it is possible to override or add new values to the [barman] section
    :param dict[str,str|None]|None main_conf: using this dictionary
        it is possible to override/add new values to the [main] section
    :return barman.config.Config: a barman configuration object
    """
    # base barman section
    base_barman = {
        'barman_home': '/some/barman/home',
        'barman_user': '******',
        'log_file': '%(barman_home)s/log/barman.log',
        'archiver': True
    }
    # base main section
    base_main = {
        'description': '" Text with quotes "',
        'ssh_command': 'ssh -c "arcfour" -p 22 [email protected]',
        'conninfo': 'host=pg01.nowhere user=postgres port=5432'
    }
    # base test section
    base_test = {
        'description': '" Text with quotes "',
        'ssh_command': 'ssh -c "arcfour" -p 22 [email protected]',
        'conninfo': 'host=pg02.nowhere user=postgres port=5433'
    }
    # update map values of the two sections
    if global_conf is not None:
        base_barman.update(global_conf)
    if main_conf is not None:
        base_main.update(main_conf)
    if test_conf is not None:
        base_test.update(test_conf)

    # writing the StringIO obj with the barman and main sections
    config_file = StringIO()
    config_file.write('\n[barman]\n')
    for key in base_barman.keys():
        config_file.write('%s = %s\n' % (key, base_barman[key]))

    config_file.write('[main]\n')
    for key in base_main.keys():
        config_file.write('%s = %s\n' % (key, base_main[key]))

    config_file.write('[test]\n')
    for key in base_test.keys():
        config_file.write('%s = %s\n' % (key, base_main[key]))

    config_file.seek(0)
    config = Config(config_file)
    config.config_file = config_name or 'build_config_from_dicts'
    return config
Exemplo n.º 3
0
 def test_quotes(self):
     fp = StringIO(MINIMAL_CONFIG.format(**os.environ))
     c = Config(fp)
     main = c.get_server('main')
     self.assertEqual(main.description, ' Text with quotes ')
     self.assertEqual(main.ssh_command,
                      'ssh -c "arcfour" -p 22 postgres@pg01')
Exemplo n.º 4
0
 def test_server_list(self):
     """
     Test parsing of a config file
     """
     fp = StringIO(TEST_CONFIG.format(**os.environ))
     c = Config(fp)
     dbs = c.server_names()
     assert set(dbs) == set(['main', 'web'])
Exemplo n.º 5
0
 def test_server_list(self):
     """
     Test parsing of a config file
     """
     fp = StringIO(TEST_CONFIG)
     c = Config(fp)
     dbs = c.server_names()
     assert set(dbs) == set(['main', 'web'])
Exemplo n.º 6
0
    def test_interpolation(self):
        fp = StringIO(MINIMAL_CONFIG.format(**os.environ))
        c = Config(fp)
        main = c.get_server('main')

        expected = dict(config=c)
        expected.update(MINIMAL_CONFIG_MAIN)
        assert main.__dict__ == expected
Exemplo n.º 7
0
    def test_interpolation(self):
        self.maxDiff = None
        fp = StringIO(MINIMAL_CONFIG.format(**os.environ))
        c = Config(fp)
        main = c.get_server('main')

        expected = dict(config=c)
        expected.update(MINIMAL_CONFIG_MAIN)
        self.assertEqual(main.__dict__, expected)
Exemplo n.º 8
0
    def test_interpolation(self):
        self.maxDiff = None
        fp = StringIO(MINIMAL_CONFIG.format(**os.environ))
        c = Config(fp)
        main = c.get_server('main')

        expected = dict(config=c)
        expected.update(MINIMAL_CONFIG_MAIN)
        self.assertEqual(main.__dict__,expected)
Exemplo n.º 9
0
 def test_interpolation(self):
     self.maxDiff = None
     fp = StringIO(MINIMAL_CONFIG.format(**os.environ))
     c = Config(fp)
     main = c.get_server('main')
     
     self.assertEqual(main.__dict__, 
         dict(MINIMAL_CONFIG_MAIN.items() +
         [('config',c)]))
Exemplo n.º 10
0
def main():
    parser = argparse.ArgumentParser(description="Barman plugin for NRPE.")
    parser.add_argument("-s",
                        "--server",
                        dest="server",
                        help="server to check")
    parser.add_argument('-w',
                        '--warning',
                        type=int,
                        dest="warning",
                        metavar="W",
                        help="warning threshold.")
    parser.add_argument('-c',
                        '--critical',
                        type=int,
                        dest="critical",
                        metavar="C",
                        help="critical threshold.")
    parser.add_argument(
        '-u'
        '--user',
        dest='user',
        metavar='U',
        help="user needed to run this script. If the "
        "current user is not this one, the script will try " +
        "to rerun itself using sudo.")

    subparsers = parser.add_subparsers()

    for key, value in ACTIONS.items():
        (action, help) = value
        subparser = subparsers.add_parser(key, help=help)
        subparser.set_defaults(action=action)

    parser.error = unknown

    args = parser.parse_args()

    user = pwd.getpwuid(os.getuid())[0]

    if args.user and user != args.user:
        import subprocess

        retval = subprocess.call(["/usr/bin/sudo", "-u", args.user] + sys.argv)
        raise SystemExit(retval)

    from barman.config import Config
    from barman.server import Server

    config = Config()
    config.load_configuration_files_directory()
    server = Server(config.get_server(args.server))

    try:
        args.action(server, args)
    except KeyError:
        unknown("The action %s does not exist." % args.action)
Exemplo n.º 11
0
 def test_quotes(self):
     """
     Test quotes management during configuration parsing
     """
     fp = StringIO(MINIMAL_CONFIG.format(**os.environ))
     c = Config(fp)
     main = c.get_server('main')
     assert main.description == ' Text with quotes '
     assert main.ssh_command == 'ssh -c "arcfour" ' \
                                '-p 22 [email protected]'
Exemplo n.º 12
0
 def test_quotes(self):
     """
     Test quotes management during configuration parsing
     """
     fp = StringIO(MINIMAL_CONFIG)
     c = Config(fp)
     main = c.get_server('main')
     assert main.description == ' Text with quotes '
     assert main.ssh_command == 'ssh -c "arcfour" ' \
                                '-p 22 [email protected]'
Exemplo n.º 13
0
    def test_interpolation(self):
        """
        Basic interpolation test
        """
        fp = StringIO(MINIMAL_CONFIG.format(**os.environ))
        c = Config(fp)
        main = c.get_server('main')

        # create the expected dictionary
        expected = build_config_dictionary({'config': main.config})
        assert main.__dict__ == expected
Exemplo n.º 14
0
    def test_interpolation(self):
        """
        Basic interpolation test
        """
        fp = StringIO(MINIMAL_CONFIG)
        c = Config(fp)
        main = c.get_server('main')

        # create the expected dictionary
        expected = build_config_dictionary({'config': main.config})
        assert main.__dict__ == expected
Exemplo n.º 15
0
    def test_config(self):
        fp = StringIO(TEST_CONFIG.format(**os.environ))
        c = Config(fp)

        main = c.get_server('main')
        expected = dict(config=c)
        expected.update(TEST_CONFIG_MAIN)
        assert main.__dict__ == expected

        web = c.get_server('web')
        expected = dict(config=c)
        expected.update(TEST_CONFIG_WEB)
        assert web.__dict__ == expected
Exemplo n.º 16
0
    def test_config(self):
        """
        Test for a basic configuration object construction
        """
        fp = StringIO(TEST_CONFIG.format(**os.environ))
        c = Config(fp)

        main = c.get_server('main')
        # create the expected dictionary
        expected = build_config_dictionary({
            'config': main.config,
            'compression': 'gzip',
            'last_backup_maximum_age': timedelta(1),
            'last_backup_minimum_size': 1048576,
            'last_wal_maximum_age': timedelta(hours=1),
            'retention_policy': 'redundancy 3',
            'reuse_backup': 'link',
            'description': 'Main PostgreSQL Database',
            'ssh_command': 'ssh -c arcfour -p 22 [email protected]',
            'wal_retention_policy': 'base',
            'custom_compression_filter': 'bzip2 -c -9',
            'wals_directory': 'wals',
            'custom_decompression_filter': 'bzip2 -c -d'
        })
        assert main.__dict__ == expected

        web = c.get_server('web')
        # create the expected dictionary
        expected = build_config_dictionary({
            'config': web.config,
            'backup_directory': '/some/barman/home/web',
            'basebackups_directory': '/some/barman/home/web/base',
            'compression': None,
            'conninfo': 'host=web01 user=postgres port=5432',
            'description': 'Web applications database',
            'incoming_wals_directory': '/some/barman/home/web/incoming',
            'name': 'web',
            'reuse_backup': None,
            'retention_policy': 'redundancy 2',
            'wals_directory': '/some/barman/home/web/wals',
            'wal_retention_policy': 'base',
            'last_backup_maximum_age': timedelta(1),
            'last_backup_minimum_size': 1048576,
            'last_wal_maximum_age': timedelta(hours=1),
            'ssh_command': 'ssh -I ~/.ssh/web01_rsa -c arcfour '
                           '-p 22 postgres@web01',
            'streaming_conninfo': 'host=web01 user=postgres port=5432',
            'streaming_wals_directory': '/some/barman/home/web/streaming',
            'errors_directory': '/some/barman/home/web/errors',
        })
        assert web.__dict__ == expected
Exemplo n.º 17
0
 def test_config_file_existence(self, capsys):
     """
     Test for the existence of a config file
     """
     # Check that an SystemExit is raised if no configuration
     # file is present inside the default configuration directories
     with patch('os.path.exists') as exists_mock:
         exists_mock.return_value = False
         with pytest.raises(SystemExit):
             Config(None)
     # Check that a SystemExit is raised if the user defined
     # configuration file does not exists
     with pytest.raises(SystemExit):
         Config('/very/fake/path/to.file')
Exemplo n.º 18
0
    def test_config(self):
        """
        Test for a basic configuration object construction
        """
        fp = StringIO(TEST_CONFIG.format(**os.environ))
        c = Config(fp)

        main = c.get_server('main')
        # create the expected dictionary
        expected = build_config_dictionary({
            'config': main.config,
            'compression': 'gzip',
            'last_backup_maximum_age': timedelta(1),
            'retention_policy': 'redundancy 3',
            'reuse_backup': 'link',
            'description': 'Main PostgreSQL Database',
            'ssh_command': 'ssh -c arcfour -p 22 [email protected]',
            'wal_retention_policy': 'base',
            'custom_compression_filter': 'bzip2 -c -9',
            'wals_directory': 'wals',
            'custom_decompression_filter': 'bzip2 -c -d',
            'backup_method': 'rsync'
        })
        assert main.__dict__ == expected

        web = c.get_server('web')
        # create the expected dictionary
        expected = build_config_dictionary({
            'config': web.config,
            'backup_directory': '/some/barman/home/web',
            'basebackups_directory': '/some/barman/home/web/base',
            'compression': None,
            'conninfo': 'host=web01 user=postgres port=5432',
            'description': 'Web applications database',
            'incoming_wals_directory': '/some/barman/home/web/incoming',
            'name': 'web',
            'reuse_backup': None,
            'retention_policy': 'redundancy 2',
            'wals_directory': '/some/barman/home/web/wals',
            'wal_retention_policy': 'base',
            'last_backup_maximum_age': timedelta(1),
            'ssh_command': 'ssh -I ~/.ssh/web01_rsa -c arcfour '
                           '-p 22 postgres@web01',
            'streaming_conninfo': 'host=web01 user=postgres port=5432',
            'streaming_wals_directory': '/some/barman/home/web/streaming',
            'errors_directory': '/some/barman/home/web/errors',
        })
        assert web.__dict__ == expected
Exemplo n.º 19
0
def main():
    parser = argparse.ArgumentParser(description="Barman plugin for NRPE.")
    parser.add_argument("-s", "--server", dest="server",
                        help="server to check")
    parser.add_argument('-w', '--warning', type=int, dest="warning",
                        metavar="W", help="warning threshold.")
    parser.add_argument('-c', '--critical', type=int, dest="critical",
                        metavar="C", help="critical threshold.")
    parser.add_argument('-u' '--user', dest='user', metavar='U',
                        help="user needed to run this script. If the "
                        "current user is not this one, the script will try " +
                        "to rerun itself using sudo.")

    subparsers = parser.add_subparsers()

    for key, value in ACTIONS.items():
        (action, help) = value
        subparser = subparsers.add_parser(key, help=help)
        subparser.set_defaults(action=action)

    parser.error = unknown

    args = parser.parse_args()

    user = pwd.getpwuid(os.getuid())[0]

    if args.user and user != args.user:
        import subprocess

        retval = subprocess.call(["/usr/bin/sudo", "-u", args.user] + sys.argv)
        raise SystemExit(retval)

    from barman.config import Config
    from barman.server import Server

    config = Config()
    config.load_configuration_files_directory()
    server = Server(config.get_server(args.server))

    try:
        args.action(server, args)
    except KeyError:
        unknown("The action %s does not exist." % args.action)
Exemplo n.º 20
0
 def test_quotes(self):
     fp = StringIO(MINIMAL_CONFIG.format(**os.environ))
     c = Config(fp)
     main = c.get_server('main')
     assert main.description == ' Text with quotes '
     assert main.ssh_command == 'ssh -c "arcfour" -p 22 postgres@pg01'
Exemplo n.º 21
0
 def test_server_list(self):
     fp = StringIO(TEST_CONFIG.format(**os.environ))
     c = Config(fp)
     dbs = c.server_names()
     self.assertEqual(set(dbs), set(['main', 'web']))
Exemplo n.º 22
0
 def test_server_list(self):
     fp = StringIO(TEST_CONFIG.format(**os.environ))
     c = Config(fp)
     dbs = c.server_names()
     self.assertEqual(set(dbs), set(['main', 'web']))