incorporate PR review comments

This commit is contained in:
Pim Kunis 2023-09-23 21:11:15 +02:00 committed by pizzapim
parent e7252c7545
commit 21f4266273
3 changed files with 48 additions and 81 deletions

View file

@ -1311,15 +1311,28 @@ properties:
required: ['service_urls'] required: ['service_urls']
additionalProperties: false additionalProperties: false
properties: properties:
service_urls: services:
type: array type: object
items: required:
type: string - url
- label
properties:
url:
type: string
example: "mastodon://accesskey/host/?visibility=direct"
label:
type: string
example: mastodon
description: | description: |
List of Apprise service URLs to publish to. A list of Apprise services to publish to with URLs and labels.
The labels are used for logging.
A full list of services and their configuration can be found at
https://github.com/caronc/apprise/wiki.
example: example:
- "mastodon://accesskey/host/?visibility=direct" - url: "slack://xoxb-1234-1234-4ddbaae6f3523ada2d/#backups"
- "pagerduty://A1BRTD4JD@TIiajkdnlazkcOXrIdevi7F/node01.local/drive_sda/" label: slackbackups
- url: "matrixs://nuxref:abc123@matrix.example.com/#general/#backups"
label: matrix
start: start:
type: object type: object
properties: properties:
@ -1332,18 +1345,7 @@ properties:
type: string type: string
description: | description: |
Specify the message body. Specify the message body.
exampe: Your backups have failed. example: Starting backup process.
notification_type:
type: string
description: |
The Apprise message type.
enum:
- info
- success
- failure
- warning
example:
- failure
finish: finish:
type: object type: object
properties: properties:
@ -1356,18 +1358,7 @@ properties:
type: string type: string
description: | description: |
Specify the message body. Specify the message body.
exampe: Your backups have failed. example: Backups successfully made.
notification_type:
type: string
description: |
The Apprise message type.
enum:
- info
- success
- failure
- warning
example:
- failure
fail: fail:
type: object type: object
properties: properties:
@ -1380,18 +1371,7 @@ properties:
type: string type: string
description: | description: |
Specify the message body. Specify the message body.
exampe: Your backups have failed. example: Your backups have failed.
notification_type:
type: string
description: |
The Apprise message type.
enum:
- info
- success
- failure
- warning
example:
- failure
states: states:
type: array type: array
items: items:

View file

@ -5,6 +5,13 @@ from apprise import NotifyType, NotifyFormat
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
state_to_notify_type = {
'start': NotifyType.INFO,
'finish': NotifyType.SUCCESS,
'fail': NotifyType.FAILURE,
'log': NotifyType.INFO
}
def initialize_monitor( def initialize_monitor(
ping_url, config, config_filename, monitoring_log_level, dry_run ping_url, config, config_filename, monitoring_log_level, dry_run
@ -17,9 +24,8 @@ def initialize_monitor(
def ping_monitor(hook_config, config, config_filename, state, monitoring_log_level, dry_run): def ping_monitor(hook_config, config, config_filename, state, monitoring_log_level, dry_run):
''' '''
Ping the configured Apprise service URLs. Ping the configured Apprise service URLs. Use the given configuration filename in any log
Use the given configuration filename in any log entries. entries. If this is a dry run, then don't actually ping anything.
If this is a dry run, then don't actually ping anything.
''' '''
run_states = hook_config.get('states', ['fail']) run_states = hook_config.get('states', ['fail'])
@ -30,60 +36,38 @@ def ping_monitor(hook_config, config, config_filename, state, monitoring_log_lev
state.name.lower(), state.name.lower(),
{ {
'title': f'A borgmatic {state.name} event happened', 'title': f'A borgmatic {state.name} event happened',
'body': f'A borgmatic {state.name} event happened', 'body': f'A borgmatic {state.name} event happened'
'notification_type': default_notify_type(state.name.lower()),
}, },
) )
# TODO: Currently not very meaningful message. if not hook_config.get('services'):
# However, the Apprise service URLs can contain sensitive info. logger.info(f'{config_filename}: No Apprise services to ping')
dry_run_label = ' (dry run; not actually pinging)' if dry_run else '' return
logger.info(f'{config_filename}: Pinging Apprise {dry_run_label}')
logger.debug(f'{config_filename}: Using Apprise ping') dry_run_string = ' (dry run; not actually pinging)' if dry_run else ''
labels_string = ', '.join(map(lambda service: service['label'], hook_config.get('services')))
logger.info(f'{config_filename}: Pinging Apprise services: {labels_string}{dry_run_string}')
title = state_config.get('title', '') title = state_config.get('title', '')
body = state_config.get('body') body = state_config.get('body')
notify_type = state_config.get('notification_type', 'success') notify_type = state_to_notify_type[state.name.lower()]
apobj = apprise.Apprise() apprise_object = apprise.Apprise()
apobj.add(hook_config.get('service_urls')) apprise_object.add(map(lambda service: service['url'], hook_config.get('services')))
if dry_run: if dry_run:
return return
result = apobj.notify( result = apprise_object.notify(
title=title, title=title,
body=body, body=body,
body_format=NotifyFormat.TEXT, body_format=NotifyFormat.TEXT,
notify_type=get_notify_type(notify_type) notify_type=notify_type)
)
if result is False: if result is False:
logger.warning(f'{config_filename}: error sending some apprise notifications') logger.warning(f'{config_filename}: error sending some apprise notifications')
def get_notify_type(s):
if s == 'info':
return NotifyType.INFO
if s == 'success':
return NotifyType.SUCCESS
if s == 'warning':
return NotifyType.WARNING
if s == 'failure':
return NotifyType.FAILURE
def default_notify_type(state):
if state == 'start':
return NotifyType.INFO
if state == 'finish':
return NotifyType.SUCCESS
if state == 'fail':
return NotifyType.FAILURE
if state == 'log':
return NotifyType.INFO
def destroy_monitor( def destroy_monitor(
ping_url_or_uuid, config, config_filename, monitoring_log_level, dry_run ping_url_or_uuid, config, config_filename, monitoring_log_level, dry_run
): # pragma: no cover ): # pragma: no cover

View file

@ -37,6 +37,9 @@ setup(
'setuptools', 'setuptools',
'apprise' 'apprise'
), ),
extra_require={
"Apprise": ["apprise"]
},
include_package_data=True, include_package_data=True,
python_requires='>=3.7', python_requires='>=3.7',
) )