def test_ping_healthchecks_hits_ping_url_with_append():
    ping_url = 'https://example.com'
    append = 'failed-so-hard'
    flexmock(module.requests).should_receive('get').with_args('{}/{}'.format(
        ping_url, append))

    module.ping_healthchecks(ping_url,
                             'config.yaml',
                             dry_run=False,
                             append=append)
def test_ping_healthchecks_hits_ping_url():
    ping_url = 'https://example.com'
    flexmock(module.requests).should_receive('get').with_args(ping_url)

    module.ping_healthchecks(ping_url, 'config.yaml', dry_run=False)
def test_ping_healthchecks_without_ping_url_does_not_raise():
    flexmock(module.requests).should_receive('get').never()

    module.ping_healthchecks(ping_url_or_uuid=None,
                             config_filename='config.yaml',
                             dry_run=False)
def test_ping_healthchecks_with_ping_uuid_hits_corresponding_url():
    ping_uuid = 'abcd-efgh-ijkl-mnop'
    flexmock(module.requests).should_receive('get').with_args(
        'https://hc-ping.com/{}'.format(ping_uuid))

    module.ping_healthchecks(ping_uuid, 'config.yaml', dry_run=False)
Exemple #5
0
def run_configuration(config_filename, config, arguments):
    '''
    Given a config filename, the corresponding parsed config dict, and command-line arguments as a
    dict from subparser name to a namespace of parsed arguments, execute its defined pruning,
    backups, consistency checks, and/or other actions.

    Yield a combination of:

      * JSON output strings from successfully executing any actions that produce JSON
      * logging.LogRecord instances containing errors from any actions or backup hooks that fail
    '''
    (location, storage, retention, consistency,
     hooks) = (config.get(section_name, {})
               for section_name in ('location', 'storage', 'retention',
                                    'consistency', 'hooks'))
    global_arguments = arguments['global']

    local_path = location.get('local_path', 'borg')
    remote_path = location.get('remote_path')
    borg_environment.initialize(storage)
    encountered_error = None
    error_repository = ''

    if 'create' in arguments:
        try:
            healthchecks.ping_healthchecks(hooks.get('healthchecks'),
                                           config_filename,
                                           global_arguments.dry_run, 'start')
            command.execute_hook(
                hooks.get('before_backup'),
                hooks.get('umask'),
                config_filename,
                'pre-backup',
                global_arguments.dry_run,
            )
            postgresql.dump_databases(hooks.get('postgresql_databases'),
                                      config_filename,
                                      global_arguments.dry_run)
        except (OSError, CalledProcessError) as error:
            encountered_error = error
            yield from make_error_log_records(
                '{}: Error running pre-backup hook'.format(config_filename),
                error)

    if not encountered_error:
        for repository_path in location['repositories']:
            try:
                yield from run_actions(
                    arguments=arguments,
                    location=location,
                    storage=storage,
                    retention=retention,
                    consistency=consistency,
                    local_path=local_path,
                    remote_path=remote_path,
                    repository_path=repository_path,
                )
            except (OSError, CalledProcessError) as error:
                encountered_error = error
                error_repository = repository_path
                yield from make_error_log_records(
                    '{}: Error running actions for repository'.format(
                        repository_path), error)

    if 'create' in arguments and not encountered_error:
        try:
            postgresql.remove_database_dumps(hooks.get('postgresql_databases'),
                                             config_filename,
                                             global_arguments.dry_run)
            command.execute_hook(
                hooks.get('after_backup'),
                hooks.get('umask'),
                config_filename,
                'post-backup',
                global_arguments.dry_run,
            )
            healthchecks.ping_healthchecks(hooks.get('healthchecks'),
                                           config_filename,
                                           global_arguments.dry_run)
        except (OSError, CalledProcessError) as error:
            encountered_error = error
            yield from make_error_log_records(
                '{}: Error running post-backup hook'.format(config_filename),
                error)

    if encountered_error:
        try:
            command.execute_hook(
                hooks.get('on_error'),
                hooks.get('umask'),
                config_filename,
                'on-error',
                global_arguments.dry_run,
                repository=error_repository,
                error=encountered_error,
                output=getattr(encountered_error, 'output', ''),
            )
            healthchecks.ping_healthchecks(hooks.get('healthchecks'),
                                           config_filename,
                                           global_arguments.dry_run, 'fail')
        except (OSError, CalledProcessError) as error:
            yield from make_error_log_records(
                '{}: Error running on-error hook'.format(config_filename),
                error)