From a08b24dca5b882c8485dbc775b6ad6e978eb07e0 Mon Sep 17 00:00:00 2001 From: mwiegand Date: Thu, 24 Jun 2021 23:47:45 +0200 Subject: [PATCH] wip --- ...iface-nodhcp.network => interface.network} | 0 bundles/systemd-networkd/items.py | 119 ++---------------- bundles/systemd/items.py | 8 +- .../systemd/files/unitfile => libs/systemd.py | 11 +- nodes/htz.mails.py | 1 + 5 files changed, 18 insertions(+), 121 deletions(-) rename bundles/systemd-networkd/files/{template-iface-nodhcp.network => interface.network} (100%) rename bundles/systemd/files/unitfile => libs/systemd.py (76%) diff --git a/bundles/systemd-networkd/files/template-iface-nodhcp.network b/bundles/systemd-networkd/files/interface.network similarity index 100% rename from bundles/systemd-networkd/files/template-iface-nodhcp.network rename to bundles/systemd-networkd/files/interface.network diff --git a/bundles/systemd-networkd/items.py b/bundles/systemd-networkd/items.py index ca78e49..364810f 100644 --- a/bundles/systemd-networkd/items.py +++ b/bundles/systemd-networkd/items.py @@ -1,24 +1,14 @@ assert node.has_bundle('systemd') -from bundlewrap.exceptions import BundleError - - files = { '/etc/network/interfaces': { 'delete': True, }, } -if node.metadata.get('systemd-networkd/enable-resolved', False): - symlinks['/etc/resolv.conf'] = { - 'target': '/run/systemd/resolve/stub-resolv.conf', - } - svc_systemd['systemd-resolved'] = {} -else: - files['/etc/resolv.conf'] = { - 'content_type': 'mako', - } - +files['/etc/resolv.conf'] = { + 'content_type': 'mako', +} directories = { '/etc/systemd/network': { @@ -26,108 +16,13 @@ directories = { }, } -mac_host_prefix = '%04x' % (node.magic_number % 65534) -generated_mac = f'52:54:00:{mac_host_prefix[0:2]}:{mac_host_prefix[2:4]}:{{}}' - -# Don't use .get() here. We might end up with a node without a network -# config! for interface, config in node.metadata['interfaces'].items(): - if config.get('dhcp', False): - if 'vlans' in config: - raise BundleError(f'{node.name} interface {interface} cannot use vlans and dhcp!') - template = 'template-iface-dhcp.network' - else: - template = 'template-iface-nodhcp.network' - - if '.' in interface: - vlan_id = int(interface.split('.')[1]) - vlan_hex = '%02x' % (vlan_id % 255) - files['/etc/systemd/network/60-iface-{}.netdev'.format(interface)] = { - 'source': 'template-iface-vlan.netdev', - 'content_type': 'mako', - 'context': { - 'interface': interface, - 'vlan': vlan_id, - 'mac': generated_mac.format(vlan_hex) - }, - 'needed_by': { - 'svc_systemd:systemd-networkd', - }, - 'triggers': { - 'svc_systemd:systemd-networkd:restart', - }, - } - weight = 61 - else: - weight = 50 - - if not config.get('ignore', False): - files['/etc/systemd/network/{}-iface-{}.network'.format(weight, interface)] = { - 'source': template, - 'content_type': 'mako', - 'context': { - 'interface': interface, - 'config': config, - }, - 'needed_by': { - 'svc_systemd:systemd-networkd', - }, - 'triggers': { - 'svc_systemd:systemd-networkd:restart', - }, - } - -for bond, config in node.metadata.get('systemd-networkd/bonds', {}).items(): - files['/etc/systemd/network/20-bond-{}.netdev'.format(bond)] = { - 'source': 'template-bond.netdev', + files[f'/etc/systemd/network/{interface}.network'] = { + 'source': 'interface.network', 'content_type': 'mako', 'context': { - 'bond': bond, - 'mode': config.get('mode', '802.3ad'), - 'prio': config.get('priority', '32768'), - }, - 'needed_by': { - 'svc_systemd:systemd-networkd', - }, - 'triggers': { - 'svc_systemd:systemd-networkd:restart', - }, - } - files['/etc/systemd/network/21-bond-{}.network'.format(bond)] = { - 'source': 'template-bond.network', - 'content_type': 'mako', - 'context': { - 'bond': bond, - 'match': config['match'], - }, - 'needed_by': { - 'svc_systemd:systemd-networkd', - }, - 'triggers': { - 'svc_systemd:systemd-networkd:restart', - }, - } - -for brname, config in node.metadata.get('systemd-networkd/bridges', {}).items(): - files['/etc/systemd/network/30-bridge-{}.netdev'.format(brname)] = { - 'source': 'template-bridge.netdev', - 'content_type': 'mako', - 'context': { - 'bridge': brname, - }, - 'needed_by': { - 'svc_systemd:systemd-networkd', - }, - 'triggers': { - 'svc_systemd:systemd-networkd:restart', - }, - } - files['/etc/systemd/network/31-bridge-{}.network'.format(brname)] = { - 'source': 'template-bridge.network', - 'content_type': 'mako', - 'context': { - 'bridge': brname, - 'match': config['match'], + 'interface': interface, + 'config': config, }, 'needed_by': { 'svc_systemd:systemd-networkd', diff --git a/bundles/systemd/items.py b/bundles/systemd/items.py index f282162..78380c5 100644 --- a/bundles/systemd/items.py +++ b/bundles/systemd/items.py @@ -28,12 +28,8 @@ for name, service in node.metadata.get('systemd/services').items(): # create unit file unit_path = f'/etc/systemd/system/{name}.service' files[unit_path] = { - 'source': 'unitfile', - 'content_type': 'mako', - 'context': { - 'data': content_data, - }, - 'triggers': [ + 'content': repo.libs.systemd.generate_unitfile(content_data), + 'triggers': [ 'action:systemd-reload', f'svc_systemd:{name}:restart', ], diff --git a/bundles/systemd/files/unitfile b/libs/systemd.py similarity index 76% rename from bundles/systemd/files/unitfile rename to libs/systemd.py index 89d7d1b..01f123e 100644 --- a/bundles/systemd/files/unitfile +++ b/libs/systemd.py @@ -1,7 +1,8 @@ -% for i, (segment, options) in enumerate(data.items()): -% if i > 0: +from mako.template import Template + +template = ''' +% for i, (segment, options) in enumerate(data.items()): -% endif [${segment}] % for option, value in options.items(): % if isinstance(value, dict): @@ -17,3 +18,7 @@ ${option}=${value} % endif % endfor % endfor +''' + +def generate_unitfile(data): + return Template(template).render(data=data).lstrip() diff --git a/nodes/htz.mails.py b/nodes/htz.mails.py index b997087..82b760e 100644 --- a/nodes/htz.mails.py +++ b/nodes/htz.mails.py @@ -82,6 +82,7 @@ 'cpu': 2, }, 'wireguard': { + # ip r add 10.0.0.0/24 via 172.19.136.2 dev wg0 'my_ip': '172.19.136.2/22', 'peers': { 'home.server': {},