Skip to content

Commit

Permalink
[#12] Adding scheduled shutdown service
Browse files Browse the repository at this point in the history
This commit adds support for a new global directive "ShutdownTime", like ContestTimes or ConfigExpirationTime, that schedules a shutdown at the time defined.

This aproach instead of calculating when to shutdown, checks every minute if the system is ready to shutdown, checks including

- Is clock in sync
- Is there a scheduled time to shutdown
- Is time to shutdown
- Is system in always mode (I think it would not be expected / allowed to shutdown the system while in contest / event mode, right?)
  • Loading branch information
DT3264 committed Oct 12, 2023
1 parent 1419977 commit 76acaf8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
8 changes: 5 additions & 3 deletions base-system/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ cp -rf usrroot/etc/apt/* /etc/apt/
apt update
apt install --yes --no-install-recommends "${INST_PACKAGES[@]}"
if [ "$DEVELOPER" = "true" ]; then
apt install --yes --no-install-recommends "${DEV_PACKAGES[@]}"
apt install --yes --no-install-recommends "${DEV_PACKAGES[@]}"
else
apt autoremove --yes --purge "${DEV_PACKAGES[@]}"
apt autoremove --yes --purge "${DEV_PACKAGES[@]}"
fi
apt autoremove --yes --purge "${REM_PACKAGES[@]}"

Expand All @@ -37,7 +37,7 @@ cp "$CHANGEDIR/../packages/hos-wallpaper/usr/sbin/hos-wallpaper" /usr/sbin/hos-w
cp "$CHANGEDIR/../packages/hos-wallpaper/usr/lib/systemd/system/happly-wallpaper@.service" /usr/lib/systemd/system/happly-wallpaper@.service

if [ "$DEVELOPER" = "true" ]; then
pushd devroot && cp --parents -afr * / && popd
pushd devroot && cp --parents -afr * / && popd
fi

## Copy tools
Expand Down Expand Up @@ -73,6 +73,8 @@ ln -sf /usr/lib/systemd/system/happly.service /etc/systemd/system/happly.service
ln -sf /usr/lib/systemd/system/happly-wallpaper.service /etc/systemd/system/happly-wallpaper.service
ln -sf /usr/lib/systemd/system/hsync.timer /etc/systemd/system/hsync.timer
ln -sf /usr/lib/hsync/hsync.sh /usr/lib/hsync/happly.sh
ln -sf /usr/lib/systemd/system/hypnotizer.timer /etc/systemd/system/hypnotizer.timer
ln -sf /usr/lib/systemd/system/hypnotizer.service /etc/systemd/system/hypnotizer.service

## Permissions
chmod 640 /etc/fstab
Expand Down
8 changes: 8 additions & 0 deletions base-system/usrroot/usr/lib/systemd/system/hypnotizer.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Unit]
Description=Service to check if is time to shutdown the machine

[Service]
ExecStart=/usr/sbin/hypnotizer

[Install]
WantedBy=multi-user.target
13 changes: 13 additions & 0 deletions base-system/usrroot/usr/lib/systemd/system/hypnotizer.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=TImer to check if is time to shutdown the machine


[Timer]
AccuracySec=1s
OnUnitInactiveSec=60s
OnBootSec=1s
OnClockChange=false
OnTimezoneChange=false

[Install]
WantedBy=timers.target
59 changes: 59 additions & 0 deletions base-system/usrroot/usr/sbin/hypnotizer
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash
# hypnotizer.sh
# This script tries to shutdown the instance
# at the time specified in the global section
# if the required conditions (time and mode) are met.
#
# Copyright (C) 2022, huronOS Project:
# <http://huronos.org>
#
# Licensed under the GNU GPL Version 2
# <http://www.gnu.org/licenses/gpl-2.0.html>
#
# Authors:
# Daniel Cerna <dcerna@huronos.org>
set -e

. /usr/lib/hsync/libhlog.so

## Check if clock is in sync, otherwise the current time is untrusted
STATE_IS_CLOCK_SYNC=$(timedatectl show | grep NTPSynchronized | cut -d= -f2)
if [ "$STATE_IS_CLOCK_SYNC" = "no" ]; then
log "System time is not in sync, exiting"
exit 0
fi

## Getting the shutdown time
SHUTDOWN_TIME="$(hos-dvar --variable-name ShutdownTime --section Global)"
if [ -z "$SHUTDOWN_TIME" ] || [ "$SHUTDOWN_TIME" = "none" ]; then
log "No shutdown scheduled, exiting."
exit 0
fi

## Getting the remaining time to shutdown
SHUTDOWN_TIME_EPOCH=$(date -d "$SHUTDOWN_TIME" +%s)
CURRENT_TIME_EPOCH=$(date +%s)
SECONDS_TO_SHUTDOWN=$((SHUTDOWN_TIME_EPOCH - CURRENT_TIME_EPOCH))
## If secondsToShutdown > 0: don't shutdown yet
## If secondsToShutdown < -300: don't shutdown (outside the 5-minute window)
## If -300 <= secondsToShutdown <= 0: try to shutdown
if [ "$SECONDS_TO_SHUTDOWN" -gt 0 ]; then
log "Scheduled shutdown scheduled for later, exiting"
exit 0
elif [ "$SECONDS_TO_SHUTDOWN" -lt -300 ]; then
log "Scheduled shutdown time has exceeded the 5-minute threshold, exiting"
exit 0
else
log "Scheduled shutdown time has been reached and is within the 5-minutes shutdown window, proceeding"
fi

## If we're not in always mode, exit
## We don't want to ruin neither an event or a contest
if [[ $(hos-dmode) != "Always" ]]; then
log "The system will shut down as soon as it goes to Always mode, exiting"
exit 0
fi

# If we hit this point, is totally safe to shutdown
log "The stars aligned, shutting down the system"
shutdown -h now
1 change: 1 addition & 0 deletions software-modules/base/03-budgie/budgie.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ systemctl daemon-reload
systemctl enable lightdm.service
systemctl enable hsync.timer
systemctl enable ipman.service
systemctl enable hypnotizer.timer

## Copy plank resolution monitor
cp -f "files/plankrm" "/usr/local/bin/plankrm"
Expand Down

0 comments on commit 76acaf8

Please sign in to comment.