Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

6.6 KB
Raw Blame
#!/usr/bin/env bash
# MSO's lifecycle adapter for the Docker/server distribution of 9Router. Upstream
# ALSO ships an npm `9router` CLI; MSO intentionally does not use that launcher to
# own the server because invoking it starts another runtime. lib/managed-apps drives
# every managed app through one adapter
# contract (--version / check / update / uninstall), so this script speaks it.
# The catalog points `command` here with commandProvesInstall: false, because
# this file exists on every checkout whether or not 9Router is installed.
#
# Docker is called bare, not via sudo: mso runs as a user in the docker group,
# and that is already what manager.ts assumes for start/stop/restart/logs.
set -euo pipefail
umask 077

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
ARTIFACT_LOCK="$ROOT/security/managed-app-artifacts.env"
[ -r "$ARTIFACT_LOCK" ] || { echo "missing managed-app artifact lock: $ARTIFACT_LOCK" >&2; exit 2; }
# shellcheck source=security/managed-app-artifacts.env
. "$ARTIFACT_LOCK"

NAME="${NINE_ROUTER_CONTAINER:-9router}"
PORT="${NINE_ROUTER_PORT:-20128}"
IMAGE="${NINE_ROUTER_IMAGE:-$NINE_ROUTER_IMAGE_REF}"
case "$IMAGE" in
  *@sha256:????????????????????????????????????????????????????????????????) ;;
  *) echo "NINE_ROUTER_IMAGE must be an immutable @sha256 reference" >&2; exit 2 ;;
esac
EXPECTED_DIGEST="${IMAGE##*@}"
[[ "$EXPECTED_DIGEST" =~ ^sha256:[0-9a-f]{64}$ ]] || {
  echo "NINE_ROUTER_IMAGE digest must be lowercase sha256" >&2
  exit 2
}
readonly NINE_ROUTER_IMAGE_REF IMAGE EXPECTED_DIGEST
EXPOSE_PUBLIC="${NINE_ROUTER_EXPOSE_PUBLIC:-0}"
case "$EXPOSE_PUBLIC" in
  0) BIND=127.0.0.1 ;;
  1) BIND=0.0.0.0 ;;
  *) echo "NINE_ROUTER_EXPOSE_PUBLIC must be 0 or 1" >&2; exit 2 ;;
esac
DATA="${NINE_ROUTER_DATA:-$HOME/.9router}"
PROXY_NETWORK="${NINE_ROUTER_PROXY_NETWORK:-dokploy-network}"
BASE="http://127.0.0.1:${PORT}"

installed() { docker ps -a --format '{{.Names}}' | grep -qx "$NAME"; }

image_has_expected_digest() {
  local image="$1"
  docker image inspect --format '{{range .RepoDigests}}{{println .}}{{end}}' "$image" 2>/dev/null \
    | grep -Eq "@${EXPECTED_DIGEST}$"
}

running_image_matches_lock() {
  installed || return 1
  local image_id
  image_id="$(docker inspect --format '{{.Image}}' "$NAME" 2>/dev/null)" || return 1
  docker image inspect --format '{{range .RepoDigests}}{{println .}}{{end}}' "$image_id" 2>/dev/null \
    | grep -Eq "@${EXPECTED_DIGEST}$"
}

# The running image's own version label — offline, no fork of the app.
label_version() {
  docker inspect --format '{{index .Config.Labels "org.opencontainers.image.version"}}' \
    "$(docker inspect --format '{{.Image}}' "$NAME" 2>/dev/null)" 2>/dev/null || true
}

cmd_version() {
  installed || { echo "9router container not found" >&2; exit 1; }
  local v
  v="$(label_version)"
  [ -n "$v" ] && { echo "$v"; return; }
  # Image built without the label: ask the app itself.
  curl -fsS -m 5 "$BASE/api/version" | sed -n 's/.*"currentVersion":"\([^"]*\)".*/\1/p'
}

