Files
coredesktop/update
T
davidandClaude Opus 5.5 06fff0d777
shellcheck / shellcheck (push) Successful in 19s
Keep a closed lid from suspending during install and update
logind ignores sleep locks when the lid closes (LidSwitchIgnoreInhibited),
so install.sh and update now also take a handle-lid-switch lock. It's a
separate systemd-inhibit, since polkit refuses that lock outside a local
session and that mustn't cost the sleep lock too. After a long sleep, a
pacman upgrade can be cut off part way and sudo's cached password expires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 21:25:39 -07:00

210 lines
9.4 KiB
Bash
Executable File

#!/bin/bash
# Updates from before 2026-10-19 (013acac's) copy this file over themselves
# while bash is still reading them, then read on from byte 1484 of the new copy.
# The exec line below starts at exactly that byte, so they run this update
# afresh; the rest of the block pads it into place, and in a normal run the
# block is a here-document nothing reads. Keep the exec line at byte 1484:
# tests/update.bats checks it.
: << 'FROM_OLD_UPDATE'
This block is padding for old updates. This block is padding for old updates.
This block is padding for old updates. This block is padding for old updates.
This block is padding for old updates. This block is padding for old updates.
This block is padding for old updates. This block is padding for old updates.
This block is padding for old updates. This block is padding for old updates.
This block is padding for old updates. This block is padding for old updates.
This block is padding for old updates. This block is padding for old updates.
This block is padding for old updates. This block is padding for old updates.
This block is padding for old updates. This block is padding for old updates.
This block is padding for old updates. This block is padding for old updates.
This block is padding for old updates. This block is padding for old updates.
This block is padding for old updates. This block is padding for old updates.
This block is padding for old updates. This block is padding for old updates.
...................
exec ~/.local/bin/update
FROM_OLD_UPDATE
# The whole script is wrapped in { ...; exit; } so bash parses all of it before
# running anything. That way, replacing this file mid-run (e.g. by git pull and
# the copy below) can't make the running copy read a mix of old and new text.
{
# Install a script atomically (copy to a temp file, then rename over the target)
install_script() {
local tmp
tmp="$(mktemp "$2.XXXXXX")" && cp "$1" "$tmp" && chmod 755 "$tmp" && mv -f "$tmp" "$2"
}
# Make a log written by script(1) readable: drop its header and footer, color
# and cursor codes, and all but the last redraw of each progress line. LC_ALL=C
# so the ranges below mean bytes, whatever the locale.
clean_log() {
LC_ALL=C sed -i -E '1{/^Script started on /d}; /^Script done on .*COMMAND_EXIT_CODE/d;
s/\x1b\][^\x07\x1b]*(\x07|\x1b\\)//g; s#\x1b\[[0-9;?]*[ -/]*[@-~]##g;
s/\x1b[()][0-9A-Za-z]//g; s/\x1b[=>]//g; s/\r+$//; s/.*\r//' "$1"
}
# Lets this update's pacman and yay upgrade the system past the guard that
# stops upgrades run without update (system/pacman-guard)
export COREDESKTOP_ALLOW_PACMAN=1
# Set by updates.d/99-reboot-check.sh when this update needs a reboot
REBOOT_NEEDED="$HOME/.local/state/coredesktop/reboot-needed"
# The same as the system menu's restart: an active desktop session needs no
# password. (sudo's cached password belongs to script(1)'s terminal, see below.)
reboot_now() {
echo "Rebooting..."
systemctl reboot || echo "Couldn't reboot. Restart from the system menu instead."
}
# If this update needs a reboot, ask on the terminal whether to reboot now. Under
# script(1), leave it to the copy outside, so the log is cleaned up first.
offer_reboot() {
local tty="${COREDESKTOP_TTY:-/dev/tty}" answer
[ -e "$REBOOT_NEEDED" ] || return 0
{ : < "$tty"; } 2> /dev/null || return 0
read -rp "Reboot now? [y/N] " answer < "$tty" || return 0
[[ "$answer" == [yY]* ]] || return 0
if [ -n "$UPDATE_IN_SCRIPT" ]; then
touch "$UPDATE_LOG.reboot"
else
reboot_now
fi
}
# Log each update to ~/.local/state/coredesktop/logs (95-cleanup.sh deletes
# old ones). In a terminal, run under script(1), so everything still sees a
# terminal and colors, menus and prompts work as before; otherwise use tee.
# UPDATE_LOG tells the copy re-run below that it's already logging.
if [[ -z "$UPDATE_LOG" ]] && mkdir -p -m 700 "$HOME/.local/state/coredesktop/logs"; then
UPDATE_LOG="$HOME/.local/state/coredesktop/logs/update-$(date +%Y-%m-%d-%H%M%S).log"
export UPDATE_LOG
if [ -t 0 ] && [ -t 1 ] && command -v script > /dev/null; then
SHELL=/bin/bash UPDATE_IN_SCRIPT=1 script -q -e -f -c "$(printf '%q ' "$0" "$@")" "$UPDATE_LOG"
status=$?
clean_log "$UPDATE_LOG"
if [ -e "$UPDATE_LOG.reboot" ]; then
rm -f "$UPDATE_LOG.reboot"
reboot_now
fi
exit "$status"
fi
exec > >(tee -a "$UPDATE_LOG") 2>&1
fi
# Print the logo
print_logo() {
cat << "EOF"
██
█ ██
██ ██████ █████ ██ █████ █████ ████ ██ █████ ██
██ ██ ██ ███ ███████ ██ ██ ██ ██ ██ ████████ ██ ██ ███████
█████ ██████ ██ ██ ██████ ██ ████████ ██ █ ███████ ██ ██
██ ██████ ███████ ██████ ██ ██████ ██████ ██████ ███████
EOF
}
# Clear screen and show logo
clear
print_logo
COREDESKTOP="$HOME/.local/share/coredesktop"
echo "Updating CoreDesktop..."
if [[ -z "$UPDATE_REEXECED" ]] && ! git -C "$COREDESKTOP" pull --ff-only; then
echo "WARNING: could not pull the latest CoreDesktop; running the update hooks already on disk."
export UPDATE_PULL_FAILED=1
fi
mkdir -p "$COREDESKTOP/updates.d"
install_script "$COREDESKTOP/update" ~/.local/bin/update
install_script "$COREDESKTOP/refresh" ~/.local/bin/refresh
install_script "$COREDESKTOP/sibling" ~/.local/bin/sibling
install_script "$COREDESKTOP/version" ~/.local/bin/version
install_script "$COREDESKTOP/configure" ~/.local/bin/configure
install_script "$COREDESKTOP/audit" ~/.local/bin/audit
install_script "$COREDESKTOP/report" ~/.local/bin/report
install_script "$COREDESKTOP/webapp" ~/.local/bin/webapp
if [[ -z "$UPDATE_REEXECED" ]]; then
export UPDATE_REEXECED=1
exec ~/.local/bin/update
fi
# An upgrade that runs out of disk space part way through can leave the system
# half-upgraded. Set UPDATE_FORCE=1 to skip this check.
MIN_FREE_GIB=10
FREE_KIB="$(df -k --output=avail / | tail -n 1 | tr -d ' ')"
if [[ -z "$UPDATE_FORCE" && "$FREE_KIB" =~ ^[0-9]+$ ]] && (( FREE_KIB < MIN_FREE_GIB * 1024 * 1024 )); then
echo "Only $(( FREE_KIB / 1024 / 1024 )) GiB free on /; updating needs at least $MIN_FREE_GIB GiB."
echo "Free up space (sudo paccache -rk1 clears old cached packages), or run: UPDATE_FORCE=1 update"
exit 1
fi
# Ask for the password once, then keep sudo's cached credentials fresh until
# update exits, so long-running hooks don't stop for a password again.
sudo -v || exit 1
while kill -0 $$ 2>/dev/null; do sudo -n -v; sleep 60; done > /dev/null 2>&1 &
SUDO_KEEPALIVE_PID=$!
trap 'kill "$SUDO_KEEPALIVE_PID" 2>/dev/null' EXIT
# Keep the computer from suspending, from idle or a closed lid, until update
# exits: a pacman upgrade cut off part way can leave it unbootable, and after a
# long sleep sudo's cached password has expired, so the next sudo times out.
# logind ignores sleep locks when the lid closes (LidSwitchIgnoreInhibited), so
# the lid takes its own lock; it's kept apart because polkit refuses it outside
# a local session, and that mustn't lose the sleep lock too. tail exits with
# update, and that ends the inhibitors.
if command -v systemd-inhibit > /dev/null; then
for what in sleep:idle handle-lid-switch; do
systemd-inhibit --what="$what" --who=CoreDesktop --why="Updating CoreDesktop" --mode=block \
tail --pid=$$ -f /dev/null > /dev/null 2>&1 &
done
fi
rm -f "$REBOOT_NEEDED"
FAILED=()
[[ -n "$UPDATE_PULL_FAILED" ]] && FAILED+=("git pull")
# Each hook runs in its own subshell so a cd, set -e, exit, or stray variable
# in one hook can't leak into or abort the hooks after it.
for plugin in "$COREDESKTOP/updates.d/"*.sh; do
[ -f "$plugin" ] || continue
(
unset -f is_installed run_update
source "$plugin"
# A hook without run_update does its work as it's sourced, so its
# last command's status is the hook's
status=$?
if declare -f run_update > /dev/null; then
if ! declare -f is_installed > /dev/null || is_installed; then
run_update
fi
else
exit "$status"
fi
)
# Checked separately: under `( ... ) ||`, bash would ignore set -e inside the hook
[ $? -eq 0 ] || FAILED+=("$(basename "$plugin")")
done
if (( ${#FAILED[@]} > 0 )); then
echo "Done, with errors in: ${FAILED[*]}"
if [ -n "$UPDATE_LOG" ]; then
echo "The full output is in $UPDATE_LOG"
# Ask, on the terminal if there is one, whether to send it to us
[ -x ~/.local/bin/report ] && ~/.local/bin/report --offer "$UPDATE_LOG" "${FAILED[@]}"
fi
offer_reboot
exit 1
fi
echo "Done."
offer_reboot
exit 0
}