Add ntfy warning

This commit is contained in:
Paul Hoffmann 2023-03-30 16:02:19 +02:00
parent b27e625a77
commit 9d5d6bee37
5 changed files with 63 additions and 0 deletions

View File

@ -29,6 +29,7 @@ import borgmatic.actions.transfer
import borgmatic.commands.completion
from borgmatic.borg import umount as borg_umount
from borgmatic.borg import version as borg_version
from borgmatic.commands import warning
from borgmatic.commands.arguments import parse_arguments
from borgmatic.config import checks, collect, convert, validate
from borgmatic.hooks import command, dispatch, monitor
@ -151,6 +152,16 @@ def run_configuration(config_filename, config, arguments):
encountered_error = error
error_repository = repository['path']
except warning.BorgmaticWarning:
dispatch.call_hooks(
'ping_monitor',
hooks,
config_filename,
monitor.MONITOR_HOOK_NAMES,
monitor.State.WARN,
monitoring_log_level,
global_arguments.dry_run,
)
try:
if using_primary_action:
# send logs irrespective of error

View File

@ -0,0 +1,2 @@
class BorgmaticWarning(Warning):
pass

View File

@ -1179,6 +1179,29 @@ properties:
description: |
Tags to attach to the message.
example: incoming_envelope
warn:
type: object
properties:
title:
type: string
description: |
The title of the message.
example: Ping!
message:
type: string
description: |
The message body to publish.
example: Your backups ended with warnings
priority:
type: string
description: |
The priority to set.
example: urgent
tags:
type: string
description: |
Tags to attach to the message.
example: incoming_envelope
states:
type: array
items:
@ -1187,6 +1210,7 @@ properties:
- start
- finish
- fail
- warn
uniqueItems: true
description: |
List of one or more monitoring states to ping for:

View File

@ -3,12 +3,14 @@ import logging
import os
import select
import subprocess
from borgmatic.commands import warning
logger = logging.getLogger(__name__)
ERROR_OUTPUT_MAX_LINE_COUNT = 25
BORG_ERROR_EXIT_CODE = 2
BORG_WARNING_EXIT_CODE = 1
def exit_code_indicates_error(command, exit_code, borg_local_path=None):
@ -26,6 +28,21 @@ def exit_code_indicates_error(command, exit_code, borg_local_path=None):
return bool(exit_code != 0)
def exit_code_indicates_borg_warning(process, exit_code, borg_local_path=None):
'''
Return True if the given exit code from running a borg command corresponds to an borg warning.
'''
if exit_code is None:
return False
command = process.args.split(' ') if isinstance(process.args, str) else process.args
if borg_local_path and command[0] == borg_local_path:
return bool(exit_code == BORG_WARNING_EXIT_CODE)
return False
def command_for_process(process):
'''
Given a process as an instance of subprocess.Popen, return the command string that was used to
@ -134,6 +151,10 @@ def log_outputs(processes, exclude_stdouts, output_log_level, borg_local_path):
still_running = True
command = process.args.split(' ') if isinstance(process.args, str) else process.args
if exit_code_indicates_borg_warning(process, exit_code, borg_local_path):
raise warning.BorgmaticWarning()
# If any process errors, then raise accordingly.
if exit_code_indicates_error(command, exit_code, borg_local_path):
# If an error occurs, include its output in the raised exception so that we don't
@ -218,6 +239,7 @@ def execute_command(
do_not_capture = bool(output_file is DO_NOT_CAPTURE)
command = ' '.join(full_command) if shell else full_command
""" logging.warning(f"{do_not_capture, output_file, shell=}") """
process = subprocess.Popen(
command,
stdin=input_file,
@ -227,6 +249,9 @@ def execute_command(
env=environment,
cwd=working_directory,
)
""" logging.warning(f"{process.args}") """
""" logging.warning(f"{process.wait()}") """
if not run_to_completion:
return process

View File

@ -8,3 +8,4 @@ class State(Enum):
FINISH = 2
FAIL = 3
LOG = 4
WARN = 5