123 lines
3.2 KiB
Bash
123 lines
3.2 KiB
Bash
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
|
|
OPENCODE_USER="${OPENCODE_USER:-opencode}"
|
|
OPENCODE_VERSION="${OPENCODE_VERSION:-}"
|
|
OPENCODE_HOME="/home/${OPENCODE_USER}"
|
|
OPENCODE_BIN="${OPENCODE_HOME}/.opencode/bin/opencode"
|
|
OPENCODE_WEB_HOSTNAME="${OPENCODE_WEB_HOSTNAME:-0.0.0.0}"
|
|
OPENCODE_WEB_PORT="${OPENCODE_WEB_PORT:-4096}"
|
|
OPENCODE_SERVER_USERNAME="${OPENCODE_SERVER_USERNAME:-opencode}"
|
|
OPENCODE_SERVER_PASSWORD="${OPENCODE_SERVER_PASSWORD:-}"
|
|
|
|
log() {
|
|
printf '[opencode-install] %s\n' "$*"
|
|
}
|
|
|
|
fail() {
|
|
printf '[opencode-install] Error: %s\n' "$*" >&2
|
|
exit 1
|
|
}
|
|
|
|
run_as_user() {
|
|
su - "$OPENCODE_USER" -c "$1"
|
|
}
|
|
|
|
install_web_service() {
|
|
local password_line="# OPENCODE_SERVER_PASSWORD="
|
|
if [[ -n "$OPENCODE_SERVER_PASSWORD" ]]; then
|
|
password_line="OPENCODE_SERVER_PASSWORD=${OPENCODE_SERVER_PASSWORD}"
|
|
fi
|
|
|
|
cat >/etc/default/opencode-web <<EOF
|
|
OPENCODE_WEB_HOSTNAME=${OPENCODE_WEB_HOSTNAME}
|
|
OPENCODE_WEB_PORT=${OPENCODE_WEB_PORT}
|
|
OPENCODE_SERVER_USERNAME=${OPENCODE_SERVER_USERNAME}
|
|
# Set OPENCODE_SERVER_PASSWORD to require HTTP basic auth for web access.
|
|
${password_line}
|
|
EOF
|
|
|
|
cat >/etc/systemd/system/opencode-web.service <<EOF
|
|
[Unit]
|
|
Description=OpenCode Web Interface
|
|
After=network-online.target
|
|
Wants=network-online.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
User=${OPENCODE_USER}
|
|
Group=${OPENCODE_USER}
|
|
WorkingDirectory=/workspace
|
|
Environment=HOME=${OPENCODE_HOME}
|
|
Environment=BROWSER=/bin/true
|
|
EnvironmentFile=-/etc/default/opencode-web
|
|
ExecStart=/bin/sh -lc 'exec /usr/local/bin/opencode web --hostname "${OPENCODE_WEB_HOSTNAME:-0.0.0.0}" --port "${OPENCODE_WEB_PORT:-4096}"'
|
|
Restart=on-failure
|
|
RestartSec=5
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
|
|
systemctl daemon-reload
|
|
systemctl enable --now opencode-web.service
|
|
}
|
|
|
|
main() {
|
|
[[ $EUID -eq 0 ]] || fail "Run as root inside the container"
|
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
|
|
log "Updating apt package index"
|
|
apt-get update
|
|
|
|
log "Installing base packages"
|
|
apt-get install -y \
|
|
bash-completion \
|
|
build-essential \
|
|
ca-certificates \
|
|
curl \
|
|
fd-find \
|
|
git \
|
|
jq \
|
|
less \
|
|
ripgrep \
|
|
sudo \
|
|
tar \
|
|
unzip
|
|
|
|
if ! id -u "$OPENCODE_USER" >/dev/null 2>&1; then
|
|
log "Creating user ${OPENCODE_USER}"
|
|
useradd --create-home --shell /bin/bash "$OPENCODE_USER"
|
|
fi
|
|
|
|
usermod -aG sudo "$OPENCODE_USER"
|
|
install -d -m 0755 -o "$OPENCODE_USER" -g "$OPENCODE_USER" /workspace
|
|
|
|
if [[ -x /usr/bin/fdfind && ! -e /usr/local/bin/fd ]]; then
|
|
ln -s /usr/bin/fdfind /usr/local/bin/fd
|
|
fi
|
|
|
|
log "Installing OpenCode"
|
|
if [[ -n "$OPENCODE_VERSION" ]]; then
|
|
run_as_user "curl -fsSL https://opencode.ai/install | bash -s -- --no-modify-path --version ${OPENCODE_VERSION}"
|
|
else
|
|
run_as_user "curl -fsSL https://opencode.ai/install | bash -s -- --no-modify-path"
|
|
fi
|
|
|
|
[[ -x "$OPENCODE_BIN" ]] || fail "OpenCode binary was not installed"
|
|
ln -sf "$OPENCODE_BIN" /usr/local/bin/opencode
|
|
|
|
log "Configuring OpenCode web service"
|
|
install_web_service
|
|
|
|
cat >/etc/profile.d/opencode-workspace.sh <<EOF
|
|
export PATH=/usr/local/bin:\$PATH
|
|
cd /workspace 2>/dev/null || true
|
|
EOF
|
|
|
|
log "Installed version: $(run_as_user "opencode --version" | tail -n 1)"
|
|
log "Web interface: http://$(hostname -I | awk '{print $1}'):${OPENCODE_WEB_PORT}"
|
|
}
|
|
|
|
main "$@"
|