phoebus_os/installer/lib/opencode.sh
Phoebus 38214ea9e7 chore: import Phoebus OS installer and Rust migration
Initial public import of the proven Bash installer (installer/) and the production Rust workspace (installer-rs/). The reference ISO, build output and local agent transcripts are gitignored.

Verified: installer/tests/run_tests.sh and cargo test --workspace both pass.
2026-09-20 02:07:33 -07:00

156 lines
4.8 KiB
Bash

#!/usr/bin/env bash
# shellcheck shell=bash
#
# Reproducible OpenCode provisioning.
#
# The vendor installer (curl | sh) is never used. We fetch a pinned release
# tarball from the official GitHub release and verify its SHA-256 against the
# local manifest before extracting it. The binary is never executed during
# verification.
opencode_manifest_file() {
printf '%s/config/opencode.manifest' "$PHOEBUS_ROOT"
}
opencode_manifest_get() {
local arch="$1" key="$2" file out
file="$(opencode_manifest_file)"
[[ -r "$file" ]] || return 1
out="$(awk -F= -v a="$arch" -v k="$key" '$1==a"."k{sub(/^[^=]*=/,""); print; exit}' "$file")"
[[ -n "$out" ]] || return 1
printf '%s' "$out"
}
opencode_version_manifest() {
local file
file="$(opencode_manifest_file)"
awk -F= '$1=="VERSION"{print $2; exit}' "$file" 2>/dev/null
}
opencode_arch() {
case "$(uname -m)" in
x86_64|amd64) printf 'x86_64' ;;
aarch64|arm64) printf 'aarch64' ;;
*) printf 'unsupported' ;;
esac
}
# opencode_bin_dir - where the verified binary lives
opencode_bin_dir() {
if [[ -n "${PHOEBUS_OPENCODE_DIR:-}" ]]; then
printf '%s' "$PHOEBUS_OPENCODE_DIR"
elif [[ -w /opt ]]; then
printf '/opt/phoebus/opencode'
else
printf '%s/opencode' "${STATE_DIR:-/tmp/ai-installer}"
fi
}
# opencode_detect - print the path to a usable opencode binary, if any
opencode_detect() {
local candidate
if command -v opencode >/dev/null 2>&1; then
command -v opencode
return 0
fi
for candidate in "$(opencode_bin_dir)/opencode" /usr/local/bin/opencode; do
[[ -x "$candidate" ]] && { printf '%s' "$candidate"; return 0; }
done
return 1
}
opencode_available() { opencode_detect >/dev/null 2>&1; }
# opencode_checksum_ok FILE EXPECTED - false for placeholders or mismatches
opencode_checksum_ok() {
local file="$1" expected="$2" got
[[ -n "$expected" ]] || return 1
[[ "$expected" =~ ^0+$ ]] && return 1
got="$(sha256sum "$file" | awk '{print $1}')"
[[ "$got" == "$expected" ]]
}
# opencode_bootstrap [DESTDIR] - download, verify and extract a pinned release
opencode_bootstrap() {
local dest="${1:-$(opencode_bin_dir)}"
local arch url sha binary tmp
arch="$(opencode_arch)"
if [[ "$arch" == "unsupported" ]]; then
log_error "OpenCode: unsupported architecture $(uname -m)"
return 1
fi
url="$(opencode_manifest_get "$arch" url)"
sha="$(opencode_manifest_get "$arch" sha256)"
binary="$(opencode_manifest_get "$arch" binary)"
binary="${binary:-opencode}"
if [[ -z "$url" || -z "$sha" ]]; then
log_error "OpenCode: no pinned manifest entry for $arch"
return 1
fi
if [[ "$sha" =~ ^0+$ ]]; then
log_error "OpenCode: manifest has a placeholder checksum for $arch; refusing"
return 1
fi
require_cmd curl sha256sum tar || return 1
mkdir -p "$dest" || return 1
tmp="$(mktemp "${TMPDIR:-/tmp}/opencode.XXXXXX.tar.gz")" || return 1
# shellcheck disable=SC2064
trap "rm -f '$tmp'" RETURN
log_info "OpenCode: fetching $url"
if ! run_cmd "download OpenCode" curl -fL --retry 3 -o "$tmp" "$url"; then
return 1
fi
if ! opencode_checksum_ok "$tmp" "$sha"; then
log_error "OpenCode: SHA-256 verification FAILED; discarding download"
rm -f "$tmp"
return 1
fi
log_info "OpenCode: checksum verified"
run_cmd "extract OpenCode" tar -xzf "$tmp" -C "$dest" || return 1
[[ -f "$dest/$binary" ]] || { log_error "OpenCode: expected binary '$binary' not found"; return 1; }
chmod 0755 "$dest/$binary"
ln -sf "$dest/$binary" "$dest/opencode" 2>/dev/null || true
log_info "OpenCode installed at $dest/$binary ($(opencode_version_manifest))"
return 0
}
# opencode_ensure - ensure a binary is present, bootstrapping if needed
opencode_ensure() {
opencode_available && return 0
log_info "OpenCode not found; bootstrapping pinned release"
opencode_bootstrap
}
# opencode_print_version BIN
opencode_print_version() {
local bin="$1"
"$bin" --version 2>/dev/null | head -n1
}
# opencode_ai_config_path - where the installer keeps AI provider config
opencode_ai_config_path() {
printf '%s/ai/opencode.json' "${STATE_DIR:-/tmp/ai-installer}"
}
# opencode_write_config - write provider/model config (never secrets)
opencode_write_config() {
local path dir provider model endpoint
path="$(opencode_ai_config_path)"
dir="$(dirname "$path")"
mkdir -p "$dir"
provider="$(config_get AI_PROVIDER)"
model="$(config_get AI_MODEL)"
endpoint="$(config_get AI_ENDPOINT)"
{
printf '{\n'
printf ' "$schema": "https://opencode.ai/config.json"\n'
if [[ -n "$provider" ]]; then printf ' ,"provider": "%s"\n' "$provider"; fi
if [[ -n "$model" ]]; then printf ' ,"model": "%s"\n' "$model"; fi
if [[ -n "$endpoint" ]]; then printf ' ,"baseURL": "%s"\n' "$endpoint"; fi
printf '}\n'
} >"$path"
chmod 0600 "$path"
log_info "wrote OpenCode config to $path (credentials are never stored here)"
}