# Update availability is measured against MSO's reviewed immutable digest, never a
# mutable registry tag or the app's own "latest" endpoint.
cmd_check() {
  installed || { echo "9router container not found" >&2; exit 1; }
  local current latest has_update
  current="$(docker inspect --format '{{.Image}}' "$NAME" 2>/dev/null | cut -c1-19)"
  latest="${EXPECTED_DIGEST:0:19}"
  if running_image_matches_lock; then has_update=false; else has_update=true; fi
  printf '{"currentVersion":"%s","latestVersion":"%s","hasUpdate":%s}
' "$current" "$latest" "$has_update"
}

# Pull latest, recreate the container, keep the data mount. Also the install:
# `docker run` against an absent container IS installing it.
cmd_update() {
  echo "==> pull $IMAGE"
  docker pull "$IMAGE"
  image_has_expected_digest "$IMAGE" || {
    echo "pulled 9Router image does not expose the locked digest $EXPECTED_DIGEST" >&2
    exit 1
  }

  # Port guard: recreating onto a port some OTHER container holds would rm the
  # wrong thing's ability to come back up and fail late. Named container wins.
  local owner
  owner="$(docker ps -a --format '{{.Names}} {{.Ports}}' | grep ":${PORT}->" | awk '{print $1}' || true)"
  if [ -n "$owner" ] && [ "$owner" != "$NAME" ]; then
    echo "abort: port $PORT is held by container '$owner', not $NAME" >&2
    exit 1
  fi

  mkdir -p "$DATA"
  local network_args=()
  if docker network inspect "$PROXY_NETWORK" >/dev/null 2>&1; then
    network_args=(--network "$PROXY_NETWORK")
  fi
  echo "==> recreate $NAME (bind $BIND:$PORT${network_args:+, network $PROXY_NETWORK})"
  docker rm -f "$NAME" 2>/dev/null || true
  docker run -d \
    --name "$NAME" \
    --restart unless-stopped \
    "${network_args[@]}" \
    -p "$BIND:$PORT:$PORT" \
    -v "$DATA:/app/data" \
    -e DATA_DIR=/app/data \
    "$IMAGE"

  echo "==> wait for $BASE"
  local i
  for i in $(seq 1 30); do
    sleep 1
    if curl -fsS -o /dev/null -m 3 "$BASE/api/health"; then
      running_image_matches_lock || { echo "running 9Router image does not match the locked digest" >&2; return 1; }
      echo "==> healthy after ${i}s ($(label_version))"
      return 0
    fi
  done
  echo "container is running but $BASE/api/health has not answered within 30s" >&2
  docker ps --filter "name=$NAME" --format '{{.Status}} {{.Ports}}' >&2
  exit 1
}

# Conservative, like the Hermes uninstall: the container goes, the image and
# ~/.9router data stay. A reinstall finds its providers and stats untouched.
cmd_uninstall() {
  local dry=0
  case "${1:-}" in
    --dry-run) dry=1 ;;
  esac
  if [ "$dry" = 1 ]; then
    echo "would remove container: $NAME"
    echo "kept: image $IMAGE, data $DATA"
    exit 0
  fi
  installed || { echo "9router container not found — nothing to remove" >&2; exit 0; }
  docker rm -f "$NAME"
  echo "removed container $NAME (kept image $IMAGE and data $DATA)"
}

case "${1:-}" in
--version) cmd_version ;;
check) cmd_check ;;
status)
  installed && docker ps --filter "name=$NAME" --format '{{.Status}} {{.Ports}}' || echo "not installed"
  ;;
install | update)
  [ "${2:-}" = "--yes" ] || [ "$1" = "install" ] || { echo "update requires --yes" >&2; exit 2; }
  cmd_update
  ;;
uninstall)
  shift
  case "${1:-}" in
  --help)
    # update.ts reads this output to verify --dry-run is still honoured
    # before offering "Preview removal".
    echo "usage: managed-app-9router uninstall --yes [--dry-run]"
    exit 0
    ;;
  --yes) shift; cmd_uninstall "${1:-}" ;;
  *) echo "uninstall requires --yes" >&2; exit 2 ;;
  esac
  ;;
*)
  echo "usage: managed-app-9router --version|check [--json]|status|install|update --yes|uninstall --yes [--dry-run]" >&2
  exit 2
  ;;
esac