improve wake on lan

This commit is contained in:
mwiegand 2022-08-09 19:58:47 +02:00
parent 9ff8dce802
commit 8a13421577
3 changed files with 31 additions and 9 deletions

View file

@ -22,7 +22,7 @@ def systemd(metadata):
'Persistent': config.get('persistent', False), 'Persistent': config.get('persistent', False),
'Unit': f'{name}.service', 'Unit': f'{name}.service',
}, },
}, },
f'{name}.service': { f'{name}.service': {
'Unit':{ 'Unit':{
'Description': f'{name} timer service', 'Description': f'{name} timer service',
@ -37,9 +37,11 @@ def systemd(metadata):
}) })
if config.get('working_dir'): if config.get('working_dir'):
units[f'{name}.service']['Service']['WorkingDirectory'] = config['working_dir'] units[f'{name}.service']['Service']['WorkingDirectory'] = config['working_dir']
if config.get('success_exit_status'):
units[f'{name}.service']['Service']['SuccessExitStatus'] = config['success_exit_status']
services[f'{name}.timer'] = {} services[f'{name}.timer'] = {}
return { return {
'systemd': { 'systemd': {
'units': units, 'units': units,

View file

@ -17,7 +17,7 @@ MIN_UPTIME=$(expr 60 \* 15)
if [[ "$UPTIME" -lt "$MIN_UPTIME" ]] if [[ "$UPTIME" -lt "$MIN_UPTIME" ]]
then then
echo "ABORT: uptime of ${UPTIME}s is lower than minimum of ${MIN_UPTIME}s" echo "ABORT: uptime of ${UPTIME}s is lower than minimum of ${MIN_UPTIME}s"
exit 0 exit 75
fi fi
# CHECK FOR RUNNING TIMERS # CHECK FOR RUNNING TIMERS
@ -30,7 +30,7 @@ do
elif systemctl is-active "$SERVICE" --quiet elif systemctl is-active "$SERVICE" --quiet
then then
echo "ABORT: service $SERVICE is running by timer" echo "ABORT: service $SERVICE is running by timer"
exit 0 exit 75
fi fi
done done
@ -40,7 +40,7 @@ LOGINS=$(netstat -tnpa | grep 'ESTABLISHED.*sshd' | tr -s ' ' | cut -d' ' -f5,7-
if ! [[ -z "$LOGINS" ]] if ! [[ -z "$LOGINS" ]]
then then
echo "ABORT: users logged in: $LOGINS" echo "ABORT: users logged in: $LOGINS"
exit 0 exit 75
fi fi
# SUSPEND! # SUSPEND!
@ -48,8 +48,26 @@ fi
if [[ "$1" = check ]] if [[ "$1" = check ]]
then then
echo "WOULD SESPEND" echo "WOULD SESPEND"
exit 0
else else
echo "SESPENDING AFTER TIMEOUT"
for i in 1 2 3 4 5 6
do
echo "TIMEOUT ${i} success"
sleep 10
# check if condition is still met
if "$0" check
then
continue
else
echo "SESPENSION ABORTED"
exit 75
fi
done
echo "SESPENDING" echo "SESPENDING"
sleep 60
systemctl suspend systemctl suspend
exit 0
fi fi

View file

@ -6,6 +6,7 @@ defaults = {
'packages': { 'packages': {
'jq': {}, 'jq': {},
'ethtool': {}, 'ethtool': {},
'net-tools': {},
}, },
}, },
} }
@ -20,6 +21,7 @@ def timer(metadata):
'suspend-if-idle': { 'suspend-if-idle': {
'command': f'suspend_if_idle', 'command': f'suspend_if_idle',
'when': 'minutely', 'when': 'minutely',
'success_exit_status': '75',
'env': { 'env': {
'THIS_SERVICE': 'suspend-if-idle.service', 'THIS_SERVICE': 'suspend-if-idle.service',
}, },
@ -35,7 +37,7 @@ def wake_command(metadata):
waker_hostname = repo.get_node(metadata.get('wol-sleeper/waker')).hostname waker_hostname = repo.get_node(metadata.get('wol-sleeper/waker')).hostname
mac = metadata.get(f"network/{metadata.get('wol-sleeper/network')}/mac") mac = metadata.get(f"network/{metadata.get('wol-sleeper/network')}/mac")
ip = ip_interface(metadata.get(f"network/{metadata.get('wol-sleeper/network')}/ipv4")).ip ip = ip_interface(metadata.get(f"network/{metadata.get('wol-sleeper/network')}/ipv4")).ip
return { return {
'wol-sleeper': { 'wol-sleeper': {
'wake_command': f"ssh -o StrictHostKeyChecking=no wol@{waker_hostname} 'wakeonlan {mac} && while ! ping {ip} -c1 -W3; do true; done'", 'wake_command': f"ssh -o StrictHostKeyChecking=no wol@{waker_hostname} 'wakeonlan {mac} && while ! ping {ip} -c1 -W3; do true; done'",
@ -49,7 +51,7 @@ def wake_command(metadata):
) )
def systemd(metadata): def systemd(metadata):
interface = metadata.get(f"network/{metadata.get('wol-sleeper/network')}/interface") interface = metadata.get(f"network/{metadata.get('wol-sleeper/network')}/interface")
return { return {
'systemd': { 'systemd': {
'units': { 'units': {