diff --git a/bin/wake b/bin/wake new file mode 100755 index 0000000..78415ad --- /dev/null +++ b/bin/wake @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 + +from bundlewrap.repo import Repository +from os.path import realpath, dirname +from sys import argv + +repo = Repository(dirname(dirname(realpath(__file__)))) + +repo.libs.wol.wake(repo.get_node(argv[1])) diff --git a/bundles/systemd-timers/metadata.py b/bundles/systemd-timers/metadata.py index 7944107..c5936af 100644 --- a/bundles/systemd-timers/metadata.py +++ b/bundles/systemd-timers/metadata.py @@ -30,6 +30,7 @@ def systemd(metadata): 'Service': { 'User': config.get('user', 'root'), 'ExecStart': config['command'], + 'Environment': config.get('env'), }, }, }) diff --git a/bundles/wol-sleeper/files/suspend_if_idle b/bundles/wol-sleeper/files/suspend_if_idle index c66bded..b324c04 100644 --- a/bundles/wol-sleeper/files/suspend_if_idle +++ b/bundles/wol-sleeper/files/suspend_if_idle @@ -1,19 +1,30 @@ #!/bin/bash -WOKE=$(expr $(journalctl -t systemd-sleep -b 0 -o json MESSAGE="System resumed." -n1 | jq -r .__REALTIME_TIMESTAMP) / 1000000) -NOW=$(date +%s) -UPTIME=$(expr $NOW - $WOKE) +# CHECK UPTIME + +RESUMED_TIMESTAMP_MICRO=$(journalctl -t systemd-sleep -b 0 -o json MESSAGE="System resumed." -n1 | jq -r .__REALTIME_TIMESTAMP) +if [[ -z "$RESUMED_TIMESTAMP_MICRO" ]] +then + UPTIME=$(cat /proc/uptime | cut -d' ' -f1 | cut -d'.' -f1) +else + RESUMED_TIMESTAMP=$(expr $RESUMED_TIMESTAMP_MICRO / 1000000) + NOW_TIMESTAMP=$(date +%s) + UPTIME=$(expr $NOW_TIMESTAMP - $RESUMED_TIMESTAMP) +fi + + MIN_UPTIME=$(expr 60 \* 15) if [[ "$UPTIME" -lt "$MIN_UPTIME" ]] then - echo "ABORT: uptime ($UPTIME s) lower than minimum ($MIN_UPTIME s)" + echo "ABORT: uptime of ${UPTIME}s is lower than minimum of ${MIN_UPTIME}s" exit 0 fi -MY_SERVICE="$2" +# CHECK FOR RUNNING TIMERS + for SERVICE in $(systemctl list-timers --no-pager --no-legend --state active -o json | jq -r '.[] | .activates') do - if [[ "$SERVICE" = "$MY_SERVICE" ]] + if [[ "$SERVICE" = "$THIS_SERVICE" ]] then continue elif systemctl is-active "$SERVICE" --quiet @@ -23,18 +34,22 @@ do fi done -LOGINS=$(netstat -tnpa | grep 'ESTABLISHED.*sshd' | wc -l) -if [[ "$LOGINS" -gt 0 ]] +# CHECK FOR ACTIVE LOGINS + +LOGINS=$(netstat -tnpa | grep 'ESTABLISHED.*sshd' | tr -s ' ' | cut -d' ' -f5,7-8 | paste -d',' -s | sed 's/,/, /') +if ! [[ -z "$LOGINS" ]] then - echo "ABORT: $LOGINS user logins" + echo "ABORT: users logged in: $LOGINS" exit 0 fi -if [[ "$1" = now ]] +# SUSPEND! + +if [[ "$1" = check ]] then + echo "WOULD SESPEND" +else echo "SESPENDING" sleep 60 systemctl suspend -else - echo "WOULD SESPEND" fi diff --git a/bundles/wol-sleeper/items.py b/bundles/wol-sleeper/items.py index 3314013..77d8511 100644 --- a/bundles/wol-sleeper/items.py +++ b/bundles/wol-sleeper/items.py @@ -3,7 +3,7 @@ if not waker_node.has_bundle('wol-waker'): raise Exception(f'waker node {waker_node.name} does not have bundle wol-waker') files = { - '/opt/suspend_if_idle': { + '/usr/local/bin/suspend_if_idle': { 'mode': '550', }, } diff --git a/bundles/wol-sleeper/metadata.py b/bundles/wol-sleeper/metadata.py index d99a4f8..a4bc575 100644 --- a/bundles/wol-sleeper/metadata.py +++ b/bundles/wol-sleeper/metadata.py @@ -5,6 +5,7 @@ defaults = { 'apt': { 'packages': { 'jq': {}, + 'ethtool': {}, }, }, } @@ -14,12 +15,14 @@ defaults = { 'systemd-timers/suspend-if-idle', ) def timer(metadata): - name = 'suspend-if-idle'# return { 'systemd-timers': { - name: { - 'command': f'/opt/suspend_if_idle now {name}.service', + 'suspend-if-idle': { + 'command': f'suspend_if_idle', 'when': 'minutely', + 'env': { + 'THIS_SERVICE': 'suspend-if-idle.service', + }, }, }, } @@ -41,7 +44,6 @@ def wake_command(metadata): @metadata_reactor.provides( - 'apt/packages/ethtool', 'systemd/units/enable-wol.service', 'systemd/services/enable-wol.service', ) @@ -49,11 +51,6 @@ def systemd(metadata): interface = metadata.get(f"network/{metadata.get('wol-sleeper/network')}/interface") return { - 'apt': { - 'packages': { - 'ethtool': {}, - }, - }, 'systemd': { 'units': { 'enable-wol.service': {