fix(launcher): run on a bare Alpine host (POSIX prologue, self-installs bash)
automations.sh was #!/usr/bin/env bash and written in bash, but a stock Alpine
box has busybox ash and no bash at all -- so the launcher died before it could
install anything:
./automations.sh -> env: 'bash': No such file or directory
curl ... | bash -> bash: not found
sh automations.sh -> syntax errors
cloud-init/base.yml already installs bash/git/curl before touching the repo;
the launcher never got the same treatment.
Give it a #!/bin/sh shebang and a strictly-POSIX prologue that ash can parse:
self-locate via ${0%/*} (busybox dirname takes `--` as the filename, so
`dirname -- "$0"` would answer "."), clone on the piped path as before, install
git/bash via apk/apt-get/dnf/yum, then exec bash on this same file. The bash
pass skips the prologue via BASH_VERSION, and an exported _AUTOMATIONS_REEXEC
guard rules out an exec loop. Everything below the handoff is the unchanged
bash launcher.
The generated bundle stub had the identical bug -- it's the other fresh-host
entry point -- so make it POSIX sh too and have it install bash before
extracting.
Docs: the one-liner now pipes to `sh`, bundle examples use `sh`.
Still bash-only on a bare Alpine host: the scp'd deployments/*/deploy.sh
artifacts.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b027de2182
commit
f71f8d615a
3 files changed
+124
-42
No files matched your search
+83
-28
@@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
#!/bin/sh
|
||||
#
|
||||
# automations.sh -- one command to run or deploy anything in this repo.
|
||||
#
|
||||
@@ -6,7 +6,7 @@
|
||||
#
|
||||
# 1. One-liner on a fresh target host (clones the repo, then launches):
|
||||
# curl -fsSL https://git.anomalous.dev/57_Wolve/automations/raw/branch/main/automations.sh \
|
||||
# | REPO_URL=https://git.anomalous.dev/57_Wolve/automations.git bash
|
||||
# | REPO_URL=https://git.anomalous.dev/57_Wolve/automations.git sh
|
||||
#
|
||||
# 2. From a clone:
|
||||
# ./automations.sh
|
||||
@@ -22,43 +22,98 @@
|
||||
# choices in, or just call the underlying deployments/<name>/deploy.sh
|
||||
# directly -- they all honor SKIP_PROMPTS=1.
|
||||
|
||||
set -euo pipefail
|
||||
# ============================================================================
|
||||
# PROLOGUE -- POSIX sh only. Everything below the "exec bash" handoff is bash.
|
||||
#
|
||||
# The shebang is /bin/sh, not bash, on purpose: a stock Alpine box has busybox
|
||||
# ash and NO bash at all, so a `#!/usr/bin/env bash` launcher dies before it
|
||||
# can install anything ("env: 'bash': No such file or directory"). This part
|
||||
# therefore has to parse and run under ash: no [[ ]], no arrays, no
|
||||
# BASH_SOURCE, no printf -v. It locates (or clones) the repo, makes sure bash
|
||||
# exists, and re-execs this same file under bash -- which then skips the
|
||||
# prologue via BASH_VERSION and runs the real launcher.
|
||||
# ============================================================================
|
||||
set -eu
|
||||
|
||||
_boot_log() { printf '\033[1;32m[+]\033[0m %s\n' "$*"; }
|
||||
_boot_die() { printf '\033[1;31m[x]\033[0m %s\n' "$*" >&2; exit 1; }
|
||||
|
||||
# Install packages with whichever manager this distro has -- apk (Alpine),
|
||||
# apt-get (Debian/Ubuntu), dnf/yum (Alma/RHEL). oslib.sh's pkg_install can't
|
||||
# help here: it's bash, and on the piped path it isn't even on disk yet.
|
||||
_boot_install() {
|
||||
if command -v apk >/dev/null 2>&1; then
|
||||
apk add -q "$@" 2>/dev/null && return 0
|
||||
apk update -q >/dev/null 2>&1 || true # stale/absent index on a fresh box
|
||||
apk add -q "$@"
|
||||
elif command -v apt-get >/dev/null 2>&1; then
|
||||
apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$@"
|
||||
elif command -v dnf >/dev/null 2>&1; then
|
||||
dnf install -y -q "$@"
|
||||
elif command -v yum >/dev/null 2>&1; then
|
||||
yum install -y -q "$@"
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
_boot_need() { # _boot_need <command> [package] -> 0 if it's available afterwards
|
||||
if command -v "$1" >/dev/null 2>&1; then return 0; fi
|
||||
_boot_log "$1 not found; installing it..."
|
||||
_boot_install "${2:-$1}" || true
|
||||
command -v "$1" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
# Self-locate, or bootstrap by cloning the repo (one-liner / piped form).
|
||||
# ----------------------------------------------------------------------------
|
||||
_self="${BASH_SOURCE[0]:-}"
|
||||
if [[ -n "$_self" && -f "$(cd "$(dirname "$_self")" 2>/dev/null && pwd)/scripts/lib.sh" ]]; then
|
||||
ROOT="$(cd "$(dirname "$_self")" && pwd)"
|
||||
ROOT=""
|
||||
# Strip the last path component ourselves rather than calling dirname: busybox
|
||||
# dirname takes its first argument literally, so `dirname -- "$0"` would answer
|
||||
# "." on Alpine. `$0` is "sh"/"bash" (no slash) when we're piped from curl.
|
||||
case "$0" in
|
||||
*/*) _dir="${0%/*}" ;;
|
||||
*) _dir="." ;;
|
||||
esac
|
||||
_dir="$(CDPATH= cd "$_dir" 2>/dev/null && pwd)" || _dir=""
|
||||
if [ -n "$_dir" ] && [ -f "$_dir/scripts/lib.sh" ]; then
|
||||
ROOT="$_dir"
|
||||
else
|
||||
# Piped via curl: we don't have the repo on disk. Clone it, then re-exec.
|
||||
# Piped via curl: we don't have the repo on disk. Clone it, then hand off.
|
||||
: "${REPO_URL:=}"
|
||||
: "${REPO_BRANCH:=main}"
|
||||
[[ -n "$REPO_URL" ]] || {
|
||||
echo "[x] Running standalone (piped). Set REPO_URL=... so I can clone the repo." >&2
|
||||
exit 1
|
||||
}
|
||||
# We need git to clone, but oslib.sh's pkg_install isn't on disk yet (that's
|
||||
# what we're cloning). Install git inline across the supported package
|
||||
# managers -- apk (Alpine), apt-get (Debian/Ubuntu), dnf/yum (Alma/RHEL).
|
||||
if ! command -v git >/dev/null 2>&1; then
|
||||
echo "[+] git not found; installing it..." >&2
|
||||
if command -v apk >/dev/null 2>&1; then apk add -q git || true
|
||||
elif command -v apt-get >/dev/null 2>&1; then { apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq git; } || true
|
||||
elif command -v dnf >/dev/null 2>&1; then dnf install -y -q git || true
|
||||
elif command -v yum >/dev/null 2>&1; then yum install -y -q git || true
|
||||
fi
|
||||
fi
|
||||
command -v git >/dev/null 2>&1 || {
|
||||
echo "[x] git is required to clone the repo, but it isn't installed and I couldn't install it automatically (need root + a supported package manager). Install git, then re-run." >&2
|
||||
exit 1
|
||||
}
|
||||
[ -n "$REPO_URL" ] || _boot_die "Running standalone (piped). Set REPO_URL=... so I can clone the repo."
|
||||
_boot_need git || _boot_die "git is required to clone the repo, but it isn't installed and I couldn't install it automatically (need root + a supported package manager). Install git, then re-run."
|
||||
_tmp="$(mktemp -d -t automations.XXXXXX)"
|
||||
echo "[+] Cloning $REPO_URL ($REPO_BRANCH)..."
|
||||
_boot_log "Cloning $REPO_URL ($REPO_BRANCH)..."
|
||||
git clone --depth 1 --branch "$REPO_BRANCH" "$REPO_URL" "$_tmp"
|
||||
exec bash "$_tmp/automations.sh" "$@"
|
||||
ROOT="$_tmp"
|
||||
fi
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
# Hand off to bash. Needed when we're running under ash/dash, and when the body
|
||||
# we want is the freshly cloned copy rather than the piped stdin we came from.
|
||||
# ----------------------------------------------------------------------------
|
||||
if [ -z "${BASH_VERSION:-}" ] || [ ! -f "$0" ] || [ "$ROOT" != "$_dir" ]; then
|
||||
if [ "${_AUTOMATIONS_REEXEC:-0}" = 1 ]; then
|
||||
# Already handed off once. If we're in bash the handoff worked and only
|
||||
# the path comparison differs (symlinked checkout) -- just continue.
|
||||
[ -n "${BASH_VERSION:-}" ] || _boot_die "Re-exec under bash did not take effect. Run it explicitly: bash $ROOT/automations.sh"
|
||||
else
|
||||
# The launcher, everything it sources (scripts/lib.sh, scripts/oslib.sh),
|
||||
# and every deploy.sh it invokes are bash. Alpine images routinely ship
|
||||
# without it, so install it before going any further.
|
||||
_boot_need bash || _boot_die "bash is required, but it isn't installed and I couldn't install it automatically (need root + a supported package manager). Install bash, then re-run."
|
||||
_AUTOMATIONS_REEXEC=1; export _AUTOMATIONS_REEXEC
|
||||
exec bash "$ROOT/automations.sh" "$@"
|
||||
fi
|
||||
fi
|
||||
|
||||
# ============================================================================
|
||||
# Running under bash from here down.
|
||||
# ============================================================================
|
||||
set -euo pipefail
|
||||
|
||||
# shellcheck source=scripts/lib.sh
|
||||
. "$ROOT/scripts/lib.sh"
|
||||
load_globals
|
||||
|
||||
Reference in new issue
Block a user