277 lines
13 KiB
Bash
Executable File
277 lines
13 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# archie-update-install -- component-aware update installation adapter.
|
|
#
|
|
# CommandInstaller passes the freshly revalidated, approved snapshot through
|
|
# ARCHIE_UPDATE_{DAEMON,AGENT}_{PREVIOUS,VERSION}. A VERSION is non-empty only
|
|
# when that component changed in the approved snapshot. This script must never
|
|
# rediscover a broader plan after approval.
|
|
set -euo pipefail
|
|
|
|
json_escape() {
|
|
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'
|
|
}
|
|
|
|
SOURCE_URL="${ARCHIE_SOURCE_URL:-https://github.com/samcharles93/archie-core.git}"
|
|
BIN_DIR="${ARCHIE_BIN_DIR:-$HOME/.local/bin}"
|
|
CONFIG_PATH="${ARCHIE_CONFIG_PATH:-${XDG_CONFIG_HOME:-$HOME/.config}/archie/config.toml}"
|
|
export PATH="/usr/local/go/bin:$PATH"
|
|
|
|
# State for the managed image, independent of archied's daemon build stamp.
|
|
AGENT_VERSION_FILE="${ARCHIE_AGENT_VERSION_FILE:-$BIN_DIR/archie-agent.version}"
|
|
DB_SNAPSHOT="${ARCHIE_DB_SNAPSHOT:-$BIN_DIR/archie-db.pre-update.dump}"
|
|
# The address the daemon dials for its state, so the watchdog can wait for it to
|
|
# accept connections before it starts archied. Read with a narrow awk: the
|
|
# daemon stays the authority on validation, and this script deliberately
|
|
# understands one scalar rather than parsing TOML. Scoped to [services.state] so the Gateway's own target cannot be
|
|
# mistaken for it, and exact on the key so target_token is not.
|
|
configured_state_store_target() {
|
|
awk '
|
|
/^[[:space:]]*\[services\.state\][[:space:]]*$/ { inside = 1; next }
|
|
/^[[:space:]]*\[/ { inside = 0 }
|
|
inside && /^[[:space:]]*target[[:space:]]*=/ {
|
|
value = $0
|
|
sub(/^[[:space:]]*target[[:space:]]*=[[:space:]]*/, "", value)
|
|
sub(/[[:space:]]*(#.*)?$/, "", value)
|
|
if (value ~ /^".*"$/) { sub(/^"/, "", value); sub(/"$/, "", value); print value }
|
|
else if (length(value) >= 2 && substr(value, 1, 1) == sprintf("%c", 39) && substr(value, length(value), 1) == sprintf("%c", 39)) print substr(value, 2, length(value) - 2)
|
|
exit
|
|
}
|
|
' "$CONFIG_PATH" 2>/dev/null || true
|
|
}
|
|
|
|
STATE_STORE_TARGET="${ARCHIE_UPDATE_STATE_STORE_TARGET:-$(configured_state_store_target)}"
|
|
|
|
GATEWAY_SERVICES="archie-state-store archie-gateway archie-ui archie-messaging archied"
|
|
GATEWAY_BINARIES="$GATEWAY_SERVICES"
|
|
|
|
SOURCE_BUILD="${ARCHIE_UPDATE_SOURCE_BUILD:-0}"
|
|
AUTO=0
|
|
for arg in "$@"; do
|
|
case "$arg" in
|
|
--from-source) SOURCE_BUILD=1 ;;
|
|
--auto) AUTO=1 ;;
|
|
*) echo "unknown option: $arg" >&2; exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
if [ "$AUTO" = 1 ] && [ -z "${ARCHIE_UPDATE_DAEMON_VERSION:-}${ARCHIE_UPDATE_AGENT_VERSION:-}" ]; then
|
|
snapshot="$("$(dirname "$0")/archie-update-check")"
|
|
export ARCHIE_UPDATE_DAEMON_PREVIOUS="$(jq -r '.Components[] | select(.ID == "daemon") | .Installed' <<< "$snapshot")"
|
|
export ARCHIE_UPDATE_DAEMON_VERSION="$(jq -r '.Components[] | select(.ID == "daemon") | .Available' <<< "$snapshot")"
|
|
export ARCHIE_UPDATE_AGENT_PREVIOUS="$(jq -r '.Components[] | select(.ID == "agent") | .Installed' <<< "$snapshot")"
|
|
export ARCHIE_UPDATE_AGENT_VERSION="$(jq -r '.Components[] | select(.ID == "agent") | .Available' <<< "$snapshot")"
|
|
fi
|
|
|
|
PREV_GW="${ARCHIE_UPDATE_DAEMON_PREVIOUS:-}"
|
|
PREV_RT="${ARCHIE_UPDATE_AGENT_PREVIOUS:-}"
|
|
GW_TARGET="${ARCHIE_UPDATE_DAEMON_VERSION:-}"
|
|
RT_TARGET="${ARCHIE_UPDATE_AGENT_VERSION:-}"
|
|
GW="$PREV_GW"
|
|
RT="$PREV_RT"
|
|
components=""
|
|
|
|
if [ -n "$GW_TARGET" ] && [ "$GW_TARGET" != "$PREV_GW" ]; then
|
|
GW="$GW_TARGET"
|
|
components="daemon"
|
|
fi
|
|
if [ -n "$RT_TARGET" ] && [ "$RT_TARGET" != "$PREV_RT" ]; then
|
|
case "$RT_TARGET" in
|
|
dev|unknown) echo "refusing to build a released runtime image with version '$RT_TARGET'" >&2; exit 1 ;;
|
|
esac
|
|
RT="$RT_TARGET"
|
|
components="${components:+$components,}agent"
|
|
fi
|
|
|
|
verify_installed_versions() {
|
|
reference="$1"
|
|
problems=""
|
|
for cmd in $GATEWAY_BINARIES; do
|
|
[ -f "$BIN_DIR/$cmd" ] || continue
|
|
reported="$("$BIN_DIR/$cmd" -version 2>/dev/null | awk -v want="$cmd" '$1 == want { print $2; exit }')"
|
|
[ -n "$reported" ] || reported="unknown"
|
|
if [ "$reported" != "$reference" ]; then
|
|
problems="${problems:+$problems }$cmd=$reported"
|
|
fi
|
|
done
|
|
if [ -n "$problems" ]; then
|
|
echo "refusing: installed binaries disagree with $reference: $problems" >&2
|
|
echo "each host binary must report the release it was built from; reinstall the outliers from archied/v$reference, or approve an update that rebuilds them" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
if [ -z "$components" ]; then
|
|
reference="$PREV_GW"
|
|
if [ -z "$reference" ]; then
|
|
reference="$("$BIN_DIR/archied" -version 2>/dev/null | awk '$1 == "archied" { print $2; exit }')"
|
|
fi
|
|
if [ -n "$reference" ] && ! verify_installed_versions "$reference"; then
|
|
exit 1
|
|
fi
|
|
echo '==> no approved component changes; nothing to install'
|
|
echo 'ARCHIE_UPDATE_RESULT {"previous":{},"installed":{},"components":[],"restart_requested":false}'
|
|
exit 0
|
|
fi
|
|
|
|
valid_release_version() {
|
|
[[ "$1" =~ ^[0-9]+\.[0-9]+\.[0-9]+([+-][0-9A-Za-z.-]+)?$ ]]
|
|
}
|
|
|
|
for target in "$GW_TARGET" "$RT_TARGET"; do
|
|
if [ -n "$target" ] && ! valid_release_version "$target"; then
|
|
echo "refusing non-release component version '$target'" >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
STATE_DIR="${ARCHIE_UPDATE_STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/archie/update}"
|
|
mkdir -p "$STATE_DIR"
|
|
chmod 700 "$STATE_DIR"
|
|
exec 9>"$STATE_DIR/lock"
|
|
if ! flock -n 9; then
|
|
echo "update locked by $(cat "$STATE_DIR/holder" 2>/dev/null); journal: $STATE_DIR/transaction" >&2
|
|
exit 1
|
|
fi
|
|
printf 'installer PID %s\n' "$$" > "$STATE_DIR/holder"
|
|
if [ -d "$STATE_DIR/transaction" ]; then
|
|
echo "unfinished update: run $STATE_DIR/transaction/watchdog --recover before retrying" >&2
|
|
exit 1
|
|
fi
|
|
|
|
tmp="$(mktemp -d)"
|
|
trap 'rm -rf "$tmp"' EXIT
|
|
source_dir="$tmp/source"
|
|
release_dir="$tmp/release"
|
|
mkdir -p "$release_dir"
|
|
release_version="${GW_TARGET:-$RT_TARGET}"
|
|
if [ -n "$GW_TARGET" ] && [ -n "$RT_TARGET" ] && [ "$GW_TARGET" != "$RT_TARGET" ]; then
|
|
echo "daemon and agent must name the same release" >&2; exit 1
|
|
fi
|
|
if [ "$SOURCE_BUILD" = 1 ]; then
|
|
tag="v$release_version"
|
|
git ls-remote --exit-code --tags "$SOURCE_URL" "refs/tags/$tag" >/dev/null || tag="archied/v$release_version"
|
|
git clone --quiet --depth=1 --branch "$tag" "$SOURCE_URL" "$source_dir"
|
|
cp "$source_dir/release.json" "$release_dir/"
|
|
GATEWAY_BINARIES="$(jq -er '.required_topology.units | join(" ")' "$release_dir/release.json")"
|
|
cp "$source_dir"/scripts/archie-update-* "$release_dir/"
|
|
if [[ ",$components," == *,daemon,* ]]; then
|
|
for cmd in $GATEWAY_BINARIES; do
|
|
[[ "$cmd" =~ ^archie[a-z0-9-]*$ ]] || { echo "invalid release unit: $cmd" >&2; exit 1; }
|
|
(cd "$source_dir" && go build -ldflags "-X github.com/samcharles93/archie-core/internal/installtype.buildType=binary -X github.com/samcharles93/archie-core/internal/buildinfo.Version=$GW -X github.com/samcharles93/archie-core/internal/buildinfo.Runtime=$RT" -o "$release_dir/$cmd" "./cmd/$cmd")
|
|
done
|
|
fi
|
|
else
|
|
case "$(uname -s)/$(uname -m)" in
|
|
Linux/x86_64) platform=linux-amd64 ;;
|
|
*) echo "no published artifact for $(uname -s)/$(uname -m)" >&2; exit 1 ;;
|
|
esac
|
|
name="archie-core-archied-v$release_version-$platform"
|
|
base="${ARCHIE_RELEASE_BASE_URL:-https://github.com/samcharles93/archie-core/releases/download}/v$release_version"
|
|
if ! curl -fLSs --max-time 300 "$base/$name.zip" -o "$tmp/$name.zip"; then
|
|
base="${ARCHIE_RELEASE_BASE_URL:-https://github.com/samcharles93/archie-core/releases/download}/archied/v$release_version"
|
|
curl -fLSs --max-time 300 "$base/$name.zip" -o "$tmp/$name.zip"
|
|
fi
|
|
curl -fLSs --max-time 60 "$base/SHA256SUMS" -o "$tmp/SHA256SUMS"
|
|
expected="$(awk -v name="$name.zip" '$2 == name {print $1}' "$tmp/SHA256SUMS")"
|
|
[[ "$expected" =~ ^[a-fA-F0-9]{64}$ ]] || { echo "missing or ambiguous archive checksum" >&2; exit 1; }
|
|
printf '%s %s\n' "$expected" "$tmp/$name.zip" | sha256sum --check --status || { echo "release checksum mismatch" >&2; exit 1; }
|
|
# Reject paths outside the single distribution directory before extracting.
|
|
while IFS= read -r entry; do
|
|
case "$entry" in "$name/"*) ;; *) echo "unsafe archive path: $entry" >&2; exit 1 ;; esac
|
|
case "/$entry/" in */../*|*/./*|*\\*) echo "unsafe archive path: $entry" >&2; exit 1 ;; esac
|
|
done < <(unzip -Z1 "$tmp/$name.zip")
|
|
if unzip -Z -l "$tmp/$name.zip" | awk '$1 ~ /^l/ {found=1} END {exit !found}'; then
|
|
echo "release contains symlinks" >&2; exit 1
|
|
fi
|
|
unzip -q "$tmp/$name.zip" -d "$tmp/unpacked"
|
|
if find "$tmp/unpacked" -type l | grep -q .; then echo "release contains symlinks" >&2; exit 1; fi
|
|
cp -a "$tmp/unpacked/$name/." "$release_dir/"
|
|
fi
|
|
|
|
# Observation and config parsing belong to the daemon, including on old hosts.
|
|
observer="$release_dir/archied"
|
|
[ -x "$observer" ] || observer="$BIN_DIR/archied"
|
|
"$observer" update-topology -config "$CONFIG_PATH" -release "$release_dir/release.json"
|
|
GATEWAY_SERVICES="$(jq -er '.required_topology.units | join(" ")' "$release_dir/release.json")"
|
|
GATEWAY_BINARIES="$GATEWAY_SERVICES"
|
|
|
|
if [[ ",$components," == *,agent,* ]]; then
|
|
if [ "$SOURCE_BUILD" = 1 ]; then
|
|
docker build --build-arg "RUNTIME_VERSION=$RT" --iidfile "$tmp/image-id" "$source_dir"
|
|
AGENT_IMAGE="$(cat "$tmp/image-id")"
|
|
else
|
|
AGENT_IMAGE="$(jq -er '.agent_image' "$release_dir/release.json")"
|
|
[[ "$AGENT_IMAGE" =~ @sha256:[a-f0-9]{64}$ ]] || { echo "refusing agent update: release has no digest-pinned image" >&2; exit 1; }
|
|
docker pull "$AGENT_IMAGE"
|
|
fi
|
|
fi
|
|
if [ "$AUTO" = 1 ]; then
|
|
status="$(curl -fsS --max-time 10 "${ARCHIE_HEALTH_URL:-http://127.0.0.1:8485}/health/detailed")"
|
|
[ "$(jq -r '.in_flight' <<< "$status")" = 0 ] || { echo "refusing auto-update: daemon has work in flight or cannot report it" >&2; exit 1; }
|
|
fi
|
|
|
|
built=""
|
|
if [[ ",$components," == *,daemon,* ]]; then
|
|
for cmd in $GATEWAY_BINARIES; do
|
|
reported="$("$release_dir/$cmd" -version | awk -v cmd="$cmd" '$1 == cmd {print $2; exit}')"
|
|
[ "$reported" = "$GW" ] || { echo "candidate $cmd reports $reported, expected $GW" >&2; exit 1; }
|
|
done
|
|
mkdir -p "$(dirname "$DB_SNAPSHOT")"
|
|
"$BIN_DIR/archie-state-store" backup -config "$CONFIG_PATH" -out "$DB_SNAPSHOT"
|
|
built="$GATEWAY_BINARIES archie-update-install archie-update-watchdog archie-update-check"
|
|
fi
|
|
|
|
# Freeze recovery code and every prior file before publishing the intent.
|
|
journal="$STATE_DIR/preparing"
|
|
rm -rf "$journal"
|
|
mkdir -p "$journal/previous"
|
|
cp "$release_dir/archie-update-watchdog" "$journal/watchdog"
|
|
cp "$observer" "$journal/helper"
|
|
if [[ ",$components," == *,agent,* ]]; then
|
|
"$journal/helper" update-image -config "$CONFIG_PATH" -journal "$journal" -prepare "$AGENT_IMAGE"
|
|
fi
|
|
chmod 700 "$journal/watchdog"
|
|
for cmd in $built; do
|
|
if [ -f "$BIN_DIR/$cmd" ]; then cp -p "$BIN_DIR/$cmd" "$journal/previous/$cmd"; fi
|
|
install -m755 "$release_dir/$cmd" "$BIN_DIR/.$cmd.new"
|
|
done
|
|
if [ -f "$AGENT_VERSION_FILE" ]; then cp -p "$AGENT_VERSION_FILE" "$journal/agent.version"; fi
|
|
# No unit/config changes are made: topology migration is explicitly refused.
|
|
export ARCHIE_UPDATE_AUTO="$AUTO"
|
|
export ARCHIE_UPDATE_STATE_DIR="$STATE_DIR"
|
|
export ARCHIE_BIN_DIR="$BIN_DIR" ARCHIE_CONFIG_PATH="$CONFIG_PATH"
|
|
export ARCHIE_UPDATE_COMPONENTS="$components" ARCHIE_UPDATE_UNITS="$GATEWAY_SERVICES" ARCHIE_UPDATE_BINARIES="$built"
|
|
export ARCHIE_UPDATE_PREVIOUS_GATEWAY="$PREV_GW" ARCHIE_UPDATE_PREVIOUS_RUNTIME="$PREV_RT"
|
|
export ARCHIE_UPDATE_INSTALLED_GATEWAY="$GW" ARCHIE_UPDATE_INSTALLED_RUNTIME="$RT"
|
|
export ARCHIE_AGENT_VERSION_FILE="$AGENT_VERSION_FILE" ARCHIE_DB_SNAPSHOT="$DB_SNAPSHOT"
|
|
export ARCHIE_UPDATE_STATE_STORE_TARGET="$STATE_STORE_TARGET"
|
|
# Bash's quoted declarations preserve spaces without interpreting journal values.
|
|
while IFS= read -r var; do declare -p "$var"; done < <(compgen -e | grep -E '^ARCHIE_(UPDATE_|BIN_DIR$|CONFIG_PATH$|HEALTH_URL$|AGENT_VERSION_FILE$|DB_SNAPSHOT$)') > "$journal/environment"
|
|
printf 'prepared\n' > "$journal/phase"
|
|
sync -f "$BIN_DIR"
|
|
sync -f "$journal"
|
|
mv "$journal" "$STATE_DIR/transaction"
|
|
sync -f "$STATE_DIR"
|
|
journal="$STATE_DIR/transaction"
|
|
# The independent watchdog holds the lock and owns promotion, restart and rollback.
|
|
systemd-run --user --unit=archied-update-restart --collect --quiet \
|
|
--setenv=ARCHIE_UPDATE_STATE_DIR="$STATE_DIR" "$journal/watchdog"
|
|
case "$components" in
|
|
daemon)
|
|
previous="{\"daemon\":\"$(json_escape "$PREV_GW")\"}"
|
|
installed="{\"daemon\":\"$(json_escape "$GW")\"}"
|
|
results='[{"id":"daemon","label":"daemon","status":"updated"}]'
|
|
;;
|
|
agent)
|
|
previous="{\"agent\":\"$(json_escape "$PREV_RT")\"}"
|
|
installed="{\"agent\":\"$(json_escape "$RT")\"}"
|
|
results='[{"id":"agent","label":"agent","status":"updated"}]'
|
|
;;
|
|
daemon,agent)
|
|
previous="{\"daemon\":\"$(json_escape "$PREV_GW")\",\"agent\":\"$(json_escape "$PREV_RT")\"}"
|
|
installed="{\"daemon\":\"$(json_escape "$GW")\",\"agent\":\"$(json_escape "$RT")\"}"
|
|
results='[{"id":"daemon","label":"daemon","status":"updated"},{"id":"agent","label":"agent","status":"updated"}]'
|
|
;;
|
|
esac
|
|
echo "ARCHIE_UPDATE_RESULT {\"previous\":$previous,\"installed\":$installed,\"components\":$results,\"restart_requested\":true}"
|