Files
goclaw/docker-entrypoint.sh
T
Thieu NguyenandGitHub 7f1f126091 fix(docker): make runtime dir creation non-fatal in entrypoint (#288)
On first start with a fresh Docker volume, mkdir for .runtime
subdirectories can fail due to a volume initialisation race condition,
causing the container to restart loop. The directories are only needed
for agent-installed packages (pip/npm), so the failure is non-fatal.
2026-03-20 07:53:29 +07:00

109 lines
3.5 KiB
Bash

#!/bin/sh
set -e
# Set up writable runtime directories for agent-installed packages.
# Rootfs is read-only; /app/data is a writable Docker volume.
RUNTIME_DIR="/app/data/.runtime"
# Non-fatal: on first start with a fresh volume the directory may not be
# writable yet (volume initialisation race on some Docker runtimes).
# The app starts fine without .runtime; package installs will fail gracefully.
mkdir -p "$RUNTIME_DIR/pip" "$RUNTIME_DIR/npm-global/lib" || true
# Python: allow agent to pip install to writable target dir
export PYTHONPATH="$RUNTIME_DIR/pip:${PYTHONPATH:-}"
export PIP_TARGET="$RUNTIME_DIR/pip"
export PIP_BREAK_SYSTEM_PACKAGES=1
export PIP_CACHE_DIR="$RUNTIME_DIR/pip-cache"
mkdir -p "$RUNTIME_DIR/pip-cache" || true
# Node.js: allow agent to npm install -g to writable prefix
# NODE_PATH includes both pre-installed system globals and runtime-installed globals.
export NPM_CONFIG_PREFIX="$RUNTIME_DIR/npm-global"
export NODE_PATH="/usr/local/lib/node_modules:$RUNTIME_DIR/npm-global/lib/node_modules:${NODE_PATH:-}"
export PATH="$RUNTIME_DIR/npm-global/bin:$RUNTIME_DIR/pip/bin:$PATH"
# System packages: re-install on-demand packages persisted across recreates.
# In Docker: entrypoint runs as root (then drops via su-exec).
# Outside Docker: may run as non-root — skip privileged operations gracefully.
APK_LIST="$RUNTIME_DIR/apk-packages"
touch "$APK_LIST" 2>/dev/null || true
if [ "$(id -u)" = "0" ]; then
chown root:goclaw "$APK_LIST" 2>/dev/null || true
chmod 0640 "$APK_LIST" 2>/dev/null || true
fi
if [ -f "$APK_LIST" ] && [ -s "$APK_LIST" ]; then
echo "Re-installing persisted system packages..."
VALID_PKGS=""
while IFS= read -r pkg || [ -n "$pkg" ]; do
pkg="$(printf '%s' "$pkg" | tr -d '[:space:]')"
case "$pkg" in
[a-zA-Z0-9@]*) VALID_PKGS="$VALID_PKGS $pkg" ;;
"") ;;
*) echo "WARNING: skipping invalid package: $pkg" ;;
esac
done < "$APK_LIST"
if [ -n "$VALID_PKGS" ]; then
# shellcheck disable=SC2086
apk add --no-cache $VALID_PKGS 2>/dev/null || \
echo "Warning: some packages failed to install"
fi
fi
# Start the root-privileged package helper (listens on /tmp/pkg.sock).
# Only in Docker (running as root). Outside Docker, pkg-helper is not available.
if [ -x /app/pkg-helper ] && [ "$(id -u)" = "0" ]; then
/app/pkg-helper &
PKG_PID=$!
for _i in 1 2 3 4; do
[ -S /tmp/pkg.sock ] && break
sleep 0.5
done
if ! [ -S /tmp/pkg.sock ]; then
echo "ERROR: pkg-helper failed to start (PID $PKG_PID)"
kill "$PKG_PID" 2>/dev/null || true
fi
fi
# Run command with privilege drop (su-exec in Docker, direct otherwise).
run_as_goclaw() {
if command -v su-exec >/dev/null 2>&1 && [ "$(id -u)" = "0" ]; then
exec su-exec goclaw "$@"
else
exec "$@"
fi
}
case "${1:-serve}" in
serve)
# Auto-upgrade (schema migrations + data hooks) before starting.
if [ -n "$GOCLAW_POSTGRES_DSN" ]; then
echo "Running database upgrade..."
if command -v su-exec >/dev/null 2>&1 && [ "$(id -u)" = "0" ]; then
su-exec goclaw /app/goclaw upgrade || \
echo "Upgrade warning (may already be up-to-date)"
else
/app/goclaw upgrade || \
echo "Upgrade warning (may already be up-to-date)"
fi
fi
run_as_goclaw /app/goclaw
;;
upgrade)
shift
run_as_goclaw /app/goclaw upgrade "$@"
;;
migrate)
shift
run_as_goclaw /app/goclaw migrate "$@"
;;
onboard)
run_as_goclaw /app/goclaw onboard
;;
version)
run_as_goclaw /app/goclaw version
;;
*)
run_as_goclaw /app/goclaw "$@"
;;
esac