Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.
refactor(scripts): move install.sh to scripts/install and handle port/cache conflicts
Dongdong Chen0ec17f09e4e7
diff --git a/README.md b/README.mdindex cf3eb1b4..0f418c68 100644--- a/README.md+++ b/README.md@@ -198,13 +198,14 @@ computer you connect. On that macOS (Apple silicon) or Linux computer, run theguided installer:```bash-./install.sh+./scripts/install/install.sh```It checks your hardware, installs and verifies a local model after yourapproval, and connects `linger-computer` to your Linger server. Run-`./install.sh --help` for day-2 commands (`--status`, `--up`, `--remove`), or-see [Bring your own runtime](docs/local-inference.md) for other model setups.+`./scripts/install/install.sh --help` for day-2 commands (`--status`, `--up`,+`--remove`), or see [Bring your own runtime](docs/local-inference.md) for other+model setups.## Learn morediff --git a/scripts/install.test.ts b/scripts/install.test.tsindex ce697e48..18175007 100644--- a/scripts/install.test.ts+++ b/scripts/install.test.ts@@ -1,10 +1,13 @@import { execFile, spawn } from "node:child_process";import {chmod,+ copyFile,mkdir,mkdtemp,readFile,+ readdir,rm,+ stat,writeFile,} from "node:fs/promises";import { createServer, type Server } from "node:http";@@ -20,7 +23,10 @@ const execFileAsync = promisify(execFile);const repositoryRoot = path.resolve(fileURLToPath(new URL("../", import.meta.url)),);-const installScriptPath = path.join(repositoryRoot, "install.sh");+const installScriptPath = path.join(+ repositoryRoot,+ "scripts/install/install.sh",+);const temporaryDirectories: string[] = [];const cleanups: Array<() => Promise<void>> = [];@@ -40,6 +46,7 @@ interface ComputerInvocation {interface FakeEngineScenario {acceptedStack?: boolean;lastRunState?: string;+ enforcePrivateRoot?: boolean;picks?: Array<{role: string;id: string;@@ -87,10 +94,12 @@ async function createFakeStackScript(): Promise<{scriptPath: string;logPath: string;+ kitHomeLogPath: string;}> {const pythonBin = await resolvePythonExecutable();const scriptPath = path.join(root, "fake-stack.sh");const logPath = path.join(root, "stack-calls.log");+ const kitHomeLogPath = path.join(root, "kit-homes.log");const statusJsonPath = path.join(root, "fixture-status.json");const hardwareJsonPath = path.join(root, "fixture-hardware.json");const gatewayPiDir = path.join(@@ -211,9 +220,25 @@ async function createFakeStackScript(await writeFile(planTemplatePath, JSON.stringify(planTemplate));const createPiOnApply = scenario.createGatewayPiOnApply !== false;+ const enforcePrivateRoot = scenario.enforcePrivateRoot ? "1" : "0";const content = `#!/bin/shset -euprintf '%s\n' "$*" >> ${JSON.stringify(logPath)}+if [ "${enforcePrivateRoot}" = "1" ] && [ "\${1:-}" = "--bootstrap-json" ]; then+ kit_root="\${LOCALMODELS_KIT_HOME:-\${XDG_CACHE_HOME:-\${HOME}/.cache}/simplex-localmodels/kits}"+ printf '%s\n' "$kit_root" >> ${JSON.stringify(kitHomeLogPath)}+ owner="$(id -u)"+ ancestor="$(dirname "$kit_root")"+ while :; do+ [ ! -L "$ancestor" ] || { printf 'stack.sh: cache root must be a private owned directory\n' >&2; exit 2; }+ if [ -e "$ancestor" ]; then+ bad_parent="$(find "$ancestor" -prune \\( \\( ! -user "$owner" ! -user 0 \\) -o \\( \\( -perm -0020 -o -perm -0002 \\) ! -perm -1000 \\) \\) -print || true)"+ [ -z "$bad_parent" ] || { printf 'stack.sh: cache root must be a private owned directory\n' >&2; exit 2; }+ fi+ [ "$ancestor" != / ] || break+ ancestor="$(dirname "$ancestor")"+ done+ficase "\${1:-}" in--bootstrap-json)printf '{"schema":"localmodels.kit-location/v1","kitRoot":%s,"python":%s,"moduleDir":"modules"}\n' \@@ -268,7 +293,7 @@ PIEOFesac`;await writeFile(scriptPath, content, { mode: 0o755 });- return { scriptPath, logPath };+ return { scriptPath, logPath, kitHomeLogPath };}async function startMockLingerServer(@@ -358,14 +383,20 @@ async function runInstallScript(options: {env: NodeJS.ProcessEnv;input?: string;+ scriptPath?: string;+ cwd?: string;},): Promise<ScriptRunResult> {return await new Promise<ScriptRunResult>((resolve, reject) => {- const child = spawn("bash", [installScriptPath, ...args], {- cwd: repositoryRoot,- env: options.env,- stdio: ["pipe", "pipe", "pipe"],- });+ const child = spawn(+ "bash",+ [options.scriptPath ?? installScriptPath, ...args],+ {+ cwd: options.cwd ?? repositoryRoot,+ env: options.env,+ stdio: ["pipe", "pipe", "pipe"],+ },+ );let stdout = "";let stderr = "";child.stdout.setEncoding("utf8");@@ -593,7 +624,7 @@ describe("root install.sh", () => {env: buildTestEnv(root, scriptPath),});expect(statusResult.code).toBe(0);- expect(statusResult.stdout).toContain("./install.sh --up");+ expect(statusResult.stdout).toContain("./scripts/install/install.sh --up");expect(statusResult.stdout).not.toContain("stack up");expect(statusResult.stdout).not.toContain("localmodels");@@ -604,11 +635,190 @@ describe("root install.sh", () => {},);expect(removeResult.code).toBe(0);- expect(removeResult.stdout).toContain("./install.sh --remove --purge");+ expect(removeResult.stdout).toContain(+ "./scripts/install/install.sh --remove --purge",+ );expect(removeResult.stdout).not.toContain("stack remove");const stackCalls = (await readFile(logPath, "utf8")).trim().split("\n");expect(stackCalls).toContain("status");expect(stackCalls).toContain("remove --purge --yes");});++ it("falls back from group-writable XDG cache to a private kit root and temp fallback", async () => {+ const root = await createTempRoot();+ const homeDir = path.join(root, "home");+ const xdgCache = path.join(root, "shared-cache");+ const privateTmp = path.join(root, "private-tmp");+ await mkdir(homeDir, { recursive: true });+ await mkdir(xdgCache, { recursive: true });+ await mkdir(privateTmp, { recursive: true });+ await chmod(root, 0o755);+ await chmod(homeDir, 0o700);+ await chmod(xdgCache, 0o775);+ await chmod(privateTmp, 0o700);++ const { scriptPath, kitHomeLogPath } = await createFakeStackScript(root, {+ enforcePrivateRoot: true,+ });++ const first = await runInstallScript(["--status"], {+ env: {+ ...buildTestEnv(root, scriptPath),+ XDG_CACHE_HOME: xdgCache,+ TMPDIR: privateTmp,+ },+ });+ expect(first.code).toBe(0);+ const firstLogged = (await readFile(kitHomeLogPath, "utf8"))+ .trim()+ .split("\n");+ expect(firstLogged[0]).toBe(path.join(homeDir, ".linger/localmodels/kits"));+ const homeKitStat = await stat(firstLogged[0] ?? "");+ expect(homeKitStat.mode & 0o777).toBe(0o700);++ await chmod(homeDir, 0o775);+ const second = await runInstallScript(["--status"], {+ env: {+ ...buildTestEnv(root, scriptPath),+ XDG_CACHE_HOME: xdgCache,+ TMPDIR: privateTmp,+ },+ });+ expect(second.code).toBe(0);+ const secondLogged = (await readFile(kitHomeLogPath, "utf8"))+ .trim()+ .split("\n");+ expect(+ secondLogged[1]?.startsWith(path.join(privateTmp, "linger-kit.")),+ ).toBe(true);+ expect(await readdir(privateTmp)).toEqual([]);+ });++ it("reassigns colliding Docker Compose ports and configures LiveKit override", async () => {+ const root = await createTempRoot();+ const fakeRepo = path.join(root, "fake-repo");+ const fakeBin = path.join(root, "fake-bin");+ const homeDir = path.join(root, "home");+ const xdgCache = path.join(root, "cache");+ const dockerLogPath = path.join(root, "docker-calls.log");+ await mkdir(path.join(fakeRepo, "scripts/install"), { recursive: true });+ await mkdir(path.join(fakeRepo, "scripts/installs"), { recursive: true });+ await mkdir(fakeBin, { recursive: true });+ await mkdir(homeDir, { recursive: true });+ await mkdir(xdgCache, { recursive: true });+ await chmod(homeDir, 0o700);+ await chmod(xdgCache, 0o700);++ const copiedInstallPath = path.join(fakeRepo, "scripts/install/install.sh");+ await copyFile(installScriptPath, copiedInstallPath);+ await chmod(copiedInstallPath, 0o755);++ const { scriptPath: fakeStackPath } = await createFakeStackScript(root, {});+ await copyFile(+ fakeStackPath,+ path.join(fakeRepo, "scripts/installs/stack.sh"),+ );+ await chmod(path.join(fakeRepo, "scripts/installs/stack.sh"), 0o755);++ await writeFile(fakeRepo + "/docker-compose.yml", "services:\n web:\n");+ await writeFile(+ path.join(fakeRepo, ".env.example"),+ [+ "WEB_PUBLIC_ORIGIN=http://localhost:3000",+ "AGENT_RUNNER_PUBLIC_GATEWAY_URL=ws://127.0.0.1:4319/v1/runner-gateway",+ "LIVEKIT_URL=ws://127.0.0.1:17880",+ "# POSTGRES_PORT=5432",+ "# TEAMHUB_HTTP_PORT=8080",+ "# WEB_HTTP_PORT=3000",+ ].join("\n") + "\n",+ );++ await writeFile(+ path.join(fakeBin, "docker"),+ `#!/bin/sh+set -eu+printf '%s\n' "$*" >> ${JSON.stringify(dockerLogPath)}+if [ "\${1:-}" = "compose" ] && [ "\${2:-}" = "version" ]; then+ printf 'Docker Compose version v2.40.2\n'+ exit 0+fi+if [ "\${1:-}" = "compose" ] && [ "\${2:-}" = "ps" ]; then+ printf '127.0.0.1:5432->5432/tcp\n'+ exit 0+fi+exit 0+`,+ { mode: 0o755 },+ );++ await writeFile(+ path.join(fakeBin, "curl"),+ `#!/bin/sh+set -eu+out=""+url=""+prev=""+for a in "$@"; do+ case "$prev" in+ -o|--output) out="$a" ;;+ esac+ case "$a" in+ http://*|https://*) url="$a" ;;+ esac+ prev="$a"+done+case "$url" in+ "http://localhost:3001/install/linger-computer.sh")+ body="# linger-computer-launcher-v2"+ if [ -n "$out" ]; then printf '%s\n' "$body" > "$out"; else printf '%s\n' "$body"; fi+ exit 0+ ;;+ *)+ exit 22+ ;;+esac+`,+ { mode: 0o755 },+ );++ const result = await runInstallScript([], {+ scriptPath: copiedInstallPath,+ cwd: fakeRepo,+ env: {+ HOME: homeDir,+ XDG_CACHE_HOME: xdgCache,+ PATH: `${fakeBin}:/usr/local/bin:/usr/bin:/bin`,+ LINGER_INSTALL_BUSY_PORTS: "3000,17880,7881,7882",+ },+ input: ["1", "y", "n", "n", "n"].join("\n") + "\n",+ });++ expect(result.code).toBe(0);+ expect(result.stdout).toContain(+ "Port(s) already in use: 3000, 17880, 7881, 7882",+ );+ expect(result.stdout).toContain("Nothing was downloaded or changed.");++ const updatedEnv = await readFile(path.join(fakeRepo, ".env"), "utf8");+ expect(updatedEnv).toContain("WEB_HTTP_PORT=3001");+ expect(updatedEnv).toContain("WEB_PUBLIC_ORIGIN=http://localhost:3001");+ expect(updatedEnv).toContain("LIVEKIT_URL=ws://127.0.0.1:17881");+ expect(updatedEnv).toContain(+ "COMPOSE_FILE=docker-compose.yml:.linger-runtime/compose.ports.yml",+ );+ expect(updatedEnv).not.toContain("POSTGRES_PORT=5433");++ const portsOverride = await readFile(+ path.join(fakeRepo, ".linger-runtime/compose.ports.yml"),+ "utf8",+ );+ expect(portsOverride).toContain("ports: !override");+ expect(portsOverride).toContain("127.0.0.1:17881:7880");+ expect(portsOverride).toContain("7883:7881");+ expect(portsOverride).toContain("7884:7882/udp");++ const dockerCalls = await readFile(dockerLogPath, "utf8");+ expect(dockerCalls).toContain("compose up --build --detach --wait");+ });});diff --git a/install.sh b/scripts/install/install.shsimilarity index 66%rename from install.shrename to scripts/install/install.shindex 571ebd7f..a4baa925 100755--- a/install.sh+++ b/scripts/install/install.sh@@ -5,15 +5,27 @@ set -Eeuo pipefail# Local Computer connection on macOS (Apple silicon) and Linux.SCRIPT_PATH="${BASH_SOURCE[0]:-}"+SCRIPT_DIR=""REPO_ROOT=""+SELF_CMD="./scripts/install/install.sh"if [[ -n "$SCRIPT_PATH" && -e "$SCRIPT_PATH" ]]; then- REPO_ROOT="$(cd -- "$(dirname -- "$SCRIPT_PATH")" && pwd -P)"+ SCRIPT_DIR="$(cd -- "$(dirname -- "$SCRIPT_PATH")" && pwd -P)"+ if [[ -f "$SCRIPT_DIR/../../docker-compose.yml" || -r "$SCRIPT_DIR/../installs/stack.sh" ]]; then+ REPO_ROOT="$(cd -- "$SCRIPT_DIR/../.." && pwd -P)"+ else+ SELF_CMD="./$(basename -- "$SCRIPT_PATH")"+ fifiWORK_DIR="$(mktemp -d "${TMPDIR:-/tmp}/linger-install.XXXXXX")"chmod 0700 "$WORK_DIR"+TEMP_KIT_DIR=""cleanup() {rm -rf "$WORK_DIR"+ if [[ -n "$TEMP_KIT_DIR" ]]; then+ chmod -R u+w "$TEMP_KIT_DIR" 2>/dev/null || true+ rm -rf "$TEMP_KIT_DIR"+ fi}trap cleanup EXIT@@ -29,8 +41,8 @@ KIT_PYTHON=""PROMPT_VALUE=""usage() {- cat <<'EOF'-Usage: ./install.sh [options]+ cat <<EOF+Usage: $SELF_CMD [options]Guided setup for Linger local models and connecting this computer to Linger.@@ -140,24 +152,52 @@ read_env_value() {' "$env_file"}+set_env_value() {+ local env_file="$1"+ local key="$2"+ local val="$3"+ local tmp_file="$WORK_DIR/env.tmp.$$"+ awk -v k="$key" -v v="$val" '+ BEGIN { replaced = 0 }+ {+ if ($0 ~ "^[[:space:]]*(#[[:space:]]*)?" k "=") {+ if (!replaced) {+ print k "=" v+ replaced = 1+ }+ next+ }+ print $0+ }+ END {+ if (!replaced) {+ print k "=" v+ }+ }+ ' "$env_file" > "$tmp_file"+ cat "$tmp_file" > "$env_file"+ rm -f "$tmp_file"+}+sanitize_engine_stream() {+ local escaped_self="${SELF_CMD//|/\\|}"sed -E \-e 's/stack\.sh:/install.sh:/g' \-e 's/on Windows use stack\.ps1/on Windows use the Linger desktop app/g' \-e 's/stack\.ps1/the Linger desktop app/g' \- -e 's/(python3 -m )?localmodels stack remove --purge/.\/install.sh --remove --purge/g' \- -e 's/stack remove --purge/.\/install.sh --remove --purge/g' \- -e 's/(python3 -m )?localmodels stack remove/.\/install.sh --remove/g' \- -e 's/stack remove/.\/install.sh --remove/g' \- -e 's/(python3 -m )?localmodels stack up/.\/install.sh --up/g' \- -e 's/stack up/.\/install.sh --up/g' \- -e 's/(python3 -m )?localmodels stack status/.\/install.sh --status/g' \- -e 's/stack status/.\/install.sh --status/g' \- -e 's/(python3 -m )?localmodels stack plan( --update)?/.\/install.sh/g' \- -e 's/stack plan( --update)?/.\/install.sh/g' \- -e 's/(python3 -m )?localmodels stack apply[^[:cntrl:]]*/.\/install.sh/g' \- -e 's/stack apply[^[:cntrl:]]*/.\/install.sh/g' \- -e 's/(python3 -m )?localmodels install/.\/install.sh/g' \+ -e "s|(python3 -m )?localmodels stack remove --purge|${escaped_self} --remove --purge|g" \+ -e "s|stack remove --purge|${escaped_self} --remove --purge|g" \+ -e "s|(python3 -m )?localmodels stack remove|${escaped_self} --remove|g" \+ -e "s|stack remove|${escaped_self} --remove|g" \+ -e "s|(python3 -m )?localmodels stack up|${escaped_self} --up|g" \+ -e "s|stack up|${escaped_self} --up|g" \+ -e "s|(python3 -m )?localmodels stack status|${escaped_self} --status|g" \+ -e "s|stack status|${escaped_self} --status|g" \+ -e "s|(python3 -m )?localmodels stack plan( --update)?|${escaped_self}|g" \+ -e "s|stack plan( --update)?|${escaped_self}|g" \+ -e "s|(python3 -m )?localmodels stack apply[^[:cntrl:]]*|${escaped_self}|g" \+ -e "s|stack apply[^[:cntrl:]]*|${escaped_self}|g" \+ -e "s|(python3 -m )?localmodels install|${escaped_self}|g" \-e 's/stack\.sh/install.sh/g'}@@ -209,7 +249,7 @@ check_supported_platform() {Linux) ;;Darwin)if [[ "$arch" != arm64 ]]; then- die "macOS local models require Apple silicon (arm64); Intel Macs can use ./install.sh --connect against a remote server or another AI provider."+ die "macOS local models require Apple silicon (arm64); Intel Macs can use $SELF_CMD --connect against a remote server or another AI provider."fi;;*)@@ -219,8 +259,103 @@ check_supported_platform() {command -v curl >/dev/null 2>&1 || die "curl is required."}+canonical_existing_parent() {+ local target="$1"+ if [[ -d "$target" ]]; then+ (cd -- "$target" 2>/dev/null && pwd -P)+ return $?+ fi+ local parent base resolved_parent+ parent="$(dirname -- "$target")"+ base="$(basename -- "$target")"+ if [[ "$parent" == "$target" ]]; then+ printf '%s\n' "$target"+ return 0+ fi+ resolved_parent="$(canonical_existing_parent "$parent")" || return 1+ if [[ "$resolved_parent" == "/" ]]; then+ printf '/%s\n' "$base"+ else+ printf '%s/%s\n' "$resolved_parent" "$base"+ fi+}++ancestors_are_private() {+ local ancestor="$1"+ local owner bad_parent=""+ owner="$(id -u)"+ while :; do+ [[ ! -L "$ancestor" ]] || return 1+ if [[ -e "$ancestor" ]]; then+ bad_parent="$(find "$ancestor" -prune \( \( ! -user "$owner" ! -user 0 \) -o \( \( -perm -0020 -o -perm -0002 \) ! -perm -1000 \) \) -print 2>/dev/null || printf 'error')"+ [[ -z "$bad_parent" ]] || return 1+ fi+ [[ "$ancestor" != "/" ]] || break+ ancestor="$(dirname -- "$ancestor")"+ done+ return 0+}++ensure_private_subdir() {+ local dir="$1"+ local owner+ owner="$(id -u)"+ if [[ -L "$dir" ]]; then+ return 1+ fi+ if [[ -e "$dir" ]]; then+ [[ -d "$dir" ]] || return 1+ local owned+ owned="$(find "$dir" -prune -user "$owner" -print 2>/dev/null || true)"+ [[ -n "$owned" ]] || return 1+ chmod 0700 "$dir" 2>/dev/null || return 1+ else+ (umask 077 && mkdir -p "$dir") 2>/dev/null || return 1+ fi+ ancestors_are_private "$dir"+}++ensure_kit_home() {+ if [[ -n "${LOCALMODELS_KIT_HOME:-}" ]]; then+ export LOCALMODELS_KIT_HOME+ return 0+ fi++ local raw_cache="${XDG_CACHE_HOME:-$HOME/.cache}"+ local canon_cache=""+ canon_cache="$(canonical_existing_parent "$raw_cache" 2>/dev/null || true)"+ if [[ -n "$canon_cache" ]] && ancestors_are_private "$canon_cache"; then+ if ensure_private_subdir "$canon_cache/simplex-localmodels" &&+ ensure_private_subdir "$canon_cache/simplex-localmodels/kits"; then+ export LOCALMODELS_KIT_HOME="$canon_cache/simplex-localmodels/kits"+ return 0+ fi+ fi++ local real_home=""+ real_home="$(canonical_existing_parent "$HOME" 2>/dev/null || true)"+ if [[ -n "$real_home" ]] && ancestors_are_private "$real_home"; then+ if ensure_private_subdir "$real_home/.linger" &&+ ensure_private_subdir "$real_home/.linger/localmodels" &&+ ensure_private_subdir "$real_home/.linger/localmodels/kits"; then+ export LOCALMODELS_KIT_HOME="$real_home/.linger/localmodels/kits"+ return 0+ fi+ fi++ local real_tmp=""+ real_tmp="$(canonical_existing_parent "${TMPDIR:-/tmp}" 2>/dev/null || true)"+ if [[ -n "$real_tmp" ]] && ancestors_are_private "$real_tmp"; then+ TEMP_KIT_DIR="$(mktemp -d "$real_tmp/linger-kit.XXXXXX")"+ chmod 0700 "$TEMP_KIT_DIR"+ export LOCALMODELS_KIT_HOME="$TEMP_KIT_DIR"+ return 0+ fi+}+ensure_engine() {check_supported_platform+ ensure_kit_homeif [[ -n "${LINGER_STACK_SCRIPT:-}" ]]; then[[ -r "$LINGER_STACK_SCRIPT" ]] || die "Configured engine helper is not readable: $LINGER_STACK_SCRIPT"STACK_SCRIPT="$LINGER_STACK_SCRIPT"@@ -281,6 +416,282 @@ compose_available() {docker compose version >/dev/null 2>&1}+collect_docker_host_ports() {+ tr ', ' '\n' | sed -nE 's/.*:([0-9]+)->[0-9]+\/(tcp|udp).*/\1\/\2/p' | sort -u+}++parse_port_from_url() {+ local url="$1"+ local fallback="$2"+ local rest="${url#*://}"+ rest="${rest%%/*}"+ if [[ "$rest" == *:* ]]; then+ local p="${rest##*:}"+ if [[ "$p" =~ ^[0-9]+$ ]]; then+ printf '%s\n' "$p"+ return 0+ fi+ fi+ printf '%s\n' "$fallback"+}++read_livekit_override_port() {+ local file="$1"+ local container_port="$2"+ local fallback="$3"+ [[ -f "$file" ]] || { printf '%s\n' "$fallback"; return 0; }+ local match=""+ match="$(sed -nE "s/.*:([0-9]+):${container_port}(\/udp)?\"?[[:space:]]*$/\1/p" "$file" | head -n 1)"+ if [[ -n "$match" ]]; then+ printf '%s\n' "$match"+ else+ printf '%s\n' "$fallback"+ fi+}++is_port_busy() {+ local port="$1"+ local proto="$2"+ local own_ports="$3"+ local docker_ports="$4"++ if [[ -n "$own_ports" ]] && printf '%s\n' "$own_ports" | grep -Fxq "$port/$proto"; then+ return 1+ fi++ if [[ -n "${LINGER_INSTALL_BUSY_PORTS+set}" ]]; then+ local spec=",${LINGER_INSTALL_BUSY_PORTS// /},"+ if [[ "$spec" == *",$port,"* || "$spec" == *",$port/$proto,"* ]]; then+ return 0+ fi+ return 1+ fi++ if [[ -n "$docker_ports" ]] && printf '%s\n' "$docker_ports" | grep -Fxq "$port/$proto"; then+ return 0+ fi++ if command -v ss >/dev/null 2>&1; then+ if [[ "$proto" == "udp" ]]; then+ ss -Huan "sport = :$port" 2>/dev/null | grep -q . && return 0+ else+ ss -Htan "sport = :$port" 2>/dev/null | grep -q . && return 0+ fi+ return 1+ fi++ if command -v lsof >/dev/null 2>&1; then+ if [[ "$proto" == "udp" ]]; then+ lsof -nP -iUDP:"$port" >/dev/null 2>&1 && return 0+ else+ lsof -nP -iTCP:"$port" -sTCP:LISTEN >/dev/null 2>&1 && return 0+ fi+ fi++ return 1+}++pick_free_port() {+ local candidate=$(( $1 + 1 ))+ local proto="$2"+ local own_ports="$3"+ local reserved="$4"+ local docker_ports="$5"+ local tries=0+ while (( tries < 200 )); do+ if [[ " $reserved " != *" $candidate "* ]] &&+ ! is_port_busy "$candidate" "$proto" "$own_ports" "$docker_ports"; then+ printf '%s\n' "$candidate"+ return 0+ fi+ candidate=$((candidate + 1))+ tries=$((tries + 1))+ done+ return 1+}++ensure_compose_ports() {+ local env_file="$REPO_ROOT/.env"+ local override_file="$REPO_ROOT/.linger-runtime/compose.ports.yml"++ local own_raw=""+ own_raw="$(cd "$REPO_ROOT" && docker compose ps --format '{{.Ports}}' 2>/dev/null || true)"+ local own_ports=""+ if [[ -n "$own_raw" ]]; then+ own_ports="$(printf '%s\n' "$own_raw" | collect_docker_host_ports)"+ fi++ local docker_ports=""+ if [[ -z "${LINGER_INSTALL_BUSY_PORTS+set}" ]]; then+ local all_raw=""+ all_raw="$(docker ps --format '{{.Ports}}' 2>/dev/null || true)"+ if [[ -n "$all_raw" ]]; then+ docker_ports="$(printf '%s\n' "$all_raw" | collect_docker_host_ports)"+ fi+ fi++ local cur_pg cur_th cur_web cur_node cur_doc cur_lk_url cur_lk_http cur_lk_tcp cur_lk_udp+ cur_pg="$(read_env_value "$env_file" "POSTGRES_PORT" || printf '5432')"+ cur_th="$(read_env_value "$env_file" "TEAMHUB_HTTP_PORT" || printf '8080')"+ cur_web="$(read_env_value "$env_file" "WEB_HTTP_PORT" || printf '3000')"+ cur_node="$(read_env_value "$env_file" "NODE_SERVICES_HTTP_PORT" || printf '4319')"+ cur_doc="$(read_env_value "$env_file" "DOCUMENT_RUNTIME_HTTP_PORT" || printf '4321')"+ cur_lk_url="$(read_env_value "$env_file" "LIVEKIT_URL" || printf 'ws://127.0.0.1:17880')"+ cur_lk_http="$(parse_port_from_url "$cur_lk_url" "17880")"+ cur_lk_tcp="$(read_livekit_override_port "$override_file" "7881" "7881")"+ cur_lk_udp="$(read_livekit_override_port "$override_file" "7882" "7882")"++ local reserved=" $cur_pg $cur_th $cur_web $cur_node $cur_doc $cur_lk_http $cur_lk_tcp $cur_lk_udp "+ local new_pg="$cur_pg"+ local new_th="$cur_th"+ local new_web="$cur_web"+ local new_node="$cur_node"+ local new_doc="$cur_doc"+ local new_lk_http="$cur_lk_http"+ local new_lk_tcp="$cur_lk_tcp"+ local new_lk_udp="$cur_lk_udp"++ local conflicts=()+ local changes=()++ if is_port_busy "$cur_pg" "tcp" "$own_ports" "$docker_ports"; then+ conflicts+=("$cur_pg")+ new_pg="$(pick_free_port "$cur_pg" "tcp" "$own_ports" "$reserved" "$docker_ports")" ||+ die "Could not find a free port for Postgres."+ reserved="$reserved$new_pg "+ changes+=("Postgres $cur_pg -> $new_pg")+ fi+ if is_port_busy "$cur_th" "tcp" "$own_ports" "$docker_ports"; then+ conflicts+=("$cur_th")+ new_th="$(pick_free_port "$cur_th" "tcp" "$own_ports" "$reserved" "$docker_ports")" ||+ die "Could not find a free port for TeamHub."+ reserved="$reserved$new_th "+ changes+=("TeamHub $cur_th -> $new_th")+ fi+ if is_port_busy "$cur_web" "tcp" "$own_ports" "$docker_ports"; then+ conflicts+=("$cur_web")+ new_web="$(pick_free_port "$cur_web" "tcp" "$own_ports" "$reserved" "$docker_ports")" ||+ die "Could not find a free port for Web."+ reserved="$reserved$new_web "+ changes+=("Web $cur_web -> $new_web")+ fi+ if is_port_busy "$cur_node" "tcp" "$own_ports" "$docker_ports"; then+ conflicts+=("$cur_node")+ new_node="$(pick_free_port "$cur_node" "tcp" "$own_ports" "$reserved" "$docker_ports")" ||+ die "Could not find a free port for Node Services."+ reserved="$reserved$new_node "+ changes+=("Node Services $cur_node -> $new_node")+ fi+ if is_port_busy "$cur_doc" "tcp" "$own_ports" "$docker_ports"; then+ conflicts+=("$cur_doc")+ new_doc="$(pick_free_port "$cur_doc" "tcp" "$own_ports" "$reserved" "$docker_ports")" ||+ die "Could not find a free port for Document Runtime."+ reserved="$reserved$new_doc "+ changes+=("Document Runtime $cur_doc -> $new_doc")+ fi+ if is_port_busy "$cur_lk_http" "tcp" "$own_ports" "$docker_ports"; then+ conflicts+=("$cur_lk_http")+ new_lk_http="$(pick_free_port "$cur_lk_http" "tcp" "$own_ports" "$reserved" "$docker_ports")" ||+ die "Could not find a free port for LiveKit HTTP."+ reserved="$reserved$new_lk_http "+ changes+=("LiveKit HTTP $cur_lk_http -> $new_lk_http")+ fi+ if is_port_busy "$cur_lk_tcp" "tcp" "$own_ports" "$docker_ports"; then+ conflicts+=("$cur_lk_tcp")+ new_lk_tcp="$(pick_free_port "$cur_lk_tcp" "tcp" "$own_ports" "$reserved" "$docker_ports")" ||+ die "Could not find a free port for LiveKit RTC TCP."+ reserved="$reserved$new_lk_tcp "+ changes+=("LiveKit RTC TCP $cur_lk_tcp -> $new_lk_tcp")+ fi+ if is_port_busy "$cur_lk_udp" "udp" "$own_ports" "$docker_ports"; then+ conflicts+=("$cur_lk_udp")+ new_lk_udp="$(pick_free_port "$cur_lk_udp" "udp" "$own_ports" "$reserved" "$docker_ports")" ||+ die "Could not find a free port for LiveKit RTC UDP."+ reserved="$reserved$new_lk_udp "+ changes+=("LiveKit RTC UDP $cur_lk_udp -> $new_lk_udp")+ fi++ if (( ${#conflicts[@]} == 0 )); then+ return 0+ fi++ local conflict_list=""+ local idx=0+ while (( idx < ${#conflicts[@]} )); do+ if (( idx > 0 )); then+ conflict_list="$conflict_list, "+ fi+ conflict_list="$conflict_list${conflicts[$idx]}"+ idx=$((idx + 1))+ done++ printf ' Port(s) already in use: %s\n' "$conflict_list"+ idx=0+ while (( idx < ${#changes[@]} )); do+ printf ' %s\n' "${changes[$idx]}"+ idx=$((idx + 1))+ done+ prompt_line " Port(s) $conflict_list are already in use. Reassign to free local ports automatically (Web -> http://localhost:$new_web)? [Y/n]: "+ local port_ans=""+ port_ans="$(lowercase "$PROMPT_VALUE")"+ if [[ -n "$port_ans" && "$port_ans" != "y" && "$port_ans" != "yes" ]]; then+ die "Free those ports or update $env_file, then rerun $SELF_CMD."+ fi++ if [[ "$new_pg" != "$cur_pg" ]]; then+ set_env_value "$env_file" "POSTGRES_PORT" "$new_pg"+ fi+ if [[ "$new_th" != "$cur_th" ]]; then+ set_env_value "$env_file" "TEAMHUB_HTTP_PORT" "$new_th"+ fi+ if [[ "$new_web" != "$cur_web" ]]; then+ set_env_value "$env_file" "WEB_HTTP_PORT" "$new_web"+ local cur_origin=""+ cur_origin="$(read_env_value "$env_file" "WEB_PUBLIC_ORIGIN" || true)"+ if [[ -z "$EXPLICIT_WEB_URL" || "$EXPLICIT_WEB_URL" =~ ^http://(localhost|127\.0\.0\.1):${cur_web}$ ]] &&+ [[ -z "$cur_origin" || "$cur_origin" =~ ^http://(localhost|127\.0\.0\.1)(:[0-9]+)?$ ]]; then+ set_env_value "$env_file" "WEB_PUBLIC_ORIGIN" "http://localhost:$new_web"+ EXPLICIT_WEB_URL=""+ WEB_URL="http://localhost:$new_web"+ fi+ fi+ if [[ "$new_node" != "$cur_node" ]]; then+ set_env_value "$env_file" "NODE_SERVICES_HTTP_PORT" "$new_node"+ local cur_gw=""+ cur_gw="$(read_env_value "$env_file" "AGENT_RUNNER_PUBLIC_GATEWAY_URL" || true)"+ if [[ -z "$cur_gw" || "$cur_gw" =~ ^ws://(127\.0\.0\.1|localhost): ]]; then+ set_env_value "$env_file" "AGENT_RUNNER_PUBLIC_GATEWAY_URL" "ws://127.0.0.1:$new_node/v1/runner-gateway"+ fi+ fi+ if [[ "$new_doc" != "$cur_doc" ]]; then+ set_env_value "$env_file" "DOCUMENT_RUNTIME_HTTP_PORT" "$new_doc"+ fi++ if [[ "$new_lk_http" != "17880" || "$new_lk_tcp" != "7881" || "$new_lk_udp" != "7882" ]]; then+ if [[ "$new_lk_http" != "$cur_lk_http" ]]; then+ if [[ -z "$cur_lk_url" || "$cur_lk_url" =~ ^ws://(127\.0\.0\.1|localhost): ]]; then+ set_env_value "$env_file" "LIVEKIT_URL" "ws://127.0.0.1:$new_lk_http"+ fi+ fi+ mkdir -p "$REPO_ROOT/.linger-runtime"+ cat <<EOF > "$override_file"+services:+ livekit:+ ports: !override+ - "127.0.0.1:$new_lk_http:7880"+ - "\${LIVEKIT_RTC_BIND_ADDRESS:-127.0.0.1}:$new_lk_tcp:7881"+ - "\${LIVEKIT_RTC_BIND_ADDRESS:-127.0.0.1}:$new_lk_udp:7882/udp"+EOF+ local cur_compose_file=""+ cur_compose_file="$(read_env_value "$env_file" "COMPOSE_FILE" || true)"+ if [[ -z "$cur_compose_file" ]]; then+ set_env_value "$env_file" "COMPOSE_FILE" "docker-compose.yml:.linger-runtime/compose.ports.yml"+ elif [[ ":$cur_compose_file:" != *":.linger-runtime/compose.ports.yml:"* ]]; then+ set_env_value "$env_file" "COMPOSE_FILE" "$cur_compose_file:.linger-runtime/compose.ports.yml"+ fi+ fi+}+start_compose_server() {if [[ ! -f "$REPO_ROOT/.env" ]]; then[[ -f "$REPO_ROOT/.env.example" ]] || die "Missing $REPO_ROOT/.env.example."@@ -288,6 +699,8 @@ start_compose_server() {printf ' Created .env from .env.example.\n'fi+ ensure_compose_ports+printf ' Starting Linger with Docker Compose...\n'(cd "$REPO_ROOT" && docker compose up --build --detach --wait) || die "Docker Compose failed to start Linger."@@ -455,7 +868,7 @@ run_connect_step() {if [[ ":${PATH}:" != *":${bin_dir}:"* ]]; thenprintf ' Note: add %s to PATH if you want to invoke linger-computer-%s directly.\n' "$bin_dir" "$env_name"fi- printf ' Day-2 commands: ./install.sh --status | ./install.sh --up | ./install.sh --remove\n'+ printf ' Day-2 commands: %s --status | %s --up | %s --remove\n' "$SELF_CMD" "$SELF_CMD" "$SELF_CMD"}step_check_server() {@@ -822,7 +1235,7 @@ with open(meta_out, "w", encoding="utf-8") as fh:IFS=$'\t' read -r plan_id fail_count < "$WORK_DIR/plan_meta.tsv"[[ -n "$plan_id" ]] || die "Generated plan did not include a plan identifier."if (( fail_count > 0 )); then- die "Preflight checks failed; fix the issues above and rerun ./install.sh."+ die "Preflight checks failed; fix the issues above and rerun $SELF_CMD."fiprompt_line " Approve download and installation? [y/N]: "@@ -834,14 +1247,14 @@ with open(meta_out, "w", encoding="utf-8") as fh:fish "$STACK_SCRIPT" apply "$WORK_DIR/plan.json" --approve "$plan_id" ||- die "Local model installation did not finish; fix the issue above and rerun ./install.sh."+ die "Local model installation did not finish; fix the issue above and rerun $SELF_CMD."}step_connect_linger() {printf '\n[4/4] Connecting this computer to Linger\n'if (( CONNECT_LINGER == 0 )); thenprintf ' Local model is ready. When your Linger server is running, connect with:\n'- printf ' ./install.sh --connect --web-url http://localhost:3000\n'+ printf ' %s --connect --web-url http://localhost:3000\n' "$SELF_CMD"return 0firun_connect_step@@ -1004,7 +1417,7 @@ while (( $# > 0 )); doexit 0;;*)- die "Unknown option: $1 (run ./install.sh --help for usage)."+ die "Unknown option: $1 (run $SELF_CMD --help for usage).";;esacdone