2026-04-14 20:11:24 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
# claude.sh — Manage the isolated Claude Code Docker environment
|
2026-04-15 17:14:37 +02:00
|
|
|
# Usage: ./claude.sh [--kube] <command> [args]
|
2026-04-14 20:11:24 +02:00
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
|
COMPOSE_FILE="$SCRIPT_DIR/docker-compose.yml"
|
|
|
|
|
PROJECT="claude-secure"
|
2026-04-15 17:14:37 +02:00
|
|
|
ALLOW_KUBE=0
|
2026-04-15 08:45:05 +02:00
|
|
|
|
2026-04-14 20:11:24 +02:00
|
|
|
# ─── Colours ──────────────────────────────────────────────────────────────────
|
|
|
|
|
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
|
|
|
|
|
info() { echo -e "${GREEN}[+]${NC} $*"; }
|
|
|
|
|
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
|
|
|
|
|
error() { echo -e "${RED}[-]${NC} $*" >&2; }
|
|
|
|
|
|
2026-04-15 17:14:37 +02:00
|
|
|
# ─── Helpers ──────────────────────────────────────────────────────────────────
|
2026-04-14 20:11:24 +02:00
|
|
|
check_deps() {
|
2026-04-15 17:14:37 +02:00
|
|
|
command -v docker &>/dev/null \
|
|
|
|
|
|| { error "Docker is not installed. https://docs.docker.com/get-docker/"; exit 1; }
|
|
|
|
|
docker compose version &>/dev/null 2>&1 \
|
|
|
|
|
|| { error "Docker Compose v2 plugin is required."; exit 1; }
|
2026-04-14 20:11:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
load_env() {
|
|
|
|
|
local env_file="$SCRIPT_DIR/.env"
|
|
|
|
|
if [[ -f "$env_file" ]]; then
|
|
|
|
|
# shellcheck disable=SC1090
|
|
|
|
|
set -a; source "$env_file"; set +a
|
|
|
|
|
fi
|
2026-04-14 22:47:04 +02:00
|
|
|
if [[ -z "${ANTHROPIC_API_KEY:-}" && -z "${CLAUDE_CODE_OAUTH_TOKEN:-}" ]]; then
|
|
|
|
|
warn "No ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN found."
|
|
|
|
|
warn "Claude Code will prompt you to authenticate on first run."
|
|
|
|
|
warn " Option 1 (API key): set ANTHROPIC_API_KEY in .env"
|
|
|
|
|
warn " Option 2 (token): run 'claude setup-token' and set CLAUDE_CODE_OAUTH_TOKEN in .env"
|
2026-04-15 17:14:37 +02:00
|
|
|
warn " Option 3 (browser): log in when prompted; port 54545 must be reachable from your browser."
|
2026-04-14 20:11:24 +02:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 17:14:37 +02:00
|
|
|
dc() { docker compose -f "$COMPOSE_FILE" -p "$PROJECT" "$@"; }
|
|
|
|
|
|
|
|
|
|
# ─── Volume args ──────────────────────────────────────────────────────────────
|
|
|
|
|
# Builds VOLUME_ARGS array for docker compose run.
|
|
|
|
|
# Validates the workspace path and optionally adds the kubeconfig mount.
|
|
|
|
|
build_volume_args() {
|
2026-04-15 08:40:50 +02:00
|
|
|
local cwd
|
|
|
|
|
cwd="$(pwd)"
|
|
|
|
|
|
2026-04-15 17:14:37 +02:00
|
|
|
# Exact-match blocklist
|
|
|
|
|
local -a exact_blocked=( / "$HOME" /root /home )
|
2026-04-15 08:40:50 +02:00
|
|
|
for dir in "${exact_blocked[@]}"; do
|
2026-04-15 17:14:37 +02:00
|
|
|
[[ "$cwd" == "$dir" ]] && {
|
|
|
|
|
error "Refusing to mount $cwd as workspace — too broad. cd into a project subdirectory first."
|
2026-04-15 08:40:50 +02:00
|
|
|
exit 1
|
2026-04-15 17:14:37 +02:00
|
|
|
}
|
2026-04-15 08:40:50 +02:00
|
|
|
done
|
|
|
|
|
|
2026-04-15 17:14:37 +02:00
|
|
|
# Any user home directory directly under /home
|
|
|
|
|
[[ "$cwd" =~ ^/home/[^/]+$ ]] && {
|
|
|
|
|
error "Refusing to mount $cwd as workspace — user home directory. cd into a project subdirectory first."
|
2026-04-15 08:43:09 +02:00
|
|
|
exit 1
|
2026-04-15 17:14:37 +02:00
|
|
|
}
|
2026-04-15 08:43:09 +02:00
|
|
|
|
2026-04-15 17:14:37 +02:00
|
|
|
# Prefix blocklist — system internals and credential/key material
|
|
|
|
|
local -a prefix_blocked=(
|
|
|
|
|
/bin /sbin /lib /lib64 /etc /usr /var /proc /sys /dev /boot /run
|
|
|
|
|
"$HOME/.ssh" /root/.ssh "$HOME/.gnupg" /root/.gnupg
|
|
|
|
|
)
|
2026-04-15 08:40:50 +02:00
|
|
|
for dir in "${prefix_blocked[@]}"; do
|
2026-04-15 17:14:37 +02:00
|
|
|
[[ "$cwd" == "$dir" || "$cwd" == "$dir/"* ]] && {
|
|
|
|
|
error "Refusing to mount $cwd as workspace — contains sensitive data. cd into a project subdirectory first."
|
2026-04-15 08:40:50 +02:00
|
|
|
exit 1
|
2026-04-15 17:14:37 +02:00
|
|
|
}
|
2026-04-15 08:40:50 +02:00
|
|
|
done
|
|
|
|
|
|
2026-04-15 17:14:37 +02:00
|
|
|
VOLUME_ARGS=("--volume" "${cwd}:/workspace:z")
|
2026-04-14 20:11:24 +02:00
|
|
|
|
2026-04-15 17:14:37 +02:00
|
|
|
if [[ "$ALLOW_KUBE" -eq 1 ]]; then
|
|
|
|
|
[[ -d "$HOME/.kube" ]] || { error "--kube: $HOME/.kube does not exist."; exit 1; }
|
|
|
|
|
VOLUME_ARGS+=("--volume" "$HOME/.kube:/home/node/.kube:ro,z")
|
2026-04-15 08:45:05 +02:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 20:11:24 +02:00
|
|
|
# ─── Commands ─────────────────────────────────────────────────────────────────
|
|
|
|
|
cmd_start() {
|
2026-04-15 17:14:37 +02:00
|
|
|
check_deps; load_env; build_volume_args
|
2026-04-14 20:11:24 +02:00
|
|
|
info "Starting proxy sidecar..."
|
2026-04-15 17:05:44 +02:00
|
|
|
dc up -d --no-build proxy
|
2026-04-14 20:11:24 +02:00
|
|
|
info "Launching Claude Code..."
|
2026-04-15 17:14:37 +02:00
|
|
|
dc run --rm --no-build --service-ports "${VOLUME_ARGS[@]}" claude "$@"
|
2026-04-14 20:11:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd_stop() {
|
|
|
|
|
check_deps
|
|
|
|
|
info "Stopping all containers..."
|
|
|
|
|
dc down
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd_update() {
|
|
|
|
|
check_deps
|
2026-04-15 17:02:43 +02:00
|
|
|
info "Pulling latest images from registry..."
|
|
|
|
|
dc pull
|
2026-04-14 20:11:24 +02:00
|
|
|
info "Update complete. Run './claude.sh start' to launch."
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd_logs() {
|
|
|
|
|
check_deps
|
2026-04-15 17:14:37 +02:00
|
|
|
dc logs -f "${1:-proxy}"
|
2026-04-14 20:11:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd_status() {
|
|
|
|
|
check_deps
|
|
|
|
|
dc ps
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd_shell() {
|
2026-04-15 17:14:37 +02:00
|
|
|
check_deps; load_env; build_volume_args
|
2026-04-14 20:11:24 +02:00
|
|
|
warn "Opening debug shell inside Claude container (non-Claude entrypoint)."
|
2026-04-15 17:14:37 +02:00
|
|
|
dc run --rm --no-build --service-ports --entrypoint /bin/bash "${VOLUME_ARGS[@]}" claude
|
2026-04-14 20:11:24 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-14 22:25:38 +02:00
|
|
|
cmd_web() {
|
2026-04-15 17:14:37 +02:00
|
|
|
check_deps; load_env
|
|
|
|
|
[[ -n "${WEBUI_PASSWORD:-}" ]] \
|
|
|
|
|
|| { error "WEBUI_PASSWORD is not set. Add it to .env before starting the web interface."; exit 1; }
|
2026-04-14 22:25:38 +02:00
|
|
|
info "Starting proxy and web interface..."
|
2026-04-15 17:05:44 +02:00
|
|
|
dc up -d --no-build webui
|
2026-04-15 17:14:37 +02:00
|
|
|
info "Web interface is up → http://0.0.0.0:7681"
|
2026-04-14 22:25:38 +02:00
|
|
|
info "Credentials: ${WEBUI_USER:-claude} / [WEBUI_PASSWORD]"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd_web_stop() {
|
|
|
|
|
check_deps
|
|
|
|
|
info "Stopping web interface..."
|
2026-04-15 17:14:37 +02:00
|
|
|
dc stop webui && dc rm -f webui
|
2026-04-14 22:25:38 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-14 20:11:24 +02:00
|
|
|
cmd_help() {
|
|
|
|
|
cat <<EOF
|
2026-04-15 17:14:37 +02:00
|
|
|
Usage: $(basename "$0") [--kube] <command> [args]
|
2026-04-14 20:11:24 +02:00
|
|
|
|
|
|
|
|
Commands:
|
2026-04-15 17:02:43 +02:00
|
|
|
start [args] Start proxy, launch Claude Code (CLI)
|
2026-04-15 17:14:37 +02:00
|
|
|
web Start proxy + web interface (browser terminal on :7681)
|
2026-04-14 22:25:38 +02:00
|
|
|
web-stop Stop the web interface (keeps proxy running)
|
|
|
|
|
stop Stop and remove all containers
|
2026-04-15 17:02:43 +02:00
|
|
|
update Pull latest images from the registry
|
2026-04-14 22:25:38 +02:00
|
|
|
logs [svc] Tail logs (default: proxy)
|
|
|
|
|
status Show container status
|
|
|
|
|
shell Open a bash shell in the Claude container (debug)
|
|
|
|
|
help Show this message
|
|
|
|
|
|
2026-04-15 08:45:05 +02:00
|
|
|
Flags (before the subcommand):
|
|
|
|
|
--kube Mount \$HOME/.kube read-only at /home/node/.kube (kubectl access)
|
|
|
|
|
|
2026-04-15 17:14:37 +02:00
|
|
|
Environment variables (set in .env):
|
|
|
|
|
ANTHROPIC_API_KEY API key auth
|
|
|
|
|
CLAUDE_CODE_OAUTH_TOKEN OAuth token auth (from 'claude setup-token')
|
|
|
|
|
IMAGE_TAG Image tag to use (default: latest)
|
|
|
|
|
WEBUI_USER Web interface username (default: claude)
|
|
|
|
|
WEBUI_PASSWORD Required for web mode
|
|
|
|
|
|
2026-04-14 20:11:24 +02:00
|
|
|
Examples:
|
2026-04-15 08:10:44 +02:00
|
|
|
cd ~/myproject && ./claude.sh start
|
2026-04-15 08:45:05 +02:00
|
|
|
cd ~/myproject && ./claude.sh --kube start
|
2026-04-14 22:25:38 +02:00
|
|
|
./claude.sh web
|
2026-04-14 20:11:24 +02:00
|
|
|
./claude.sh logs proxy
|
|
|
|
|
./claude.sh shell
|
|
|
|
|
EOF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# ─── Dispatch ─────────────────────────────────────────────────────────────────
|
2026-04-15 08:45:05 +02:00
|
|
|
while [[ "${1:-}" == --* ]]; do
|
|
|
|
|
case "$1" in
|
|
|
|
|
--kube) ALLOW_KUBE=1; shift ;;
|
|
|
|
|
*) break ;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
2026-04-14 20:11:24 +02:00
|
|
|
case "${1:-help}" in
|
2026-04-15 17:14:37 +02:00
|
|
|
start|run) shift; cmd_start "$@" ;;
|
|
|
|
|
stop) cmd_stop ;;
|
|
|
|
|
web) cmd_web ;;
|
|
|
|
|
web-stop) cmd_web_stop ;;
|
|
|
|
|
update) cmd_update ;;
|
|
|
|
|
logs) shift; cmd_logs "${1:-}" ;;
|
|
|
|
|
status) cmd_status ;;
|
|
|
|
|
shell) cmd_shell ;;
|
|
|
|
|
help|-h|--help) cmd_help ;;
|
2026-04-14 20:11:24 +02:00
|
|
|
*)
|
|
|
|
|
error "Unknown command: ${1}"
|
|
|
|
|
cmd_help
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|