overall better handling and usage of exceptions
bundlewrap/pipeline/head This commit looks good Details

This commit is contained in:
Franzi 2021-04-02 18:57:13 +02:00
parent 5d5930265a
commit f8bbe00d47
Signed by: kunsi
GPG Key ID: 12E3D2136B818350
21 changed files with 57 additions and 28 deletions

View File

@ -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 = {

View File

@ -1,4 +1,4 @@
assert node.has_bundle('zfs')
repo.libs.tools.require_bundle(node, 'zfs')
from os.path import join

View File

@ -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': {},

View File

@ -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': {

View File

@ -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 = {

View File

@ -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

View File

@ -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}"'

View File

@ -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': {},

View File

@ -1,4 +1,4 @@
assert node.has_bundle('nodejs')
repo.libs.tools.require_bundle(node, 'nodejs')
directories = {
'/opt/mx-puppet-discord/src': {

View File

@ -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.

View File

@ -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',

View File

@ -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': {},

View File

@ -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': {},

View File

@ -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': {

View File

@ -3,7 +3,9 @@
from requests import get
from sys import argv, exit
assert len(argv) == 3, 'Usage: check_github_for_new_release <repo> <version>'
if not len(argv) == 3:
print('Usage: check_github_for_new_release <repo> <version>')
exit(3)
repo = argv[1]
current_version = argv[2]

View File

@ -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': {

View File

@ -1,4 +1,4 @@
assert node.has_bundle('systemd')
repo.libs.tools.require_bundle(node, 'systemd')
streams = {
's1': {

View File

@ -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)

View File

@ -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)

View File

@ -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(

View File

@ -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}')