#!/bin/bash set -e export HOME=/home/node export BUN_INSTALL="$HOME/.bun" export PATH="$BUN_INSTALL/bin:$HOME/.opencode/bin:/usr/local/bin:$PATH" source /usr/local/lib/ocm/container-user.sh source /usr/local/lib/ocm/opencode-release.sh grant_kvm_access() { local dev="${1:-/dev/kvm}" [ -e "$dev" ] || return 0 local dev_gid group_name holder dev_gid="$(stat -c '%g' "$dev" 2>/dev/null)" || return 0 case "$dev_gid" in ''|*[!0-9]*) return 0 ;; esac holder="$(getent group "$dev_gid" 2>/dev/null | cut -d: -f1 || true)" if [ -n "$holder" ]; then group_name="$holder" else group_name="kvm" if ! groupadd -g "$dev_gid" "$group_name"; then echo "ERROR: could not create group '$group_name' (gid $dev_gid) required for $dev access" >&2 return 1 fi fi if ! usermod -aG "$group_name" node; then echo "ERROR: could not add node to group '$group_name' (gid $dev_gid) required for $dev access" >&2 return 1 fi if ! runuser -u node -- test -r "$dev" || ! runuser -u node -- test -w "$dev"; then echo "ERROR: node cannot access $dev (group '$group_name', gid $dev_gid)" >&2 echo "ERROR: grant the container group access to $dev or run the sandbox overlay (docker-compose.sandbox.yml)" >&2 return 1 fi echo "Granted node access to $dev (group '$group_name', gid $dev_gid)" } ensure_auth_secret() { local secret_file="$1" [ -n "${AUTH_SECRET:-}" ] && return 0 if [ ! -s "$secret_file" ]; then mkdir -p "$(dirname "$secret_file")" || return 1 (umask 077 && head -c 32 /dev/urandom | base64 > "$secret_file") || { echo "ERROR: could not write a generated AUTH_SECRET to $secret_file" >&2 return 1 } echo "Generated AUTH_SECRET and saved it to $secret_file" fi AUTH_SECRET="$(cat "$secret_file")" || return 1 export AUTH_SECRET } OPENCODE_SUPPORTED_FLOOR="${OPENCODE_BUNDLED_VERSION:-}" read_opencode_version() { local binary output binary="$(command -v "${1:-opencode}" 2>/dev/null || true)" if [ -z "$binary" ] || [ ! -x "$binary" ]; then return 0 fi output="$(runuser -u node -- "$binary" --version 2>&1)" || return 0 parse_opencode_version_output "$output" } install_opencode() { local opencode_version="${OPENCODE_BUNDLED_VERSION:-}" if [ -z "$opencode_version" ]; then echo "ERROR: OPENCODE_BUNDLED_VERSION is not set; refusing to guess the pinned OpenCode build" >&2 return 1 fi echo "Installing OpenCode ${opencode_version}..." download_opencode_to "$opencode_version" "$HOME/.opencode/bin/opencode" } reconcile_persisted_opencode() { local persisted_path="$HOME/.opencode/bin/opencode" [ -e "$persisted_path" ] || return 0 local persisted_version persisted_version="$(read_opencode_version "$persisted_path")" if [ -z "$persisted_version" ]; then echo "Persisted OpenCode at $persisted_path is malformed or unversioned; removing it to fall back to the bundled binary" rm -f "$persisted_path" return 0 fi if ! is_supported_opencode_version "$persisted_version"; then echo "Persisted OpenCode $persisted_version is outside the supported range $(supported_opencode_range); removing it so the bundled OpenCode $OPENCODE_SUPPORTED_FLOOR binary is used" rm -f "$persisted_path" return 0 fi echo "Persisted OpenCode $persisted_version is usable; retaining it" } echo "Checking Bun installation..." if ! command -v bun >/dev/null 2>&1; then echo "Bun not found. Installing..." curl -fsSL https://bun.sh/install | bash if ! command -v bun >/dev/null 2>&1; then echo "Failed to install Bun. Exiting." exit 1 fi echo "Bun installed successfully" else BUN_VERSION=$(bun --version 2>&1 || echo "unknown") echo "Bun is installed (version: $BUN_VERSION)" fi echo "Checking OpenCode installation..." reconcile_persisted_opencode if ! command -v opencode >/dev/null 2>&1; then echo "OpenCode not found. Installing..." install_opencode if ! command -v opencode >/dev/null 2>&1; then echo "Failed to install OpenCode. Exiting." exit 1 fi echo "OpenCode installed successfully" fi OPENCODE_VERSION="$(read_opencode_version)" [ -n "$OPENCODE_VERSION" ] || OPENCODE_VERSION="unknown" echo "OpenCode is installed (version: $OPENCODE_VERSION)" if [ "$OPENCODE_VERSION" != "unknown" ]; then if is_supported_opencode_version "$OPENCODE_VERSION"; then echo "OpenCode version is within the supported range $(supported_opencode_range)" else echo "OpenCode version $OPENCODE_VERSION is outside the supported range $(supported_opencode_range)" echo "Reinstalling bundled OpenCode version ${OPENCODE_BUNDLED_VERSION}..." install_opencode OPENCODE_VERSION="$(read_opencode_version)" [ -n "$OPENCODE_VERSION" ] || OPENCODE_VERSION="unknown" echo "OpenCode reinstalled as version: $OPENCODE_VERSION" fi fi echo "Starting OpenCode Manager Backend..." if ! ensure_auth_secret /app/data/.auth-secret; then exit 1 fi if ! align_container_user node; then exit 1 fi if ! grant_kvm_access; then echo "WARNING: continuing without /dev/kvm access; agent sandboxing will report itself unavailable" >&2 fi warn_if_workspace_owner_differs /workspace "$OCM_TARGET_UID" "$OCM_TARGET_GID" mkdir -p /app/data /workspace /home/node/.cache /home/node/.opencode /home/node/.microsandbox chown -R node:node /app/data /workspace /home/node exec runuser -u node -- "$@"