本文整理汇总了Python中barman.hooks.HookScriptRunner类的典型用法代码示例。如果您正苦于以下问题:Python HookScriptRunner类的具体用法?Python HookScriptRunner怎么用?Python HookScriptRunner使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HookScriptRunner类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_backup_info_corner_cases
def test_backup_info_corner_cases(self, command_mock):
# BackupManager mock
backup_manager = self.build_backup_manager('test_server', 'test_file')
backup_manager.config.post_test_hook = 'not_existent_script'
backup_manager.get_previous_backup.return_value = None
# BackupInfo mock
backup_info = MagicMock(name='backup_info')
backup_info.get_basebackup_directory.return_value = 'backup_directory'
backup_info.backup_id = '123456789XYZ'
backup_info.error = 'Test error'
backup_info.status = 'FAILED'
# the actual test
script = HookScriptRunner(backup_manager, 'test_hook', 'post')
script.env_from_backup_info(backup_info)
expected_env = {
'BARMAN_PHASE': 'post',
'BARMAN_VERSION': version,
'BARMAN_SERVER': 'test_server',
'BARMAN_CONFIGURATION': 'test_file',
'BARMAN_HOOK': 'test_hook',
'BARMAN_BACKUP_DIR': 'backup_directory',
'BARMAN_BACKUP_ID': '123456789XYZ',
'BARMAN_ERROR': 'Test error',
'BARMAN_STATUS': 'FAILED',
'BARMAN_PREVIOUS_ID': '',
}
script.run()
assert command_mock.call_count == 1
assert command_mock.call_args[1]['env_append'] == expected_env
示例2: test_wal_info_corner_cases
def test_wal_info_corner_cases(self, command_mock):
# BackupManager mock
backup_manager = build_backup_manager(name="test_server")
backup_manager.config.pre_test_hook = "not_existent_script"
# WalFileInfo mock
timestamp = time.time()
wal_info = MagicMock(name="wal_info")
wal_info.name = "XXYYZZAABBCC"
wal_info.size = 1234567
wal_info.time = timestamp
wal_info.compression = None
wal_info.fullpath.return_value = "/incoming/directory"
# the actual test
script = HookScriptRunner(backup_manager, "test_hook", "pre")
script.env_from_wal_info(wal_info)
expected_env = {
"BARMAN_PHASE": "pre",
"BARMAN_VERSION": version,
"BARMAN_SERVER": "test_server",
"BARMAN_CONFIGURATION": "build_config_from_dicts",
"BARMAN_HOOK": "test_hook",
"BARMAN_SEGMENT": "XXYYZZAABBCC",
"BARMAN_FILE": "/incoming/directory",
"BARMAN_SIZE": "1234567",
"BARMAN_TIMESTAMP": str(timestamp),
"BARMAN_COMPRESSION": "",
}
script.run()
assert command_mock.call_count == 1
assert command_mock.call_args[1]["env_append"] == expected_env
示例3: test_wal_info_corner_cases
def test_wal_info_corner_cases(self, command_mock):
# BackupManager mock
backup_manager = build_backup_manager(name='test_server')
backup_manager.config.pre_test_hook = 'not_existent_script'
# WalFileInfo mock
timestamp = time.time()
wal_info = MagicMock(name='wal_info')
wal_info.name = 'XXYYZZAABBCC'
wal_info.size = 1234567
wal_info.time = timestamp
wal_info.compression = None
wal_info.fullpath.return_value = '/incoming/directory'
# the actual test
script = HookScriptRunner(backup_manager, 'test_hook', 'pre')
script.env_from_wal_info(wal_info, '/somewhere', Exception('BOOM!'))
expected_env = {
'BARMAN_PHASE': 'pre',
'BARMAN_VERSION': version,
'BARMAN_SERVER': 'test_server',
'BARMAN_CONFIGURATION': 'build_config_from_dicts',
'BARMAN_HOOK': 'test_hook',
'BARMAN_SEGMENT': 'XXYYZZAABBCC',
'BARMAN_FILE': '/somewhere',
'BARMAN_SIZE': '1234567',
'BARMAN_TIMESTAMP': str(timestamp),
'BARMAN_COMPRESSION': '',
'BARMAN_RETRY': '0',
'BARMAN_ERROR': 'BOOM!',
}
script.run()
assert command_mock.call_count == 1
assert command_mock.call_args[1]['env_append'] == expected_env
示例4: test_backup_info_exception
def test_backup_info_exception(self, command_mock):
# BackupManager mock
backup_manager = build_backup_manager(name="test_server")
backup_manager.config.pre_test_hook = "not_existent_script"
backup_manager.get_previous_backup = MagicMock()
backup_manager.get_previous_backup.side_effect = UnknownBackupIdException()
# BackupInfo mock
backup_info = MagicMock(name="backup_info")
backup_info.get_basebackup_directory.return_value = "backup_directory"
backup_info.backup_id = "123456789XYZ"
backup_info.error = None
backup_info.status = "OK"
# the actual test
script = HookScriptRunner(backup_manager, "test_hook", "pre")
script.env_from_backup_info(backup_info)
expected_env = {
"BARMAN_PHASE": "pre",
"BARMAN_VERSION": version,
"BARMAN_SERVER": "test_server",
"BARMAN_CONFIGURATION": "build_config_from_dicts",
"BARMAN_HOOK": "test_hook",
"BARMAN_BACKUP_DIR": "backup_directory",
"BARMAN_BACKUP_ID": "123456789XYZ",
"BARMAN_ERROR": "",
"BARMAN_STATUS": "OK",
"BARMAN_PREVIOUS_ID": "",
}
script.run()
assert command_mock.call_count == 1
assert command_mock.call_args[1]["env_append"] == expected_env
示例5: test_wal_info
def test_wal_info(self, command_mock):
# BackupManager mock
backup_manager = self.build_backup_manager('test_server', 'test_file')
backup_manager.config.pre_test_hook = 'not_existent_script'
# WalFileInfo mock
wal_info = MagicMock(name='wal_info')
wal_info.name = 'XXYYZZAABBCC'
wal_info.size = 1234567
wal_info.time = 1337133713
wal_info.compression = 'gzip'
wal_info.fullpath.return_value = '/incoming/directory'
# the actual test
script = HookScriptRunner(backup_manager, 'test_hook', 'pre')
script.env_from_wal_info(wal_info)
expected_env = {
'BARMAN_PHASE': 'pre',
'BARMAN_VERSION': version,
'BARMAN_SERVER': 'test_server',
'BARMAN_CONFIGURATION': 'test_file',
'BARMAN_HOOK': 'test_hook',
'BARMAN_SEGMENT': 'XXYYZZAABBCC',
'BARMAN_FILE': '/incoming/directory',
'BARMAN_SIZE': '1234567',
'BARMAN_TIMESTAMP': '1337133713',
'BARMAN_COMPRESSION': 'gzip',
}
script.run()
assert command_mock.call_count == 1
assert command_mock.call_args[1]['env_append'] == expected_env
示例6: test_backup_info_exception
def test_backup_info_exception(self, command_mock):
# BackupManager mock
backup_manager = build_backup_manager(name='test_server')
backup_manager.config.pre_test_hook = 'not_existent_script'
backup_manager.get_previous_backup = MagicMock()
backup_manager.get_previous_backup.side_effect = \
UnknownBackupIdException()
# BackupInfo mock
backup_info = MagicMock(name='backup_info')
backup_info.get_basebackup_directory.return_value = 'backup_directory'
backup_info.backup_id = '123456789XYZ'
backup_info.error = None
backup_info.status = 'OK'
# the actual test
script = HookScriptRunner(backup_manager, 'test_hook', 'pre')
script.env_from_backup_info(backup_info)
expected_env = {
'BARMAN_PHASE': 'pre',
'BARMAN_VERSION': version,
'BARMAN_SERVER': 'test_server',
'BARMAN_CONFIGURATION': 'build_config_from_dicts',
'BARMAN_HOOK': 'test_hook',
'BARMAN_BACKUP_DIR': 'backup_directory',
'BARMAN_BACKUP_ID': '123456789XYZ',
'BARMAN_ERROR': '',
'BARMAN_STATUS': 'OK',
'BARMAN_PREVIOUS_ID': '',
'BARMAN_RETRY': '0',
}
script.run()
assert command_mock.call_count == 1
assert command_mock.call_args[1]['env_append'] == expected_env
示例7: backup
def backup(self):
"""
Performs a backup for the server
"""
_logger.debug("initialising backup information")
self.executor.init()
backup_info = None
try:
# Create the BackupInfo object representing the backup
backup_info = BackupInfo(
self.server,
backup_id=datetime.datetime.now().strftime('%Y%m%dT%H%M%S'))
backup_info.save()
self.backup_cache_add(backup_info)
output.info(
"Starting backup for server %s in %s",
self.config.name,
backup_info.get_basebackup_directory())
# Run the pre-backup-script if present.
script = HookScriptRunner(self, 'backup_script', 'pre')
script.env_from_backup_info(backup_info)
script.run()
# Run the pre-backup-retry-script if present.
retry_script = RetryHookScriptRunner(
self, 'backup_retry_script', 'pre')
retry_script.env_from_backup_info(backup_info)
retry_script.run()
# Do the backup using the BackupExecutor
self.executor.backup(backup_info)
# Compute backup size and fsync it on disk
self.backup_fsync_and_set_sizes(backup_info)
# Mark the backup as DONE
backup_info.set_attribute("status", "DONE")
# Use BaseException instead of Exception to catch events like
# KeyboardInterrupt (e.g.: CRTL-C)
except BaseException, e:
msg_lines = str(e).strip().splitlines()
if backup_info:
# Use only the first line of exception message
# in backup_info error field
backup_info.set_attribute("status", "FAILED")
# If the exception has no attached message use the raw type name
if len(msg_lines) == 0:
msg_lines = [type(e).__name__]
backup_info.set_attribute(
"error",
"failure %s (%s)" % (
self.executor.current_action, msg_lines[0]))
output.error("Backup failed %s.\nDETAILS: %s\n%s",
self.executor.current_action, msg_lines[0],
'\n'.join(msg_lines[1:]))
示例8: test_no_exception
def test_no_exception(self, command_mock):
# BackupManager mock
backup_manager = build_backup_manager(name='test_server')
backup_manager.config.pre_test_hook = 'not_existent_script'
# Command mock executed by HookScriptRunner
expected_exception = Exception('Test error')
command_mock.side_effect = expected_exception
command_mock.return_value.return_value = 0
# the actual test
script = HookScriptRunner(backup_manager, 'test_hook', 'pre')
assert script.run() is None # exception
assert script.exception == expected_exception
assert command_mock.call_count == 1
示例9: test_missing_config
def test_missing_config(self, command_mock):
# BackupManager mock
backup_manager = self.build_backup_manager('test_server', 'test_file')
# if configuration line is missing then the script is disabled
del backup_manager.config.pre_test_hook
# Command mock executed by HookScriptRunner
command_mock.side_effect = Exception('Test error')
command_mock.return_value.return_value = 0
# the actual test
script = HookScriptRunner(backup_manager, 'test_hook', 'pre')
assert script.run() is None # disabled script
assert script.exception is None
assert command_mock.call_count == 0
示例10: test_missing_config
def test_missing_config(self, command_mock):
# BackupManager mock
backup_manager = build_backup_manager(name='test_server')
# Make sure 'pre_test_hook' doesn't exists in configuration
# (it should not happen)
if hasattr(backup_manager.config, 'pre_test_hook'):
del backup_manager.config.pre_test_hook
# Command mock executed by HookScriptRunner
command_mock.side_effect = Exception('Test error')
command_mock.return_value.return_value = 0
# the actual test
script = HookScriptRunner(backup_manager, 'test_hook', 'pre')
assert script.run() is None # disabled script
assert script.exception is None
assert command_mock.call_count == 0
示例11: test_general_no_phase
def test_general_no_phase(self, command_mock):
# BackupManager mock
backup_manager = self.build_backup_manager('test_server', 'test_file')
backup_manager.config.test_hook = 'not_existent_script'
# Command mock executed by HookScriptRunner
command_mock.return_value.return_value = 0
# the actual test
script = HookScriptRunner(backup_manager, 'test_hook')
expected_env = {
'BARMAN_VERSION': version,
'BARMAN_SERVER': 'test_server',
'BARMAN_CONFIGURATION': 'test_file',
'BARMAN_HOOK': 'test_hook',
}
assert script.run() == 0
assert command_mock.call_count == 1
assert command_mock.call_args[1]['env_append'] == expected_env
示例12: cron_wal_archival
def cron_wal_archival(self, compressor, wal_info):
"""
Archive a WAL segment from the incoming directory.
This function returns a WalFileInfo object.
:param compressor: the compressor for the file (if any)
:param wal_info: WalFileInfo of the WAL file is being processed
"""
destfile = wal_info.fullpath(self.server)
destdir = os.path.dirname(destfile)
srcfile = os.path.join(self.config.incoming_wals_directory,
wal_info.name)
# Run the pre_archive_script if present.
script = HookScriptRunner(self, 'archive_script', 'pre')
script.env_from_wal_info(wal_info, srcfile)
script.run()
mkpath(destdir)
if compressor:
compressor.compress(srcfile, destfile)
shutil.copystat(srcfile, destfile)
os.unlink(srcfile)
else:
shutil.move(srcfile, destfile)
# execute fsync() on the archived WAL containing directory
fsync_dir(destdir)
# execute fsync() also on the incoming directory
fsync_dir(self.config.incoming_wals_directory)
# execute fsync() on the archived WAL file
file_fd = os.open(destfile, os.O_RDONLY)
os.fsync(file_fd)
os.close(file_fd)
stat = os.stat(destfile)
wal_info.size = stat.st_size
wal_info.compression = compressor and compressor.compression
# Run the post_archive_script if present.
script = HookScriptRunner(self, 'archive_script', 'post')
script.env_from_wal_info(wal_info)
script.run()
示例13: test_general_no_phase
def test_general_no_phase(self, command_mock):
# BackupManager mock
backup_manager = build_backup_manager(name="test_server")
backup_manager.config.test_hook = "not_existent_script"
# Command mock executed by HookScriptRunner
command_mock.return_value.return_value = 0
# the actual test
script = HookScriptRunner(backup_manager, "test_hook")
expected_env = {
"BARMAN_VERSION": version,
"BARMAN_SERVER": "test_server",
"BARMAN_CONFIGURATION": "build_config_from_dicts",
"BARMAN_HOOK": "test_hook",
}
assert script.run() == 0
assert command_mock.call_count == 1
assert command_mock.call_args[1]["env_append"] == expected_env
示例14: test_general
def test_general(self, command_mock):
# BackupManager mock
backup_manager = build_backup_manager(name='test_server')
backup_manager.config.pre_test_hook = 'not_existent_script'
# Command mock executed by HookScriptRunner
command_mock.return_value.return_value = 0
# the actual test
script = HookScriptRunner(backup_manager, 'test_hook', 'pre')
expected_env = {
'BARMAN_PHASE': 'pre',
'BARMAN_VERSION': version,
'BARMAN_SERVER': 'test_server',
'BARMAN_CONFIGURATION': 'build_config_from_dicts',
'BARMAN_HOOK': 'test_hook',
'BARMAN_RETRY': '0',
}
assert script.run() == 0
assert command_mock.call_count == 1
assert command_mock.call_args[1]['env_append'] == expected_env
示例15: archive_wal
def archive_wal(self, compressor, wal_info):
"""
Archive a WAL segment and update the wal_info object
:param compressor: the compressor for the file (if any)
:param WalFileInfo wal_info: the WAL file is being processed
"""
src_file = wal_info.orig_filename
src_dir = os.path.dirname(src_file)
dst_file = wal_info.fullpath(self.server)
tmp_file = dst_file + '.tmp'
dst_dir = os.path.dirname(dst_file)
error = None
try:
# Run the pre_archive_script if present.
script = HookScriptRunner(self.backup_manager,
'archive_script', 'pre')
script.env_from_wal_info(wal_info, src_file)
script.run()
# Run the pre_archive_retry_script if present.
retry_script = RetryHookScriptRunner(self.backup_manager,
'archive_retry_script',
'pre')
retry_script.env_from_wal_info(wal_info, src_file)
retry_script.run()
# Check if destination already exists
if os.path.exists(dst_file):
src_uncompressed = src_file
dst_uncompressed = dst_file
dst_info = WalFileInfo.from_file(dst_file)
try:
comp_manager = self.backup_manager.compression_manager
if dst_info.compression is not None:
dst_uncompressed = dst_file + '.uncompressed'
comp_manager.get_compressor(
compression=dst_info.compression).decompress(
dst_file, dst_uncompressed)
if wal_info.compression:
src_uncompressed = src_file + '.uncompressed'
comp_manager.get_compressor(
compression=wal_info.compression).decompress(
src_file, src_uncompressed)
# Directly compare files.
# When the files are identical
# raise a MatchingDuplicateWalFile exception,
# otherwise raise a DuplicateWalFile exception.
if filecmp.cmp(dst_uncompressed, src_uncompressed):
raise MatchingDuplicateWalFile(wal_info)
else:
raise DuplicateWalFile(wal_info)
finally:
if src_uncompressed != src_file:
os.unlink(src_uncompressed)
if dst_uncompressed != dst_file:
os.unlink(dst_uncompressed)
mkpath(dst_dir)
# Compress the file only if not already compressed
if compressor and not wal_info.compression:
compressor.compress(src_file, tmp_file)
shutil.copystat(src_file, tmp_file)
os.rename(tmp_file, dst_file)
os.unlink(src_file)
# Update wal_info
stat = os.stat(dst_file)
wal_info.size = stat.st_size
wal_info.compression = compressor.compression
else:
# Try to atomically rename the file. If successful,
# the renaming will be an atomic operation
# (this is a POSIX requirement).
try:
os.rename(src_file, dst_file)
except OSError:
# Source and destination are probably on different
# filesystems
shutil.copy2(src_file, tmp_file)
os.rename(tmp_file, dst_file)
os.unlink(src_file)
# At this point the original file has been removed
wal_info.orig_filename = None
# Execute fsync() on the archived WAL file
file_fd = os.open(dst_file, os.O_RDONLY)
os.fsync(file_fd)
os.close(file_fd)
# Execute fsync() on the archived WAL containing directory
fsync_dir(dst_dir)
# Execute fsync() also on the incoming directory
fsync_dir(src_dir)
except Exception as e:
# In case of failure save the exception for the post scripts
error = e
raise
# Ensure the execution of the post_archive_retry_script and
#.........这里部分代码省略.........