Compare commits

...

3 Commits

Author SHA1 Message Date
Chirag Aggarwal aa6f1e6623 Support multiple actions with ping_monitor
Signed-off-by: Chirag Aggarwal <thechiragaggarwal@gmail.com>
2023-03-29 15:55:17 +05:30
Chirag Aggarwal 2d728d57d7 minor; log on skipping cronitor due to KeyError
Signed-off-by: Chirag Aggarwal <thechiragaggarwal@gmail.com>
2023-03-29 14:13:54 +05:30
Chirag Aggarwal 6ce6367a26 --skip-monitoring flag
Signed-off-by: Chirag Aggarwal <thechiragaggarwal@gmail.com>
2023-03-29 03:32:05 +05:30
3 changed files with 100 additions and 53 deletions

View File

@ -145,6 +145,13 @@ def make_parsers():
action='store_true',
help='Go through the motions, but do not actually write to any repositories',
)
global_group.add_argument(
'-nm',
'--skip-monitoring',
dest='skip_monitoring',
action='store_true',
help='Skip reporting any data to the configured monitoring services',
)
global_group.add_argument(
'-nc', '--no-color', dest='no_color', action='store_true', help='Disable colored output'
)

View File

@ -66,7 +66,10 @@ def run_configuration(config_filename, config, arguments):
error_repository = ''
using_primary_action = {'create', 'prune', 'compact', 'check'}.intersection(arguments)
monitoring_log_level = verbosity_to_log_level(global_arguments.monitoring_verbosity)
action_name = next(iter(arguments))
skip_monitoring = getattr(global_arguments, 'skip_monitoring', False)
action_names = [
action for action in arguments.keys() if action != 'global' and isinstance(action, str)
]
try:
local_borg_version = borg_version.local_borg_version(storage, local_path)
@ -75,7 +78,7 @@ def run_configuration(config_filename, config, arguments):
return
try:
if using_primary_action:
if using_primary_action and not skip_monitoring:
dispatch.call_hooks(
'initialize_monitor',
hooks,
@ -84,17 +87,18 @@ def run_configuration(config_filename, config, arguments):
monitoring_log_level,
global_arguments.dry_run,
)
if using_primary_action:
dispatch.call_hooks(
'ping_monitor',
hooks,
config_filename,
monitor.MONITOR_HOOK_NAMES,
monitor.State.START,
monitoring_log_level,
global_arguments.dry_run,
action_name,
)
for action_name in action_names:
dispatch.call_hooks(
'ping_monitor',
hooks,
config_filename,
monitor.MONITOR_HOOK_NAMES,
monitor.State.START,
monitoring_log_level,
global_arguments.dry_run,
action_name,
)
except (OSError, CalledProcessError) as error:
if command.considered_soft_failure(config_filename, error):
return
@ -105,7 +109,9 @@ def run_configuration(config_filename, config, arguments):
if not encountered_error:
repo_queue = Queue()
for repo in location['repositories']:
repo_queue.put((repo, 0),)
repo_queue.put(
(repo, 0),
)
while not repo_queue.empty():
repository_path, retry_num = repo_queue.get()
@ -129,7 +135,9 @@ def run_configuration(config_filename, config, arguments):
)
except (OSError, CalledProcessError, ValueError) as error:
if retry_num < retries:
repo_queue.put((repository_path, retry_num + 1),)
repo_queue.put(
(repository_path, retry_num + 1),
)
tuple( # Consume the generator so as to trigger logging.
log_error_records(
f'{repository_path}: Error running actions for repository',
@ -153,18 +161,19 @@ def run_configuration(config_filename, config, arguments):
error_repository = repository_path
try:
if using_primary_action:
if using_primary_action and not skip_monitoring:
# send logs irrespective of error
dispatch.call_hooks(
'ping_monitor',
hooks,
config_filename,
monitor.MONITOR_HOOK_NAMES,
monitor.State.LOG,
monitoring_log_level,
global_arguments.dry_run,
action_name,
)
for action_name in action_names:
dispatch.call_hooks(
'ping_monitor',
hooks,
config_filename,
monitor.MONITOR_HOOK_NAMES,
monitor.State.LOG,
monitoring_log_level,
global_arguments.dry_run,
action_name,
)
except (OSError, CalledProcessError) as error:
if command.considered_soft_failure(config_filename, error):
return
@ -174,17 +183,18 @@ def run_configuration(config_filename, config, arguments):
if not encountered_error:
try:
if using_primary_action:
dispatch.call_hooks(
'ping_monitor',
hooks,
config_filename,
monitor.MONITOR_HOOK_NAMES,
monitor.State.FINISH,
monitoring_log_level,
global_arguments.dry_run,
action_name,
)
if using_primary_action and not skip_monitoring:
for action_name in action_names:
dispatch.call_hooks(
'ping_monitor',
hooks,
config_filename,
monitor.MONITOR_HOOK_NAMES,
monitor.State.FINISH,
monitoring_log_level,
global_arguments.dry_run,
action_name,
)
dispatch.call_hooks(
'destroy_monitor',
hooks,
@ -212,16 +222,17 @@ def run_configuration(config_filename, config, arguments):
error=encountered_error,
output=getattr(encountered_error, 'output', ''),
)
dispatch.call_hooks(
'ping_monitor',
hooks,
config_filename,
monitor.MONITOR_HOOK_NAMES,
monitor.State.FAIL,
monitoring_log_level,
global_arguments.dry_run,
action_name,
)
for action_name in action_names:
dispatch.call_hooks(
'ping_monitor',
hooks,
config_filename,
monitor.MONITOR_HOOK_NAMES,
monitor.State.FAIL,
monitoring_log_level,
global_arguments.dry_run,
action_name,
)
dispatch.call_hooks(
'destroy_monitor',
hooks,
@ -411,19 +422,39 @@ def run_actions(
)
elif action_name == 'rlist':
yield from borgmatic.actions.rlist.run_rlist(
repository, storage, local_borg_version, action_arguments, local_path, remote_path,
repository,
storage,
local_borg_version,
action_arguments,
local_path,
remote_path,
)
elif action_name == 'list':
yield from borgmatic.actions.list.run_list(
repository, storage, local_borg_version, action_arguments, local_path, remote_path,
repository,
storage,
local_borg_version,
action_arguments,
local_path,
remote_path,
)
elif action_name == 'rinfo':
yield from borgmatic.actions.rinfo.run_rinfo(
repository, storage, local_borg_version, action_arguments, local_path, remote_path,
repository,
storage,
local_borg_version,
action_arguments,
local_path,
remote_path,
)
elif action_name == 'info':
yield from borgmatic.actions.info.run_info(
repository, storage, local_borg_version, action_arguments, local_path, remote_path,
repository,
storage,
local_borg_version,
action_arguments,
local_path,
remote_path,
)
elif action_name == 'break-lock':
borgmatic.actions.break_lock.run_break_lock(
@ -436,7 +467,12 @@ def run_actions(
)
elif action_name == 'borg':
borgmatic.actions.borg.run_borg(
repository, storage, local_borg_version, action_arguments, local_path, remote_path,
repository,
storage,
local_borg_version,
action_arguments,
local_path,
remote_path,
)
command.execute_hook(
@ -629,7 +665,8 @@ def collect_configuration_run_summary_logs(configs, arguments):
logger.info(f"Unmounting mount point {arguments['umount'].mount_point}")
try:
borg_umount.unmount_archive(
mount_point=arguments['umount'].mount_point, local_path=get_local_path(configs),
mount_point=arguments['umount'].mount_point,
local_path=get_local_path(configs),
)
except (CalledProcessError, OSError) as error:
yield from log_error_records('Error unmounting mount point', error)

View File

@ -37,6 +37,9 @@ def ping_monitor(hook_config, config_filename, state, monitoring_log_level, dry_
try:
ping_url = f"{hook_config[action_name]}/{MONITOR_STATE_TO_CRONITOR[state]}"
except KeyError:
logger.debug(
f'{config_filename}: Skipping Cronitor {state.name.lower()} ping due to unconfigured action: {action_name}'
)
return
logger.info(f'{config_filename}: Pinging Cronitor {state.name.lower()}{dry_run_label}')