openpod

An opencode plugin that spins up a dedicated virtual machine for each session to isolate agent work.
git clone https://unitehenry.com/git/openpod.git
Log | Files | Refs | README

ubuntu.sh (7053B)


      1 #!/usr/bin/env bash
      2 set -euo pipefail
      3 
      4 readonly OPENPOD_USER="openpod"
      5 readonly OPENPOD_PLUGIN="github:unitehenry/openpod"
      6 readonly OPENCODE_INSTALLER="https://opencode.ai/v2/install"
      7 readonly LIMA_REPO="https://github.com/lima-vm/lima"
      8 
      9 log() { printf '\n==> %s\n' "$*"; }
     10 warn() { printf '\nwarning: %s\n' "$*" >&2; }
     11 die() { printf '\nerror: %s\n' "$*" >&2; exit 1; }
     12 
     13 if [[ "${EUID}" -ne 0 ]]; then
     14   die "run this script as root (for example: sudo bash install/ubuntu.sh)"
     15 fi
     16 
     17 OPENPOD_HOME=""
     18 OPENPOD_GROUP=""
     19 OPENCODE_BIN=""
     20 LIMA_BIN=""
     21 WORK_DIR=""
     22 RELOGIN_REQUIRED=0
     23 
     24 cleanup() {
     25   if [[ -n "${WORK_DIR}" && -d "${WORK_DIR}" ]]; then
     26     rm -rf "${WORK_DIR}"
     27   fi
     28 }
     29 trap cleanup EXIT
     30 
     31 # Run a command as the openpod user with a predictable environment.
     32 as_openpod() {
     33   (
     34     cd "${OPENPOD_HOME}"
     35     runuser -u "${OPENPOD_USER}" -- env \
     36       -u XDG_RUNTIME_DIR \
     37       HOME="${OPENPOD_HOME}" \
     38       USER="${OPENPOD_USER}" \
     39       LOGNAME="${OPENPOD_USER}" \
     40       SHELL=/bin/bash \
     41       TMPDIR=/tmp \
     42       PATH="${OPENPOD_HOME}/.local/bin:${OPENPOD_HOME}/.opencode/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
     43       "$@"
     44   )
     45 }
     46 
     47 check_host() {
     48   [[ -r /etc/os-release ]] || die "cannot read /etc/os-release"
     49 
     50   # shellcheck disable=SC1091
     51   . /etc/os-release
     52 
     53   [[ "${ID:-}" == "ubuntu" ]] ||
     54     die "this script targets Ubuntu 24, but this host is '${ID:-unknown}'"
     55 
     56   [[ "${VERSION_ID:-}" == 24.* ]] ||
     57     die "this script targets Ubuntu 24, but this host is '${VERSION_ID:-unknown}'"
     58 
     59   command -v runuser >/dev/null 2>&1 ||
     60     die "runuser is required (install util-linux)"
     61 
     62   case "$(uname -m)" in
     63     x86_64 | amd64)
     64       ARCH="x86_64"
     65       QEMU_PACKAGES=(qemu-system-x86 qemu-utils ovmf)
     66       QEMU_BIN="qemu-system-x86_64"
     67       ;;
     68     aarch64 | arm64)
     69       ARCH="aarch64"
     70       QEMU_PACKAGES=(qemu-system-arm qemu-utils qemu-efi-aarch64)
     71       QEMU_BIN="qemu-system-aarch64"
     72       ;;
     73     *)
     74       die "unsupported architecture: $(uname -m)"
     75       ;;
     76   esac
     77 }
     78 
     79 create_openpod_user() {
     80   if getent passwd "${OPENPOD_USER}" >/dev/null 2>&1; then
     81     log "User ${OPENPOD_USER} already exists"
     82   else
     83     log "Creating user ${OPENPOD_USER}"
     84     useradd --create-home --user-group --shell /bin/bash \
     85       --comment "OpenPod" "${OPENPOD_USER}"
     86   fi
     87 
     88   OPENPOD_HOME="$(getent passwd "${OPENPOD_USER}" | cut -d: -f6)"
     89   OPENPOD_GROUP="$(id -gn "${OPENPOD_USER}")"
     90 
     91   [[ -n "${OPENPOD_HOME}" && -d "${OPENPOD_HOME}" ]] ||
     92     die "could not determine ${OPENPOD_USER}'s home directory"
     93 
     94   if [[ "$(stat -c '%U' "${OPENPOD_HOME}")" != "${OPENPOD_USER}" ]]; then
     95     chown "${OPENPOD_USER}:${OPENPOD_GROUP}" "${OPENPOD_HOME}"
     96   fi
     97 
     98   OPENCODE_BIN="${OPENPOD_HOME}/.opencode/bin/opencode"
     99   LIMA_BIN="${OPENPOD_HOME}/.local/bin/limactl"
    100 }
    101 
    102 install_system_packages() {
    103   log "Installing QEMU and base packages"
    104 
    105   apt-get update
    106   DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a apt-get install -y \
    107     --no-install-recommends \
    108     ca-certificates \
    109     curl \
    110     git \
    111     openssh-client \
    112     "${QEMU_PACKAGES[@]}"
    113 }
    114 
    115 setup_kvm() {
    116   if [[ ! -e /dev/kvm ]]; then
    117     warn "/dev/kvm is not available. Lima will fall back to software emulation, which is very slow. Enable virtualization in the BIOS/UEFI (or on the VM host) for usable performance."
    118     return
    119   fi
    120 
    121   getent group kvm >/dev/null 2>&1 || groupadd kvm
    122 
    123   if id -nG "${OPENPOD_USER}" | tr ' ' '\n' | grep -qx kvm; then
    124     return
    125   fi
    126 
    127   log "Adding ${OPENPOD_USER} to the kvm group"
    128   usermod -aG kvm "${OPENPOD_USER}"
    129   RELOGIN_REQUIRED=1
    130 }
    131 
    132 install_lima() {
    133   if [[ -x "${LIMA_BIN}" ]]; then
    134     log "limactl is already installed for ${OPENPOD_USER}: $(as_openpod "${LIMA_BIN}" --version | head -n 1)"
    135     return
    136   fi
    137 
    138   log "Finding the latest Lima release"
    139 
    140   local latest_url release_tag version archive agents
    141   latest_url="$(as_openpod curl -fsSLI -o /dev/null -w '%{url_effective}' "${LIMA_REPO}/releases/latest")" ||
    142     die "failed to query the latest Lima release"
    143 
    144   release_tag="${latest_url##*/}"
    145   version="${release_tag#v}"
    146 
    147   [[ "${release_tag}" == v* && -n "${version}" ]] ||
    148     die "could not parse the Lima release tag from '${latest_url}'"
    149 
    150   install -d -o "${OPENPOD_USER}" -g "${OPENPOD_GROUP}" -m 0755 \
    151     "${OPENPOD_HOME}/.local"
    152 
    153   WORK_DIR="$(as_openpod mktemp -d)"
    154   archive="${WORK_DIR}/lima.tar.gz"
    155   agents="${WORK_DIR}/lima-additional-guestagents.tar.gz"
    156 
    157   log "Installing Lima ${version} as ${OPENPOD_USER}"
    158 
    159   as_openpod curl -fsSL --retry 3 --retry-delay 2 \
    160     "${LIMA_REPO}/releases/download/${release_tag}/lima-${version}-Linux-${ARCH}.tar.gz" \
    161     -o "${archive}"
    162 
    163   as_openpod tar -C "${OPENPOD_HOME}/.local" -xzf "${archive}"
    164 
    165   # Guest agents are shipped in a separate archive since Lima 1.1. They let
    166   # Lima manage additional guest operating systems.
    167   if as_openpod curl -fsSL --retry 3 --retry-delay 2 \
    168     "${LIMA_REPO}/releases/download/${release_tag}/lima-additional-guestagents-${version}-Linux-${ARCH}.tar.gz" \
    169     -o "${agents}"; then
    170     as_openpod tar -C "${OPENPOD_HOME}/.local" -xzf "${agents}"
    171   else
    172     warn "additional Lima guest agents were not available for ${version}; continuing without them"
    173   fi
    174 
    175   [[ -x "${LIMA_BIN}" ]] ||
    176     die "limactl was not installed at ${LIMA_BIN}"
    177 
    178   as_openpod "${LIMA_BIN}" --version >/dev/null
    179 }
    180 
    181 install_opencode() {
    182   log "Installing OpenCode V2 as ${OPENPOD_USER}"
    183 
    184   as_openpod bash -c '
    185     set -euo pipefail
    186     curl -fsSL --retry 3 --retry-delay 2 "$1" | bash -s -- --no-modify-path
    187   ' bash "${OPENCODE_INSTALLER}"
    188 
    189   [[ -x "${OPENCODE_BIN}" ]] ||
    190     die "OpenCode was not installed at ${OPENCODE_BIN}"
    191 
    192   as_openpod "${OPENCODE_BIN}" --version >/dev/null
    193 }
    194 
    195 ensure_path_in_file() {
    196   local file="$1"
    197 
    198   if as_openpod grep -q '^# >>> openpod >>>$' "${file}" 2>/dev/null; then
    199     return
    200   fi
    201 
    202   as_openpod sh -c '
    203     {
    204       printf "\n# >>> openpod >>>\n"
    205       printf "export PATH=\"\$HOME/.local/bin:\$HOME/.opencode/bin:\$PATH\"\n"
    206       printf "# <<< openpod <<<\n"
    207     } >> "$1"
    208   ' sh "${file}"
    209 }
    210 
    211 ensure_openpod_path() {
    212   log "Configuring PATH for ${OPENPOD_USER}"
    213   ensure_path_in_file "${OPENPOD_HOME}/.profile"
    214   ensure_path_in_file "${OPENPOD_HOME}/.bashrc"
    215 }
    216 
    217 install_plugin() {
    218   log "Installing the OpenPod plugin for ${OPENPOD_USER}"
    219 
    220   as_openpod "${OPENCODE_BIN}" plugin add "${OPENPOD_PLUGIN}"
    221 }
    222 
    223 pair_opencode() {
    224   log "Pairing OpenCode for ${OPENPOD_USER}"
    225 
    226   as_openpod "${OPENCODE_BIN}" pair
    227 }
    228 
    229 verify_install() {
    230   log "Verifying installation"
    231 
    232   command -v "${QEMU_BIN}" >/dev/null 2>&1 ||
    233     die "${QEMU_BIN} is missing"
    234   command -v qemu-img >/dev/null 2>&1 ||
    235     die "qemu-img is missing"
    236 
    237   [[ -x "${LIMA_BIN}" ]] ||
    238     die "${LIMA_BIN} is missing"
    239 
    240   as_openpod "${LIMA_BIN}" --version >/dev/null
    241   as_openpod "${OPENCODE_BIN}" --version >/dev/null
    242   as_openpod "${OPENCODE_BIN}" plugin list
    243 }
    244 
    245 main() {
    246   check_host
    247   create_openpod_user
    248   install_system_packages
    249   setup_kvm
    250   install_lima
    251   install_opencode
    252   ensure_openpod_path
    253   install_plugin
    254   verify_install
    255   pair_opencode
    256 }
    257 
    258 main "$@"