feat(install): handle updates — stop old daemon, back up, install new version

- detect installed version via --version; skip when already up to date
- stop the running daemon and keep agent-arka.previous rollback copy on upgrade/downgrade
This commit is contained in:
ixoblakp committed 2026-10-01 00:04:56 +03:00
1 parent 57e627bad0
commit 05141af8fe
1 file changed
+38
+38
View File
@@ -157,6 +157,19 @@ ensure_path() {
warn "Added ${bindir} to PATH in ${rc} (open a new shell, or: source ${rc})"
}
# Normalize a CalVer "YYYY.M.D" into a single sortable integer (YYYYMMDD pads).
ver_cmp_key() {
printf '%s' "$1" | awk -F. '{ printf "%04d%02d%02d", $1, $2, $3 }' 2>/dev/null || printf '%s' 0
}
# Installed version, or empty when there is no binary/detectable version.
installed_version() {
local bin="$1" out
[[ -x "$bin" ]] || return 1
out=$("$bin" --version 2>/dev/null | head -n 1) || return 1
printf '%s\n' "$out" | grep -oE '[0-9]{4}\.[0-9]+(\.[0-9]+)?' | tail -n 1
}
main() {
printf '%s\n' "${C_BOLD}Agent Arka${C_RESET} — web dashboard installer"
command -v curl >/dev/null 2>&1 || die "curl is required"
@@ -183,6 +196,31 @@ main() {
info "Latest: ${version}"
fi
# Update handling: detect an existing install and skip / stop / back it up.
local existing
existing=$(installed_version "${bindir}/agent-arka" 2>/dev/null || true)
if [[ -n "$existing" ]]; then
local ek tk
ek=$(ver_cmp_key "$existing")
tk=$(ver_cmp_key "$version")
if [[ "$ek" == "$tk" ]]; then
info "Already up to date: agent-arka ${existing} (target ${version})"
ensure_path "$bindir"
return 0
fi
if [[ "$ek" -gt "$tk" ]]; then
warn "Downgrading ${existing} -> ${version}"
else
info "Upgrading ${existing} -> ${version}"
fi
# Bring the running daemon down before replacing the binary.
if AGENT_ARKA_HOME="${HOME_DIR}" LIBREFANG_HOME="${HOME_DIR}" "${bindir}/agent-arka" stop >/dev/null 2>&1; then
ok "Stopped running agent-arka ${existing}"
fi
# Keep a rollback copy of the current binary.
cp -f "${bindir}/agent-arka" "${bindir}/agent-arka.previous" 2>/dev/null || true
fi
local tag="${TAG_PREFIX}${version}"
local asset="agent-arka-${version}-${PLATFORM}.tar.gz"
local url="${PUBLIC_DL}/${tag}/${asset}"