From f8bbe00d4714e5a929f464c715e1dbdc209a3f0c Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Fri, 2 Apr 2021 18:57:13 +0200 Subject: [PATCH] overall better handling and usage of exceptions --- bundles/apt/items.py | 7 ++++++- bundles/backup-server/items.py | 2 +- bundles/c3voc-addons/items.py | 13 ++++++++++--- bundles/dovecot/items.py | 2 +- bundles/element-web/items.py | 4 ++-- bundles/icinga2/items.py | 4 ++-- bundles/iptables/metadata.py | 6 ++++-- bundles/letsencrypt/items.py | 2 +- bundles/mx-puppet-discord/items.py | 2 +- bundles/pacman/items.py | 5 ++++- bundles/postfix/items.py | 2 +- bundles/postfixadmin/items.py | 4 ++-- bundles/powerdnsadmin/items.py | 4 ++-- bundles/rspamd/items.py | 2 +- bundles/sshmon/files/check_github_for_new_release | 4 +++- bundles/systemd-networkd/items.py | 2 +- bundles/voc-loudness-monitor/items.py | 2 +- bundles/wireguard/items.py | 2 +- bundles/zfs/files/check_zpool_space | 3 ++- items/zfs_dataset.py | 3 ++- libs/tools.py | 10 +++++++++- 21 files changed, 57 insertions(+), 28 deletions(-) diff --git a/bundles/apt/items.py b/bundles/apt/items.py index 193c76c..97e8039 100644 --- a/bundles/apt/items.py +++ b/bundles/apt/items.py @@ -1,3 +1,5 @@ +from bundlewrap.exceptions import BundleError + supported_os = { 'debian': { 10: 'buster', @@ -9,7 +11,10 @@ supported_os = { }, } -assert supported_os[node.os][node.os_version[0]], '{}: OS {} {} is not supported by bundle:apt'.format(node.name, node.os, node.os_version) +try: + supported_os[node.os][node.os_version[0]] +except (KeyError, IndexError): + raise BundleError(f'{node.name}: OS {node.os} {node.os_version} is not supported by bundle:apt') actions = { diff --git a/bundles/backup-server/items.py b/bundles/backup-server/items.py index c2c9d7f..662b5f7 100644 --- a/bundles/backup-server/items.py +++ b/bundles/backup-server/items.py @@ -1,4 +1,4 @@ -assert node.has_bundle('zfs') +repo.libs.tools.require_bundle(node, 'zfs') from os.path import join diff --git a/bundles/c3voc-addons/items.py b/bundles/c3voc-addons/items.py index 743d7f1..42b0904 100644 --- a/bundles/c3voc-addons/items.py +++ b/bundles/c3voc-addons/items.py @@ -1,6 +1,13 @@ -assert not node.has_bundle('apt'), f'{node.name} - bundle:apt is incompatible with ansible managed c3voc hosts' -assert not node.has_bundle('nginx'), f'{node.name} - bundle:nginx is incompatible with ansible managed c3voc hosts' -assert not node.has_bundle('users'), f'{node.name} - bundle:users is incompatible with ansible managed c3voc hosts' +from bundlewrap.exceptions import BundleError + +CONFLICTING_BUNDLES = { + 'apt', + 'nginx', + 'users', +} + +if any(node.has_bundle(i) for i in CONFLICTING_BUNDLES): + raise BundleError(f'{node.name}: bundle:c3voc-addons conflicts with bundles: {", ".join(sorted(CONFLICTING_BUNDLES))}') pkg_apt = { 'apt-transport-https': {}, diff --git a/bundles/dovecot/items.py b/bundles/dovecot/items.py index bca5d41..a10ebc2 100644 --- a/bundles/dovecot/items.py +++ b/bundles/dovecot/items.py @@ -1,6 +1,6 @@ # Postfix bundle creates metadata and directories which are also used # by this bundle -assert node.has_bundle('postfix') +repo.libs.tools.require_bundle(node, 'postfix') files = { '/etc/dovecot/dovecot.conf': { diff --git a/bundles/element-web/items.py b/bundles/element-web/items.py index 59f4a9f..4e2ed3d 100644 --- a/bundles/element-web/items.py +++ b/bundles/element-web/items.py @@ -1,7 +1,7 @@ -assert node.has_bundle('nodejs') - from bundlewrap.metadata import metadata_to_json +repo.libs.tools.require_bundle(node, 'nodejs') + element_web_root = '/var/www/{}'.format(node.metadata['element-web']['url']) directories = { diff --git a/bundles/icinga2/items.py b/bundles/icinga2/items.py index d4c26e4..7cc600a 100644 --- a/bundles/icinga2/items.py +++ b/bundles/icinga2/items.py @@ -1,5 +1,5 @@ -assert node.has_bundle('postgresql') -assert node.has_bundle('sshmon') +repo.libs.tools.require_bundle(node, 'postgresql') +repo.libs.tools.require_bundle(node, 'sshmon') from os.path import join diff --git a/bundles/iptables/metadata.py b/bundles/iptables/metadata.py index 40c5539..c7b1c6e 100644 --- a/bundles/iptables/metadata.py +++ b/bundles/iptables/metadata.py @@ -1,3 +1,5 @@ +from bundlewrap.exceptions import BundleError + @metadata_reactor.provides( 'iptables/bundle_rules/iptables', ) @@ -14,14 +16,14 @@ def port_rules_to_iptables(metadata): port, proto = portdef.split('/', 2) if proto not in {'udp'}: - raise Exception(f'iptables/port_rules: illegal identifier {portdef} in metadata for {node.name}') + raise BundleError(f'iptables/port_rules: illegal identifier {portdef} in metadata for {node.name}') else: port = portdef proto = 'tcp' for target in targets: if port == '*' and target == '*': - raise Exception('iptables/port_rules: setting both port and target to * is unsupported') + raise BundleError('iptables/port_rules: setting both port and target to * is unsupported') comment = f'-m comment --comment "iptables port_rules {target}"' diff --git a/bundles/letsencrypt/items.py b/bundles/letsencrypt/items.py index d55f12f..9a6f0ca 100644 --- a/bundles/letsencrypt/items.py +++ b/bundles/letsencrypt/items.py @@ -1,4 +1,4 @@ -assert node.has_bundle('nginx'), 'letsencrypt needs nginx' +repo.libs.tools.require_bundle(node, 'nginx', 'letsencrypt bundle needs nginx for http challenge') pkg_apt = { 'dehydrated': {}, diff --git a/bundles/mx-puppet-discord/items.py b/bundles/mx-puppet-discord/items.py index df1dad6..27bddea 100644 --- a/bundles/mx-puppet-discord/items.py +++ b/bundles/mx-puppet-discord/items.py @@ -1,4 +1,4 @@ -assert node.has_bundle('nodejs') +repo.libs.tools.require_bundle(node, 'nodejs') directories = { '/opt/mx-puppet-discord/src': { diff --git a/bundles/pacman/items.py b/bundles/pacman/items.py index 2975762..24c98f0 100644 --- a/bundles/pacman/items.py +++ b/bundles/pacman/items.py @@ -1,4 +1,7 @@ -assert node.os == 'arch' +from bundlewrap.exceptions import BundleError + +if not node.os == 'arch': + raise BundleError(f'{node.name}: bundle:pacman requires arch linux') # This is more targeted to GUI systems. This is intentional. diff --git a/bundles/postfix/items.py b/bundles/postfix/items.py index 9ee6227..3a7ff0b 100644 --- a/bundles/postfix/items.py +++ b/bundles/postfix/items.py @@ -1,5 +1,5 @@ if node.has_bundle('postfixadmin'): - assert node.has_bundle('letsencrypt') + repo.libs.tools.require_bundle(node, 'letsencrypt') mynetworks = { '127.0.0.0/8', diff --git a/bundles/postfixadmin/items.py b/bundles/postfixadmin/items.py index 558f85a..ffd5c6b 100644 --- a/bundles/postfixadmin/items.py +++ b/bundles/postfixadmin/items.py @@ -1,5 +1,5 @@ -assert node.has_bundle('php') -assert node.has_bundle('postfix') +repo.libs.tools.require_bundle(node, 'php') +repo.libs.tools.require_bundle(node, 'postfix') directories = { '/opt/postfixadmin': {}, diff --git a/bundles/powerdnsadmin/items.py b/bundles/powerdnsadmin/items.py index cb9345c..010776a 100644 --- a/bundles/powerdnsadmin/items.py +++ b/bundles/powerdnsadmin/items.py @@ -1,5 +1,5 @@ -assert node.has_bundle('nodejs') -assert node.has_bundle('postgresql') +repo.libs.tools.require_bundle(node, 'nodejs') +repo.libs.tools.require_bundle(node, 'postgresql') directories = { '/opt/powerdnsadmin/src': {}, diff --git a/bundles/rspamd/items.py b/bundles/rspamd/items.py index 4fd93ad..787ff3b 100644 --- a/bundles/rspamd/items.py +++ b/bundles/rspamd/items.py @@ -1,7 +1,7 @@ from os import listdir from os.path import join -assert node.has_bundle('redis') +repo.libs.tools.require_bundle(node, 'redis', 'rspamd does not work without a redis cache') directories = { '/etc/rspamd/local.d': { diff --git a/bundles/sshmon/files/check_github_for_new_release b/bundles/sshmon/files/check_github_for_new_release index e7cf747..e386f4b 100644 --- a/bundles/sshmon/files/check_github_for_new_release +++ b/bundles/sshmon/files/check_github_for_new_release @@ -3,7 +3,9 @@ from requests import get from sys import argv, exit -assert len(argv) == 3, 'Usage: check_github_for_new_release ' +if not len(argv) == 3: + print('Usage: check_github_for_new_release ') + exit(3) repo = argv[1] current_version = argv[2] diff --git a/bundles/systemd-networkd/items.py b/bundles/systemd-networkd/items.py index 316d8ed..72123d6 100644 --- a/bundles/systemd-networkd/items.py +++ b/bundles/systemd-networkd/items.py @@ -1,6 +1,6 @@ from bundlewrap.exceptions import BundleError -assert node.has_bundle('systemd') +repo.libs.tools.require_bundle(node, 'systemd') files = { '/etc/network/interfaces': { diff --git a/bundles/voc-loudness-monitor/items.py b/bundles/voc-loudness-monitor/items.py index 0617089..11bc989 100644 --- a/bundles/voc-loudness-monitor/items.py +++ b/bundles/voc-loudness-monitor/items.py @@ -1,4 +1,4 @@ -assert node.has_bundle('systemd') +repo.libs.tools.require_bundle(node, 'systemd') streams = { 's1': { diff --git a/bundles/wireguard/items.py b/bundles/wireguard/items.py index 65451c6..7f56ed9 100644 --- a/bundles/wireguard/items.py +++ b/bundles/wireguard/items.py @@ -1,6 +1,6 @@ from ipaddress import ip_network -assert node.has_bundle('systemd-networkd') +repo.libs.tools.require_bundle(node, 'systemd-networkd') network = ip_network(node.metadata['wireguard']['my_ip'], strict=False) diff --git a/bundles/zfs/files/check_zpool_space b/bundles/zfs/files/check_zpool_space index 7262bbd..23a2f80 100644 --- a/bundles/zfs/files/check_zpool_space +++ b/bundles/zfs/files/check_zpool_space @@ -9,7 +9,8 @@ import re def to_bytes(size): suffixes = ['', 'K', 'M', 'G', 'T', 'P'] number, suffix = re.match(r'([0-9\.]+)([A-Z]?)', size).groups() - assert suffix in suffixes, 'Unexpected suffix "{}" in size "{}"'.format(suffix, size) + if suffix not in suffixes: + print('Unexpected suffix "{}" in size "{}"'.format(suffix, size) return float(number) * 1024**suffixes.index(suffix) diff --git a/items/zfs_dataset.py b/items/zfs_dataset.py index 3735c24..90a7886 100644 --- a/items/zfs_dataset.py +++ b/items/zfs_dataset.py @@ -1,5 +1,6 @@ from pipes import quote +from bundlewrap.exceptions import BundleError from bundlewrap.items import Item from bundlewrap.utils.text import mark_for_translation as _ @@ -120,7 +121,7 @@ class ZFSDataset(Item): yield item.id if not pool_item_found: - raise Exception(_( + raise BundleError(_( "ZFS dataset {dataset} resides on pool {pool} but item " "{dep} does not exist" ).format( diff --git a/libs/tools.py b/libs/tools.py index 3050bb3..d96feec 100644 --- a/libs/tools.py +++ b/libs/tools.py @@ -1,6 +1,6 @@ from ipaddress import ip_address, ip_network, IPv4Address, IPv4Network -from bundlewrap.exceptions import NoSuchGroup, NoSuchNode +from bundlewrap.exceptions import NoSuchGroup, NoSuchNode, BundleError from bundlewrap.utils.text import bold, red from bundlewrap.utils.ui import io @@ -78,3 +78,11 @@ def remove_more_specific_subnets(input_subnets) -> list: out.append(str(net)) return out + + +def require_bundle(node, bundle, hint=''): + # It's considered bad style to use assert statements outside of tests. + # That's why this little helper function exists, so we have an easy + # way of defining bundle requirements in other bundles. + if not node.has_bundle(bundle): + raise BundleError(f'{node.name} requires bundle {bundle}, but wasn\'t found! {hint}')