mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
245 lines
11 KiB
Bash
Executable file
245 lines
11 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# ARTEX update script: (1) Docker update (rebuild the image from this checkout) (2) local build update (rebuild the binary)
|
|
# Counterpart of install.sh: install handles the first-time setup, update upgrades to a new version.
|
|
# No manual DB migration needed — artex idempotently re-runs schema.sql on every start (with ADD COLUMN/CREATE
|
|
# INDEX IF NOT EXISTS), so "restart equals migrate". Data (pgdata volume, ./data, ./skills) is not affected.
|
|
set -euo pipefail
|
|
cd "$(cd "$(dirname "$0")" && pwd)"
|
|
|
|
info(){ printf '\033[36m[*]\033[0m %s\n' "$*"; }
|
|
ok(){ printf '\033[32m[+]\033[0m %s\n' "$*"; }
|
|
warn(){ printf '\033[33m[!]\033[0m %s\n' "$*"; }
|
|
die(){ printf '\033[31m[x]\033[0m %s\n' "$*" >&2; exit 1; }
|
|
ask(){ local p="$1" d="${2:-}" a; read -rp "$p${d:+ [$d]}: " a; echo "${a:-$d}"; }
|
|
rand(){ head -c 18 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c 24; }
|
|
|
|
# ── Build identity: keep .env and the binary in step with the checkout ─────────
|
|
BUILD_VERSION="dev"
|
|
BUILD_REVISION=""
|
|
|
|
# set_env replaces KEY=... in .env (or appends it) without touching other lines.
|
|
set_env(){
|
|
local key="$1" value="$2" tmp
|
|
tmp="$(mktemp)"
|
|
awk -v k="$key" -v v="$value" '
|
|
$0 ~ "^" k "=" { print k "=" v; found=1; next }
|
|
{ print }
|
|
END { if (!found) print k "=" v }
|
|
' .env > "$tmp"
|
|
chmod --reference=.env "$tmp" 2>/dev/null || true
|
|
mv "$tmp" .env
|
|
}
|
|
|
|
# stamp_version records the commit the image is about to be built from. Without
|
|
# it docker-compose.yml passes ARTEX_VERSION=dev and every local install reports
|
|
# the same meaningless version in the UI and the startup banner.
|
|
stamp_version(){
|
|
if ! command -v git >/dev/null 2>&1 || ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
|
warn "not a git working copy: leaving ARTEX_VERSION in .env unchanged"
|
|
return 0
|
|
fi
|
|
BUILD_VERSION="$(git describe --tags --always --dirty 2>/dev/null || git rev-parse --short HEAD)"
|
|
BUILD_REVISION="$(git rev-parse HEAD 2>/dev/null || true)"
|
|
if [ -f .env ]; then
|
|
set_env ARTEX_VERSION "$BUILD_VERSION"
|
|
[ -n "$BUILD_REVISION" ] && set_env ARTEX_REVISION "$BUILD_REVISION"
|
|
fi
|
|
ok "Build identity: ${BUILD_VERSION}${BUILD_REVISION:+ (${BUILD_REVISION:0:12})}"
|
|
}
|
|
|
|
# normalize_env rewrites a CRLF .env as LF. A Windows checkout with
|
|
# core.autocrlf=true materializes CRLF, and every value then carries a trailing
|
|
# \r: compose reports an empty required variable and PostgreSQL receives a
|
|
# password with a stray CR.
|
|
normalize_env(){
|
|
[ -f .env ] || return 0
|
|
grep -q $'\r' .env 2>/dev/null || return 0
|
|
sed -i.bak 's/\r$//' .env && rm -f .env.bak
|
|
ok ".env converted from CRLF to LF"
|
|
}
|
|
|
|
# ensure_runner_token fills ARTEX_RUNNER_TOKEN in .env once. The panel and the
|
|
# runner authenticate the internal execution API with it; an empty value makes
|
|
# the panel fail closed, so an existing .env gets one before the upgrade.
|
|
ensure_runner_token(){
|
|
[ -f .env ] || return 0
|
|
grep -q '^ARTEX_RUNNER_TOKEN=[^[:space:]]' .env && return 0
|
|
set_env ARTEX_RUNNER_TOKEN "$(rand)"
|
|
ok "ARTEX_RUNNER_TOKEN generated"
|
|
}
|
|
|
|
# ── Optional: sync the repo to the latest code (compose/scripts/local build sources all update through it) ───────
|
|
sync_repo(){
|
|
[ -d .git ] && command -v git >/dev/null 2>&1 || { warn "not a git working copy, skipping git pull"; return 0; }
|
|
if [ "$(ask 'Pull the latest code (git pull --ff-only)? (y/n)' y)" = y ]; then
|
|
if ! git pull --ff-only; then
|
|
# Continuing would rebuild the OLD tree and still print "Build complete":
|
|
# exactly the silent stale upgrade this script exists to prevent.
|
|
cat >&2 <<'RECOVER'
|
|
[!] git pull could not fast-forward. This happens when the checkout has local
|
|
changes, or when it was cloned before the history rewrite of 2026-10-23.
|
|
Refusing to rebuild stale code. Recover, then rerun this script:
|
|
keep your changes: git stash && git fetch origin && git reset --hard origin/main && git stash pop
|
|
discard them: git fetch origin && git reset --hard origin/main
|
|
RECOVER
|
|
die "aborting: nothing was rebuilt"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# resolve_docker_sock picks and validates the host Docker API socket before the
|
|
# images are rebuilt. Docker Engine uses /var/run/docker.sock; rootless Podman
|
|
# exposes the same API under XDG_RUNTIME_DIR, so a hardcoded path cannot work
|
|
# there (issue #742). persist_docker_sock records it in .env, and only the host
|
|
# side is configurable: compose mounts it at the fixed in-container path the
|
|
# runner dials.
|
|
DOCKER_SOCK_RESOLVED=""
|
|
DOCKER_ENGINE_PODMAN=0
|
|
resolve_docker_sock(){
|
|
local sock="" engine=""
|
|
DOCKER_SOCK_RESOLVED=""
|
|
DOCKER_ENGINE_PODMAN=0
|
|
case "${DOCKER_HOST:-}" in
|
|
"") ;;
|
|
unix://*) sock="${DOCKER_HOST#unix://}" ;;
|
|
*) die "the sandbox runner needs a local Unix Docker socket, but DOCKER_HOST=${DOCKER_HOST} cannot be mounted into a container. Unset DOCKER_HOST or point it at a unix:// socket" ;;
|
|
esac
|
|
|
|
# The CLI has no .Server.Engine field; the platform name is where Docker and
|
|
# Podman identify themselves ("Docker Engine - Community", "Podman Engine").
|
|
# Detection also runs for an explicit DOCKER_HOST so the warning below covers
|
|
# the common Podman-compat setup.
|
|
engine="$(docker version --format '{{.Server.Platform.Name}}' 2>/dev/null || true)"
|
|
case "$engine" in
|
|
*[Pp]odman*) DOCKER_ENGINE_PODMAN=1 ;;
|
|
esac
|
|
|
|
# Podman advertises a Docker-compatible socket; ask it where its API socket
|
|
# actually is instead of assuming the Docker default.
|
|
if [ "$DOCKER_ENGINE_PODMAN" = 1 ] && [ -z "$sock" ]; then
|
|
if command -v podman >/dev/null 2>&1; then
|
|
sock="$(podman info --format '{{.Host.RemoteSocket.Path}}' 2>/dev/null || true)"
|
|
fi
|
|
[ -n "$sock" ] || sock="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/podman/podman.sock"
|
|
fi
|
|
[ -n "$sock" ] || sock="/var/run/docker.sock"
|
|
|
|
# Docker Desktop on macOS publishes /var/run/docker.sock as a symlink into
|
|
# ~/.docker/run, so follow it (test -S and find -L) instead of refusing it.
|
|
if [ ! -S "$sock" ]; then
|
|
die "no Docker API socket at $sock; if the engine uses another path, set DOCKER_HOST=unix:///path/to.sock and rerun (issue #742)"
|
|
fi
|
|
if [ ! -r "$sock" ] || [ ! -w "$sock" ]; then
|
|
die "the Docker API socket $sock is not readable and writable by $(id -un); add that user to the engine's group or run a rootless engine"
|
|
fi
|
|
if find -L "$sock" -maxdepth 0 -perm -0002 2>/dev/null | grep -q .; then
|
|
die "the Docker API socket $sock is world-writable; run 'chmod o-w $sock' and rerun"
|
|
fi
|
|
DOCKER_SOCK_RESOLVED="$sock"
|
|
if [ "$DOCKER_ENGINE_PODMAN" = 1 ]; then
|
|
warn "Podman detected: the API socket is supported, but task networking, cgroup limits and port publishing are not verified yet (#742); run one task and report what breaks"
|
|
fi
|
|
}
|
|
|
|
# persist_docker_sock records the resolved socket in .env. The default path is
|
|
# left implicit so existing .env files and installations do not change.
|
|
persist_docker_sock(){
|
|
if [ -z "$DOCKER_SOCK_RESOLVED" ] || [ ! -f .env ]; then
|
|
return 0
|
|
fi
|
|
if [ "$DOCKER_SOCK_RESOLVED" = "/var/run/docker.sock" ]; then
|
|
return 0
|
|
fi
|
|
set_env ARTEX_DOCKER_SOCK "$DOCKER_SOCK_RESOLVED"
|
|
ok "Docker API socket: $DOCKER_SOCK_RESOLVED"
|
|
}
|
|
|
|
# ensure_docker_engine refuses a container engine the sandbox cannot use, before
|
|
# the images are rebuilt, and resolves the socket the runner will mount.
|
|
ensure_docker_engine(){
|
|
local info
|
|
if ! info="$(docker info 2>&1)"; then
|
|
warn "the Docker engine is not reachable:"
|
|
printf '%s\n' "$info" | sed 's/^/ /' >&2
|
|
die "start Docker (or add your user to the docker group and log in again), then rerun this script"
|
|
fi
|
|
resolve_docker_sock
|
|
}
|
|
|
|
# ── Compose v2 detection (plugin or standalone v2 binary) ─────
|
|
COMPOSE_CMD=()
|
|
detect_compose(){
|
|
local ver
|
|
if docker compose version >/dev/null 2>&1; then
|
|
COMPOSE_CMD=(docker compose)
|
|
return 0
|
|
elif command -v docker-compose >/dev/null 2>&1; then
|
|
ver="$(docker-compose --version 2>/dev/null || true)"
|
|
if [[ "$ver" =~ version[[:space:]]+v?([2-9]|[1-9][0-9]+)\. ]]; then
|
|
COMPOSE_CMD=(docker-compose)
|
|
return 0
|
|
fi
|
|
fi
|
|
return 1
|
|
}
|
|
|
|
# ── (1) Docker update ───────────────────────────────
|
|
update_docker(){
|
|
command -v docker >/dev/null 2>&1 \
|
|
|| die "docker not detected; install and deploy with ./install.sh first"
|
|
detect_compose \
|
|
|| die "Compose v2 ('docker compose' plugin or standalone 'docker-compose' v2) not detected; install Docker Compose v2 and retry"
|
|
docker buildx version >/dev/null 2>&1 \
|
|
|| die "docker buildx not detected; the image build needs it (see the Docker section of the README)"
|
|
[ -f .env ] || die ".env not found; run ./install.sh first to complete the initial deployment"
|
|
|
|
ensure_docker_engine
|
|
normalize_env
|
|
ensure_runner_token
|
|
persist_docker_sock
|
|
# Touch only artex and artex-runner: postgres is pinned to 16-alpine and does not
|
|
# need upgrading along (pulling it wastes bandwidth, and a major version bump
|
|
# risks compatibility). The worker and proxy images are built locally because the
|
|
# runner creates one container of each per task.
|
|
info "Building the panel and the sandbox images from this checkout…"
|
|
"${COMPOSE_CMD[@]}" build artex artex-runner
|
|
"${COMPOSE_CMD[@]}" --profile build-only build artex-worker artex-egress-proxy
|
|
info "Recreating artex and artex-runner (schema migrates automatically on restart)…"
|
|
"${COMPOSE_CMD[@]}" up -d artex artex-runner
|
|
ok "Update complete → http://localhost:8787"
|
|
info "View logs: ${COMPOSE_CMD[*]} logs -f artex"
|
|
info "Clean up old images (optional): docker image prune -f"
|
|
}
|
|
|
|
# ── (2) Local build update ──────────────────────────────
|
|
update_local(){
|
|
command -v go >/dev/null 2>&1 || die "Go (>=1.26) not detected: https://go.dev/dl/"
|
|
[ -f config.json ] || warn "config.json not found — use ./install.sh instead for a first-time deployment"
|
|
ok "Go: $(go version)"
|
|
LDFLAGS="-X main.version=${BUILD_VERSION}"
|
|
[ -n "${BUILD_REVISION}" ] && LDFLAGS="${LDFLAGS} -X main.revision=${BUILD_REVISION}"
|
|
|
|
if command -v npm >/dev/null 2>&1; then
|
|
info "Rebuilding frontend static assets…"
|
|
( cd web && npm ci --ignore-scripts && npm run build:static )
|
|
rm -rf server/webui/dist && mkdir -p server/webui && cp -r web/out server/webui/dist
|
|
info "Recompiling the embedded single binary (version ${BUILD_VERSION})…"
|
|
CGO_ENABLED=0 go build -tags embedui -trimpath -ldflags "${LDFLAGS}" -o artex ./cmd/artex
|
|
else
|
|
warn "npm not detected: building a backend **without the embedded frontend** (run the frontend separately with npm run dev)"
|
|
CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o artex ./cmd/artex
|
|
fi
|
|
ok "Build complete → ./artex"
|
|
warn "Restart the running artex process to take effect (schema migrates automatically on restart)"
|
|
}
|
|
|
|
echo "=============================="
|
|
echo " ARTEX Update"
|
|
echo " 1) Docker update (pull new image and rebuild)"
|
|
echo " 2) Local update (rebuild with go)"
|
|
echo "=============================="
|
|
case "$(ask 'Choose' 1)" in
|
|
1) sync_repo; stamp_version; update_docker ;;
|
|
2) sync_repo; stamp_version; update_local ;;
|
|
*) die "invalid choice" ;;
|
|
esac
|