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 Sep 4, 2023
1 parent afc0c2f commit 1eae9ae
Show file tree
Hide file tree
Showing 5 changed files with 68 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,16 +24,16 @@ 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[@]}"

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

## Copy tools
Expand Down Expand Up @@ -69,6 +69,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
41 changes: 41 additions & 0 deletions base-system/usrroot/usr/sbin/hypnotizer
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
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"
fi

## Getting the shutdown time
# SHUTDOWN_TIME="2023-09-02T15:05:00"
# SHUTDOWN_TIME="$(get_directives_var Global )"
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 < 0: try to shutdown
if [ "$SECONDS_TO_SHUTDOWN" -gt 0 ]; then
log "Scheduled shutdown scheduled for later, exiting"
exit 0
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 starts 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 1eae9ae

Please sign in to comment.