From 776e8768767e266ac84f389734e64d67c93e4c98 Mon Sep 17 00:00:00 2001 From: mwiegand Date: Wed, 10 Nov 2021 02:43:45 +0100 Subject: [PATCH] swapfile --- bundles/systemd-swap/items.py | 50 ++++++++++++++++++++++++++++++++ bundles/systemd-swap/metadata.py | 14 +++++++++ bundles/systemd/items.py | 2 +- bundles/systemd/metadata.py | 4 +-- nodes/home.openhab.py | 9 +----- 5 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 bundles/systemd-swap/items.py create mode 100644 bundles/systemd-swap/metadata.py diff --git a/bundles/systemd-swap/items.py b/bundles/systemd-swap/items.py new file mode 100644 index 0000000..f23886f --- /dev/null +++ b/bundles/systemd-swap/items.py @@ -0,0 +1,50 @@ +size = node.metadata.get('systemd-swap/size') +assert isinstance(size, int) + +actions = { + 'stop_swap': { + 'command': f'systemctl stop swapfile.swap', + 'unless': f'rm /swapfile', + 'triggered': True, + }, + 'create_swapfile': { + 'command': f'dd if=/dev/zero of=/swapfile bs={size} count=1', + 'unless': f'stat -c %s /swapfile | grep ^{size}$', + 'preceded_by': { + 'action:stop_swap', + }, + 'triggers': { + 'action:initialize_swapfile', + 'svc_systemd:swapfile.swap:restart', + }, + }, + 'initialize_swapfile': { + 'command': f'mkswap /swapfile', + 'triggered': True, + 'needs': { + 'action:create_swapfile', + } + }, +} + +files = { + '/swapfile': { + 'content_type': 'any', + 'mode': '600', + 'triggers': { + 'svc_systemd:swapfile.swap:restart', + }, + } +} + +svc_systemd = { + 'swapfile.swap': { + 'preceded_by': { + 'action:initialize_swapfile', + }, + 'needs': { + 'action:systemd-reload', + 'action:create_swapfile', + }, + }, +} diff --git a/bundles/systemd-swap/metadata.py b/bundles/systemd-swap/metadata.py new file mode 100644 index 0000000..1b2c143 --- /dev/null +++ b/bundles/systemd-swap/metadata.py @@ -0,0 +1,14 @@ +defaults = { + 'systemd-swap': { + 'size': 2*10**9, + }, + 'systemd': { + 'units': { + 'swapfile.swap': { + 'Swap': { + 'What': '/swapfile', + }, + }, + }, + }, +} diff --git a/bundles/systemd/items.py b/bundles/systemd/items.py index c7fab56..e28a2b5 100644 --- a/bundles/systemd/items.py +++ b/bundles/systemd/items.py @@ -18,7 +18,7 @@ for name, unit in node.metadata.get('systemd/units').items(): 'svc_systemd:systemd-networkd:restart', ], } - elif extension in ['timer', 'service', 'mount']: + elif extension in ['timer', 'service', 'mount', 'swap']: path = f'/etc/systemd/system/{name}' dependencies = { 'triggers': [ diff --git a/bundles/systemd/metadata.py b/bundles/systemd/metadata.py index d8fb272..617c2cd 100644 --- a/bundles/systemd/metadata.py +++ b/bundles/systemd/metadata.py @@ -14,7 +14,7 @@ def units(metadata): for name, config in metadata.get('systemd/units').items(): extension = name.split('.')[-1] - if extension not in ['timer', 'service', 'network', 'netdev', 'mount']: + if extension not in ['timer', 'service', 'network', 'netdev', 'mount', 'swap']: raise Exception(f'unknown extension {extension}') if not config.get('Install/WantedBy'): @@ -47,7 +47,7 @@ def services(metadata): for name, config in metadata.get('systemd/services').items(): extension = name.split('.')[-1] - if extension not in ['timer', 'service', 'mount']: + if extension not in ['timer', 'service', 'mount', 'swap']: raise Exception(f'unknown extension: {extension}') return { diff --git a/nodes/home.openhab.py b/nodes/home.openhab.py index e339ad3..27df6d7 100644 --- a/nodes/home.openhab.py +++ b/nodes/home.openhab.py @@ -10,17 +10,10 @@ 'zfs', 'openhab', 'java', + 'systemd-swap', ], 'metadata': { 'FIXME_dont_touch_sshd': True, - 'telegraf': { - 'config': { - 'agent': { - 'flush_interval': '30s', - 'interval': '30s', - }, - }, - }, 'id': '0afcde75-95c8-4fbd-b4c2-8a0fcc92884a', 'network': { 'internal': {