#!/usr/bin/env bash # ARTEX install script: 1) all-Docker 2) local build & run 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 or binary is 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. One conversion keeps the rest of the script simple. 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 the token is generated with the rest of the setup. 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" } # json_str prints $1 as a JSON string literal: backslash, double quote and the # control characters read -r can return are escaped. json_str(){ local s="$1" s="${s//\\/\\\\}"; s="${s//\"/\\\"}" s="${s//$'\t'/\\t}"; s="${s//$'\r'/\\r}" printf '"%s"' "$s" } # sed_repl escapes $1 for the replacement side of s|...|...|: \, | and &. sed_repl(){ printf '%s' "$1" | sed 's/[\\|&]/\\&/g'; } # ── docker / compose environment check / auto-install ───────── 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 } ensure_docker(){ local need_compose="${1:-1}" if command -v docker >/dev/null 2>&1; then if [ "$need_compose" = "0" ]; then ok "docker detected"; return fi if detect_compose; then ok "docker and ${COMPOSE_CMD[*]} detected"; return fi die "docker detected, but Compose v2 ('docker compose' plugin or standalone 'docker-compose' v2) was not found; install Docker Compose v2 and retry: https://docs.docker.com/compose/install/" fi warn "docker not detected" case "$(uname -s)" in Linux) if [ "$(ask 'Install Docker automatically? (y/n)' y)" = y ]; then install_docker_linux sudo usermod -aG docker "$USER" || true if [ "$need_compose" != "0" ]; then if ! detect_compose; then if sudo docker compose version >/dev/null 2>&1; then COMPOSE_CMD=(docker compose) else die "docker installed but Compose v2 ('docker compose' or standalone 'docker-compose' v2) is unavailable; install Compose v2 and retry" fi fi fi ok "Docker installed (re-login required for the group change to take effect)" else die "please install docker yourself and retry" fi ;; Darwin) die "on macOS please install Docker Desktop: https://www.docker.com/products/docker-desktop/" ;; *) die "please install docker yourself and retry" ;; esac } # install_docker_linux uses get.docker.com only on the distros it supports. That # script keys its repo URL off /etc/os-release ID + codename, so on derivatives # such as Kali it writes a repo for a nonexistent release (debian kali-rolling) # and leaves apt broken. Other apt-based distros get the distro's own packages. install_docker_linux(){ local id="" id_like="" if [ -r /etc/os-release ]; then id="$(. /etc/os-release && echo "${ID:-}")" id_like="$(. /etc/os-release && echo "${ID_LIKE:-}")" fi case "$id" in ubuntu|debian|raspbian|centos|fedora|rhel|sles) curl -fsSL https://get.docker.com | sh return ;; esac command -v apt-get >/dev/null 2>&1 \ || die "automatic Docker install is not supported on '${id:-unknown}'; please install docker + the compose plugin yourself and retry" info "'${id:-unknown}' (like: ${id_like:-n/a}) is not supported by get.docker.com; installing from the distro's apt repos" # Drop a broken repo left by an earlier get.docker.com run (it fails every apt update). # Match the URI and the distro id anywhere in the file: in a one-line .list entry # they share a line, but in a deb822 .sources file the URIs and Suites fields are # on separate lines. -w keeps "kali" matching inside "kali-rolling". local f for f in /etc/apt/sources.list.d/docker.list /etc/apt/sources.list.d/docker.sources; do if [ -n "$id" ] && [ -f "$f" ] \ && grep -qF "download.docker.com/linux/debian" "$f" 2>/dev/null \ && grep -qwF "$id" "$f" 2>/dev/null; then warn "removing broken Docker repo $f" sudo rm -f "$f" fi done sudo apt-get update sudo apt-get install -y docker.io # The compose v2 package name varies across Debian derivatives; take the first that exists. local p for p in docker-compose-plugin docker-compose-v2 docker-compose; do if apt-cache madison "$p" 2>/dev/null | grep -q .; then sudo apt-get install -y "$p" && break fi done # docker.io only suggests buildx on some releases, and the image build needs it. for p in docker-buildx-plugin docker-buildx; do if apt-cache madison "$p" 2>/dev/null | grep -q .; then sudo apt-get install -y "$p" && break fi done # Kali (and some others) don't start services on package install. if command -v systemctl >/dev/null 2>&1; then sudo systemctl enable --now docker || true fi } # The Dockerfile uses --platform=$BUILDPLATFORM, which needs BuildKit. Without the # buildx plugin Compose only warns, falls back to the legacy builder and fails on # the first FROM with 'failed to parse platform'. ensure_buildx(){ docker buildx version >/dev/null 2>&1 && return case "$(uname -s)" in Darwin) die "docker buildx not detected; the image build needs it. Docker Desktop includes it; with Homebrew's docker CLI (e.g. Colima) run 'brew install docker-buildx' and register the plugin as its caveats describe" ;; *) die "docker buildx not detected; the image build needs it. Install docker-buildx-plugin (Docker's repo) or docker-buildx (distro repo) and retry" ;; esac } # resolve_docker_sock picks and validates the host Docker API socket before any # image is built. 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 four images are built, 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 } # ── 1) All-Docker ─────────────────────────────── install_docker(){ ensure_docker ensure_buildx ensure_docker_engine normalize_env if [ ! -f .env ]; then cp .env.example .env 2>/dev/null || true local pw key pw="$(ask 'Postgres password (press Enter to generate randomly)' "$(rand)")" key="$(ask 'ANTHROPIC_API_KEY (may be empty, configure later in the UI)' '')" sed -i.bak "s|^POSTGRES_PASSWORD=.*|POSTGRES_PASSWORD=$(sed_repl "$pw")|" .env sed -i.bak "s|^ANTHROPIC_API_KEY=.*|ANTHROPIC_API_KEY=$(sed_repl "$key")|" .env rm -f .env.bak normalize_env ok ".env generated (POSTGRES_PASSWORD set)" else info "reusing existing .env" fi stamp_version ensure_runner_token persist_docker_sock 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 "starting..." "${COMPOSE_CMD[@]}" up -d ok "started → http://localhost:8787" info "view logs: ${COMPOSE_CMD[*]} logs -f artex" } # ── 2) Local build & run ────────────────────────────── install_local(){ echo "Database setup:" echo " 1) Connect to an existing PostgreSQL" echo " 2) Start a PostgreSQL with Docker (requires docker)" case "$(ask 'Choose' 1)" in 2) ensure_docker 0 local pw; pw="$(ask 'Postgres password (press Enter for random)' "$(rand)")" docker run -d --name artex-pg -p 127.0.0.1:5432:5432 \ -e POSTGRES_USER=artex -e POSTGRES_PASSWORD="$pw" -e POSTGRES_DB=artex \ -v artex-pg:/var/lib/postgresql/data postgres:16-alpine DB_HOST=127.0.0.1 DB_PORT=5432 DB_USER=artex DB_PASS="$pw" DB_NAME=artex DB_SSL=disable ;; *) DB_HOST="$(ask 'Database host' 127.0.0.1)" DB_PORT="$(ask 'Port' 5432)" DB_USER="$(ask 'User' artex)" DB_PASS="$(ask 'Password' '')" DB_NAME="$(ask 'Database name' artex)" DB_SSL="$(ask 'sslmode (disable/require)' disable)" ;; esac # generate config.json (string values JSON-escaped: a password may contain " or \) case "$DB_PORT" in (*[!0-9]*|'') die "port must be a number: $DB_PORT" ;; esac cat > config.json </dev/null 2>&1 || die "Go not detected; please install Go (>=1.26) first: https://go.dev/dl/" ok "Go: $(go version)" stamp_version LDFLAGS="-X main.version=${BUILD_VERSION}" [ -n "${BUILD_REVISION}" ] && LDFLAGS="${LDFLAGS} -X main.revision=${BUILD_REVISION}" # embedding the frontend requires node to produce the static assets if command -v npm >/dev/null 2>&1; then info "building 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 "compiling embedded single binary..." 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 npm run dev separately for the frontend)" CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o artex ./cmd/artex fi ok "build complete → ./artex" info "starting... (Ctrl-C to exit)" ./artex } echo "==============================" echo " ARTEX Install" echo " 1) All-Docker install" echo " 2) Run locally (go build)" echo "==============================" case "$(ask 'Choose' 1)" in 1) install_docker ;; 2) install_local ;; *) die "invalid choice" ;; esac