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', action='store_true',
help='Go through the motions, but do not actually write to any repositories', 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( global_group.add_argument(
'-nc', '--no-color', dest='no_color', action='store_true', help='Disable colored output' '-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 = '' error_repository = ''
using_primary_action = {'create', 'prune', 'compact', 'check'}.intersection(arguments) using_primary_action = {'create', 'prune', 'compact', 'check'}.intersection(arguments)
monitoring_log_level = verbosity_to_log_level(global_arguments.monitoring_verbosity) 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: try:
local_borg_version = borg_version.local_borg_version(storage, local_path) local_borg_version = borg_version.local_borg_version(storage, local_path)
@ -75,7 +78,7 @@ def run_configuration(config_filename, config, arguments):
return return
try: try:
if using_primary_action: if using_primary_action and not skip_monitoring:
dispatch.call_hooks( dispatch.call_hooks(
'initialize_monitor', 'initialize_monitor',
hooks, hooks,
@ -84,17 +87,18 @@ def run_configuration(config_filename, config, arguments):
monitoring_log_level, monitoring_log_level,
global_arguments.dry_run, global_arguments.dry_run,
) )
if using_primary_action:
dispatch.call_hooks( for action_name in action_names:
'ping_monitor', dispatch.call_hooks(
hooks, 'ping_monitor',
config_filename, hooks,
monitor.MONITOR_HOOK_NAMES, config_filename,
monitor.State.START, monitor.MONITOR_HOOK_NAMES,
monitoring_log_level, monitor.State.START,
global_arguments.dry_run, monitoring_log_level,
action_name, global_arguments.dry_run,
) action_name,
)
except (OSError, CalledProcessError) as error: except (OSError, CalledProcessError) as error:
if command.considered_soft_failure(config_filename, error): if command.considered_soft_failure(config_filename, error):
return return
@ -105,7 +109,9 @@ def run_configuration(config_filename, config, arguments):
if not encountered_error: if not encountered_error:
repo_queue = Queue() repo_queue = Queue()
for repo in location['repositories']: for repo in location['repositories']:
repo_queue.put((repo, 0),) repo_queue.put(
(repo, 0),
)
while not repo_queue.empty(): while not repo_queue.empty():
repository_path, retry_num = repo_queue.get() repository_path, retry_num = repo_queue.get()
@ -129,7 +135,9 @@ def run_configuration(config_filename, config, arguments):
) )
except (OSError, CalledProcessError, ValueError) as error: except (OSError, CalledProcessError, ValueError) as error:
if retry_num < retries: 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. tuple( # Consume the generator so as to trigger logging.
log_error_records( log_error_records(
f'{repository_path}: Error running actions for repository', f'{repository_path}: Error running actions for repository',
@ -153,18 +161,19 @@ def run_configuration(config_filename, config, arguments):
error_repository = repository_path error_repository = repository_path
try: try:
if using_primary_action: if using_primary_action and not skip_monitoring:
# send logs irrespective of error # send logs irrespective of error
dispatch.call_hooks( for action_name in action_names:
'ping_monitor', dispatch.call_hooks(
hooks, 'ping_monitor',
config_filename, hooks,
monitor.MONITOR_HOOK_NAMES, config_filename,
monitor.State.LOG, monitor.MONITOR_HOOK_NAMES,
monitoring_log_level, monitor.State.LOG,
global_arguments.dry_run, monitoring_log_level,
action_name, global_arguments.dry_run,
) action_name,
)
except (OSError, CalledProcessError) as error: except (OSError, CalledProcessError) as error:
if command.considered_soft_failure(config_filename, error): if command.considered_soft_failure(config_filename, error):
return return
@ -174,17 +183,18 @@ def run_configuration(config_filename, config, arguments):
if not encountered_error: if not encountered_error:
try: try:
if using_primary_action: if using_primary_action and not skip_monitoring:
dispatch.call_hooks( for action_name in action_names:
'ping_monitor', dispatch.call_hooks(
hooks, 'ping_monitor',
config_filename, hooks,
monitor.MONITOR_HOOK_NAMES, config_filename,
monitor.State.FINISH, monitor.MONITOR_HOOK_NAMES,
monitoring_log_level, monitor.State.FINISH,
global_arguments.dry_run, monitoring_log_level,
action_name, global_arguments.dry_run,
) action_name,
)
dispatch.call_hooks( dispatch.call_hooks(
'destroy_monitor', 'destroy_monitor',
hooks, hooks,
@ -212,16 +222,17 @@ def run_configuration(config_filename, config, arguments):
error=encountered_error, error=encountered_error,
output=getattr(encountered_error, 'output', ''), output=getattr(encountered_error, 'output', ''),
) )
dispatch.call_hooks( for action_name in action_names:
'ping_monitor', dispatch.call_hooks(
hooks, 'ping_monitor',
config_filename, hooks,
monitor.MONITOR_HOOK_NAMES, config_filename,
monitor.State.FAIL, monitor.MONITOR_HOOK_NAMES,
monitoring_log_level, monitor.State.FAIL,
global_arguments.dry_run, monitoring_log_level,
action_name, global_arguments.dry_run,
) action_name,
)
dispatch.call_hooks( dispatch.call_hooks(
'destroy_monitor', 'destroy_monitor',
hooks, hooks,
@ -411,19 +422,39 @@ def run_actions(
) )
elif action_name == 'rlist': elif action_name == 'rlist':
yield from borgmatic.actions.rlist.run_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': elif action_name == 'list':
yield from borgmatic.actions.list.run_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': elif action_name == 'rinfo':
yield from borgmatic.actions.rinfo.run_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': elif action_name == 'info':
yield from borgmatic.actions.info.run_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': elif action_name == 'break-lock':
borgmatic.actions.break_lock.run_break_lock( borgmatic.actions.break_lock.run_break_lock(
@ -436,7 +467,12 @@ def run_actions(
) )
elif action_name == 'borg': elif action_name == 'borg':
borgmatic.actions.borg.run_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( 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}") logger.info(f"Unmounting mount point {arguments['umount'].mount_point}")
try: try:
borg_umount.unmount_archive( 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: except (CalledProcessError, OSError) as error:
yield from log_error_records('Error unmounting mount point', 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: try:
ping_url = f"{hook_config[action_name]}/{MONITOR_STATE_TO_CRONITOR[state]}" ping_url = f"{hook_config[action_name]}/{MONITOR_STATE_TO_CRONITOR[state]}"
except KeyError: except KeyError:
logger.debug(
f'{config_filename}: Skipping Cronitor {state.name.lower()} ping due to unconfigured action: {action_name}'
)
return return
logger.info(f'{config_filename}: Pinging Cronitor {state.name.lower()}{dry_run_label}') logger.info(f'{config_filename}: Pinging Cronitor {state.name.lower()}{dry_run_label}')