Build selected components from a temporary worktree at the exact approved tag commit instead of the moving branch tip. Validate release version inputs and require combined component tags to resolve to the same source commit.\n\nRefs #606
154 lines
6.0 KiB
Bash
Executable File
154 lines
6.0 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'
|
|
}
|
|
|
|
REPO="${ARCHIE_REPO_DIR:-$HOME/projects/archie-core}"
|
|
BIN_DIR="${ARCHIE_BIN_DIR:-$HOME/.local/bin}"
|
|
export PATH="/usr/local/go/bin:$PATH"
|
|
|
|
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
|
|
|
|
if [ -z "$components" ]; then
|
|
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
|
|
|
|
cd "$REPO"
|
|
echo "==> fetching approved component releases"
|
|
git pull --ff-only
|
|
|
|
# Build from the exact approved release commit, not the moving branch tip. A
|
|
# temporary worktree keeps the operator's main checkout on its branch.
|
|
if [[ ",$components," == *,daemon,* ]]; then
|
|
gw_commit="$(git -C "$REPO" rev-parse --verify "refs/tags/archied/v$GW^{commit}")"
|
|
fi
|
|
if [[ ",$components," == *,agent,* ]]; then
|
|
rt_commit="$(git -C "$REPO" rev-parse --verify "refs/tags/archie/v$RT^{commit}")"
|
|
fi
|
|
case "$components" in
|
|
daemon) build_commit="$gw_commit" ;;
|
|
agent) build_commit="$rt_commit" ;;
|
|
daemon,agent)
|
|
if [ "$gw_commit" != "$rt_commit" ]; then
|
|
echo "approved component tags do not identify the same source commit" >&2
|
|
exit 1
|
|
fi
|
|
build_commit="$gw_commit"
|
|
;;
|
|
esac
|
|
|
|
tmp="$(mktemp -d)"
|
|
source_dir="$tmp/source"
|
|
cleanup() {
|
|
git -C "$REPO" worktree remove --force "$source_dir" >/dev/null 2>&1 || true
|
|
rm -rf "$tmp"
|
|
}
|
|
trap cleanup EXIT
|
|
git -C "$REPO" worktree add --detach "$source_dir" "$build_commit"
|
|
cd "$source_dir"
|
|
|
|
# Refresh adapters from the approved release source. The running shell
|
|
# continues with this copy; subsequent updates use the current contract.
|
|
install -m755 "$source_dir/scripts/archie-update-install" "$BIN_DIR/archie-update-install"
|
|
install -m755 "$source_dir/scripts/archie-update-watchdog" "$BIN_DIR/archie-update-watchdog"
|
|
install -m755 "$source_dir/scripts/archie-update-check" "$BIN_DIR/archie-update-check"
|
|
|
|
if [[ ",$components," == *,daemon,* ]]; then
|
|
echo "==> building archied $GW"
|
|
go build -ldflags "-X main.gatewayVersion=$GW -X main.runtimeVersion=$RT -X github.com/samcharles93/archie-core/internal/installtype.buildType=binary" \
|
|
-o "$tmp/archied" ./cmd/archied
|
|
fi
|
|
if [[ ",$components," == *,agent,* ]]; then
|
|
echo "==> building archie-agent $RT"
|
|
go build -ldflags "-X github.com/samcharles93/archie-core/internal/app/agentworker.version=$RT -X github.com/samcharles93/archie-core/internal/installtype.buildType=binary" \
|
|
-o "$tmp/archie-agent" ./cmd/archie-agent
|
|
fi
|
|
|
|
if [[ ",$components," == *,daemon,* ]]; then
|
|
echo "==> backing up current archied binary"
|
|
[ -f "$BIN_DIR/archied" ] && cp -f "$BIN_DIR/archied" "$BIN_DIR/archied.prev"
|
|
echo "==> installing archied"
|
|
install -m755 "$tmp/archied" "$BIN_DIR/archied"
|
|
fi
|
|
if [[ ",$components," == *,agent,* ]]; then
|
|
echo "==> backing up current archie-agent binary"
|
|
[ -f "$BIN_DIR/archie-agent" ] && cp -f "$BIN_DIR/archie-agent" "$BIN_DIR/archie-agent.prev"
|
|
echo "==> installing archie-agent"
|
|
install -m755 "$tmp/archie-agent" "$BIN_DIR/archie-agent"
|
|
echo "==> rebuilding agent image for runtime $RT"
|
|
docker compose build --build-arg "RUNTIME_VERSION=$RT" agent
|
|
fi
|
|
|
|
echo "==> handing off to the update watchdog for restart and health verification"
|
|
systemd-run --user --unit=archied-update-restart --collect --quiet \
|
|
--setenv=ARCHIE_BIN_DIR="$BIN_DIR" \
|
|
--setenv=ARCHIE_HEALTH_URL="${ARCHIE_HEALTH_URL:-http://127.0.0.1:8420}" \
|
|
--setenv=ARCHIE_UPDATE_REPORT_PATH="${ARCHIE_UPDATE_REPORT_PATH:-}" \
|
|
--setenv=ARCHIE_UPDATE_CHANNEL="${ARCHIE_UPDATE_CHANNEL:-}" \
|
|
--setenv=ARCHIE_UPDATE_CHAT_ID="${ARCHIE_UPDATE_CHAT_ID:-0}" \
|
|
--setenv=ARCHIE_UPDATE_THREAD_ID="${ARCHIE_UPDATE_THREAD_ID:-0}" \
|
|
--setenv=ARCHIE_UPDATE_COMPONENTS="$components" \
|
|
--setenv=ARCHIE_UPDATE_PREVIOUS_GATEWAY="$PREV_GW" \
|
|
--setenv=ARCHIE_UPDATE_PREVIOUS_RUNTIME="$PREV_RT" \
|
|
--setenv=ARCHIE_UPDATE_INSTALLED_GATEWAY="$GW" \
|
|
--setenv=ARCHIE_UPDATE_INSTALLED_RUNTIME="$RT" \
|
|
"$BIN_DIR/archie-update-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}"
|