Stop deploy.sh running under sudo or from an outdated checkout
Under sudo, ssh reads root's config and keys, so a custom Port silently disappears and the deploy fails with "connect to host ... port 22: Connection refused". deploy.sh needs no root locally (install.sh runs under sudo on each host), so it now refuses when run as root via sudo, naming the user to run it as. A real root login is still allowed. Deploying from a checkout behind its upstream ships an older agent than the server expects, and the dashboard keeps saying "agent is older" after a deploy that looked fine. deploy.sh now fetches the upstream (20s limit) and, if the checkout is behind, asks on a terminal or refuses otherwise; --allow-outdated deploys anyway and --dry-run only warns. A failed fetch warns and carries on; no checkout or no upstream skips the check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4f45187ca0
commit
c3ed3c026b
2 files changed
+148
-1
No files matched your search
+43
-1
@@ -8,6 +8,7 @@
|
||||
# agent/deploy.sh --rotate web01 issue a new token, then reinstall
|
||||
# agent/deploy.sh --dry-run show what would happen
|
||||
# agent/deploy.sh -- --with-needrestart pass the rest to install.sh
|
||||
# agent/deploy.sh --allow-outdated deploy even if this checkout is behind
|
||||
#
|
||||
# Settings: agent/deploy.conf (see deploy.conf.example). Hosts: agent/hosts,
|
||||
# one per line: `<ssh target> [kernelwatch name]`; the name defaults to the
|
||||
@@ -149,10 +150,47 @@ deploy_one() {
|
||||
fi
|
||||
}
|
||||
|
||||
usage() { sed -n '2,16s/^# \{0,1\}//p' "$0"; }
|
||||
usage() { sed -n '2,17s/^# \{0,1\}//p' "$0"; }
|
||||
|
||||
# Under sudo, ssh would read root's ~/.ssh/config and keys instead of yours (a
|
||||
# custom Port or IdentityFile silently disappears, so it tries port 22). Nothing
|
||||
# here needs root locally: install.sh runs under sudo on each host.
|
||||
refuse_sudo() {
|
||||
[[ -n ${SUDO_USER:-} && $(id -u) == 0 ]] || return 0
|
||||
die "don't run this with sudo: it uses your own SSH config and keys, and runs sudo on each host itself. Run it as $SUDO_USER."
|
||||
}
|
||||
|
||||
# Deploying from a checkout that's behind its upstream ships an old agent: the
|
||||
# dashboard keeps showing "agent is older" after a deploy that looked fine.
|
||||
# Fetches with a time limit; a failed fetch only warns. Behind: asks on a
|
||||
# terminal, otherwise needs --allow-outdated. Skipped outside a git checkout or
|
||||
# without an upstream branch.
|
||||
check_checkout() {
|
||||
local repo upstream behind
|
||||
repo=$(git -C "$here" rev-parse --show-toplevel 2>/dev/null) || return 0
|
||||
upstream=$(git -C "$repo" rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null) || return 0
|
||||
local -a fetch=(git -C "$repo" fetch --quiet)
|
||||
command -v timeout >/dev/null && fetch=(timeout 20 "${fetch[@]}")
|
||||
if ! GIT_TERMINAL_PROMPT=0 "${fetch[@]}" >/dev/null 2>&1; then
|
||||
echo "deploy: warning: couldn't fetch $upstream to check this checkout is up to date" >&2
|
||||
return 0
|
||||
fi
|
||||
behind=$(git -C "$repo" rev-list --count "HEAD..$upstream" 2>/dev/null) || return 0
|
||||
((behind > 0)) || return 0
|
||||
local msg="this checkout is $behind commit(s) behind $upstream, so it would deploy an older agent (git pull first)"
|
||||
if ((allow_outdated || dry_run)); then
|
||||
echo "deploy: warning: $msg" >&2
|
||||
return 0
|
||||
fi
|
||||
[[ -t 0 && -t 2 ]] || die "$msg; pass --allow-outdated to deploy it anyway"
|
||||
local answer
|
||||
read -r -p "deploy: $msg. Deploy anyway? [y/N] " answer
|
||||
[[ $answer == [yY]* ]] || die "stopped; nothing was deployed"
|
||||
}
|
||||
|
||||
mode=upgrade
|
||||
dry_run=0
|
||||
allow_outdated=0
|
||||
targets=()
|
||||
install_args=()
|
||||
while (($#)); do
|
||||
@@ -165,6 +203,7 @@ while (($#)); do
|
||||
--register) mode=register ;;
|
||||
--rotate) mode=rotate ;;
|
||||
--dry-run) dry_run=1 ;;
|
||||
--allow-outdated) allow_outdated=1 ;;
|
||||
-h | --help) usage; exit 0 ;;
|
||||
-*) usage >&2; exit 2 ;;
|
||||
*) targets+=("$1") ;;
|
||||
@@ -172,6 +211,7 @@ while (($#)); do
|
||||
shift
|
||||
done
|
||||
|
||||
refuse_sudo
|
||||
load_config
|
||||
[[ -n $SERVER_URL ]] || die "SERVER_URL is not set in $CONF"
|
||||
[[ $mode == upgrade || -n $DASHBOARD ]] || die "DASHBOARD is not set in $CONF"
|
||||
@@ -182,6 +222,8 @@ if ((${#targets[@]} == 0)); then
|
||||
((${#targets[@]})) || die "no hosts given and none in $HOSTS_FILE"
|
||||
fi
|
||||
|
||||
check_checkout
|
||||
|
||||
failed=()
|
||||
for t in "${targets[@]}"; do
|
||||
say "==> $t"
|
||||
|
||||
Reference in new issue
Block a user