diff --git a/bundles/gitea/items.py b/bundles/gitea/items.py index 3819d77..416406a 100644 --- a/bundles/gitea/items.py +++ b/bundles/gitea/items.py @@ -4,7 +4,7 @@ downloads['/usr/local/bin/gitea'] = { 'url': f'https://dl.gitea.io/gitea/{version}/gitea-{version}-linux-amd64', 'sha256': node.metadata.get('gitea/sha256'), 'triggers': { - 'svc_systemd:gitea:restart', + 'svc_systemd:gitea.service:restart', }, 'preceded_by': { 'action:stop_gitea', @@ -17,7 +17,7 @@ directories['/var/lib/gitea'] = { 'owner': 'git', 'mode': '0700', 'triggers': { - 'svc_systemd:gitea:restart', + 'svc_systemd:gitea.service:restart', }, } @@ -40,6 +40,6 @@ files['/etc/gitea/app.ini'] = { 'owner': 'git', 'context': node.metadata['gitea'], 'triggers': { - 'svc_systemd:gitea:restart', + 'svc_systemd:gitea.service:restart', }, } diff --git a/bundles/gitea/metadata.py b/bundles/gitea/metadata.py index fcfad64..fd4c123 100644 --- a/bundles/gitea/metadata.py +++ b/bundles/gitea/metadata.py @@ -28,8 +28,8 @@ defaults = { }, }, 'systemd': { - 'services': { - 'gitea': { + 'units': { + 'gitea.service': { 'content': { 'Unit': { 'Description': 'gitea', @@ -51,12 +51,13 @@ defaults = { 'WantedBy': 'multi-user.target', }, }, - 'needs': [ - 'action:chmod_gitea', - 'download:/usr/local/bin/gitea', - 'file:/etc/systemd/system/gitea.service', - 'file:/etc/gitea/app.ini', - ], + 'item': { + 'needs': [ + 'action:chmod_gitea', + 'download:/usr/local/bin/gitea', + 'file:/etc/gitea/app.ini', + ], + }, }, }, }, diff --git a/bundles/network/metadata.py b/bundles/network/metadata.py index bd97df3..c579ad8 100644 --- a/bundles/network/metadata.py +++ b/bundles/network/metadata.py @@ -7,31 +7,33 @@ defaults = { @metadata_reactor.provides( - 'systemd-networkd/networks', + 'systemd/units', ) -def systemd_networkd(metadata): +def units(metadata): units = {} for type, network in metadata.get('network').items(): - units[type] = { - 'Match': { - 'Name': network['interface'], - }, - 'Network': { - 'DHCP': 'no', - 'IPv6AcceptRA': 'no', + units[f'{type}.network'] = { + 'content': { + 'Match': { + 'Name': network['interface'], + }, + 'Network': { + 'DHCP': 'no', + 'IPv6AcceptRA': 'no', + } } } for i in [4, 6]: if network.get(f'ipv{i}', None): - units[type].update({ + units[f'{type}.network']['content'].update({ f'Address#ipv{i}': { 'Address': network[f'ipv{i}'], }, }) if f'gateway{i}' in network: - units[type].update({ + units[f'{type}.network']['content'].update({ f'Route#ipv{i}': { 'Gateway': network[f'gateway{i}'], 'GatewayOnlink': 'yes', @@ -40,7 +42,7 @@ def systemd_networkd(metadata): return { - 'systemd-networkd': { - 'networks': units, + 'systemd': { + 'units': units, } } diff --git a/bundles/systemd-networkd/items.py b/bundles/systemd-networkd/items.py index dc804dd..89f1dc1 100644 --- a/bundles/systemd-networkd/items.py +++ b/bundles/systemd-networkd/items.py @@ -16,21 +16,21 @@ directories = { }, } -for type, path in { - 'networks': '/etc/systemd/network/{}.network', - 'netdevs': '/etc/systemd/network/{}.netdev', -}.items(): - for name, config in node.metadata.get(f'systemd-networkd/{type}').items(): - files[path.format(name)] = { - 'content': repo.libs.systemd.generate_unitfile(config), - 'needed_by': { - 'svc_systemd:systemd-networkd', - }, - 'triggers': { - 'svc_systemd:systemd-networkd:restart', - }, - } - +# for type, path in { +# 'networks': '/etc/systemd/network/{}.network', +# 'netdevs': '/etc/systemd/network/{}.netdev', +# }.items(): +# for name, config in node.metadata.get(f'systemd-networkd/{type}').items(): +# files[path.format(name)] = { +# 'content': repo.libs.systemd.generate_unitfile(config), +# 'needed_by': { +# 'svc_systemd:systemd-networkd', +# }, +# 'triggers': { +# 'svc_systemd:systemd-networkd:restart', +# }, +# } +# svc_systemd = { 'systemd-networkd': {}, } diff --git a/bundles/systemd-timers/items.py b/bundles/systemd-timers/items.py index 1c050e9..8ea5f8c 100644 --- a/bundles/systemd-timers/items.py +++ b/bundles/systemd-timers/items.py @@ -1,26 +1,27 @@ -# svc_systemd['cron'] = { -# 'enabled': False, -# } - -for name, config in node.metadata.get('systemd-timers').items(): - files[f'/etc/systemd/system/{name}.timer'] = { - 'content': repo.libs.systemd.generate_unitfile({ - 'Unit':{ - 'Description': f'{name} timer', - }, - 'Timer': { - 'OnCalendar': config['when'], - 'Persistent': config.get('persistent', False), - 'Unit': f'{name}.service', - }, - 'Install': { - 'WantedBy': 'multi-user.target', - } - }), - 'triggers': [ - 'action:systemd-reload', - f'svc_systemd:{name}:restart', - ], - } - - svc_systemd[f'{name}.timer'] = {} +# # svc_systemd['cron'] = { +# # 'enabled': False, +# # } +# +# for name, config in node.metadata.get('systemd-timers').items(): +# files[f'/etc/systemd/system/{name}.timer'] = { +# 'content': repo.libs.systemd.generate_unitfile({ +# 'Unit':{ +# 'Description': f'{name} timer', +# }, +# 'Timer': { +# 'OnCalendar': config['when'], +# 'Persistent': config.get('persistent', False), +# 'Unit': f'{name}.service', +# }, +# 'Install': { +# 'WantedBy': 'multi-user.target', +# } +# }), +# 'triggers': [ +# 'action:systemd-reload', +# f'svc_systemd:{name}:restart', +# ], +# } +# +# svc_systemd[f'{name}.timer'] = {} +# # diff --git a/bundles/systemd-timers/metadata.py b/bundles/systemd-timers/metadata.py index 6102abf..1ef111a 100644 --- a/bundles/systemd-timers/metadata.py +++ b/bundles/systemd-timers/metadata.py @@ -3,14 +3,41 @@ defaults = { } +@metadata_reactor.provides( + 'systemd/services', +) +def timers(metadata): + return { + 'systemd': { + 'units': { + f'{name}.timer': { + 'content': { + 'Unit':{ + 'Description': f'{name} timer', + }, + 'Timer': { + 'OnCalendar': config['when'], + 'Persistent': config.get('persistent', False), + 'Unit': f'{name}.service', + }, + 'Install': { + 'WantedBy': 'multi-user.target', + } + }, + } for name, config in metadata.get('systemd-timers').items() + }, + }, + } + + @metadata_reactor.provides( 'systemd/services', ) def services(metadata): return { 'systemd': { - 'services': { - name: { + 'units': { + f'{name}.service': { 'content': { 'Unit':{ 'Description': f'{name} timer service', @@ -19,9 +46,11 @@ def services(metadata): 'ExecStart': config['command'], }, }, - 'enabled': False, - 'running': False, - } for name, config in metadata.get('systemd-timers').items() + 'item': { + 'enabled': False, + 'running': False, + }, + } for name, config in metadata.get('systemd-timers').items() }, }, } diff --git a/bundles/systemd/README.md b/bundles/systemd/README.md new file mode 100644 index 0000000..3adb596 --- /dev/null +++ b/bundles/systemd/README.md @@ -0,0 +1,23 @@ +# SYSTEMD + +## metadata + +```python +{ + 'systemd': { + 'units': { + 'test.service': { + # optional: will be derived from unit extension + 'path': '/etc/systemd/system/test.service', + # content of the unit file + 'content': { + }, + # bw item data + # applies to unitfile and svc_systemd aswell, if present + 'item': { + }, + } + } + }, +} +``` diff --git a/bundles/systemd/items.py b/bundles/systemd/items.py index 78380c5..28f4401 100644 --- a/bundles/systemd/items.py +++ b/bundles/systemd/items.py @@ -6,39 +6,18 @@ actions = { 'command': 'systemctl daemon-reload', 'cascade_skip': False, 'triggered': True, - 'needed_by': { - 'svc_systemd:', - }, }, } -for name, service in node.metadata.get('systemd/services').items(): - # dont call a service 'service' explicitly - if name.endswith('.service'): - raise Exception(name) - - # split unit file content data from item data - content_data = service.pop('content') - - # default WantedBy=multi-user.target - content_data\ - .setdefault('Install', {})\ - .setdefault('WantedBy', {'multi-user.target'}) - - # create unit file - unit_path = f'/etc/systemd/system/{name}.service' - files[unit_path] = { - 'content': repo.libs.systemd.generate_unitfile(content_data), - 'triggers': [ - 'action:systemd-reload', - f'svc_systemd:{name}:restart', - ], +for name, config in node.metadata.get('systemd/units').items(): + files[config['path']] = { + 'content': repo.libs.systemd.generate_unitfile(config['content']), + **config['item'], } + files[config['path']].setdefault('triggers', []).append("action:systemd-reload") - # service depends on unit file - service\ - .setdefault('needs', [])\ - .append(f'file:{unit_path}') - - # service - svc_systemd[name] = service +for name, config in node.metadata.get('systemd/services').items(): + svc_systemd[name] = { + **config, + } + svc_systemd[name].setdefault('needs', []).append("action:systemd-reload") diff --git a/bundles/systemd/metadata.py b/bundles/systemd/metadata.py index 883127b..9e7d128 100644 --- a/bundles/systemd/metadata.py +++ b/bundles/systemd/metadata.py @@ -1,5 +1,65 @@ defaults = { 'systemd': { + 'units': {}, 'services': {}, } } + + +# create a svc_sytemd item for each .service and .timer unit +@metadata_reactor#.provides( +# 'systemd/services', +#) +def unit_services(metadata): + services = {} + + for name, config in metadata.get('systemd/units').items(): + if name.split('.')[-1] not in ['timer', 'service']: + continue + + services[name] = config['item'] + services[name].setdefault('needs', []).append(f"file:{config.get('path')}") + + return { + 'systemd': { + 'services': services, + } + } + + +# add defaults to units +@metadata_reactor#.provides( +# 'systemd/units', +#) +def unit_defaults(metadata): + units = {} + + for name in metadata.get('systemd/units').keys(): + extension = name.split('.')[-1] + + if extension in ['netdev', 'network']: + units[name] = { + 'path': f'/etc/systemd/network/{name}', + 'item': { + 'triggers': [ + 'svc_systemd:systemd-networkd:restart', + ] + } + } + elif extension in ['timer', 'service']: + units[name] = { + 'path': f'/etc/systemd/system/{name}', + 'item': { + 'triggers': [ + f'svc_systemd:{name}:restart', + ] + }, + } + else: + raise Exception(f'unknown unit extension: "{extension}"') + + return { + 'systemd': { + 'units': units, + } + } diff --git a/bundles/wireguard/metadata.py b/bundles/wireguard/metadata.py index 13cd9e3..2c8c9ef 100644 --- a/bundles/wireguard/metadata.py +++ b/bundles/wireguard/metadata.py @@ -26,7 +26,7 @@ defaults = { @metadata_reactor.provides( - 'systemd-networkd/networks', + 'systemd/units', ) def systemd_networkd_networks(metadata): network = { @@ -60,16 +60,18 @@ def systemd_networkd_networks(metadata): }) return { - 'systemd-networkd': { - 'networks': { - 'wireguard': network, + 'systemd': { + 'units': { + 'wireguard.network': { + 'content': network, + }, }, }, } @metadata_reactor.provides( - 'systemd-networkd/netdevs', + 'systemd/units', ) def systemd_networkd_netdevs(metadata): netdev = { @@ -99,9 +101,11 @@ def systemd_networkd_netdevs(metadata): }) return { - 'systemd-networkd': { - 'netdevs': { - 'wireguard': netdev, + 'systemd': { + 'units': { + 'wireguard.netdev': { + 'content': netdev, + }, }, }, }