Make regent-deploy update itself from the build host
This commit is contained in:
1 file changed
+55
@@ -16,11 +16,66 @@ set -euo pipefail
|
||||
HOST=${REGENT_BUILD_HOST:-nuc-zt}
|
||||
REPO=${REGENT_REPO_PATH:-'~/src/regent'}
|
||||
REMOTE="$REPO/build-aux/deploy-dev.sh"
|
||||
REMOTE_SELF="$REPO/build-aux/regent-deploy.sh"
|
||||
APP_ID=org.kosmos.raucao.Regent
|
||||
|
||||
say() { printf 'regent-deploy: %s\n' "$*"; }
|
||||
die() { printf 'regent-deploy: %s\n' "$*" >&2; exit 1; }
|
||||
|
||||
# Keep this script current.
|
||||
#
|
||||
# It is installed as a *copy*, so without this it silently keeps running whatever
|
||||
# was installed the first time: a fix landed in the repo, the wrapper on the
|
||||
# workstation was still the old one, and the only symptom was a step that never
|
||||
# ran. Everything else here is fetched from the build host on every invocation —
|
||||
# this makes the wrapper part of that.
|
||||
#
|
||||
# Guards, because replacing yourself is not something to be careless about: only
|
||||
# a non-empty file that carries this script's own marker is accepted, the current
|
||||
# copy is kept as `.bak`, the swap is a rename, and `REGENT_DEPLOY_UPDATED`
|
||||
# guarantees a re-exec cannot loop. `REGENT_DEPLOY_NO_UPDATE=1` skips it.
|
||||
self_update() {
|
||||
[ -z "${REGENT_DEPLOY_UPDATED:-}" ] || return 0
|
||||
[ -z "${REGENT_DEPLOY_NO_UPDATE:-}" ] || return 0
|
||||
|
||||
local fetched
|
||||
fetched=$(mktemp "${TMPDIR:-/tmp}/regent-deploy.XXXXXX") || return 0
|
||||
|
||||
if ! ssh "$HOST" "cat $REMOTE_SELF" >"$fetched" 2>/dev/null; then
|
||||
rm -f "$fetched"
|
||||
return 0 # the version check below will report an unreachable host
|
||||
fi
|
||||
|
||||
if [ ! -s "$fetched" ] || ! grep -qF 'regent-deploy: %s' "$fetched"; then
|
||||
say "note: $HOST did not return a usable regent-deploy; keeping this copy" >&2
|
||||
rm -f "$fetched"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if cmp -s "$fetched" "$0"; then
|
||||
rm -f "$fetched"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [ ! -w "$0" ]; then
|
||||
say "note: $0 is not writable, so it cannot update itself" >&2
|
||||
rm -f "$fetched"
|
||||
return 0
|
||||
fi
|
||||
|
||||
say "updating regent-deploy from $HOST"
|
||||
cp -p "$0" "$0.bak" 2>/dev/null || true
|
||||
chmod --reference="$0" "$fetched" 2>/dev/null || chmod +x "$fetched"
|
||||
if mv "$fetched" "$0"; then
|
||||
REGENT_DEPLOY_UPDATED=1 exec "$0" "$@"
|
||||
fi
|
||||
|
||||
say "could not replace $0; continuing with the installed copy" >&2
|
||||
rm -f "$fetched"
|
||||
}
|
||||
|
||||
self_update "$@"
|
||||
|
||||
# The staged tree is rooted at /usr, so its desktop file says `Exec=regent`. That
|
||||
# is right for a system install — /usr/bin is on the session's PATH — but wrong
|
||||
# for this one: the binary just landed in ~/.local/bin, which a GNOME session
|
||||
|
||||
Reference in new issue
Block a user