#!/bin/sh # Kriya One installer for Linux, macOS and Windows (WSL2): one assistant in one container (docs/kriya-one/INSTALL-ONE.md). # curl -fsSL https://getkriya.in/install.sh | sh # curl -fsSL https://getkriya.in/install.sh | sh -s -- --yes --memory 6g --share ~/Documents:docs:ro # The downloads (this script, install.ps1, reach.sh and SHA256SUMS) come from the Kriya portal; KRIYA_ONE_DOWNLOAD= # uses a mirror (or a local copy) instead. # Windows (PowerShell, Docker Desktop): deploy/one/install.ps1. Needs Docker (Docker Desktop, Docker Engine, OrbStack, # Colima) or Podman. It asks (Enter keeps the default; --yes or KRIYA_ONE_YES=1: no questions) the name, the port, the # memory and CPU limits, where the data lives and a disk soft cap, the folders to share and the network mode (Linux); # remembers the answers in ~/Kriya//kriya-one.env (0600) for --upgrade; prints the setup URL (no setup code # on this machine; another device needs the code it prints) and opens it. Data: ~/Kriya//{data,home} and # ~/Kriya/shared. Without Docker it sets up # an engine, after printing what this machine needs and what is here (macOS: Colima, Lima and the docker CLI downloaded # into ~/Kriya/engine, checksum-verified: no Homebrew, no sudo, no question; Linux and WSL: rootless Docker there, and # if the machine lacks something only root can add, a script of those steps, ~/Kriya/setup-as-admin.sh, which the user # runs, or the installer runs with sudo after showing it: --with-sudo). # It also installs itself as the `kriya-one` command (~/.local/bin/kriya-one, added to PATH): the same script, which # run as `kriya-one ` manages the instance (start, stop, restart, status, logs, open, update, config, # rollback, uninstall, version, list; kriya-one help) and keeps itself up to date on `kriya-one update`. # The whole script is functions, called on the last line: a download cut short runs nothing. # shellcheck disable=SC2015,SC2088,SC2016 # a && b || c with an echo as b; "~/" is matched as typed, then expanded; # Go templates ({{…}} with $k, $v) are in single quotes on purpose set -eu usage() { cat <<'EOF' Kriya One installer. sh install.sh [options] (curl -fsSL …/install.sh | sh -s -- [options]) --name NAME the instance's name: container kriya-one-NAME, data ~/Kriya/NAME (default assistant). Several on one machine are fine: each is a separate person's private assistant, with its own data, sign-in, engine, memory, port, URL and app (docs/kriya-one/INSTALL-ONE.md → Several on one machine) --display-name TEXT how the app and its window are called, e.g. "Therapist" → "Kriya One · Therapist" (default: the name, capitalised; none for "assistant") --accent COLOUR a #rrggbb badge on the app's icon, so two installed apps tell apart (also --brand; default: one picked from the name; "none": no badge) --port PORT the panel on 127.0.0.1:PORT, its canvas on PORT+100 (default 8810, or the first free pair after it: the installer says which) --public-url URL the address other devices use, when it isn't the HTTPS one below (links in mails, the setup line); the container still uses http://localhost:PORT itself --image IMAGE default ghcr.io/trueskillacademy/kriya-one:latest (kriya-one:local after docker/build-one.sh) --memory SIZE memory limit, e.g. 4g or 6144m, or none (default half this machine's memory, 3g to 8g) --cpus N CPU limit, e.g. 2 or 1.5 (default all) --data DIR where the data lives (default ~/Kriya, or KRIYA_DIR) --disk GB a SOFT cap, not enforced: Kriya One warns in its settings above 90 % of it, and kriya-one status shows the use (default 50; Docker can't cap a bind-mounted folder) --share PATH[:NAME][:ro] a host folder the assistant sees at /shared/NAME (repeat it; :ro read-only). Only what you name: nothing is shared by default. /shared itself is this instance's own folder ~/Kriya/NAME/shared, never another instance's --network bridge|host bridge (default): the LAN works, this machine is closed except the ports you open in Settings; host (Linux only, advanced): the container shares this machine's network, no firewall --bind ADDRESS the panel's address (default 127.0.0.1: this machine only; 0.0.0.0 opens it, use HTTPS) --https tailscale|local[:ADDRESS] HTTPS for other devices (default: localhost only). tailscale: `tailscale serve` on PORT+10000 (panel) and PORT+10100 (canvas), or, when either is served for something else already, the first free pair H, H+100 from 18000: an existing serve entry is never changed. Uninstall removes only this instance's entries. local: Caddy on PORT+10000/+10100/+10001 (KRIYA_HTTPS_OFFSET changes 10000) --no-engine-setup no Docker here: stop with how to install it (default: set one up in ~/Kriya/engine, running as you: Colima on macOS, no Homebrew, no sudo; rootless Docker on Linux and WSL; also KRIYA_ONE_NO_ENGINE_SETUP=1) --check print what this machine needs and what is here (the requirements list), then exit; changes nothing --with-sudo Linux: when the machine needs one-time admin steps for that engine, run them here with sudo (after showing them) instead of stopping with the script ~/Kriya/setup-as-admin.sh to run yourself --engine apple macOS 26+ on Apple silicon: Apple's container tool instead of Colima (not supported yet) --desktop | --no-desktop ignored: Kriya One Desktop is retired; install the web UI as an app (PWA) instead --model-url URL a local model for the engine, no Claude or other cloud account needed: its base URL, an Anthropic-compatible (/v1/messages) server such as Ollama 0.14+, llama-server or vLLM, e.g. http://10.0.0.20:11434 (localhost: this machine; its port is opened in the firewall) --model NAME that model's name, e.g. qwen3-coder:30b (default: the first it lists). The setup page opens on Local model with them filled in; an API key, if it needs one, goes in there --no-open don't open the browser --no-shortcut no shortcut on the desktop and in the app list (default: one, "Kriya One" or "Kriya One (Therapist)", that opens it; it removes its desktop copy once the app is installed). Saved: an upgrade keeps it that way (also KRIYA_ONE_NO_SHORTCUT=1) --yes no questions: the defaults and the options given (also KRIYA_ONE_YES=1) --upgrade pull the image again and recreate the container with the saved options (the old one is kept, stopped, as kriya-one-NAME-old-) --rollback swap back to the newest kept container (run it again to undo) --uninstall remove the container, the kept ones, its network and its own tailscale serve entries (never the data: it says where it is) --adopt take over a Kriya One container made by hand (docker run): its settings (ports, memory, CPUs, mounts, image) are read into ~/Kriya/NAME/kriya-one.env, so kriya-one config/update/list work and a later recreate keeps them. Nothing is recreated now. With --name NAME. --no-share no shared folders (with --upgrade: drops the saved ones) Afterwards the kriya-one command does all of this: kriya-one help. Env: KRIYA_ONE_IMAGE, KRIYA_DIR, KRIYA_ONE_YES, KRIYA_ONE_VERIFY=1 (cosign verify the image), KRIYA_ONE_DOWNLOAD (the download base, default https://getkriya.in). EOF } # moved_image IMAGE: IMAGE at its new address when it is at one Kriya One moved from (same tag or digest), else fail. moved_image() { _new=${DEFAULT_IMAGE%:*} for _o in $MOVED_FROM_IMAGES; do [ "$_o" != "$_new" ] || continue case "$1" in "$_o":*|"$_o"@*) printf '%s%s\n' "$_new" "${1#"$_o"}"; return 0 ;; "$_o") printf '%s\n' "$_new"; return 0 ;; esac done return 1 } say() { printf '\033[1m%s\033[0m\n' "$*"; } note() { printf '%s\n' "$*"; } warn() { printf 'kriya-one: %s\n' "$*" >&2; } die() { printf 'kriya-one: %s\n' "$*" >&2; exit 1; } DEFAULT_IMAGE=ghcr.io/trueskillacademy/kriya-one:latest # Where Kriya One's images came from before (kept updated as a mirror): an install that saved one of these moves to # DEFAULT_IMAGE's repository, same tag, on its next update or reinstall (moved_image). Custom images are left alone. MOVED_FROM_IMAGES="ghcr.io/abhishekashokvkumar/kriya-one" # the mirror (release-identity.json "mirrors") # The kriya-one command's revision: kriya-one update takes the portal's copy when its revision is this one or newer. CLI_REV=3 CFGDIR="${XDG_CONFIG_HOME:-$HOME/.config}/kriya-one" # The public downloads (the Kriya portal): install.sh, install.ps1, reach.sh and SHA256SUMS. DOWNLOAD_BASE=${KRIYA_ONE_DOWNLOAD:-https://getkriya.in} DOWNLOAD_BASE=${DOWNLOAD_BASE%/} # Kriya One's own container engine, in the user's own folder, for a machine without one. macOS (no Homebrew, no # sudo): Colima (MIT), Lima (Apache-2.0) and the docker CLI (Apache-2.0). Linux and WSL: rootless Docker, Docker's # static binaries and rootless extras (Apache-2.0). Each a pinned release checked against its sha256 here. The pins # are the projects' own published checksums (Colima's colima-Darwin-.sha256sum, Lima's SHA256SUMS, Docker's # .sha256); bump them per release like the harness pins: node scripts/engine-pins.mjs (docs/RELEASING.md). # The mirrors below change where they come from, never what is accepted. COLIMA_VERSION=0.10.3 COLIMA_SHA256_ARM64=980ad8bf61a4ca370243f4cb41401a61276dcd2c2502bee7b9b86f9250169f34 COLIMA_SHA256_X86_64=3082737fe8a98afda11cba7d9a20b6e56fe80c6153464beda04bec630758770b LIMA_VERSION=2.3.0 LIMA_SHA256_ARM64=b7af444bd8f91d3c307477e562f2cf3bd7595d62b9429185e4fa841563578cfc LIMA_SHA256_X86_64=d58bc7d6b8158d638720d8c8fdb17eb58527dfead6a29a5b60f48a35ee4a65a6 DOCKER_CLI_VERSION=29.10.1 DOCKER_CLI_SHA256_ARM64=16bb7878c5bf13dd8c25575bcb0328aec7781200e3442e15ddaa20c14f276be9 DOCKER_CLI_SHA256_X86_64=8d8858a06dd5acf89aec39b8dd6754cc657648a8f50263adfef0971df63ab925 COLIMA_URL=${KRIYA_ONE_COLIMA_URL:-https://github.com/abiosoft/colima/releases/download} LIMA_URL=${KRIYA_ONE_LIMA_URL:-https://github.com/lima-vm/lima/releases/download} DOCKER_CLI_URL=${KRIYA_ONE_DOCKER_CLI_URL:-https://download.docker.com/mac/static/stable} DOCKER_LINUX_VERSION=29.10.1 DOCKER_LINUX_SHA256_X86_64=061642059fa7f2ed9b2f04a327a951cff4249585cf04b2661c2a6539197c9962 DOCKER_LINUX_SHA256_AARCH64=a38c8a9ef6d17bce07499bfd3a3688542274769dc3b4f75c06ea2705fe65053e DOCKER_EXTRAS_SHA256_X86_64=11eb5fa9a88e5971e71713aa277dbd916b7d368c6625844cacffa1c42b8df60c DOCKER_EXTRAS_SHA256_AARCH64=9b677eb3bb0ce21c4e6aae3e44678309250f1aa6fbc95f66a94de7b88b04e45b DOCKER_LINUX_URL=${KRIYA_ONE_DOCKER_LINUX_URL:-https://download.docker.com/linux/static/stable} # Where it lives: bin/ (macOS: colima, limactl, docker; Linux: dockerd, docker, rootlesskit, …), macOS: libexec/ and # share/ (Lima's), colima/ (Colima's home: its VM and the docker socket); Linux: docker/ (the data), run/ (the # socket); env.sh (for the user's own terminal) and VERSIONS. The admin steps a Linux machine may need are written # next to it, ~/Kriya/setup-as-admin.sh. KENG=${KRIYA_ONE_ENGINE_DIR:-$HOME/Kriya/engine} COSIGN_ID='^https://github\.com/trueskillacademy/kriya-one/\.github/workflows/release\.yml@refs/tags/v' # ---------------------------------------------------------------- questions: from the terminal, not stdin (stdin is # this script under curl | sh), or the default when there is no terminal or --yes TTY=0 ask() { # ask VAR "question" "default" _a="" if [ "$TTY" = 1 ]; then printf '%s [%s]: ' "$2" "$3" >/dev/tty IFS= read -r _a &1); then eval "$1=\$_out"; return 0; fi [ "$TTY" = 1 ] && [ $_n -lt 4 ] || die "$_out" printf ' %s\n' "$_out" >/dev/tty _n=$((_n + 1)) done } yesno() { # yesno "question" y|n → 0 for yes _yn="" ask _yn "$1" "$2" case "$_yn" in y|Y|yes|YES|Yes) return 0 ;; *) return 1 ;; esac } # ---------------------------------------------------------------- checks (print the normalised value) check_name() { echo "$1" | grep -Eq '^[a-z0-9][a-z0-9-]{0,30}$' && echo "$1" || { echo "--name: a-z, 0-9 and -, up to 31 characters"; return 1; }; } check_port() { echo "$1" | grep -Eq '^[0-9]{4,5}$' && [ "$1" -ge 1024 ] && [ "$1" -le 65000 ] && echo "$1" || { echo "--port: 1024-65000"; return 1; }; } check_memory() { case "$1" in none|NONE|0) echo none; return 0 ;; esac _m=$(echo "$1" | tr 'GMB' 'gmb' | sed 's/b$//') echo "$_m" | grep -Eq '^[0-9]{1,7}[gm]?$' || { echo "--memory: a size like 4g or 6144m"; return 1; } case "$_m" in *g) _mb=$(( ${_m%g} * 1024 )) ;; *m) _mb=${_m%m} ;; *) _mb=$(( _m * 1024 )); _m="${_m}g" ;; esac [ "$_mb" -ge 1024 ] || { echo "--memory: at least 1g (3g or more is comfortable)"; return 1; } echo "$_m" } check_cpus() { if [ -z "$1" ] || [ "$1" = all ]; then echo ""; return 0; fi echo "$1" | grep -Eq '^[0-9]{1,3}(\.[0-9]{1,2})?$' && echo "$1" || { echo "--cpus: a number like 2 or 1.5 (or all)"; return 1; } } check_disk() { echo "$1" | grep -Eq '^[0-9]{1,6}$' && [ "$1" -ge 1 ] && echo "$1" || { echo "--disk: whole GB, e.g. 50"; return 1; }; } check_network() { case "$1" in bridge|host) echo "$1" ;; *) echo "--network: bridge or host"; return 1 ;; esac; } # check_model_url URL → the URL (http:// added to host:port), or why not. check_model_url() { _u="$1" case "$_u" in http://*|https://*) ;; *) _u="http://$_u" ;; esac _u=${_u%/} echo "$_u" | grep -Eq '^https?://[A-Za-z0-9._-]+(:[0-9]{1,5})?(/[A-Za-z0-9._~/-]*)?$' && echo "$_u" || { echo "--model-url: an http:// or https:// address, e.g. http://10.0.0.20:11434"; return 1; } } check_model_name() { echo "$1" | grep -Eq '^[A-Za-z0-9][A-Za-z0-9._:/@+-]{0,127}$' && echo "$1" || { echo "--model: a model name like qwen3-coder:30b"; return 1; }; } # The host port to open for a model URL on this machine (localhost or one of its addresses), else nothing. model_host_port() { _h=$(echo "$1" | sed -E 's#^https?://##; s#[/].*$##') _p=${_h##*:}; [ "$_p" != "$_h" ] || { case "$1" in https://*) _p=443 ;; *) _p=80 ;; esac; } _h=${_h%:*} case " localhost 127.0.0.1 host.docker.internal $(host_ips) " in *" $_h "*) echo "$_p" ;; esac } check_bind() { echo "$1" | grep -Eq '^([0-9]{1,3}\.){3}[0-9]{1,3}$' && echo "$1" || { echo "--bind: an IPv4 address (127.0.0.1, or 0.0.0.0 for every interface)"; return 1; }; } check_display() { echo "$1" | grep -Eq "^[A-Za-z0-9][A-Za-z0-9 .'&_-]{0,29}\$" && echo "$1" || { echo "--display-name: letters, digits, spaces and . ' & _ -, up to 30 characters"; return 1; }; } check_accent() { case "$1" in none) echo none; return 0 ;; esac _c=$(echo "$1" | tr 'ABCDEF' 'abcdef'); case "$_c" in \#*) ;; *) _c="#$_c" ;; esac echo "$_c" | grep -Eq '^#[0-9a-f]{6}$' && echo "$_c" || { echo "--accent: a colour like #2f9e44 (or none)"; return 1; } } check_public_url() { _u=${1%/}; echo "$_u" | grep -Eq '^https?://[A-Za-z0-9._-]+(:[0-9]{1,5})?$' && echo "$_u" || { echo "--public-url: like https://kriya.example.com or https://box.tailnet.ts.net:18850 (no path)"; return 1; }; } # The default display name: the name, capitalised ("therapist" → "Therapist"); none for "assistant". default_display() { [ "$1" = assistant ] || echo "$1" | awk '{ print toupper(substr($0, 1, 1)) substr($0, 2) }'; } # A badge colour picked from the name (the same name, the same colour): eight that read on light and dark. default_accent() { [ "$1" != assistant ] || { echo none; return 0; } _sum=$(printf '%s' "$1" | od -An -tu1 | tr -s ' \n' ' ' | awk '{ s = 0; for (i = 1; i <= NF; i++) s += $i; print s % 8 }') set -- '#2f9e44' '#1971c2' '#e8590c' '#9c36b5' '#c2255c' '#0c8599' '#f08c00' '#5f3dc4' shift "$_sum"; echo "$1" } expand() { case "$1" in "~") echo "$HOME" ;; "~/"*) echo "$HOME/${1#\~/}" ;; *) echo "$1" ;; esac; } realdir() { (cd "$1" 2>/dev/null && pwd -P); } # check_share SPEC → "path:name:ro|rw", refusing /, the home folder, the data folder (and what holds it), what isn't # there, and names already taken (SHARES). check_share() { _s="$1" _mode=rw case "$_s" in *:ro) _mode=ro; _s=${_s%:ro} ;; *:rw) _s=${_s%:rw} ;; esac _p="$_s" _nm="" case "$_s" in *:*) _p=${_s%:*}; _nm=${_s##*:} ;; esac _p=$(expand "$_p") case "$_p" in *'|'*|*:*) echo "--share $1: a path with | or : can't be shared"; return 1 ;; esac [ -d "$_p" ] || { echo "--share $1: no folder $_p"; return 1; } _r=$(realdir "$_p") || { echo "--share $1: can't open $_p"; return 1; } _home=$(realdir "$HOME" || echo "$HOME") _k=$(realdir "$KDIR" || echo "$KDIR") [ "$_r" != / ] || { echo "--share /: not the whole machine; share the folders you mean"; return 1; } [ "$_r" != "$_home" ] || { echo "--share $1: not your whole home folder; share the folders you mean"; return 1; } case "$_r/" in "$_k/"*) echo "--share $1: that is Kriya's own data folder ($KDIR)"; return 1 ;; esac case "$_k/" in "$_r/"*) echo "--share $1: that holds Kriya's own data folder ($KDIR)"; return 1 ;; esac [ -n "$_nm" ] || _nm=$(basename "$_r" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9._-]/-/g; s/^[^a-z0-9]*//' | cut -c1-63) echo "$_nm" | grep -Eq '^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$' || { echo "--share $1: the name (after the :) is letters, digits, . _ -"; return 1; } _old_ifs=$IFS; IFS='|' for _e in $SHARES; do [ "$(echo "$_e" | cut -d: -f2)" != "$_nm" ] || { IFS=$_old_ifs; echo "--share $1: /shared/$_nm is taken (give it another name: PATH:NAME)"; return 1; }; done IFS=$_old_ifs echo "$_r:$_nm:$_mode" } add_share() { _ok=$(check_share "$1") || die "$_ok"; SHARES="${SHARES:+$SHARES|}$_ok"; } # ---------------------------------------------------------------- the machine find_engine() { ENGINE="" engine_env # An install first says what this machine needs and what is here (the requirements), before it changes anything. [ "${ACTION:-}" != install ] || [ "${KRIYA_ONE_QUIET_ENGINE:-0}" = 1 ] || req_report if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then ENGINE=docker elif command -v podman >/dev/null 2>&1; then ENGINE=podman elif own_engine_ready; then own_engine_start elif mac_desktop_start; then ENGINE=docker elif [ "${ACTION:-}" = install ] && [ "${ENGINE_SETUP:-0}" = 1 ] && engine_setup_os; then [ -z "${MAC_DESKTOP_FAILED:-}" ] || note "${MAC_DESKTOP_FAILED}, so Kriya One sets up its own engine (Colima) instead." setup_engine elif [ -n "${MAC_DESKTOP_FAILED:-}" ]; then die "${MAC_DESKTOP_FAILED}: open it yourself, wait until it says it is running, then run this again." elif command -v docker >/dev/null 2>&1; then die "Docker is installed but doesn't answer you (start Docker Desktop, or: colima start; on Linux it may be stopped, or you're not in the docker group). Or run the Kriya One installer: it sets up its own engine that runs as you." else case "$(uname -s)" in Darwin) die "needs Docker or Podman: install Docker Desktop (https://www.docker.com/products/docker-desktop/) or OrbStack, or run this again without --no-engine-setup: it downloads Colima into $KENG (no admin password)" ;; Linux) die "needs Docker or Podman: run this again without --no-engine-setup (it sets up rootless Docker in $KENG, running as you), or install Docker or your distro's podman package yourself" ;; *) die "unsupported system $(uname -s): use Linux, macOS or Windows (install.cmd, install.ps1, or WSL2)" ;; esac fi } # ---------------------------------------------------------------- the requirements: what this machine needs, what is here # A plain list, printed before an install changes anything. macOS: everything Kriya One needs is the user's own (Colima # in ~/Kriya/engine), so no admin step ever. Linux (WSL too): an engine that works is used; else Kriya One's own rootless # Docker in ~/Kriya/engine, which needs newuidmap/newgidmap, subordinate IDs and user namespaces: what is missing of # those is the only admin work (REQ_ADMIN), done by a script the user sees first (setup-as-admin.sh). # KRIYA_ONE_SYSROOT (tests only) is put in front of /proc, /sys and /etc where it looks. SYSROOT=${KRIYA_ONE_SYSROOT:-} req_line() { printf ' %-30s %s\n' "$1:" "$2"; } req_report() { say "What Kriya One needs on this machine, and what is here:" req_line "Container engine" "$(engine_state)" case "$(uname -s)" in Darwin) _mv=$(sw_vers -productVersion 2>/dev/null || echo "?") if [ "${_mv%%.*}" -ge 13 ] 2>/dev/null; then req_line "macOS 13+ (for Colima's VM)" "macOS $_mv, $(uname -m): ok" else req_line "macOS 13+ (for Colima's VM)" "macOS $_mv: older (QEMU, if installed; else update macOS)"; fi req_line "Admin password" "not needed: Kriya One's files go into $KENG" req_line "Homebrew" "not needed, not used" ;; Linux) engine_works && { req_line "Admin steps" "none"; return 0; } linux_req_check req_line "newuidmap, newgidmap (uidmap)" "$REQ_UIDMAP" req_line "subordinate IDs (/etc/subuid)" "$REQ_SUBID" req_line "user namespaces" "$REQ_USERNS" [ -z "$REQ_GROUP" ] || req_line "Docker group" "$REQ_GROUP" req_line "memory/CPU limits" "$REQ_CGROUP" req_line "systemd user session" "$REQ_SYSTEMD" if [ -z "$REQ_ADMIN" ]; then req_line "Admin steps" "none: everything runs as you" elif [ "${ENGINE_SETUP:-0}" != 1 ]; then req_line "Admin steps" "$(echo "$REQ_ADMIN" | wc -w | tr -d ' ') for Kriya One's own engine (not written: --no-engine-setup)" else req_line "Admin steps" "$(echo "$REQ_ADMIN" | wc -w | tr -d ' ') (written to a script you see first, below)"; fi ;; esac } engine_works() { { command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; } || command -v podman >/dev/null 2>&1; } # engine_state: the container engine line: what is here, and what Kriya One does about it. engine_state() { if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then echo "Docker $(docker version --format '{{.Server.Version}}' 2>/dev/null || true), running: used as it is"; return; fi if command -v podman >/dev/null 2>&1; then echo "Podman: used as it is"; return; fi if own_engine_ready; then echo "Kriya One's own, in $KENG, stopped: will start it"; return; fi [ "$(uname -s)" != Darwin ] || [ -z "$(mac_desktop_app)" ] || { echo "$(basename "$(mac_desktop_app)" .app) is installed but not running: will start it (and wait up to ${KRIYA_ONE_DESKTOP_WAIT:-90} s)"; return; } _how="missing" if command -v docker >/dev/null 2>&1; then case "$(docker info 2>&1 >/dev/null)" in *ermission\ denied*) _how="Docker is installed, but you can't use it (you're not in the docker group)" ;; *) _how="Docker is installed but doesn't answer (stopped?)" ;; esac fi if [ "${ENGINE_SETUP:-0}" != 1 ]; then echo "$_how (--no-engine-setup: nothing is set up)" elif [ "$(uname -s)" = Darwin ]; then echo "$_how: Kriya One installs Colima into $KENG (runs as you)" else echo "$_how: Kriya One sets up rootless Docker in $KENG (runs as you)"; fi } # linux_req_check: REQ_UIDMAP, REQ_SUBID, REQ_USERNS, REQ_GROUP, REQ_CGROUP, REQ_SYSTEMD (the words) and REQ_ADMIN (the # admin steps still needed: uidmap subid userns-clone max-userns apparmor). linux_req_check() { REQ_ADMIN="" REQ_GROUP="" _u=$(id -un) _id=$(id -u) if command -v newuidmap >/dev/null 2>&1 && command -v newgidmap >/dev/null 2>&1; then REQ_UIDMAP="here" else REQ_UIDMAP="missing (admin step: install uidmap)"; REQ_ADMIN="$REQ_ADMIN uidmap"; fi if grep -qs -e "^$_u:" -e "^$_id:" "$SYSROOT/etc/subuid" && grep -qs -e "^$_u:" -e "^$_id:" "$SYSROOT/etc/subgid"; then REQ_SUBID="here for $_u" else REQ_SUBID="missing for $_u (admin step: add 65536 of each)"; REQ_ADMIN="$REQ_ADMIN subid"; fi REQ_USERNS="allowed" if [ "$(cat "$SYSROOT/proc/sys/kernel/unprivileged_userns_clone" 2>/dev/null || echo 1)" = 0 ]; then REQ_USERNS="off (kernel.unprivileged_userns_clone=0; admin step)"; REQ_ADMIN="$REQ_ADMIN userns-clone" fi if [ "$(cat "$SYSROOT/proc/sys/user/max_user_namespaces" 2>/dev/null || echo 1)" = 0 ]; then REQ_USERNS="off (user.max_user_namespaces=0; admin step)"; REQ_ADMIN="$REQ_ADMIN max-userns" fi if [ "$(cat "$SYSROOT/proc/sys/kernel/apparmor_restrict_unprivileged_userns" 2>/dev/null || echo 0)" = 1 ] \ && ! grep -qsF "\"$KENG/bin/rootlesskit\"" "$SYSROOT/etc/apparmor.d/kriya-one-rootlesskit"; then REQ_USERNS="blocked by AppArmor (admin step: allow them for Kriya One's rootlesskit only)"; REQ_ADMIN="$REQ_ADMIN apparmor" fi if command -v docker >/dev/null 2>&1 && ! [ -x "$KENG/bin/dockerd-rootless.sh" ]; then case "$(docker info 2>&1 >/dev/null)" in *ermission\ denied*) REQ_GROUP="you're not in it (not needed: Kriya One runs its own engine as you)" ;; esac fi _cg="$SYSROOT/sys/fs/cgroup/user.slice/user-$_id.slice/user@$_id.service/cgroup.controllers" if grep -qsw memory "$_cg" && grep -qsw cpu "$_cg"; then REQ_CGROUP="enforced (cgroup v2, delegated)" else REQ_CGROUP="not enforced by a rootless engine here (it runs anyway; docs: cgroup delegation)"; fi if systemctl --user show-environment >/dev/null 2>&1; then REQ_SYSTEMD="yes: the engine is a user service" else REQ_SYSTEMD="no: kriya-one start starts the engine"; fi } # ---------------------------------------------------------------- the engine, set up for someone who has none # Only on an install, after the requirements list; --no-engine-setup (or KRIYA_ONE_NO_ENGINE_SETUP=1) stops with the # "needs Docker" message instead. Never Homebrew; sudo only on Linux, only for the steps in setup-as-admin.sh, only # after showing them, and only when the user picks it (b, or --with-sudo). --engine apple (Apple's `container`, macOS # 26+ on Apple silicon) is not supported yet: it says so. engine_setup_os() { case "$(uname -s)" in Darwin|Linux) return 0 ;; *) return 1 ;; esac } setup_engine() { case "$(uname -s)" in Linux) setup_linux_engine ;; Darwin) setup_mac_engine ;; esac } # engine_fetch URL SHA256 FILE: URL into FILE, kept only when its sha256 is SHA256 (the pin); else removed, and why. engine_fetch() { note " $1" curl -fsSL --retry 2 -m 900 -o "$3" "$1" || { rm -f "$3"; warn "couldn't download $1"; return 1; } _got=$(sha256 "$3") [ "$_got" = "$2" ] || { rm -f "$3"; warn "$(basename "$1") doesn't match its pinned checksum (sha256 $_got, pinned $2): refused, nothing installed"; return 1; } } # engine_dir_ok: $KENG made and writable (it needs no admin rights). engine_dir_ok() { { mkdir -p "$KENG" && [ -w "$KENG" ]; } 2>/dev/null || { warn "can't write $KENG (it needs no admin rights; is the folder yours? ls -ld \"$KENG\")"; return 1; }; } # engine_envfile LINE: $KENG/env.sh, for the user's own terminal (never added to a shell startup file by Kriya One). engine_envfile() { _ef=$(sq "$KENG/env.sh") { printf '# Kriya One'"'"'s container engine in this shell: . %s (made by the Kriya One installer)\n' "$_ef" printf '%s\n' "$1" } >"$KENG/env.sh" } engine_wait() { _w=0; while ! docker info >/dev/null 2>&1; do [ "$_w" -lt "${1:-30}" ] || return 1; sleep 1; _w=$((_w + 1)); done; } # engine_env: Kriya One's own engine, when it is here: its bin first on PATH (macOS: its Colima home too), and its socket # as DOCKER_HOST unless one is set. For this run only (the installer, kriya-one, the shortcuts' launchers); shell # startup files are never changed. engine_env() { case "$(uname -s)" in Darwin) [ -x "$KENG/bin/colima" ] || return 0; COLIMA_HOME="$KENG/colima"; export COLIMA_HOME; _sock="$KENG/colima/default/docker.sock" ;; Linux) [ -x "$KENG/bin/dockerd-rootless.sh" ] || return 0; _sock="$KENG/run/docker.sock" ;; *) return 0 ;; esac case ":$PATH:" in *":$KENG/bin:"*) ;; *) PATH="$KENG/bin:$PATH" ;; esac DOCKER_HOST=${DOCKER_HOST:-unix://$_sock} export PATH DOCKER_HOST } # own_engine_ready: Kriya One's own engine has been started here before (stopped now, or docker would have answered). own_engine_ready() { case "$(uname -s)" in Darwin) [ -x "$KENG/bin/colima" ] && [ -d "$KENG/colima" ] ;; Linux) [ -x "$KENG/bin/dockerd-rootless.sh" ] && [ -d "$KENG/docker" ] ;; *) return 1 ;; esac } # own_engine_start: start it again as it was set up (after a restart, say), then wait for docker. own_engine_start() { [ "${KRIYA_ONE_QUIET_ENGINE:-0}" = 1 ] || printf '%s\n' "Starting Kriya One's container engine (in $KENG)…" >&2 case "$(uname -s)" in Darwin) colima start >&2 || die "Colima didn't start (above). In Terminal: . \"$KENG/env.sh\" && colima start" engine_wait || die "Colima is running but docker can't reach it at $DOCKER_HOST: in Terminal, . \"$KENG/env.sh\" && colima status" ;; Linux) linux_engine_start || die "Kriya One's engine didn't start: see $KENG/dockerd.log (or: journalctl --user -u kriya-one-docker)" ;; esac ENGINE=docker } # engine_offer_remove: after an uninstall, once no Kriya One container is left in it, Kriya One's own engine is # offered for removal: in a terminal it asks (default no); else (--yes, no terminal) it stays and it says how. engine_offer_remove() { case "$(uname -s)" in Darwin) [ -x "$KENG/bin/colima" ] || return 0 _rm=". \"$KENG/env.sh\" && colima delete --force && rm -rf \"$KENG\"" ;; Linux) [ -x "$KENG/bin/dockerd-rootless.sh" ] || return 0 _rm="systemctl --user disable --now kriya-one-docker; \"$KENG/bin/rootlesskit\" rm -rf \"$KENG/docker\"; rm -rf \"$KENG\"" ;; *) return 0 ;; esac _left=$($ENGINE ps -a --format '{{.Names}}' 2>/dev/null | grep -E '^kriya-one-' | tr '\n' ' ' | sed 's/ $//' || true) if [ -n "$_left" ]; then note "Kriya One's container engine stays in $KENG: $_left still use it."; return 0; fi if [ "$YES" != 1 ] && (: /dev/null; then TTY=1 if yesno "Also remove Kriya One's container engine, $KENG (its VM or data and anything else in it)? (y/n)" n; then if [ "$(uname -s)" = Darwin ]; then colima delete --force >/dev/null 2>&1 || colima stop >/dev/null 2>&1 || true else linux_engine_stop # Files the containers made belong to subordinate IDs: removed from inside the engine's user namespace. "$KENG/bin/rootlesskit" rm -rf "$KENG/docker" >/dev/null 2>&1 || true fi if rm -rf "${KENG:?}" 2>/dev/null; then note "removed $KENG"; else warn "couldn't remove all of $KENG: $_rm"; fi return 0 fi fi note "Kriya One's container engine stays in $KENG. To remove it: $_rm" } # ---------------------------------------------------------------- macOS: Kriya One's own engine in ~/Kriya/engine # Colima runs a small Linux VM as the user (Apple's Virtualization on macOS 13+), Lima (limactl, and its guest agent # in share/lima) underneath, the docker CLI talks to it through Colima's socket. Nothing here runs sudo or Homebrew, # and nothing asks: whatever would need an admin stops with a message instead. curl doesn't set the quarantine # attribute, so Gatekeeper doesn't stop the downloaded binaries; signed, notarised builds would still be better # (docs/kriya-one/INSTALL-ONE.md). # mac_desktop_app: Docker Desktop's or OrbStack's app, when one is installed (else nothing). mac_desktop_app() { for _a in /Applications/Docker.app /Applications/OrbStack.app "$HOME/Applications/Docker.app" "$HOME/Applications/OrbStack.app"; do [ -d "$_a" ] && { echo "$_a"; return 0; } done } # mac_desktop_start: Docker Desktop or OrbStack installed but not running: opened, and docker waited for, at most # KRIYA_ONE_DESKTOP_WAIT seconds (90). Every line on stdout, in order: what it does, then whether it came up. When it # doesn't, MAC_DESKTOP_FAILED says so plainly for the caller (the fallback, or the error). mac_desktop_start() { MAC_DESKTOP_FAILED="" [ "$(uname -s)" = Darwin ] || return 1 _app=$(mac_desktop_app); [ -n "$_app" ] || return 1 _dn=$(basename "$_app" .app); _dw=${KRIYA_ONE_DESKTOP_WAIT:-90} note "${_dn} is installed but not running: starting it and waiting for it (up to ${_dw} s)…" # A failed open is said, but still waited for: the app may come up anyway (it was starting already, say). _oe=$(open -a "$_app" 2>&1 >/dev/null) || note "macOS didn't open ${_dn}${_oe:+ ($_oe)}; waiting in case it starts anyway…" PATH="$PATH:$HOME/.docker/bin:/usr/local/bin:$HOME/.orbstack/bin:$_app/Contents/Resources/bin"; export PATH _w=0 while ! docker info >/dev/null 2>&1; do if [ "$_w" -ge "$_dw" ]; then MAC_DESKTOP_FAILED="${_dn} didn't start within ${_dw} s"; return 1; fi sleep 1; _w=$((_w + 1)) [ $((_w % 15)) != 0 ] || note " still waiting for ${_dn} (${_w} s)…" done note "${_dn} is running." } setup_mac_engine() { [ "$ENGINE_KIND" != apple ] || die "--engine apple (Apple's container tool) isn't supported yet. Leave it out to use Colima." # Sized from the answers: the container's memory limit + 2 GB for the VM, its CPUs (all: the Mac's), its disk cap + 20 GB. if [ -z "$MEMORY" ]; then ask_valid MEMORY check_memory "Memory limit" "$(default_memory)"; else MEMORY=$(check_memory "$MEMORY") || die "$MEMORY"; fi if [ -z "$CPUS_SET" ]; then ask_valid CPUS check_cpus "CPU limit (a number, or all)" all; else CPUS=$(check_cpus "$CPUS") || die "$CPUS"; fi CPUS_SET=1 if [ -z "$DISK" ]; then ask_valid DISK check_disk "Disk soft cap in GB (Kriya One warns above 90 %)" 50; else DISK=$(check_disk "$DISK") || die "$DISK"; fi case "$MEMORY" in *g) _vm=${MEMORY%g} ;; *m) _vm=$(( (${MEMORY%m} + 1023) / 1024 )) ;; *) _vm=4 ;; esac _vm=$(( _vm + 2 )) _vc=${CPUS:-$(sysctl -n hw.ncpu 2>/dev/null || echo 4)}; _vc=${_vc%%.*}; [ "${_vc:-0}" -ge 1 ] || _vc=1 _vd=$(( DISK + 20 )) _vt=$(mac_vm_type) || die "$_vt" # No question: the founder's rule for macOS (no sudo, no Homebrew, no prompt). It says what it does and does it. say "Docker isn't installed, so Kriya One is installing Colima (a free Docker equivalent that runs as you, no admin password needed) into ${KENG}…" note "(its VM: $_vm GB of memory, $_vc CPUs, a $_vd GB disk)" mac_engine_install || die "the container engine isn't installed (above); nothing changed outside $KENG. Fix that, or install Docker Desktop or OrbStack yourself, then run this again." engine_env DOCKER_HOST="unix://$KENG/colima/default/docker.sock"; export DOCKER_HOST note "Starting Colima ($_vt; its first start downloads its Linux image, a few minutes)…" colima start --vm-type "$_vt" --runtime docker --cpu "$_vc" --memory "$_vm" --disk "$_vd" \ || die "Colima didn't start (above). In Terminal: . \"$KENG/env.sh\" && colima start --vm-type $_vt --cpu $_vc --memory $_vm --disk $_vd" engine_wait || die "Colima is running but docker can't reach it at $DOCKER_HOST: in Terminal, . \"$KENG/env.sh\" && colima status" ENGINE=docker note "Colima is running, from $KENG (Kriya One starts it again when it needs it: kriya-one start)." note "To use docker and colima in your own Terminal: . \"$KENG/env.sh\" (add that line to ~/.zshrc to keep it)" } # mac_vm_type: vz (Apple's Virtualization, macOS 13 or newer), else qemu when QEMU is installed already, else why not. mac_vm_type() { _mv=$(sw_vers -productVersion 2>/dev/null || echo 0) if [ "${_mv%%.*}" -ge 13 ] 2>/dev/null; then echo vz; return 0; fi case "$(uname -m)" in arm64|aarch64) _qa=aarch64 ;; *) _qa=x86_64 ;; esac if command -v qemu-img >/dev/null 2>&1 && command -v "qemu-system-$_qa" >/dev/null 2>&1; then echo qemu; return 0; fi echo "Kriya One's engine (Colima) needs macOS 13 or newer for Apple's Virtualization (this Mac has $_mv), or QEMU, which isn't installed. Update macOS (System Settings → General → Software Update), or install Docker Desktop or OrbStack yourself, then run this again." return 1 } # mac_engine_install: Colima, Lima and the docker CLI for this Mac's processor into $KENG (bin, libexec, share). All # three are downloaded and checked before any is put in place; the set that is there already, with these pins, stays. mac_engine_install() { case "$(uname -m)" in arm64|aarch64) _ca=arm64 _da=aarch64 _cs=$COLIMA_SHA256_ARM64 _ls=$LIMA_SHA256_ARM64 _ds=$DOCKER_CLI_SHA256_ARM64 ;; x86_64) _ca=x86_64 _da=x86_64 _cs=$COLIMA_SHA256_X86_64 _ls=$LIMA_SHA256_X86_64 _ds=$DOCKER_CLI_SHA256_X86_64 ;; *) warn "no Colima for this Mac's processor ($(uname -m))"; return 1 ;; esac _pins=$(printf 'colima %s\nlima %s\ndocker-cli %s' "$COLIMA_VERSION" "$LIMA_VERSION" "$DOCKER_CLI_VERSION") if [ -x "$KENG/bin/colima" ] && [ -x "$KENG/bin/limactl" ] && [ -x "$KENG/bin/docker" ] && [ "$(cat "$KENG/VERSIONS" 2>/dev/null)" = "$_pins" ]; then return 0; fi engine_dir_ok || return 1 _st="$KENG/.new.$$" rm -rf "$_st"; mkdir -p "$_st/dl" "$_st/root/bin" || return 1 note "Downloading Colima $COLIMA_VERSION, Lima $LIMA_VERSION and the docker CLI $DOCKER_CLI_VERSION ($_ca), each checked against its pinned sha256:" if engine_fetch "$COLIMA_URL/v$COLIMA_VERSION/colima-Darwin-$_ca" "$_cs" "$_st/dl/colima" \ && engine_fetch "$LIMA_URL/v$LIMA_VERSION/lima-$LIMA_VERSION-Darwin-$_ca.tar.gz" "$_ls" "$_st/dl/lima.tar.gz" \ && engine_fetch "$DOCKER_CLI_URL/$_da/docker-cli_$DOCKER_CLI_VERSION.tgz" "$_ds" "$_st/dl/docker.tgz" \ && tar -xzf "$_st/dl/lima.tar.gz" -C "$_st/root" \ && tar -xzf "$_st/dl/docker.tgz" -C "$_st/root" bin/docker \ && cp "$_st/dl/colima" "$_st/root/bin/colima" && chmod 755 "$_st/root/bin/colima" "$_st/root/bin/docker" \ && [ -x "$_st/root/bin/limactl" ]; then : else rm -rf "$_st"; return 1 fi for _d in bin libexec share; do rm -rf "${KENG:?}/$_d" [ ! -d "$_st/root/$_d" ] || mv "$_st/root/$_d" "$KENG/$_d" || { rm -rf "$_st"; return 1; } done rm -rf "$_st" printf '%s\n' "$_pins" >"$KENG/VERSIONS" engine_envfile "$(printf 'export PATH=%s:"$PATH" COLIMA_HOME=%s DOCKER_HOST=%s' "$(sq "$KENG/bin")" "$(sq "$KENG/colima")" "$(sq "unix://$KENG/colima/default/docker.sock")")" note "Installed in $KENG/bin: colima, limactl and docker." } # ---------------------------------------------------------------- Linux (WSL too): Kriya One's own rootless Docker # Docker's static binaries and its rootless extras (rootlesskit, dockerd-rootless.sh) in ~/Kriya/engine/bin, the # daemon running as the user (its data in ~/Kriya/engine/docker, its socket ~/Kriya/engine/run/docker.sock), as a # systemd user service when there is a user session, else in the background (kriya-one start starts it again). setup_linux_engine() { linux_req_check [ -z "$REQ_ADMIN" ] || linux_admin_steps say "No Docker here that works for you, so Kriya One is setting up its own: rootless Docker $DOCKER_LINUX_VERSION in $KENG (runs as you, no sudo)…" linux_engine_install || die "the container engine isn't installed (above); nothing changed outside $KENG. Fix that, or install Docker yourself, then run this again." engine_env DOCKER_HOST="unix://$KENG/run/docker.sock"; export DOCKER_HOST linux_engine_start || die "Kriya One's engine didn't start (above; its log: $KENG/dockerd.log or journalctl --user -u kriya-one-docker)" ENGINE=docker note "Kriya One's engine is running, from $KENG (kriya-one start starts it again when it needs it)." note "To use docker in your own terminal: . \"$KENG/env.sh\"" } # linux_engine_install: Docker's static binaries and rootless extras for this processor into $KENG/bin, both checked # before either is put in place; the set that is there already, with these pins, stays. linux_engine_install() { case "$(uname -m)" in x86_64|amd64) _la=x86_64 _ds=$DOCKER_LINUX_SHA256_X86_64 _xs=$DOCKER_EXTRAS_SHA256_X86_64 ;; aarch64|arm64) _la=aarch64 _ds=$DOCKER_LINUX_SHA256_AARCH64 _xs=$DOCKER_EXTRAS_SHA256_AARCH64 ;; *) warn "no rootless Docker for this processor ($(uname -m))"; return 1 ;; esac _pins="docker-rootless $DOCKER_LINUX_VERSION" if [ -x "$KENG/bin/dockerd" ] && [ -x "$KENG/bin/dockerd-rootless.sh" ] && [ "$(cat "$KENG/VERSIONS" 2>/dev/null)" = "$_pins" ]; then return 0; fi engine_dir_ok || return 1 _st="$KENG/.new.$$" rm -rf "$_st"; mkdir -p "$_st/dl" "$_st/bin" || return 1 note "Downloading Docker $DOCKER_LINUX_VERSION and its rootless extras ($_la), each checked against its pinned sha256:" if engine_fetch "$DOCKER_LINUX_URL/$_la/docker-$DOCKER_LINUX_VERSION.tgz" "$_ds" "$_st/dl/docker.tgz" \ && engine_fetch "$DOCKER_LINUX_URL/$_la/docker-rootless-extras-$DOCKER_LINUX_VERSION.tgz" "$_xs" "$_st/dl/extras.tgz" \ && tar -xzf "$_st/dl/docker.tgz" -C "$_st/bin" --strip-components 1 \ && tar -xzf "$_st/dl/extras.tgz" -C "$_st/bin" --strip-components 1 \ && [ -x "$_st/bin/dockerd" ] && [ -x "$_st/bin/dockerd-rootless.sh" ] && [ -x "$_st/bin/rootlesskit" ]; then : else rm -rf "$_st"; return 1 fi rm -rf "${KENG:?}/bin" && mv "$_st/bin" "$KENG/bin" || { rm -rf "$_st"; return 1; } rm -rf "$_st" printf '%s\n' "$_pins" >"$KENG/VERSIONS" engine_envfile "$(printf 'export PATH=%s:"$PATH" DOCKER_HOST=%s' "$(sq "$KENG/bin")" "$(sq "unix://$KENG/run/docker.sock")")" note "Installed in $KENG/bin: dockerd, docker, containerd, rootlesskit and dockerd-rootless.sh." } # linux_engine_start: the daemon as the user: a systemd user service (kriya-one-docker) when there is a user session, # else in the background with its pid in $KENG/run/dockerd.pid. Then docker is waited for (60 s). linux_engine_start() { mkdir -p "$KENG/run" && chmod 700 "$KENG/run" || return 1 if systemctl --user show-environment >/dev/null 2>&1; then _ud="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user" mkdir -p "$_ud" || return 1 linux_engine_unit >"$_ud/kriya-one-docker.service.tmp" && mv -f "$_ud/kriya-one-docker.service.tmp" "$_ud/kriya-one-docker.service" || return 1 systemctl --user daemon-reload >/dev/null 2>&1 || true systemctl --user enable kriya-one-docker.service >/dev/null 2>&1 || true systemctl --user restart kriya-one-docker.service || return 1 else nohup env PATH="$KENG/bin:$PATH" XDG_RUNTIME_DIR="$KENG/run" "$KENG/bin/dockerd-rootless.sh" --data-root "$KENG/docker" \ >"$KENG/dockerd.log" 2>&1 "$KENG/run/dockerd.pid" fi engine_wait 60 || { warn "the engine didn't answer at $DOCKER_HOST"; tail -n 15 "$KENG/dockerd.log" 2>/dev/null >&2 || true; return 1; } } linux_engine_stop() { systemctl --user disable --now kriya-one-docker.service >/dev/null 2>&1 || true rm -f "${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user/kriya-one-docker.service" [ ! -f "$KENG/run/dockerd.pid" ] || kill "$(cat "$KENG/run/dockerd.pid")" >/dev/null 2>&1 || true } # The user service, as Docker's own rootless setup tool writes it (dockerd-rootless-setuptool.sh), in $KENG. linux_engine_unit() { cat </dev/null 2>&1 || die "sudo isn't here: run $1 as root yourself (su -c 'sh $1'), then this installer again" say "These are the commands it runs with sudo ($1):" sed 's/^/ /' "$1" if [ "$TTY" = 1 ] && [ "$YES" != 1 ]; then yesno "Run them now with sudo? (y/n)" y || die "nothing ran. Run it yourself (sudo sh $1), then this installer again."; fi sudo sh "$1" || die "the admin steps failed (above): fix that, run sudo sh $1 again, then this installer" } # admin_script_write PATH: the root steps for REQ_ADMIN, each checked first (idempotent), each saying what and why. admin_script_write() { _u=$(id -un) _n=0 { cat <&2; exit 1; } EOF for _k in $REQ_ADMIN; do _n=$((_n + 1)) echo case "$_k" in uidmap) echo "# $_n. newuidmap and newgidmap: they map $_u's subordinate IDs (step below) into the engine's user namespace." if command -v apt-get >/dev/null 2>&1; then echo 'command -v newuidmap >/dev/null 2>&1 || apt-get install -y uidmap || { apt-get update && apt-get install -y uidmap; }' elif command -v dnf >/dev/null 2>&1; then echo 'command -v newuidmap >/dev/null 2>&1 || dnf install -y shadow-utils' elif command -v yum >/dev/null 2>&1; then echo 'command -v newuidmap >/dev/null 2>&1 || yum install -y shadow-utils' elif command -v zypper >/dev/null 2>&1; then echo 'command -v newuidmap >/dev/null 2>&1 || zypper --non-interactive install shadow' elif command -v pacman >/dev/null 2>&1; then echo 'command -v newuidmap >/dev/null 2>&1 || pacman -S --noconfirm shadow' elif command -v apk >/dev/null 2>&1; then echo 'command -v newuidmap >/dev/null 2>&1 || apk add shadow-uidmap' else echo 'command -v newuidmap >/dev/null 2>&1 || { echo "install newuidmap/newgidmap (often the package uidmap or shadow) with your package manager" >&2; exit 1; }'; fi ;; subid) _s=$(cat "$SYSROOT/etc/subuid" "$SYSROOT/etc/subgid" 2>/dev/null | awk -F: 'NF >= 3 && $2 ~ /^[0-9]+$/ { e = $2 + $3; if (e > m) m = e } END { print (m > 100000) ? m : 100000 }') echo "# $_n. Subordinate user and group IDs for $_u: 65536 of each, from $_s (after every range already given out)." echo "# The engine's containers run as these IDs, never as root or as another user." printf "grep -q '^%s:' %s 2>/dev/null || echo '%s:%s:65536' >> %s\n" "$_u" "$SYSROOT/etc/subuid" "$_u" "$_s" "$SYSROOT/etc/subuid" printf "grep -q '^%s:' %s 2>/dev/null || echo '%s:%s:65536' >> %s\n" "$_u" "$SYSROOT/etc/subgid" "$_u" "$_s" "$SYSROOT/etc/subgid" ;; userns-clone) echo "# $_n. Unprivileged user namespaces on (Debian's switch), now and after a restart: the engine runs in one." echo "[ \"\$(cat $SYSROOT/proc/sys/kernel/unprivileged_userns_clone)\" = 1 ] || sysctl -w kernel.unprivileged_userns_clone=1" echo "grep -qs '^kernel.unprivileged_userns_clone = 1' $SYSROOT/etc/sysctl.d/90-kriya-one.conf || echo 'kernel.unprivileged_userns_clone = 1' >> $SYSROOT/etc/sysctl.d/90-kriya-one.conf" ;; max-userns) echo "# $_n. User namespaces allowed at all (user.max_user_namespaces was 0), now and after a restart." echo "[ \"\$(cat $SYSROOT/proc/sys/user/max_user_namespaces)\" != 0 ] || sysctl -w user.max_user_namespaces=15000" echo "grep -qs '^user.max_user_namespaces = 15000' $SYSROOT/etc/sysctl.d/90-kriya-one.conf || echo 'user.max_user_namespaces = 15000' >> $SYSROOT/etc/sysctl.d/90-kriya-one.conf" ;; apparmor) echo "# $_n. AppArmor (Ubuntu 23.10+) blocks unprivileged user namespaces: a profile that allows them for Kriya One's own" echo "# rootlesskit only ($KENG/bin/rootlesskit), as Docker's rootless guide does for its own; then it is loaded." printf '%s\n' "grep -qsF '\"$KENG/bin/rootlesskit\"' $SYSROOT/etc/apparmor.d/kriya-one-rootlesskit || printf '%s\\n' 'abi ,' 'include ' '' '\"$KENG/bin/rootlesskit\" flags=(unconfined) {' ' userns,' ' include if exists ' '}' > $SYSROOT/etc/apparmor.d/kriya-one-rootlesskit" echo "apparmor_parser -r $SYSROOT/etc/apparmor.d/kriya-one-rootlesskit" ;; esac done echo echo 'echo "Kriya One: the admin steps are done. Go back to the installer (press Enter there), or run it again."' } >"$1.tmp" && mv -f "$1.tmp" "$1" && chmod 644 "$1" } # A Linux engine on this machine's own network (where host mode means something): not Docker Desktop, not WSL. linux_native() { [ "$(uname -s)" = Linux ] || return 1 # Kriya One's own rootless engine: its "host" network is rootlesskit's, not this machine's. [ "${DOCKER_HOST:-}" != "unix://$KENG/run/docker.sock" ] || return 1 grep -qi microsoft /proc/version 2>/dev/null && return 1 ! $ENGINE info --format '{{.OperatingSystem}}' 2>/dev/null | grep -qi 'docker desktop' } mem_total_mb() { _b=$($ENGINE info --format '{{.MemTotal}}' 2>/dev/null || true) if echo "$_b" | grep -Eq '^[0-9]{8,}$'; then echo $(( _b / 1048576 )); return; fi if [ -r /proc/meminfo ]; then awk '/^MemTotal:/ {print int($2 / 1024); exit}' /proc/meminfo; return; fi _b=$(sysctl -n hw.memsize 2>/dev/null || echo 0); echo $(( _b / 1048576 )) } # Half the machine's (or the Docker VM's) memory, 3g to 8g, never more than there is. default_memory() { _t=$(mem_total_mb) _g=$(( _t / 2048 )) [ "$_g" -le 8 ] || _g=8 [ "$_g" -ge 3 ] || _g=3 [ "$_t" -eq 0 ] || [ $(( _g * 1024 )) -le "$_t" ] || _g=$(( _t / 1024 )) [ "$_g" -ge 1 ] || _g=1 echo "${_g}g" } # This machine's own addresses (the firewall keeps them closed, like the host itself). macOS has no hostname -I. host_ips() { if [ "$(uname -s)" = Darwin ]; then ifconfig 2>/dev/null | awk '$1 == "inet" && $2 != "127.0.0.1" {print $2}' | tr '\n' ' ' else hostname -I 2>/dev/null || true fi } free_gb() { _d="$1" while [ ! -d "$_d" ]; do _d=$(dirname "$_d"); done df -Pk "$_d" 2>/dev/null | awk 'NR == 2 {print int($4 / 1048576)}' } # The TCP listeners on port $1: "ADDRESS:PORT PROGRAM PID" lines (PROGRAM and PID are - when not shown: ss shows # another user's programs only to root). ss (Linux), else lsof (macOS). port_listeners() { if command -v ss >/dev/null 2>&1; then ss -ltnp 2>/dev/null | awk -v p="$1" '$1 == "State" { next } { a = $4; n = a; sub(/.*:/, "", n); if (n != p) next prog = "-"; pid = "-" if (match($0, /users:\(\("[^"]*",pid=[0-9]+/)) { u = substr($0, RSTART + 9, RLENGTH - 9); prog = u; sub(/".*/, "", prog); pid = u; sub(/.*pid=/, "", pid) } print a, prog, pid }' elif command -v lsof >/dev/null 2>&1; then lsof -nP -iTCP:"$1" -sTCP:LISTEN 2>/dev/null | awk 'NR > 1 { print $9, $1, $2 }' fi } # Does a listener on address $1 (without its port) keep the instance from binding $2 (an IPv4 address, --bind)? # One on every address does; with --bind 0.0.0.0 any IPv4 one does; else only one on that very address. So the # instance's own forwarders (on a docker bridge, on the LAN: the same port on other addresses) don't count. bind_clash() { case "$1" in '*'|0.0.0.0|'[::]'|'::'|'[::ffff:0.0.0.0]') return 0 ;; esac if [ "$2" = 0.0.0.0 ]; then case "$1" in \[*) return 1 ;; *) return 0 ;; esac; fi [ "$1" = "$2" ] || [ "$1" = "[::ffff:$2]" ] } # The listener that takes port $1 on the address this instance binds (BIND, default 127.0.0.1): "ADDRESS:PORT # PROGRAM PID", else nothing. port_holder() { port_listeners "$1" | while read -r _la _lprog _lpid; do _lh=${_la%:*}; _lh=${_lh%%\%*} if bind_clash "$_lh" "${BIND:-127.0.0.1}"; then echo "$_la $_lprog $_lpid"; break; fi done } port_taken() { [ -n "$(port_holder "$1")" ] || # A container (another Kriya One, stopped or not) that publishes it: it takes the port back when it starts. port_owner "$1" | grep -q . } # Every container's host ports, once per run: "name port port …" lines (published ports, and a host-network Kriya # One's UPA_PORT/UPA_CANVAS_PORT). This instance's own containers (kriya-one-NAME, its kept -old-* ones) are left out. engine_ports() { [ -z "${_EPORTS+x}" ] || return 0 _EPORTS="" [ -n "${ENGINE:-}" ] || return 0 _ids=$($ENGINE ps -aq 2>/dev/null | tr '\n' ' ') [ -n "$_ids" ] || return 0 # shellcheck disable=SC2086 # the ids are words _EPORTS=$($ENGINE container inspect --format '{{.Name}}{{range $k, $v := .HostConfig.PortBindings}}{{range $v}} {{.HostPort}}{{end}}{{end}}{{range .Config.Env}} {{.}}{{end}}' $_ids 2>/dev/null | sed 's#^/##' | awk '{ o = $1; for (i = 2; i <= NF; i++) { if ($i ~ /^[0-9]+$/) o = o " " $i; else if ($i ~ /^UPA_(CANVAS_)?PORT=[0-9]+$/) { sub(/^[^=]*=/, "", $i); o = o " " $i } } print o }' | grep -Ev "^kriya-one-${NAME:-}(-old-[0-9-]+)? " || true) } # The container that publishes host port $1 (running or stopped), else nothing. port_owner() { engine_ports; printf '%s\n' "$_EPORTS" | awk -v p="$1" '{ for (i = 2; i <= NF; i++) if ($i == p) { print $1; exit } }'; } # tailscale serve: the HTTPS ports it serves now (one per line), and which local port each proxies ("PORT TARGET"). ts_json() { command -v "${KRIYA_TAILSCALE:-tailscale}" >/dev/null 2>&1 && "${KRIYA_TAILSCALE:-tailscale}" serve status --json 2>/dev/null | tr -d ' \n\t\r' || true; } ts_ports() { ts_json | grep -o '[":][0-9]\{1,5\}":{' | tr -cd '0-9\n' | sort -un; } ts_targets() { ts_json | grep -o ':[0-9]\{1,5\}":{"Handlers":{"/":{"Proxy":"http://\(127\.0\.0\.1\|localhost\):[0-9]\{1,5\}"' | sed 's/^:\([0-9]*\)".*:\([0-9]*\)"$/\1 \2/'; } # free_pair FROM: the first port P from FROM on (to FROM+89) with P and P+100 free here; also free P+10000 and # P+10100 in tailscale serve when it can (so the HTTPS address lines up), else the first locally free pair. free_pair() { _ts=" $(ts_ports | tr '\n' ' ') " _first="" _p=$1 while [ "$_p" -lt $(($1 + 90)) ]; do if ! port_taken "$_p" && ! port_taken $((_p + 100)); then [ -n "$_first" ] || _first=$_p case "$_ts" in *" $((_p + 10000)) "*|*" $((_p + 10100)) "*) ;; *) echo "$_p"; return 0 ;; esac fi _p=$((_p + 1)) done [ -n "$_first" ] && echo "$_first" } # Why a port is taken, for the message: "nginx (pid 42, listening on 0.0.0.0:8810)", "the container # kriya-one-professional" or "another program". port_why() { _ph=$(port_holder "$1") if [ -n "$_ph" ]; then # shellcheck disable=SC2086 # "ADDRESS PROGRAM PID": three words set -- $_ph if [ "$2" != - ]; then echo "$2 (pid $3, listening on $1)"; else echo "another program (listening on $1)"; fi return 0 fi _o=$(port_owner "$1"); if [ -n "$_o" ]; then echo "the container $_o"; else echo "another program"; fi } sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}'; else shasum -a 256 "$1" | awk '{print $1}'; fi; } # ---------------------------------------------------------------- the saved answers (~/Kriya//kriya-one.env) save_env() { for _v in "$NAME" "$PORT" "$IMAGE" "$MEMORY" "$CPUS" "$KDIR" "$DISK" "$SHARES" "$NETWORK" "$BIND" "$DISPLAY_NAME" "$ACCENT" "$PUBLIC_URL" "$SHARED_DIR" "$DOCKER_NET" "$HTTPS" "$HTTPS_URL"; do case "$_v" in *' '*) die "a value has a line break" ;; esac done ( umask 077 { echo "# Kriya One \"$NAME\": the installer's answers (kriya-one config and update reuse them). Written $(date '+%Y-%m-%d %H:%M')." echo "KRIYA_ONE_NAME=$NAME" echo "KRIYA_ONE_PORT=$PORT" echo "KRIYA_ONE_CANVAS_PORT=$((PORT + 100))" echo "KRIYA_ONE_IMAGE=$IMAGE" echo "KRIYA_ONE_MEMORY=$MEMORY" echo "KRIYA_ONE_CPUS=$CPUS" echo "KRIYA_DIR=$KDIR" echo "KRIYA_ONE_DISK_GB=$DISK" echo "KRIYA_ONE_SHARES=$SHARES" echo "KRIYA_ONE_NETWORK=$NETWORK" echo "KRIYA_ONE_BIND=$BIND" echo "KRIYA_ONE_DISPLAY_NAME=$DISPLAY_NAME" echo "KRIYA_ONE_ACCENT=$ACCENT" echo "KRIYA_ONE_PUBLIC_URL=$PUBLIC_URL" # /shared in the container: this instance's own folder (an install before 0.4.2: the old common ~/Kriya/shared). echo "KRIYA_ONE_SHARED_DIR=$SHARED_DIR" echo "KRIYA_ONE_DOCKER_NET=$DOCKER_NET" echo "KRIYA_ONE_HTTPS=${HTTPS:-$HTTPS_SAVED}" echo "KRIYA_ONE_HTTPS_URL=$HTTPS_URL" echo "KRIYA_ONE_SHORTCUT=${SHORTCUT:-1}" } >"$ENVFILE.tmp" ) chmod 600 "$ENVFILE.tmp" mv -f "$ENVFILE.tmp" "$ENVFILE" # Where this one's data is, for the kriya-one command (~/.config/kriya-one/.dir). { mkdir -p "$CFGDIR" && printf '%s\n' "$KDIR" >"$CFGDIR/$NAME.dir"; } 2>/dev/null || true } # The data folder an install saved for NAME (kriya-one without --data), else empty. saved_dir() { [ -f "$CFGDIR/$1.dir" ] && head -1 "$CFGDIR/$1.dir" || true; } # The options of a container set aside (kept/.env), so kriya-one rollback brings them back with it. keep_env() { [ -f "$ENVFILE" ] && mkdir -p "$KDIR/$NAME/kept" && cp -p "$ENVFILE" "$KDIR/$NAME/kept/$1.env" || true; } # Only what the command line didn't give (it wins). Never sourced: only these keys are read. load_env() { [ -f "$ENVFILE" ] || return 1 while IFS= read -r _l || [ -n "$_l" ]; do _k=${_l%%=*} _v=${_l#*=} case "$_k" in KRIYA_ONE_PORT) [ -n "$PORT" ] || PORT=$_v ;; KRIYA_ONE_IMAGE) [ -n "$IMAGE" ] || IMAGE=$_v ;; KRIYA_ONE_MEMORY) [ -n "$MEMORY" ] || MEMORY=$_v ;; KRIYA_ONE_CPUS) [ -n "$CPUS_SET" ] || { CPUS=$_v; CPUS_SET=1; } ;; KRIYA_ONE_DISK_GB) [ -n "$DISK" ] || DISK=$_v ;; KRIYA_ONE_SHARES) [ "$SHARES_SET" = 1 ] || SHARES=$_v ;; KRIYA_ONE_NETWORK) [ -n "$NETWORK" ] || NETWORK=$_v ;; KRIYA_ONE_BIND) [ -n "$BIND" ] || BIND=$_v ;; KRIYA_ONE_DISPLAY_NAME) [ -n "$DISPLAY_SET" ] || { DISPLAY_NAME=$_v; DISPLAY_SET=1; } ;; KRIYA_ONE_ACCENT) [ -n "$ACCENT" ] || ACCENT=$_v ;; KRIYA_ONE_PUBLIC_URL) [ -n "$PUBLIC_URL" ] || PUBLIC_URL=$_v ;; KRIYA_ONE_SHARED_DIR) [ -n "$SHARED_DIR" ] || SHARED_DIR=$_v ;; KRIYA_ONE_DOCKER_NET) [ -n "$DOCKER_NET" ] || DOCKER_NET=$_v ;; KRIYA_ONE_HTTPS) [ -n "$HTTPS_SAVED" ] || HTTPS_SAVED=$_v ;; KRIYA_ONE_HTTPS_URL) [ -n "$HTTPS_URL" ] || HTTPS_URL=$_v ;; KRIYA_ONE_SHORTCUT) [ -n "${SHORTCUT:-}" ] || SHORTCUT=$_v ;; esac done <"$ENVFILE" # Saved before 0.4.2 (no KRIYA_ONE_SHARED_DIR): /shared stays the common ~/Kriya/shared it always was, on the # default bridge, so an upgrade changes nothing there. A new install gets its own of both. [ -n "$SHARED_DIR" ] || SHARED_DIR=$KDIR/shared grep -q '^KRIYA_ONE_DOCKER_NET=' "$ENVFILE" 2>/dev/null || DOCKER_NET=${DOCKER_NET:-bridge} return 0 } # ---------------------------------------------------------------- the image get_image() { # kriya-one config recreates with the image it has (KRIYA_ONE_NOPULL=1): no pull. if [ "${KRIYA_ONE_NOPULL:-0}" = 1 ] && $ENGINE image inspect "$IMAGE" >/dev/null 2>&1; then note "image: $IMAGE (as it is, not pulled)" return 0 fi # A local name (no registry, e.g. kriya-one:local from docker/build-one.sh) isn't pulled. if [ "${IMAGE#*/}" = "$IMAGE" ] && $ENGINE image inspect "$IMAGE" >/dev/null 2>&1; then note "image: $IMAGE (local, not pulled)" return 0 fi say "Pulling $IMAGE (about 1.3 GB the first time)…" $ENGINE pull "$IMAGE" || $ENGINE image inspect "$IMAGE" >/dev/null 2>&1 || die "couldn't get $IMAGE" _dg=$($ENGINE image inspect --format '{{range .RepoDigests}}{{.}} {{end}}' "$IMAGE" 2>/dev/null || true) note "image: $IMAGE${_dg:+ ($_dg)}" # Signed releases: cosign keyless, the release workflow's identity. Releases aren't signed yet, so it is opt-in. if [ "${KRIYA_ONE_VERIFY:-0}" = 1 ]; then command -v cosign >/dev/null 2>&1 || die "KRIYA_ONE_VERIFY=1 needs cosign (https://docs.sigstore.dev/cosign/system_config/installation/)" cosign verify --certificate-oidc-issuer https://token.actions.githubusercontent.com \ --certificate-identity-regexp "$COSIGN_ID" "$IMAGE" >/dev/null || die "cosign: $IMAGE is not signed by Kriya's release workflow" note "image: signature checked (cosign)" fi } # ---------------------------------------------------------------- the container create_container() { CPORT=$((PORT + 100)) for _p in "$PORT" "$CPORT"; do if port_taken "$_p"; then die "port $_p is taken on ${BIND:-127.0.0.1} by $(port_why "$_p"): choose another with --port (the canvas uses port + 100), e.g. --port $(free_pair 8810 || echo 8850)"; fi done mkdir -p "$KDIR/$NAME/data" "$KDIR/$NAME/home" "$SHARED_DIR" HOSTIPS=$(host_ips | tr -s ' \t' ',' | sed 's/^,*//; s/,*$//') _tz="${TZ:-$( (readlink /etc/localtime 2>/dev/null || echo UTC) | sed 's#.*/zoneinfo/##')}" set -- run -d --name "$CNAME" --restart unless-stopped --stop-timeout 30 --cap-add DAC_READ_SEARCH --shm-size 1g \ --label one.kriya.instance="$NAME" if [ "$NETWORK" = host ]; then # This machine's network: no firewall (so no NET_ADMIN: it would be the host's own iptables) and no -p: the # panel binds the host's port itself. set -- "$@" --network host -e UPA_HOST="$BIND" -e UPA_PORT="$PORT" -e UPA_CANVAS_PORT="$CPORT" else # Its own bridge network (kriya-one-NAME): two Kriya Ones on one machine can't reach each other's containers # (Docker keeps separate bridges apart); each one's firewall keeps the host closed as before. if [ "$DOCKER_NET" != bridge ]; then $ENGINE network inspect "$DOCKER_NET" >/dev/null 2>&1 || $ENGINE network create --label one.kriya.instance="$NAME" "$DOCKER_NET" >/dev/null || die "couldn't create the network $DOCKER_NET" set -- "$@" --network "$DOCKER_NET" fi set -- "$@" --hostname kriya-one --cap-add NET_ADMIN --add-host host.docker.internal:host-gateway \ -p "$BIND:$PORT:7800" -p "$BIND:$CPORT:7900" fi # KRIYA_ONE_URL is the host-local address (the server's own links to itself, and what it used to parse the port # from); KRIYA_ONE_PORT is the panel's host port; KRIYA_ONE_PUBLIC_URL, when given, is what other devices use (else # the server derives it from its HTTPS setup). # KRIYA_ONE_BIND: the address the port is published on; 127.0.0.1 tells the server that every request comes from # this machine, so its setup asks no setup code (server/onethismachine.ts). set -- "$@" -e KRIYA_ONE_URL="http://localhost:$PORT" -e KRIYA_ONE_PORT="$PORT" -e KRIYA_ONE_NETWORK="$NETWORK" -e KRIYA_ONE_BIND="$BIND" -e UPA_HOST_IPS="$HOSTIPS" \ -e TZ="$_tz" -e KRIYA_ONE_DISK_GB="$DISK" -e KRIYA_ONE_IMAGE="$IMAGE" -e KRIYA_ONE_NAME="$NAME" -e KRIYA_ONE_SHARES="$SHARES" [ -z "$DISPLAY_NAME" ] || set -- "$@" -e KRIYA_ONE_DISPLAY_NAME="$DISPLAY_NAME" [ -z "$ACCENT" ] || [ "$ACCENT" = none ] || set -- "$@" -e KRIYA_ONE_ACCENT="$ACCENT" [ -z "$PUBLIC_URL" ] || set -- "$@" -e KRIYA_ONE_PUBLIC_URL="$PUBLIC_URL" if [ -n "$MEMORY" ] && [ "$MEMORY" != none ]; then if [ "$ENGINE" = podman ] && ! podman info --format '{{.Host.CgroupControllers}}' 2>/dev/null | grep -q memory; then warn "podman here can't limit memory (no cgroup memory controller delegated to you): --memory $MEMORY skipped" else set -- "$@" --memory "$MEMORY" --memory-swap "$MEMORY" fi fi [ -z "$CPUS" ] || set -- "$@" --cpus "$CPUS" # --model-url / --model: the setup page's Engine step opens on Local model with them (server/one.ts localPreset), # and a model on this machine gets its port opened in the firewall (the rest of this machine stays closed). if [ -n "$MODEL_URL" ]; then set -- "$@" -e KRIYA_ONE_MODEL_URL="$MODEL_URL" -e KRIYA_ONE_MODEL="$MODEL_NAME" _mp=$(model_host_port "$MODEL_URL") [ -z "$_mp" ] || [ "$NETWORK" = host ] || set -- "$@" -e UPA_HOST_PORTS="$_mp" fi set -- "$@" -v "$KDIR/$NAME/data:/agent" -v "$KDIR/$NAME/home:/home/agent" -v "$SHARED_DIR:/shared" _roots=/shared _old_ifs=$IFS; IFS='|' for _e in $SHARES; do IFS=$_old_ifs _sp=${_e%:*:*} _rest=${_e#"${_e%:*:*}":} _sn=${_rest%%:*} _sm=${_rest##*:} [ -d "$_sp" ] || die "the shared folder $_sp isn't there any more (re-run with the --share options you want)" # Made by you, so Docker doesn't make a root-owned mount point in the /shared folder. mkdir -p "$SHARED_DIR/$_sn" if [ "$_sm" = ro ]; then set -- "$@" -v "$_sp:/shared/$_sn:ro"; else set -- "$@" -v "$_sp:/shared/$_sn"; fi _roots="$_roots:/shared/$_sn" done IFS=$_old_ifs # The file roots and Claude's extra folders (server/config.ts: colon-separated). [ "$_roots" = /shared ] || set -- "$@" -e UPA_FILE_ROOTS="$_roots" -e UPA_ADD_DIRS="$_roots" # Podman runs rootless as you: keep your uid as the container's agent user, so the files in ~/Kriya stay yours. [ "$ENGINE" != podman ] || set -- "$@" --userns=keep-id:uid=1000,gid=1000 set -- "$@" "$IMAGE" $ENGINE "$@" >/dev/null } containers_named() { $ENGINE ps -a --format '{{.Names}}' 2>/dev/null | grep -E "^$1\$" || true; } exists() { $ENGINE container inspect "$1" >/dev/null 2>&1; } newest_old() { containers_named "kriya-one-$NAME-old-[0-9-]+" | sort | tail -1; } # kriya-one--old-, -2, -3… when one of that second is there (a rollback right after an upgrade). stamp() { _st="kriya-one-$NAME-old-$(date +%Y%m%d-%H%M%S)" _si=2 _st0=$_st while exists "$_st"; do _st="$_st0-$_si"; _si=$((_si + 1)); done echo "$_st" } # ---------------------------------------------------------------- helpers pick() { # pick SUMS REGEX → "sha file" of the first file matching sed 's/ \*/ /' "$1" | while read -r _h _f; do if echo "$_f" | grep -Eq "$2"; then echo "$_h $_f"; break; fi done } # ---------------------------------------------------------------- wait, then the setup URL wait_ready() { URL="http://localhost:$PORT" _i=0 until curl -fsS "$URL/healthz" >/dev/null 2>&1; do _i=$((_i + 1)); [ $_i -gt "${KRIYA_ONE_WAIT:-120}" ] && die "it didn't start: $ENGINE logs $CNAME" sleep 1 done _code=$($ENGINE logs "$CNAME" 2>&1 | sed -n 's/.*first-run setup code: \([A-Z0-9-]*\).*/\1/p' | tail -1) PENDING=0 SETUP_PATH=/ LOCAL_PATH=/ _st=$(curl -fsS "$URL/api/one/status" 2>/dev/null || true) if echo "$_st" | grep -q '"pending":true' && [ -n "$_code" ]; then PENDING=1 # Other devices: the setup code in the link. This machine: none, when the server says so (0.4.8 and later, with # the port on 127.0.0.1 or the host network: server/onethismachine.ts). SETUP_PATH="/setup?code=$_code" if echo "$_st" | grep -q '"codeNeeded":false'; then LOCAL_PATH=/setup; else LOCAL_PATH=$SETUP_PATH; fi fi OPENURL="$URL$LOCAL_PATH" } # The address other devices use, once known: the HTTPS one (saved by setup_https), else --public-url, else nothing. remote_url() { if [ -n "${HTTPS_URL:-}" ]; then echo "$HTTPS_URL"; elif [ -n "${PUBLIC_URL:-}" ]; then echo "$PUBLIC_URL"; fi; } # HTTPS for other devices (Kriya One settings → Other devices does the same later): tailscale serve, or Caddy with a # local certificate authority for this machine's LAN address. deploy/one/reach.sh does the host's part. setup_https() { case "$HTTPS" in tailscale) _plan='{"mode":"tailscale"}' ;; local|local:*) _addr="${HTTPS#local}"; _addr="${_addr#:}" [ -n "$_addr" ] || _addr=$(host_ips | awk '{print $1}') [ -n "$_addr" ] || die "--https local:" echo "$_addr" | grep -Eq '^[A-Za-z0-9.-]+$' || die "--https local:
: a name or an IPv4 address" _plan="{\"mode\":\"local\",\"host\":\"$_addr\"}" ;; *) die "--https tailscale or local[:
] (an own domain is set in Kriya One settings → Other devices)" ;; esac $ENGINE exec -u upa "$CNAME" node /opt/upa/server/onereach-cli.ts set-https "$_plan" >/dev/null || die "couldn't save the HTTPS choice" _reach="$(dirname "$0")/reach.sh" if [ ! -f "$_reach" ]; then _rd=$(mktemp -d); _reach="$_rd/reach.sh" curl -fsSL -o "$_reach" "$DOWNLOAD_BASE/reach.sh" && curl -fsSL -o "$_rd/SHA256SUMS" "$DOWNLOAD_BASE/SHA256SUMS" || die "couldn't fetch reach.sh from $DOWNLOAD_BASE" _rs=$(pick "$_rd/SHA256SUMS" '^reach\.sh$') [ -n "$_rs" ] && [ "$(sha256 "$_reach")" = "${_rs%% *}" ] || die "reach.sh doesn't match its checksum in $DOWNLOAD_BASE/SHA256SUMS" fi _rout=$(mktemp) if KRIYA_DOCKER="$ENGINE" KRIYA_REACH_OUT="$_rout" sh "$_reach" --name "$NAME" https on; then HTTPS_URL=$(sed -n 's/^url=//p' "$_rout" | tail -1) else say "HTTPS isn't on yet: see above, then run reach.sh --name $NAME https on" fi rm -f "$_rout" } # tailscale serve entries of this instance: the pair reach.sh recorded when it made them (~/.config/kriya-one/ # NAME.serve), when they still proxy to its ports; with no record (made by 0.4.1), its P+10000/P+10100 pair when # both proxy to its ports. An entry anyone else made is never touched. ts_unserve() { # ts_unserve PANEL_PORT [ -n "$1" ] || return 0 _ts=${KRIYA_TAILSCALE:-tailscale} _rec="$CFGDIR/$NAME.serve" command -v "$_ts" >/dev/null 2>&1 || { rm -f "$_rec"; return 0; } if [ -f "$_rec" ]; then _h=$(awk -v p="$1" '$1 == p { print $2; exit }' "$_rec"); else _h=$(($1 + 10000)); fi if [ -n "$_h" ] && ts_targets | grep -qx "$_h $1" && ts_targets | grep -qx "$((_h + 100)) $(($1 + 100))"; then for _hp in "$_h" $((_h + 100)); do "$_ts" serve --yes --https="$_hp" off >/dev/null 2>&1 && note "removed tailscale serve :$_hp (this instance's)" || warn "couldn't remove tailscale serve :$_hp (tailscale serve --https=$_hp off)" done fi rm -f "$_rec" if ts_json | grep -q "\"/kriya-$NAME/hooks\""; then "$_ts" funnel --yes --https="${KRIYA_FUNNEL_PORT:-10000}" --set-path="/kriya-$NAME/hooks" off >/dev/null 2>&1 && note "removed the Funnel path /kriya-$NAME/hooks" || true fi } open_url() { [ "$OPEN" = 1 ] || return 0 if command -v open >/dev/null 2>&1 && [ "$(uname -s)" = Darwin ]; then open "$1" elif command -v wslview >/dev/null 2>&1; then wslview "$1" elif command -v xdg-open >/dev/null 2>&1 && [ -n "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]; then xdg-open "$1" >/dev/null 2>&1 & fi } # ---------------------------------------------------------------- the shortcut (0.4.7 N15) # After an install, a shortcut on the desktop and in the app list ("Kriya One", "Kriya One (Therapist)": one per # instance) that opens this instance's address, for whoever closed the tab. Linux: kriya-one-NAME.desktop in # ~/.local/share/applications and on the Desktop; macOS: a small "Kriya One….app" in ~/Applications/Kriya and on the # Desktop. Both run a tiny launcher (~/.config/kriya-one/launchers/NAME.sh) that asks Kriya One first (GET # /api/one/status, no sign-in): once its app was installed from this machine's browser (server/appinstall.ts) it opens # that app (else the address, which the browser hands to it) and removes its own desktop copy; it never removes # anything while the app isn't installed. Setup still pending: kriya-one open (the setup link with its code). Not # answering: kriya-one start first. --no-shortcut (KRIYA_ONE_NO_SHORTCUT=1, saved) makes none and removes them. # kriya-one uninstall removes them. WSL: none (install.ps1 makes the Windows one). shortcut_label() { if [ "$NAME" = assistant ]; then echo "Kriya One"; else echo "Kriya One (${DISPLAY_NAME:-$NAME})"; fi; } shortcut_os() { case "$(uname -s)" in Darwin) echo mac ;; Linux) if grep -qsi microsoft /proc/version || [ -n "${WSL_DISTRO_NAME:-}" ]; then echo wsl; else echo linux; fi ;; *) echo none ;; esac } launcher_dir() { echo "$CFGDIR/launchers"; } # The Desktop folder, when there is one (never made here: a machine without a desktop gets no desktop copy). desktop_dir() { _dd="" [ "$(shortcut_os)" = mac ] || _dd=$(xdg-user-dir DESKTOP 2>/dev/null || true) [ -n "$_dd" ] && [ "$_dd" != "$HOME" ] || _dd="$HOME/Desktop" [ -d "$_dd" ] && echo "$_dd" } apps_dir() { if [ "$(shortcut_os)" = mac ]; then echo "$HOME/Applications/Kriya"; else echo "${XDG_DATA_HOME:-$HOME/.local/share}/applications"; fi; } # This instance's shortcut in the app list, and its desktop copy (DESK: the folder, else nothing). shortcut_file() { if [ "$(shortcut_os)" = mac ]; then echo "$1/$(shortcut_label).app"; else echo "$1/kriya-one-$NAME.desktop"; fi; } # sq TEXT: TEXT in single quotes for a shell script. sq() { printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")"; } # shortcut_launcher DESKCOPY: the launcher script (the port and the desktop copy written into it). shortcut_launcher() { cat </dev/null || true; } st=$(status) # Not answering (stopped, or the machine just started): start it, then wait a little. if [ -z "$st" ] && [ -x "$KRIYA_ONE" ]; then sh "$KRIYA_ONE" start --name "$NAME" --no-open >/dev/null 2>&1 || true i=0; while [ -z "$st" ] && [ $i -lt 30 ]; do sleep 1; i=$((i + 1)); st=$(status); done fi open_url() { if [ "$(uname -s)" = Darwin ]; then open "$1" elif command -v xdg-open >/dev/null 2>&1; then xdg-open "$1" >/dev/null 2>&1 else echo "$1" fi } # The app installed from Chrome, Edge, Chromium, Brave (or Safari's Add to Dock), by its name: open it. open_app() { [ -n "$1" ] || return 1 if [ "$(uname -s)" = Darwin ]; then for d in "$HOME/Applications/Chrome Apps.localized" "$HOME/Applications/Edge Apps.localized" "$HOME/Applications/Chromium Apps.localized" "$HOME/Applications/Brave Browser Apps.localized" "$HOME/Applications"; do [ -d "$d/$1.app" ] && open "$d/$1.app" && return 0 done return 1 fi for f in "${XDG_DATA_HOME:-$HOME/.local/share}"/applications/chrome-*.desktop "${XDG_DATA_HOME:-$HOME/.local/share}"/applications/msedge-*.desktop \ "${XDG_DATA_HOME:-$HOME/.local/share}"/applications/chromium-*.desktop "${XDG_DATA_HOME:-$HOME/.local/share}"/applications/brave-*.desktop; do [ -f "$f" ] && grep -qxF "Name=$1" "$f" || continue if command -v gio >/dev/null 2>&1 && gio launch "$f" >/dev/null 2>&1; then return 0; fi command -v gtk-launch >/dev/null 2>&1 && gtk-launch "$(basename "$f" .desktop)" >/dev/null 2>&1 && return 0 done return 1 } case "$st" in *'"pending":true'*) [ -x "$KRIYA_ONE" ] && exec sh "$KRIYA_ONE" open --name "$NAME" ;; *'"appInstalled":true'*) # Installed as an app: this desktop copy isn't needed any more (the app has its own icon). if [ -n "$DESK" ] && [ -e "$DESK" ]; then rm -rf "$DESK" && : >"$TIDIED"; fi app=$(printf '%s' "$st" | sed -n 's/.*"appName":"\([^"]*\)".*/\1/p') open_app "$app" && exit 0 ;; esac open_url "$URL" EOF } # mac_app DIR LAUNCHER ICON [LABEL ID]: a minimal .app bundle that runs the launcher (default: the "Kriya One" one). mac_app() { _ml=${4:-$(shortcut_label)} _mi=${5:-one.kriya.shortcut.$NAME} rm -rf "$1" && mkdir -p "$1/Contents/MacOS" "$1/Contents/Resources" || return 1 cp "$2" "$1/Contents/MacOS/launch" && chmod 755 "$1/Contents/MacOS/launch" || return 1 _ic="" [ ! -f "$3" ] || { cp "$3" "$1/Contents/Resources/icon.icns" && _ic=" CFBundleIconFileicon"; } cat >"$1/Contents/Info.plist" < CFBundleName$(printf '%s' "$_ml" | sed 's/&/\&/g') CFBundleDisplayName$(printf '%s' "$_ml" | sed 's/&/\&/g') CFBundleIdentifier$_mi CFBundleExecutablelaunch CFBundlePackageTypeAPPL CFBundleVersion1 LSUIElement $_ic EOF } # desktop_entry LAUNCHER ICON: a Linux .desktop file that runs the launcher. desktop_entry() { # Exec: in double quotes, with " ` $ \ escaped and % doubled (the Desktop Entry spec). _ex=$(printf '%s' "$1" | sed 's/[\\"`$]/\\\\&/g; s/%/%%/g') printf '[Desktop Entry]\nType=Application\nVersion=1.0\nName=%s\nComment=Open your Kriya One assistant\nExec=sh "%s"\n' "$(shortcut_label)" "$_ex" [ ! -f "$2" ] || printf 'Icon=%s\n' "$2" printf 'Terminal=false\nStartupNotify=false\nCategories=Utility;\nX-Kriya-One-Instance=%s\n' "$NAME" } # shortcut_make: this instance's shortcuts, made or brought up to date (its port can change). The desktop copy on a # first install, or when there is one already; never again once the launcher tidied it away or the app is installed. shortcut_make() { _so=$(shortcut_os) case "$_so" in linux|mac) ;; *) return 0 ;; esac _ld=$(launcher_dir) _ad=$(apps_dir) _dd=$(desktop_dir || true) _lf="$_ld/$NAME.sh" _af=$(shortcut_file "$_ad") _df="" [ -z "$_dd" ] || _df=$(shortcut_file "$_dd") # A label that changed (a new display name): the old shortcut files of this instance go first. shortcut_remove_files keep-launcher _desk=0 if [ -n "$_df" ] && [ ! -f "$_ld/$NAME.tidied" ] && ! curl -fsS -m 3 "http://127.0.0.1:$PORT/api/one/status" 2>/dev/null | grep -q '"appInstalled":true'; then if [ "$ACTION" = install ] || [ "${_had_desk:-0}" = 1 ]; then _desk=1; fi fi mkdir -p "$_ld" "$_ad" 2>/dev/null || { warn "couldn't make $_ld or $_ad: no shortcut"; return 0; } shortcut_launcher "$([ "$_desk" = 1 ] && echo "$_df")" >"$_lf.tmp" && chmod 755 "$_lf.tmp" && mv -f "$_lf.tmp" "$_lf" || { warn "couldn't write $_lf: no shortcut"; return 0; } # The app's own icon (with its badge), from the running instance. _png="$_ld/$NAME.png" curl -fsS -m 10 -o "$_png.tmp" "http://127.0.0.1:$PORT/brand/icon/512" 2>/dev/null && mv -f "$_png.tmp" "$_png" || rm -f "$_png.tmp" if [ "$_so" = mac ]; then _icns="$_ld/$NAME.icns" if [ -f "$_png" ] && command -v sips >/dev/null 2>&1 && command -v iconutil >/dev/null 2>&1; then _set="$_ld/$NAME.iconset"; rm -rf "$_set"; mkdir -p "$_set" for _s in 16 32 128 256; do sips -z "$_s" "$_s" "$_png" --out "$_set/icon_${_s}x${_s}.png" >/dev/null 2>&1; sips -z $((_s * 2)) $((_s * 2)) "$_png" --out "$_set/icon_${_s}x${_s}@2x.png" >/dev/null 2>&1; done iconutil -c icns "$_set" -o "$_icns" >/dev/null 2>&1 || rm -f "$_icns" rm -rf "$_set" fi mac_app "$_af" "$_lf" "$_icns" || { warn "couldn't make $_af"; return 0; } [ "$_desk" = 0 ] || cp -R "$_af" "$_df" || warn "couldn't put the shortcut on the Desktop" else desktop_entry "$_lf" "$_png" >"$_af" && chmod 755 "$_af" || { warn "couldn't write $_af"; return 0; } if [ "$_desk" = 1 ]; then cp "$_af" "$_df" && chmod 755 "$_df" || warn "couldn't put the shortcut on the Desktop" # GNOME's desktop runs only a .desktop file it trusts. command -v gio >/dev/null 2>&1 && gio set "$_df" metadata::trusted true >/dev/null 2>&1 || true fi command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$_ad" >/dev/null 2>&1 || true fi if [ "$_desk" = 1 ]; then note "Shortcut: \"$(shortcut_label)\" on the Desktop and in your apps (it opens $URL/)." else note "Shortcut: \"$(shortcut_label)\" in your apps (it opens $URL/)."; fi } # shortcut_remove_files [keep-launcher]: this instance's shortcut files (any label it had: by the instance in them). shortcut_remove_files() { _had_desk=0 _ld=$(launcher_dir) _ad=$(apps_dir) _dd=$(desktop_dir || true) for _d in "$_ad" ${_dd:+"$_dd"}; do [ -d "$_d" ] || continue if [ "$(shortcut_os)" = mac ]; then for _f in "$_d"/*.app; do [ -f "$_f/Contents/Info.plist" ] && grep -q "one.kriya.shortcut.$NAME" "$_f/Contents/Info.plist" || continue rm -rf "$_f"; [ "$_d" = "$_ad" ] || _had_desk=1 done else for _f in "$_d"/*.desktop; do [ -f "$_f" ] && grep -qx "X-Kriya-One-Instance=$NAME" "$_f" || continue rm -f "$_f"; [ "$_d" = "$_ad" ] || _had_desk=1 done fi done [ "${1:-}" = keep-launcher ] && return 0 rm -f "$_ld/$NAME.sh" "$_ld/$NAME.png" "$_ld/$NAME.icns" "$_ld/$NAME.tidied" rmdir "$_ld" 2>/dev/null || true [ "$(shortcut_os)" != mac ] || rmdir "$_ad" 2>/dev/null || true } shortcut_remove() { _was=0; [ ! -f "$(launcher_dir)/$NAME.sh" ] || _was=1 shortcut_remove_files [ "$_was" = 0 ] || note "removed the shortcut \"$(shortcut_label)\"" } # ---------------------------------------------------------------- the control app (0.4.8): "Kriya One Server" # A second shortcut per instance, "Kriya One Server" ("Kriya One Server (Therapist)"), that opens the control app: # kriya-one-panel (deploy/one/panel, a small Go program next to the kriya-one command), a page on 127.0.0.1 that shows # the status and runs Start, Stop, Restart, Update, Logs, Settings, Rollback and Uninstall through the kriya-one # command, also when Kriya One or Docker is stopped. Without that program: kriya-one menu in a terminal, else the cheat # sheet. Its files are its own (X-Kriya-One-Server=NAME, one.kriya.server.NAME), so the "Kriya One" shortcut tidying # itself away once the app is installed never touches it; kriya-one uninstall (and --no-shortcut) removes it. server_label() { if [ "$NAME" = assistant ]; then echo "Kriya One Server"; else echo "Kriya One Server (${DISPLAY_NAME:-$NAME})"; fi; } server_file() { if [ "$(shortcut_os)" = mac ]; then echo "$1/$(server_label).app"; else echo "$1/kriya-one-server-$NAME.desktop"; fi; } panel_bin() { if [ "$(shortcut_os)" = wsl ]; then echo ""; else echo "$(cli_bin)/kriya-one-panel"; fi; } # panel_asset: this machine's kriya-one-panel download, e.g. kriya-one-panel-darwin-arm64. panel_asset() { case "$(uname -s)" in Darwin) _po=darwin ;; Linux) _po=linux ;; *) return 1 ;; esac case "$(uname -m)" in x86_64|amd64) _pa=amd64 ;; aarch64|arm64) _pa=arm64 ;; *) return 1 ;; esac echo "kriya-one-panel-$_po-$_pa" } # install_panel: kriya-one-panel next to the kriya-one command: KRIYA_ONE_PANEL (a file), else one next to this # script when it runs from a file (not under curl | sh: never one in the current folder), else the portal's, checked # against its SHA256SUMS. None: the shortcut opens kriya-one menu instead. install_panel() { _pb=$(panel_bin); [ -n "$_pb" ] || return 0 _pn=$(panel_asset) || return 0 _psrc="" _pd="" if [ -n "${KRIYA_ONE_PANEL:-}" ] && [ -f "$KRIYA_ONE_PANEL" ]; then _psrc=$KRIYA_ONE_PANEL elif [ -f "$0" ] && [ -f "$(dirname "$0")/$_pn" ]; then _psrc="$(dirname "$0")/$_pn" else _pd=$(mktemp -d) if curl -fsSL -m 120 -o "$_pd/$_pn" "$DOWNLOAD_BASE/$_pn" 2>/dev/null && curl -fsSL -m 30 -o "$_pd/SHA256SUMS" "$DOWNLOAD_BASE/SHA256SUMS" 2>/dev/null; then _ps=$(pick "$_pd/SHA256SUMS" "^$_pn\$") if [ -n "$_ps" ] && [ "$(sha256 "$_pd/$_pn")" = "${_ps%% *}" ]; then _psrc="$_pd/$_pn" else warn "$DOWNLOAD_BASE/$_pn doesn't match its checksum in SHA256SUMS: not used"; fi fi fi if [ -z "$_psrc" ]; then [ -z "$_pd" ] || rm -rf "$_pd" [ -x "$_pb" ] || note "(the control app isn't available for this machine yet: the \"$(server_label)\" shortcut opens kriya-one menu)" return 0 fi if ! { [ -x "$_pb" ] && cmp -s "$_psrc" "$_pb"; }; then { mkdir -p "$(cli_bin)" && cp "$_psrc" "$_pb.tmp" && chmod 755 "$_pb.tmp" && mv -f "$_pb.tmp" "$_pb"; } || warn "couldn't write $_pb" fi [ -z "$_pd" ] || rm -rf "$_pd" } # server_launcher: the control app's launcher: the panel, else kriya-one menu in a terminal, else the cheat sheet. server_launcher() { cat <"$f" && chmod 755 "$f" && exec open "$f" fi for t in x-terminal-emulator gnome-terminal konsole xfce4-terminal mate-terminal tilix kitty alacritty xterm; do command -v "$t" >/dev/null 2>&1 || continue case "$t" in gnome-terminal|tilix|mate-terminal) exec "$t" -- sh -c "$cmd" ;; xfce4-terminal) exec "$t" -x sh -c "$cmd" ;; *) exec "$t" -e sh -c "$cmd" ;; esac done # No terminal either: the cheat sheet, in a window when there is one. if command -v zenity >/dev/null 2>&1; then sh "$KRIYA_ONE" help --short --name "$NAME" | zenity --text-info --title "Kriya One" --width 640 --height 420 else sh "$KRIYA_ONE" help --short --name "$NAME"; fi EOF } # server_shortcut_make: the "Kriya One Server" shortcut, in the app list and on the Desktop. The desktop copy: on an # install, the first time an update brings it, or when it is there already; one deleted by hand stays deleted. server_shortcut_make() { _so=$(shortcut_os) case "$_so" in linux|mac) ;; *) return 0 ;; esac _ld=$(launcher_dir) _ad=$(apps_dir) _dd=$(desktop_dir || true) _lf="$_ld/$NAME-server.sh" _af=$(server_file "$_ad") _df="" [ -z "$_dd" ] || _df=$(server_file "$_dd") _first=0; [ -f "$_ld/$NAME-server.made" ] || _first=1 server_shortcut_files _desk=0 if [ -n "$_df" ] && { [ "$_first" = 1 ] || [ "$ACTION" = install ] || [ "${_had_sdesk:-0}" = 1 ]; }; then _desk=1; fi mkdir -p "$_ld" "$_ad" 2>/dev/null || return 0 server_launcher >"$_lf.tmp" && chmod 755 "$_lf.tmp" && mv -f "$_lf.tmp" "$_lf" || { warn "couldn't write $_lf"; return 0; } _png="$_ld/$NAME.png" if [ "$_so" = mac ]; then mac_app "$_af" "$_lf" "$_ld/$NAME.icns" "$(server_label)" "one.kriya.server.$NAME" || { warn "couldn't make $_af"; return 0; } [ "$_desk" = 0 ] || cp -R "$_af" "$_df" || warn "couldn't put \"$(server_label)\" on the Desktop" else _ex=$(printf '%s' "$_lf" | sed 's/[\\"`$]/\\\\&/g; s/%/%%/g') { printf '[Desktop Entry]\nType=Application\nVersion=1.0\nName=%s\nComment=Start, stop, update and set up your Kriya One server\nExec=sh "%s"\n' "$(server_label)" "$_ex" [ ! -f "$_png" ] || printf 'Icon=%s\n' "$_png" printf 'Terminal=false\nStartupNotify=false\nCategories=Utility;\nX-Kriya-One-Server=%s\n' "$NAME" } >"$_af" && chmod 755 "$_af" || { warn "couldn't write $_af"; return 0; } if [ "$_desk" = 1 ]; then cp "$_af" "$_df" && chmod 755 "$_df" || warn "couldn't put \"$(server_label)\" on the Desktop" command -v gio >/dev/null 2>&1 && gio set "$_df" metadata::trusted true >/dev/null 2>&1 || true fi command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$_ad" >/dev/null 2>&1 || true fi : >"$_ld/$NAME-server.made" } # server_shortcut_files: this instance's "Kriya One Server" files removed (_had_sdesk: one was on the Desktop). server_shortcut_files() { _had_sdesk=0 _ld=$(launcher_dir) _ad=$(apps_dir) _dd=$(desktop_dir || true) for _d in "$_ad" ${_dd:+"$_dd"}; do [ -d "$_d" ] || continue if [ "$(shortcut_os)" = mac ]; then for _f in "$_d"/*.app; do [ -f "$_f/Contents/Info.plist" ] && grep -q "one.kriya.server.$NAME" "$_f/Contents/Info.plist" || continue rm -rf "$_f"; [ "$_d" = "$_ad" ] || _had_sdesk=1 done else for _f in "$_d"/*.desktop; do [ -f "$_f" ] && grep -qx "X-Kriya-One-Server=$NAME" "$_f" || continue rm -f "$_f"; [ "$_d" = "$_ad" ] || _had_sdesk=1 done fi done } server_shortcut_remove() { _was=0; [ ! -f "$(launcher_dir)/$NAME-server.sh" ] || _was=1 server_shortcut_files rm -f "$(launcher_dir)/$NAME-server.sh" "$(launcher_dir)/$NAME-server.made" rmdir "$(launcher_dir)" 2>/dev/null || true [ "$(shortcut_os)" != mac ] || rmdir "$(apps_dir)" 2>/dev/null || true [ "$_was" = 0 ] || note "removed the shortcut \"$(server_label)\"" } # cheat_sheet: the kriya-one commands in a few friendly lines (after an install or update, and kriya-one help --short). cheat_sheet() { _nm=$(name_flag) echo say "Your Kriya One${DISPLAY_NAME:+ \"$DISPLAY_NAME\"}: the kriya-one command${_nm:+ (add$_nm to each)}" printf ' %-24s %s\n' \ "kriya-one open" "open it in your browser" \ "kriya-one status" "is it running? its version and address" \ "kriya-one stop / start" "stop it, or start it again" \ "kriya-one restart" "stop and start it" \ "kriya-one update" "get the newest version (keeps your settings)" \ "kriya-one logs" "what it has been doing (logs -f: live)" \ "kriya-one config" "its settings: port, memory, shared folders" \ "kriya-one services" "speech and model servers it uses" \ "kriya-one voice" "where speech runs: browser, servers or off" \ "kriya-one vocab" "words to recognise: list, import FILE" \ "kriya-one brand" "the assistant's name, business and logo" \ "kriya-one join-code" "a code to join a machine with Kriya Host" \ "kriya-one rollback" "go back to the version before the last update" \ "kriya-one uninstall" "remove it (your data stays)" \ "kriya-one menu" "all of this as a menu" \ "kriya-one help" "every command, in detail" if [ "${SHORTCUT:-1}" = 0 ]; then note "The command is in $(cli_bin). The control app: kriya-one panel$_nm (no shortcut: --no-shortcut)." else note "The command is in $(cli_bin). The control app: \"$(server_label)\" on your Desktop and in your apps (or: kriya-one panel$_nm)."; fi } # ---------------------------------------------------------------- the kriya-one command # This script, installed as ~/.local/bin/kriya-one. `kriya-one ` manages an installed Kriya One (the # options saved in ~/Kriya//kriya-one.env); anything else is the installer, as under curl | sh. cli_usage() { cat <<'EOF' kriya-one: manage Kriya One on this machine. kriya-one [--name NAME] [options] status [--json] running or not, health, version, ports, memory/CPU/disk limits, where the data is (--json: for programs; it also answers when Docker isn't running) menu all of this as a numbered menu in the terminal panel the control app ("Kriya One Server"): status and buttons in the browser, on 127.0.0.1 only start | stop | restart logs [-f] [--tail N] the container's log (-f: follow it) open open the app in the browser (the setup link with its code while setup is pending; it prints the HTTPS address for other devices too) update [--check] check ghcr.io for a newer Kriya One; show it and what changed; then, after asking (--yes: don't ask), pull it and recreate the container with the saved options (the old one is kept for kriya-one rollback). --check: only report. It also updates this command itself. config show the saved options config --memory 8g --cpus 4 --disk 60 --share PATH[:NAME][:ro] (repeat) | --no-share | --port P | --bind ADDR --add-share PATH[:NAME][:ro] | --remove-share NAME (repeat; the other shared folders stay) | --shares (list) --display-name TEXT | --accent COLOUR (the badge: kriya-one brand --display-name/--accent does the same) change them and recreate the container (the data stays; the old one is kept for rollback). macOS with Colima: resizes the Colima VM when it's too small, after asking. config --host-ports none|all|LIST which of this machine's ports the assistant may reach (LIST: 22,11434,8000-8010), as Settings → Devices → Network; no recreate: it offers the restart the firewall needs (--yes: restart) rollback swap back to the container from before the last update or config (again: swap forward) uninstall [--purge] remove the container, the kept ones and its shortcut; --purge also deletes the data (after asking) version this command's and the running Kriya One's version help [--short] this list (--short: the short cheat sheet the installer prints) list the Kriya Ones installed here: name, version, URL, health, memory (* the default one) use NAME make NAME the default, so the other commands don't need --name (use --none: no default) install [options] install another one (the installer: kriya-one install --help) adopt --name NAME take over a container made by hand (see kriya-one install --help, --adopt) services [list] the services (speech, local models, embeddings, …) as Settings → Models & services shows them: status, default, address; voice; the firewall ports they need. --json: as JSON services add ADDRESS [--kind asr|tts|llm|embedding|image|vision|video|other] [--label TEXT] [--model M] [--voice V] [--protocol kriya|openai|auto] [--language L] [--default] [--token-stdin] [--no-test] add one (tested first, like Settings' Add: the kind is found when not given); a token only from stdin (echo "$TOKEN" | kriya-one services add … --token-stdin), never on the command line. Speech-to-text (--kind asr) only: --protocol kriya (POST /v1/transcribe), openai (an OpenAI-compatible server: POST /v1/audio/transcriptions, with --model) or auto (found when added, the default); --language: the language hint it gets, e.g. en, hi or en-IN services test ID test it now (exit 1 when it fails) services default ID make it the default of its kind (speech: what "Voice servers" uses) services remove ID remove it, after asking (--yes: don't ask) voice [--mode browser|servers|off] [--asr ID] [--tts ID] [--fallback on|off] where speech runs (no option: show it). servers: the default speech services, or --asr/--tts. --fallback: when a voice server is down, use the browser's voice (on by default). When the firewall must open a port for the services in use, it offers the restart that does it (--yes: restart without asking), as Settings → Restart. vocab list [--json] the words to recognise (Settings → Vocabulary), as lines vocab import reads (--json: as JSON) vocab import FILE|- [--replace] add the words of FILE (or stdin): lines WORD or WORD: SOUNDS LIKE, SOUNDS LIKE (# comments), a Markdown table Word | Sounds like, or the JSON of vocab list --json (another Kriya One's), as Settings → Vocabulary → Import a list reads them. A word there already gets the new variants; --replace: the file becomes the whole list. Yours, so no approval card. brand [--assistant-name NAME] [--org BUSINESS] [--logo FILE|-] [--no-logo] [--display-name TEXT] [--accent COLOUR] the name and brand as Settings → Name and brand shows them (no option: show them). --logo: an SVG, PNG, JPEG or WebP (an SVG is cleaned, as there). --display-name/--accent: the badge of this Kriya One (e.g. #2f9e44), set on the container: it is recreated, as kriya-one config does join-code [--label TEXT] [--url ADDRESS] a join code for Kriya Host (Settings → Machines), with the command to run on the machine to join. --url: the address that machine reaches this one at (default: its public/HTTPS address) --name NAME (or KRIYA_ONE_INSTANCE=NAME): which one. Default: the one `kriya-one use` chose, else the only one there is, else assistant. --data DIR: its data folder, when the saved one isn't found. --yes: no questions. EOF } cli_bin() { echo "${KRIYA_ONE_BIN:-$HOME/.local/bin}"; } rev_of() { sed -n 's/^CLI_REV=\([0-9][0-9]*\).*/\1/p' "$1" 2>/dev/null | head -1; } # fetch_script DIR: the portal's install.sh in DIR/install.sh, checked against its SHA256SUMS. fetch_script() { curl -fsSL -m 30 -o "$1/install.sh" "$DOWNLOAD_BASE/install.sh" && curl -fsSL -m 30 -o "$1/SHA256SUMS" "$DOWNLOAD_BASE/SHA256SUMS" || return 1 _fs=$(pick "$1/SHA256SUMS" '^install\.sh$') [ -n "$_fs" ] && [ "$(sha256 "$1/install.sh")" = "${_fs%% *}" ] || { warn "$DOWNLOAD_BASE/install.sh doesn't match its checksum in SHA256SUMS: not used"; return 1; } } # install_cli: this script as $(cli_bin)/kriya-one (from the file being run, or under curl | sh the portal's copy), # and that folder on PATH. install_cli() { _bd=$(cli_bin) _src="" FETCHED="" if [ -f "$0" ] && [ -n "$(rev_of "$0")" ]; then _src=$0 elif FETCHED=$(mktemp -d) && fetch_script "$FETCHED" && [ -n "$(rev_of "$FETCHED/install.sh")" ]; then _src=$FETCHED/install.sh else [ -z "$FETCHED" ] || rm -rf "$FETCHED" note "(the kriya-one command isn't installed: the installer at $DOWNLOAD_BASE doesn't have it yet)" return 0 fi _dst="$_bd/kriya-one" if ! { [ -f "$_dst" ] && cmp -s "$_src" "$_dst"; }; then { mkdir -p "$_bd" && cp "$_src" "$_dst.tmp" && chmod 755 "$_dst.tmp" && mv -f "$_dst.tmp" "$_dst"; } || { warn "couldn't write $_dst"; [ -z "$FETCHED" ] || rm -rf "$FETCHED"; return 0; } note "The kriya-one command is installed: $_dst" fi [ -z "$FETCHED" ] || rm -rf "$FETCHED" install_panel path_add "$_bd" } # path_add DIR: on PATH in the shell's startup file, when it isn't on PATH now. path_add() { case ":$PATH:" in *":$1:"*) return 0 ;; esac case "$1" in "$HOME"/*) _pd="\$HOME/${1#"$HOME"/}" ;; *) _pd=$1 ;; esac case "${SHELL:-}" in */zsh) _rc="$HOME/.zshrc" ;; */bash) if [ "$(uname -s)" = Darwin ]; then _rc="$HOME/.bash_profile"; else _rc="$HOME/.bashrc"; fi ;; */fish) note "kriya-one is in $1: add it to PATH (fish: fish_add_path $1)"; return 0 ;; *) _rc="$HOME/.profile" ;; esac if grep -qsF "$_pd" "$_rc" || grep -qsF "$1" "$_rc"; then note "kriya-one is in $1 (on PATH in $_rc already: a new terminal finds it)"; return 0 fi # shellcheck disable=SC2016 # $PATH is for the startup file printf '\n# the kriya-one command (Kriya One)\nexport PATH="%s:$PATH"\n' "$_pd" >>"$_rc" || { note "kriya-one is in $1: add that folder to PATH"; return 0; } note "kriya-one is in $1, added to PATH in $_rc: open a new terminal (or run: export PATH=\"$1:\$PATH\")" } # self_update ARGS: kriya-one update first takes the portal's newer copy of this command, then runs it. self_update() { [ "${KRIYA_ONE_FRESH:-0}" != 1 ] || return 0 FETCHED=$(mktemp -d) fetch_script "$FETCHED" 2>/dev/null || { rm -rf "$FETCHED"; return 0; } _new=$FETCHED/install.sh _nr=$(rev_of "$_new") if [ -n "$_nr" ] && [ "$_nr" -ge "$CLI_REV" ] && ! cmp -s "$_new" "$0"; then case " $* " in *" --check "*) note "A newer kriya-one command is available (revision $_nr): kriya-one update installs it." ;; *) _dst="$(cli_bin)/kriya-one" mkdir -p "$(cli_bin)" && cp "$_new" "$_dst.tmp" && chmod 755 "$_dst.tmp" && mv -f "$_dst.tmp" "$_dst" || { rm -rf "$FETCHED"; warn "couldn't update $_dst"; return 0; } rm -rf "$FETCHED" note "The kriya-one command is updated (revision $_nr)." KRIYA_ONE_FRESH=1 exec sh "$_dst" update "$@" ;; esac fi rm -rf "$FETCHED" } # ---------------------------------------------------------------- the registry: is there a newer image? (anonymous, # the Docker Registry HTTP API v2 with the token its 401 names; curl, sed and awk only) # reg_parse IMAGE → REG REPO TAG reg_parse() { _ri=${1%@*} TAG=latest case "${_ri##*/}" in *:*) TAG=${_ri##*:}; _ri=${_ri%:*} ;; esac _rf=${_ri%%/*} case "$_ri" in */*) case "$_rf" in *.*|*:*|localhost) REG=$_rf REPO=${_ri#*/} ;; *) REG=registry-1.docker.io REPO=$_ri ;; esac ;; *) REG=registry-1.docker.io REPO="library/$_ri" ;; esac } ACCEPT='application/vnd.oci.image.index.v1+json,application/vnd.docker.distribution.manifest.list.v2+json,application/vnd.oci.image.manifest.v1+json,application/vnd.docker.distribution.manifest.v2+json' # reg_get PATH OUT → the body in OUT, the headers in OUT.h (a bearer token the first time the registry asks for one) reg_get() { set -- "$1" "$2" -fsSL -m 30 -H "Accept: $ACCEPT" -D "$2.h" -o "$2" [ -z "${REG_TOKEN:-}" ] || set -- "$@" -H "Authorization: Bearer $REG_TOKEN" case "$REG" in localhost|localhost:*|127.0.0.1|127.0.0.1:*) _rs=http ;; *) _rs=https ;; esac _ru="$_rs://$REG/v2/$REPO/$1" _rp=$1 _ro=$2 shift 2 curl "$@" "$_ru" 2>/dev/null && return 0 [ -z "${REG_TOKEN:-}" ] || return 1 _wa=$(curl -sS -m 30 -o /dev/null -D - "$_ru" 2>/dev/null | tr -d '\r' | sed -n 's/^[Ww][Ww][Ww]-[Aa]uthenticate: *[Bb]earer *//p' | head -1) [ -n "$_wa" ] || return 1 _realm=$(echo "$_wa" | sed -n 's/.*realm="\([^"]*\)".*/\1/p') _svc=$(echo "$_wa" | sed -n 's/.*service="\([^"]*\)".*/\1/p') _scope=$(echo "$_wa" | sed -n 's/.*scope="\([^"]*\)".*/\1/p') [ -n "$_realm" ] || return 1 REG_TOKEN=$(curl -fsS -m 30 -G "$_realm" ${_svc:+--data-urlencode "service=$_svc"} --data-urlencode "scope=${_scope:-repository:$REPO:pull}" 2>/dev/null | tr -d ' \n\r\t' | sed -n 's/.*"\(access_\)\{0,1\}token":"\([^"]*\)".*/\2/p' | head -1) [ -n "$REG_TOKEN" ] || return 1 reg_get "$_rp" "$_ro" } json_flat() { tr -d '\n\r\t' <"$1" | sed 's/" *: *"/":"/g; s/" *: *{/":{/g; s/, *"/,"/g; s/{ *"/{"/g'; } # reg_latest IMAGE ARCH → R_DIGEST (the tag's digest), R_PDIGEST (this platform's), R_VERSION, R_REVISION, R_NOTES reg_latest() { reg_parse "$1" _rd=$(mktemp -d) REG_TOKEN="" R_DIGEST="" R_PDIGEST="" R_VERSION="" R_REVISION="" R_NOTES="" reg_get "manifests/$TAG" "$_rd/m" || { rm -rf "$_rd"; return 1; } R_DIGEST=$(tr -d '\r' <"$_rd/m.h" | sed -n 's/^[Dd]ocker-[Cc]ontent-[Dd]igest: *//p' | tail -1) [ -n "$R_DIGEST" ] || R_DIGEST="sha256:$(sha256 "$_rd/m")" _mf=$_rd/m if json_flat "$_rd/m" | grep -q '"manifests":'; then # An index: the manifest for this platform (an entry per {"mediaType" …, its platform inside). R_PDIGEST=$(json_flat "$_rd/m" | awk '{ gsub(/\{"mediaType"/, "\n{\"mediaType\""); print }' | grep "\"architecture\":\"$2\"" | grep '"os":"linux"' | head -1 | sed -n 's/.*"digest":"\(sha256:[0-9a-f]*\)".*/\1/p') [ -n "$R_PDIGEST" ] || { rm -rf "$_rd"; return 1; } reg_get "manifests/$R_PDIGEST" "$_rd/p" || { rm -rf "$_rd"; return 1; } _mf=$_rd/p else R_PDIGEST=$R_DIGEST fi _cfg=$(json_flat "$_mf" | sed -n 's/.*"config":{[^}]*"digest":"\(sha256:[0-9a-f]*\)".*/\1/p') [ -n "$_cfg" ] || { rm -rf "$_rd"; return 1; } reg_get "blobs/$_cfg" "$_rd/c" || { rm -rf "$_rd"; return 1; } json_flat "$_rd/c" >"$_rd/cf" R_VERSION=$(sed -n 's/.*"org\.opencontainers\.image\.version":"\([^"]*\)".*/\1/p' "$_rd/cf") R_REVISION=$(sed -n 's/.*"org\.opencontainers\.image\.revision":"\([^"]*\)".*/\1/p' "$_rd/cf") R_NOTES=$(sed -n 's/.*"one\.kriya\.notes":"\([^"]*\)".*/\1/p' "$_rd/cf") rm -rf "$_rd" } # ver_gt A B: A is a newer version than B (dotted numbers, a leading v left out; a pre-release after - is older than # its release, and rc.5 newer than rc.4) ver_gt() { awk -v a="$1" -v b="$2" 'BEGIN { sub(/^v/, "", a); sub(/^v/, "", b) pa = index(a, "-") ? substr(a, index(a, "-") + 1) : ""; pb = index(b, "-") ? substr(b, index(b, "-") + 1) : "" sub(/-.*/, "", a); sub(/-.*/, "", b) na = split(a, x, "."); nb = split(b, y, "."); n = na > nb ? na : nb for (i = 1; i <= n; i++) { if (x[i] + 0 > y[i] + 0) exit 0; if (x[i] + 0 < y[i] + 0) exit 1 } if (pa == pb || pa == "") exit (pa == pb || pb == "") ? 1 : 0 if (pb == "") exit 1 na = split(pa, x, "."); nb = split(pb, y, "."); n = na > nb ? na : nb for (i = 1; i <= n; i++) { if (x[i] == y[i]) continue if (x[i] ~ /^[0-9]+$/ && y[i] ~ /^[0-9]+$/) exit (x[i] + 0 > y[i] + 0) ? 0 : 1 exit (x[i] > y[i]) ? 0 : 1 } exit 1 }' } engine_arch() { _ea=$($ENGINE version --format '{{.Server.Arch}}' 2>/dev/null || $ENGINE info --format '{{.Host.Arch}}' 2>/dev/null || uname -m) case "$_ea" in x86_64|amd64) echo amd64 ;; aarch64|arm64) echo arm64 ;; *) echo "$_ea" ;; esac } # changes FROM TO: what changed, from the portal's CHANGELOG ("## 0.5.0 …" headings, newest first) or a notes URL. changes() { _cl=$(curl -fsSL -m 15 "$DOWNLOAD_BASE/CHANGELOG" 2>/dev/null || true) _shown=0 if [ -n "$_cl" ]; then _out=$(printf '%s\n' "$_cl" | awk -v from="$1" -v to="$2" ' function gt(a, b, x, y, na, nb, n, i) { sub(/^v/, "", a); sub(/^v/, "", b); sub(/-.*/, "", a); sub(/-.*/, "", b) na = split(a, x, "."); nb = split(b, y, "."); n = na > nb ? na : nb for (i = 1; i <= n; i++) { if (x[i] + 0 > y[i] + 0) return 1; if (x[i] + 0 < y[i] + 0) return 0 } return 0 } /^## / { v = $2; on = (from == "" || gt(v, from)) && (to == "" || !gt(v, to)) } on && shown < 40 { print " " $0; shown++ }') [ -z "$_out" ] || { note "What's new:"; printf '%s\n' "$_out"; _shown=1; } fi [ -z "${R_NOTES:-}" ] || { note "Release notes: $R_NOTES"; _shown=1; } [ "$_shown" = 1 ] || note "(no release notes published: $DOWNLOAD_BASE/CHANGELOG)" } # ---------------------------------------------------------------- the subcommands # cli_name: --name, else the only Kriya One installed here, else assistant; then its data folder and saved options. cli_name() { [ -n "$NAME" ] || NAME=${KRIYA_ONE_INSTANCE:-} if [ -z "$NAME" ]; then NAME=$(cli_name_only) || die "several Kriya Ones here ($(cli_names | tr '\n' ' ' | sed 's/ $//')): say which with --name NAME, or choose a default: kriya-one use NAME" fi NAME=$(check_name "$NAME") || die "$NAME" CNAME="kriya-one-$NAME" [ -n "$KDIR" ] || KDIR=$(saved_dir "$NAME") KDIR=$(expand "${KDIR:-$HOME/Kriya}") case "$KDIR" in /*) ;; *) KDIR="$(pwd)/$KDIR" ;; esac ENVFILE="$KDIR/$NAME/kriya-one.env" load_env || true } cli_names() { { for _f in "$CFGDIR"/*.dir; do [ -f "$_f" ] && basename "$_f" .dir; done for _f in "$HOME"/Kriya/*/kriya-one.env; do [ -f "$_f" ] && basename "$(dirname "$_f")"; done } 2>/dev/null | sort -u } # The default instance (kriya-one use NAME): ~/.config/kriya-one/default, else nothing. default_name() { [ -f "$CFGDIR/default" ] && head -1 "$CFGDIR/default" | grep -E '^[a-z0-9][a-z0-9-]{0,30}$' || true; } # name_flag: " --name NAME" when NAME isn't what a bare kriya-one command picks now, else nothing. name_flag() { _pk=$(cli_name_only 2>/dev/null) && [ "$_pk" = "$NAME" ] && return 0 echo " --name $NAME" } cli_name_only() { _names=$(cli_names) _def=$(default_name) if [ -n "$_def" ] && echo "$_names" | grep -qx "$_def"; then echo "$_def" elif [ -z "$_names" ]; then echo assistant elif [ "$(echo "$_names" | wc -l)" -eq 1 ]; then echo "$_names" elif echo "$_names" | grep -qx assistant; then echo assistant else return 1 fi } need_installed() { exists "$CNAME" || die "no container $CNAME here: install it first (curl -fsSL $DOWNLOAD_BASE/install.sh | sh${NAME:+ -s -- --name $NAME})"; } label() { $ENGINE container inspect --format "{{index .Config.Labels \"$1\"}}" "$CNAME" 2>/dev/null | sed 's/^$//'; } # The panel's host port of the container itself (published 7800, or a host-network one's UPA_PORT), else 8810. live_port() { _lp=$($ENGINE port "$CNAME" 7800/tcp 2>/dev/null | sed -n 's/.*:\([0-9][0-9]*\)$/\1/p' | head -1) [ -n "$_lp" ] || _lp=$($ENGINE container inspect --format '{{range .Config.Env}}{{println .}}{{end}}' "$CNAME" 2>/dev/null | sed -n 's/^UPA_PORT=//p' | head -1) [ -n "$_lp" ] || _lp=$($ENGINE container inspect --format '{{range $k, $v := .HostConfig.PortBindings}}{{if eq $k "7800/tcp"}}{{range $v}}{{.HostPort}}{{end}}{{end}}{{end}}' "$CNAME" 2>/dev/null) echo "${_lp:-8810}" } # The data folder's size in GB (du, at most 20 s), else nothing. disk_used_gb() { _to=""; command -v timeout >/dev/null 2>&1 && _to="timeout 20" _k=$( { $_to du -sk "$1" 2>/dev/null; true; } | awk 'NR == 1 {print $1}') [ -n "$_k" ] && awk -v k="$_k" 'BEGIN { printf "%.1f", k / 1048576 }' } human_bytes() { awk -v b="$1" 'BEGIN { if (b >= 1073741824) printf "%.3g GB", b / 1073741824; else printf "%d MB", b / 1048576 }'; } cli_status() { if ! exists "$CNAME"; then note "$CNAME: not installed here" [ ! -f "$ENVFILE" ] || note " (its data and options are still in $KDIR/$NAME)" return 1 fi _f=$($ENGINE container inspect --format '{{.State.Status}}|{{.State.StartedAt}}|{{.Config.Image}}|{{.HostConfig.Memory}}|{{.HostConfig.NanoCpus}}|{{.HostConfig.NetworkMode}}' "$CNAME") _state=$(echo "$_f" | cut -d'|' -f1) _since=$(echo "$_f" | cut -d'|' -f2) _img=$(echo "$_f" | cut -d'|' -f3) _mem=$(echo "$_f" | cut -d'|' -f4) _cpu=$(echo "$_f" | cut -d'|' -f5) _net=$(echo "$_f" | cut -d'|' -f6) PORT=${PORT:-$(live_port)} _health="not running" if [ "$_state" = running ]; then if curl -fsS -m 5 "http://localhost:$PORT/healthz" >/dev/null 2>&1; then _health="healthy (answers on http://localhost:$PORT)"; else _health="running, but the panel doesn't answer yet (kriya-one logs$(name_flag))"; fi fi _v=$(label org.opencontainers.image.version) say "Kriya One \"$NAME\"${DISPLAY_NAME:+ ($DISPLAY_NAME)} — $CNAME: $_state" note " health: $_health" note " URL: http://localhost:$PORT/ (this machine)$( [ -z "$(remote_url)" ] || echo " $(remote_url)/ (other devices)")" [ "$_state" != running ] || note " since: $_since" note " version: ${_v:-unknown} image: $_img" if [ "$_net" = host ]; then note " ports: $BIND:$PORT (panel), $((PORT + 100)) (canvas), host network" else note " ports: ${BIND:-127.0.0.1}:$PORT (panel), ${BIND:-127.0.0.1}:$((PORT + 100)) (canvas), ${NETWORK:-bridge} network ($_net)" fi if [ "${_mem:-0}" -gt 0 ] 2>/dev/null; then _ml=$(human_bytes "$_mem"); else _ml="none"; fi if [ "${_cpu:-0}" -gt 0 ] 2>/dev/null; then _cl=$(awk -v n="$_cpu" 'BEGIN { printf "%g", n / 1e9 }'); else _cl="all"; fi _use="" [ "$_state" != running ] || _use=$($ENGINE stats --no-stream --format '{{.MemUsage}} · CPU {{.CPUPerc}}' "$CNAME" 2>/dev/null | head -1) note " memory: limit $_ml cpus: $_cl${_use:+ now: $_use}" _du=$(disk_used_gb "$KDIR/$NAME") note " disk: ${_du:+$_du GB used of }a soft cap of ${DISK:-50} GB (a warning in Settings above 90 %, not enforced) $(free_gb "$KDIR" || echo '?') GB free there" note " data: $KDIR/$NAME options: $ENVFILE" note " /shared: ${SHARED_DIR:-$KDIR/shared}" [ -z "${SHARES:-}" ] || note " shared: $(echo "$SHARES" | tr '|' '\n' | sed 's#^\(.*\):\([^:]*\):\([^:]*\)$#/shared/\2 (\3) ← \1#' | paste -sd ',' - | sed 's/,/, /g')" _kept=$(containers_named "kriya-one-$NAME-old-[0-9-]+" | sort | tr '\n' ' ') [ -z "$_kept" ] || note " kept: $_kept(kriya-one rollback)" return 0 } cli_shares() { _nm=$(name_flag) if [ -z "${SHARES:-}" ]; then note "Kriya One \"$NAME\" has no shared folders (besides its own ${SHARED_DIR:-$KDIR/shared} at /shared)." else say "Kriya One \"$NAME\": shared folders" echo "$SHARES" | tr '|' '\n' | sed 's#^\(.*\):\([^:]*\):\([^:]*\)$# /shared/\2 \3 ← \1#' fi note "Add one: kriya-one config$_nm --add-share PATH[:NAME][:ro] remove one: kriya-one config$_nm --remove-share NAME" } cli_show_config() { [ -f "$ENVFILE" ] || die "no saved options in $ENVFILE" say "Kriya One \"$NAME\": the saved options ($ENVFILE)" note " port: ${PORT:-} (canvas $(( ${PORT:-8810} + 100 ))) bind: ${BIND:-127.0.0.1} network: ${NETWORK:-bridge}" note " image: ${IMAGE:-}" note " memory: ${MEMORY:-} cpus: ${CPUS:-all} disk soft cap: ${DISK:-} GB" note " data: $KDIR/$NAME" if [ -n "${SHARES:-}" ]; then echo "$SHARES" | tr '|' '\n' | sed 's#^\(.*\):\([^:]*\):\([^:]*\)$# share: \1 → /shared/\2 (\3)#' else note " shares: none" fi _nm=$(name_flag) note "Change them: kriya-one config$_nm --memory 8g --cpus 4 --disk 60 --share PATH:NAME[:ro]" } # colima_fit MEMORY CPUS DISK: macOS with Colima, a VM too small for the new limits grows (after asking). colima_fit() { [ "$(uname -s)" = Darwin ] && command -v colima >/dev/null 2>&1 && colima status >/dev/null 2>&1 || return 0 _prof=${COLIMA_PROFILE:-default} _row=$(colima list 2>/dev/null | awk -v p="$_prof" '$1 == p { print $4, $5, $6 }') [ -n "$_row" ] || return 0 _vc=$(echo "$_row" | awk '{ print int($1) }') _vm=$(echo "$_row" | awk '{ print int($2) }') _vd=$(echo "$_row" | awk '{ print int($3) }') case "$1" in *g) _wm=${1%g} ;; *m) _wm=$(( (${1%m} + 1023) / 1024 )) ;; *) _wm=4 ;; esac _wm=$(( _wm + 2 )) _wc=$(echo "${2:-0}" | awk '{ n = $1 + 0; print (n == int(n)) ? n : int(n) + 1 }') _wd=$(( ${3:-50} + 20 )) _nm=$_vm _nc=$_vc _nd=$_vd [ "$_wm" -le "$_vm" ] || _nm=$_wm [ "$_wc" -le "$_vc" ] || _nc=$_wc [ "$_wd" -le "$_vd" ] || _nd=$_wd [ "$_nm" != "$_vm" ] || [ "$_nc" != "$_vc" ] || [ "$_nd" != "$_vd" ] || return 0 say "Colima's VM has $_vm GB of memory, $_vc CPUs and a $_vd GB disk; the new limits want $_nm GB, $_nc CPUs and $_nd GB." if [ "$YES" != 1 ]; then [ "$TTY" = 1 ] || { warn "run it in a terminal to answer, or pass --yes; the VM stays as it is"; return 0; } yesno "Resize it now? Everything in Colima stops for a minute (y/n)" y || { note "the VM stays as it is: the container may get less than its limit"; return 0; } fi colima stop --profile "$_prof" || die "colima stop failed (above)" set -- --profile "$_prof" --memory "$_nm" --cpu "$_nc" [ "$_nd" = "$_vd" ] || set -- "$@" --disk "$_nd" colima start "$@" || die "colima didn't start again (above): colima start $*" docker info >/dev/null 2>&1 || die "Colima is running but docker can't reach it: docker context use colima" } # engine_up: Docker (or Podman) answers here. engine_up() { { command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; } || { command -v podman >/dev/null 2>&1 && podman info >/dev/null 2>&1; }; } # docker_hint: what starts the container engine on this machine. docker_hint() { case "$(uname -s)" in Darwin) if [ -x "$KENG/bin/colima" ]; then echo "Kriya One's engine (Colima, in $KENG) isn't running. kriya-one start starts it (or in Terminal: . \"$KENG/env.sh\" && colima start)." elif command -v colima >/dev/null 2>&1; then echo "Docker isn't running. Start it in Terminal: colima start" elif [ -d /Applications/Docker.app ]; then echo "Docker isn't running. Open Docker Desktop (Applications), wait for it to say it is running, then try again." elif [ -d /Applications/OrbStack.app ]; then echo "Docker isn't running. Open OrbStack (Applications), then try again." else echo "Docker isn't installed. Run the Kriya One install command again: it sets Docker up."; fi ;; *) if [ -x "$KENG/bin/dockerd-rootless.sh" ]; then echo "Kriya One's engine (rootless Docker, in $KENG) isn't running. kriya-one start starts it." elif grep -qsi microsoft /proc/version; then echo "Docker isn't running. Start Docker Desktop on Windows, then try again." elif command -v docker >/dev/null 2>&1; then echo "Docker isn't running. Start it in a terminal: sudo systemctl start docker" elif command -v podman >/dev/null 2>&1; then echo "Podman doesn't answer. In a terminal: podman info" else echo "Docker isn't installed. Run the Kriya One install command again: it sets Docker up."; fi ;; esac } # jstr TEXT: TEXT as a JSON string. jstr() { printf '"%s"' "$(printf '%s' "$1" | tr -d '\000-\011\013-\037' | sed 's/\\/\\\\/g; s/"/\\"/g' | awk 'NR > 1 { printf "\\n" } { printf "%s", $0 }')"; } # kriya-one status --json: the status for the control app (kriya-one-panel), one line of JSON. cli_status_json() { _inst=false _state=missing _health=stopped _v="" _img=${IMAGE:-} _since="" _st="" _kept="" if exists "$CNAME"; then _inst=true _f=$($ENGINE container inspect --format '{{.State.Status}}|{{.State.StartedAt}}|{{.Config.Image}}' "$CNAME") _state=${_f%%|*} _since=$(echo "$_f" | cut -d'|' -f2) _img=${_f##*|} _v=$(label org.opencontainers.image.version) PORT=${PORT:-$(live_port)} if [ "$_state" = running ]; then _st=$(curl -fsS -m 4 "http://127.0.0.1:$PORT/api/one/status" 2>/dev/null || true) if [ -n "$_st" ]; then _health=healthy; else _health=starting; fi fi _kept=$(containers_named "kriya-one-$NAME-old-[0-9-]+" | sort | tail -1) fi _get() { printf '%s' "$_st" | sed -nE "s/.*\"$1\":(true|false|\"[^\"]*\").*/\1/p" | tr -d '"'; } _b() { case "$1" in true|false) echo "$1" ;; *) echo null ;; esac; } # The control app (KRIYA_ONE_PANEL=1, its page on 127.0.0.1 behind its token) shows the setup code while setup isn't # finished, for another device (0.4.9 R6); nothing else gets it here (kriya-one open prints it in a terminal). _sc="" if [ "${KRIYA_ONE_PANEL:-}" = 1 ] && [ "$(_get pending)" = true ]; then _code=$($ENGINE logs "$CNAME" 2>&1 | sed -n 's/.*first-run setup code: \([A-Z0-9]\{4\}-[A-Z0-9]\{4\}\).*/\1/p' | tail -1) [ -z "$_code" ] || _sc=",\"setupCode\":\"$_code\"" fi printf '{"name":%s,"display":%s,"docker":true,"engineCmd":%s,"installed":%s,"saved":%s,"state":%s,"health":%s,"since":%s,"version":%s,"image":%s,"port":%s,"url":%s,"otherUrl":%s,"engine":%s,"pending":%s,"setupDone":%s,"kept":%s,"data":%s,"memory":%s,"cpus":%s,"bind":%s,"cli":%s,"cliRev":%s%s}\n' \ "$(jstr "$NAME")" "$(jstr "${DISPLAY_NAME:-}")" "$(jstr "$ENGINE")" "$_inst" "$([ -f "$ENVFILE" ] && echo true || echo false)" "$(jstr "$_state")" "$(jstr "$_health")" "$(jstr "$_since")" \ "$(jstr "$_v")" "$(jstr "$_img")" "${PORT:-null}" "$(jstr "${PORT:+http://localhost:$PORT/}")" "$(jstr "$(remote_url)")" "$(jstr "$(_get engine)")" "$(_b "$(_get pending)")" "$(_b "$(_get setupDone)")" \ "$(jstr "$_kept")" "$(jstr "$KDIR/$NAME")" "$(jstr "${MEMORY:-}")" "$(jstr "${CPUS:-all}")" "$(jstr "${BIND:-127.0.0.1}")" "$(jstr "$0")" "$CLI_REV" "$_sc" } # kriya-one menu: everything as a numbered menu in the terminal (the control app's fallback). cli_menu() { (: /dev/null || die "kriya-one menu needs a terminal (or: kriya-one help)" _self=$0 while :; do _s="not installed" ! exists "$CNAME" || _s=$($ENGINE container inspect --format '{{.State.Status}}' "$CNAME" 2>/dev/null) echo say "Kriya One \"$NAME\"${DISPLAY_NAME:+ ($DISPLAY_NAME)}: $_s" printf ' %-4s %s\n' 1 "Open Kriya One" 2 "Status" 3 "Start" 4 "Stop" 5 "Restart" 6 "Update to the newest version" \ 7 "Show the log (last 200 lines)" 8 "Follow the log (Ctrl-C to stop)" 9 "Settings (config)" 10 "Services" 11 "Voice" \ 12 "Roll back to the version before" 13 "Uninstall (your data stays)" 14 "The control app" h "Help: every command" q "Quit" printf 'Choose: ' >/dev/tty IFS= read -r _c /dev/tty IFS= read -r _c /dev/null; then TTY=1; fi if [ "$_sub" = list ]; then cli_list; return 0; fi if [ "$_sub" = help ]; then if [ "$_short" = 1 ]; then cli_name; cheat_sheet; else cli_usage; fi return 0 fi # The control app asks for the status also when Docker isn't running: then it says so, with what starts it. if [ "$_sub" = status ] && [ "$_json" = 1 ] && ! engine_up; then cli_name printf '{"name":%s,"display":%s,"docker":false,"dockerHint":%s,"saved":%s,"cli":%s,"cliRev":%s}\n' "$(jstr "$NAME")" "$(jstr "${DISPLAY_NAME:-}")" \ "$(jstr "$(docker_hint)")" "$([ -f "$ENVFILE" ] && echo true || echo false)" "$(jstr "$0")" "$CLI_REV" return 0 fi find_engine cli_name _nm=$(name_flag) case "$_sub" in menu) cli_menu ;; panel) _pb=$(panel_bin) if [ -n "$_pb" ] && [ -x "$_pb" ]; then exec "$_pb" --name "$NAME"; fi note "(the control app isn't installed here: the menu instead)" cli_menu ;; version) note "kriya-one command: revision $CLI_REV ($0)" if exists "$CNAME"; then note "Kriya One \"$NAME\": $(label org.opencontainers.image.version || true) ($($ENGINE container inspect --format '{{.Config.Image}}' "$CNAME"), revision $(label org.opencontainers.image.revision || true))" else note "Kriya One \"$NAME\": not installed here"; fi ;; status) if [ "$_json" = 1 ]; then cli_status_json; else cli_status; fi ;; start) need_installed $ENGINE start "$CNAME" >/dev/null PORT=${PORT:-$(live_port)}; wait_ready say "Kriya One \"$NAME\" is running: $URL/" ;; stop) need_installed $ENGINE stop "$CNAME" >/dev/null say "Kriya One \"$NAME\" is stopped (kriya-one start$_nm starts it; it doesn't start with the machine until then)" ;; restart) need_installed say "Restarting ${CNAME}…" $ENGINE restart "$CNAME" >/dev/null PORT=${PORT:-$(live_port)}; wait_ready say "Kriya One \"$NAME\" is running again: $URL/" ;; logs) need_installed if [ "$_follow" = 1 ]; then exec $ENGINE logs --tail "$_tail" -f "$CNAME"; else $ENGINE logs --tail "$_tail" "$CNAME"; fi ;; open) need_installed PORT=${PORT:-$(live_port)} # While setup is pending: the setup page (no code on this machine, when the server says so), and for other # devices the link with its setup code (from the log). URL="http://localhost:$PORT" SETUP_PATH=/ LOCAL_PATH=/ _code= _st=$(curl -fsS -m 5 "$URL/api/one/status" 2>/dev/null || true) if echo "$_st" | grep -q '"pending":true'; then _code=$($ENGINE logs "$CNAME" 2>&1 | sed -n 's/.*first-run setup code: \([A-Z0-9-]*\).*/\1/p' | tail -1) [ -z "$_code" ] || SETUP_PATH="/setup?code=$_code" if echo "$_st" | grep -q '"codeNeeded":false'; then LOCAL_PATH=/setup; else LOCAL_PATH=$SETUP_PATH; fi fi echo "$URL$LOCAL_PATH" [ -z "$(remote_url)" ] || echo "$(remote_url)$SETUP_PATH (other devices)" [ -z "$_code" ] || [ "$LOCAL_PATH" != /setup ] || echo "setup code for another device: $_code" open_url "$URL$LOCAL_PATH" ;; update) cli_update ;; config) if [ "$_list" = 1 ] && [ -z "$_cfg" ]; then cli_shares; return 0; fi # --host-ports: alone, the restart its firewall needs is offered; with other options, their recreate applies it. if [ "$_hpset" = 1 ]; then set -- --name "$NAME" --host-ports "$_hp" [ -z "$KDIR" ] || set -- "$@" --data "$KDIR" [ "$YES" != 1 ] || set -- "$@" --yes if [ -z "$_cfg" ]; then cli_services network "$@"; return; fi _recreate=1; cli_services network "$@" || exit $?; _recreate=0 YES=1 fi if [ -z "$_cfg" ]; then cli_show_config; return 0; fi # --add-share / --remove-share: the saved list, changed (the installer's checks for each new one). if [ -n "$_adds$_rms" ]; then [ -z "$_shares" ] && [ "$_nosh" = 0 ] || die "--share/--no-share replace the list: use them, or --add-share/--remove-share, not both" for _r in $_rms; do echo "${SHARES:-}" | tr '|' '\n' | grep -q ":$_r:r[ow]\$" || die "no shared folder named $_r (kriya-one config --shares lists them)" SHARES=$(echo "${SHARES:-}" | tr '|' '\n' | grep -v ":$_r:r[ow]\$" | paste -sd '|' - || true) done if [ -n "$_adds" ]; then _old_ifs=$IFS; IFS=' ' set -f for _s in $_adds; do IFS=$_old_ifs; add_share "$_s"; done set +f; IFS=$_old_ifs fi if [ -n "${SHARES:-}" ]; then _shares=$(echo "$SHARES" | tr '|' '\n'); else _nosh=1; fi fi [ -f "$ENVFILE" ] || die "no saved options in $ENVFILE: install it first" need_installed # Check the new values first: nothing changes when one is wrong. [ -z "$_cm" ] || { _cm=$(check_memory "$_cm") || die "$_cm"; } [ -z "$_cc" ] || { _cc=$(check_cpus "$_cc") || die "$_cc"; } [ -z "$_cd" ] || { _cd=$(check_disk "$_cd") || die "$_cd"; } colima_fit "${_cm:-$MEMORY}" "${_cc:-$CPUS}" "${_cd:-$DISK}" # Then the installer's --upgrade with these options and the image the container has (no pull). set -- --upgrade --yes --no-open --name "$NAME" --data "$KDIR" for _k in memory cpus disk port bind network image; do eval "_v=\${_opt_$_k-}" eval "[ -z \"\${_opt_$_k+x}\" ]" || set -- "$@" "--$_k" "$_v" done [ -z "${_opt_accent+x}" ] || set -- "$@" --accent "$_opt_accent" [ -z "${_opt_display_name+x}" ] || set -- "$@" --display-name "$_opt_display_name" [ "$_nosh" = 0 ] || set -- "$@" --no-share if [ -n "$_shares" ]; then _old_ifs=$IFS; IFS=' ' set -f for _s in $_shares; do set -- "$@" --share "$_s"; done set +f; IFS=$_old_ifs fi say "Recreating $CNAME with the new options (the data stays in $KDIR/$NAME)…" KRIYA_ONE_NOPULL=1 KRIYA_ONE_QUIET_ENGINE=1 main "$@" ;; rollback) KRIYA_ONE_QUIET_ENGINE=1 main --rollback --no-open --name "$NAME" --data "$KDIR" ;; uninstall) _yes=$YES KRIYA_ONE_PURGE=$_purge KRIYA_ONE_QUIET_ENGINE=1 main --uninstall --name "$NAME" --data "$KDIR" YES=$_yes if [ "$_purge" = 1 ]; then if [ "$YES" != 1 ]; then [ "$TTY" = 1 ] || die "--purge deletes $KDIR/$NAME: run it in a terminal to answer, or add --yes" yesno "Delete $KDIR/$NAME (its chats, files, settings and sign-ins) for good? (y/n)" n || { note "kept: $KDIR/$NAME"; return 0; } fi # Files the container made as another user (rootful Podman): removed from a container, as root there. rm -rf "${KDIR:?}/${NAME:?}" 2>/dev/null || $ENGINE run --rm -u 0 --entrypoint rm -v "$KDIR:/k" "${IMAGE:-$DEFAULT_IMAGE}" -rf "/k/$NAME" >/dev/null 2>&1 || true [ ! -e "$KDIR/$NAME" ] || die "couldn't delete all of $KDIR/$NAME: delete it yourself" rm -f "$CFGDIR/$NAME.dir" [ "$(default_name)" != "$NAME" ] || rm -f "$CFGDIR/default" say "Deleted $KDIR/$NAME." if [ -z "$(cli_names)" ]; then rm -f "$(cli_bin)/kriya-one" "$(cli_bin)/kriya-one-panel" note "No Kriya One is left here: the kriya-one command is removed too, and its control app." fi fi ;; *) die "unknown command: $_sub (kriya-one help)" ;; esac } # kriya-one services … / voice … / vocab … / brand … / join-code … (and config --host-ports: network): Settings → # Models & services, Voice, Vocabulary, Brand, Machines → join code and Devices → Network from the command line. # The container's server does the work (server/onecli-run.ts asks it over a socket only its uid can open: the same # calls as Settings, so the registry, the firewall file and the restart it needs are the same). This side takes --name, # --data and --yes, asks before a remove, sends a file of this machine in on stdin (vocab import FILE, brand --logo # FILE), recreates the container for the badge (brand --accent/--display-name, as config), and offers the restart that # opens the firewall for a new service or host port (_recreate=1: a recreate follows, which applies it). cli_services() { _what=$1; shift KDIR="${KRIYA_DIR:-}" YES="${KRIYA_ONE_YES:-0}" TTY=0 _stdin="" _in="" _imp=0 _bacc="" _bdisp="" _bdset=0 _n=$# while [ "$_n" -gt 0 ]; do _a=$1; shift; _n=$((_n - 1)) case "$_a" in --*=*) [ "$_a" = "${_a#--name=}" ] && [ "$_a" = "${_a#--data=}" ] && { set -- "${_a#*=}" "$@"; _a=${_a%%=*}; _n=$((_n + 1)); } ;; esac case "$_a" in --name|--data) [ "$_n" -gt 0 ] || die "$_a needs a value" if [ "$_a" = --name ]; then NAME=$1; else KDIR=$1; fi shift; _n=$((_n - 1)) ;; --name=*) NAME=${_a#*=} ;; --data=*) KDIR=${_a#*=} ;; --yes|-y) YES=1 ;; -h|--help) cli_usage; return 0 ;; --token-stdin) _stdin=-i; set -- "$@" "$_a" ;; --logo) [ "$_n" -gt 0 ] || die "--logo needs a file (or - for stdin)" _v=$1; shift; _n=$((_n - 1)); _stdin=-i if [ "$_v" != - ]; then { [ -f "$_v" ] && [ -r "$_v" ]; } || die "--logo $_v: no such file here"; _in=$_v; fi set -- "$@" --logo - ;; --accent|--display-name) if [ "$_what" != brand ]; then set -- "$@" "$_a"; continue; fi [ "$_n" -gt 0 ] || die "$_a needs a value" if [ "$_a" = --accent ]; then _bacc=$1; else _bdisp=$1; _bdset=1; fi shift; _n=$((_n - 1)) ;; *) # vocab import FILE: the file of this machine goes in on stdin, as "-". if [ "$_what" = vocab ] && [ "$_imp" = 1 ] && { [ "$_a" = - ] || [ "${_a#-}" = "$_a" ]; }; then _imp=2 _stdin=-i if [ "$_a" != - ]; then { [ -f "$_a" ] && [ -r "$_a" ]; } || die "vocab import $_a: no such file here"; _in=$_a; fi set -- "$@" - else [ "$_what" != vocab ] || [ "$_a" != import ] || [ "$_imp" != 0 ] || _imp=1 set -- "$@" "$_a" fi ;; esac done if [ "$YES" != 1 ] && (: /dev/null; then TTY=1; fi find_engine cli_name _nm=$(name_flag) need_installed _badge=0; { [ -z "$_bacc" ] && [ "$_bdset" = 0 ]; } || _badge=1 # brand with only the badge: nothing for the server, only the recreate. if [ "$_badge" = 1 ] && [ $# -eq 0 ]; then cli_badge; return; fi [ "$($ENGINE container inspect --format '{{.State.Running}}' "$CNAME" 2>/dev/null)" = true ] || die "Kriya One \"$NAME\" isn't running: kriya-one start$_nm" # Kriya One before 0.4.7 has no command line for this: say so rather than node's "Cannot find module". $ENGINE exec "$CNAME" test -f /opt/upa/server/onecli-run.ts 2>/dev/null || die "Kriya One \"$NAME\" ($(label org.opencontainers.image.version || true)) is older than 0.4.7, which has kriya-one $_what: kriya-one update$_nm first, or use Settings" case "$_what" in services|voice) ;; *) $ENGINE exec "$CNAME" grep -q join-code /opt/upa/server/onecli-run.ts 2>/dev/null || die "Kriya One \"$NAME\" ($(label org.opencontainers.image.version || true)) is older than 0.4.9, which has kriya-one $(if [ "$_what" = network ]; then echo 'config --host-ports'; else echo "$_what"; fi): kriya-one update$_nm first, or use Settings" ;; esac _run="$ENGINE exec $_stdin -u upa $CNAME node /opt/upa/server/onecli-run.ts $_what" if [ "$_what" = services ] && [ "${1:-}" = remove ]; then [ $# -ge 2 ] || die "kriya-one services remove ID (kriya-one services list shows the ids)" if [ "$YES" != 1 ]; then [ "$TTY" = 1 ] || die "kriya-one services remove asks first: run it in a terminal to answer, or add --yes" _desc=$($_run show "$2") || exit 1 yesno "Remove the service $2: $_desc? Kriya One stops using it (y/n)" n || { note "kept: $2"; return 0; } fi fi _rc=0 if [ -n "$_in" ]; then $_run "$@" <"$_in" || _rc=$?; else $_run "$@" || _rc=$?; fi if [ "$_rc" = 0 ] && [ "$_badge" = 1 ]; then cli_badge; return; fi [ "$_rc" = 3 ] || return "$_rc" # A recreate follows (config with other options too): it applies the firewall. [ "${_recreate:-0}" != 1 ] || return 0 # 3: done, and the firewall lets the new service through only after a restart (Settings → Restart). if [ "$YES" = 1 ] || { [ "$TTY" = 1 ] && yesno "Restart Kriya One \"$NAME\" now so its firewall lets them through? (y/n)" y; }; then say "Restarting ${CNAME}…" $ENGINE restart "$CNAME" >/dev/null PORT=${PORT:-$(live_port)}; wait_ready say "Kriya One \"$NAME\" is running again: $URL/" else note "Not restarted: kriya-one restart$_nm lets them through." fi } # brand --accent / --display-name: the badge the installer sets on the container (KRIYA_ONE_ACCENT / # KRIYA_ONE_DISPLAY_NAME), so it is recreated with them, as kriya-one config does (the data stays). cli_badge() { set -- config --name "$NAME" [ -z "$KDIR" ] || set -- "$@" --data "$KDIR" [ -z "$_bacc" ] || set -- "$@" --accent "$_bacc" [ "$_bdset" != 1 ] || set -- "$@" --display-name "$_bdisp" cli "$@" } # kriya-one use NAME: the default instance for the commands without --name (~/.config/kriya-one/default). cli_use() { case "${1:-}" in "") _d=$(default_name); if [ -n "$_d" ]; then note "the default Kriya One is \"$_d\" (kriya-one use NAME changes it; kriya-one use --none: no default)"; else note "no default chosen (kriya-one use NAME): $(cli_name_only 2>/dev/null || echo 'say --name NAME each time')"; fi; return 0 ;; --none) rm -f "$CFGDIR/default"; note "no default Kriya One: the commands take the only one, else --name"; return 0 ;; -h|--help) cli_usage; return 0 ;; esac _u=$(check_name "$1") || die "$_u" cli_names | grep -qx "$_u" || die "no Kriya One \"$_u\" here (kriya-one list)" mkdir -p "$CFGDIR" && printf '%s\n' "$_u" >"$CFGDIR/default" || die "couldn't write $CFGDIR/default" say "The default Kriya One is now \"$_u\": kriya-one status, logs, update, … act on it without --name." } # kriya-one list: every instance (the * one is the default), with its version, address, health and memory. cli_list() { _names=$(cli_names) [ -n "$_names" ] || { note "no Kriya One installed here"; return 0; } find_engine _def=$(cli_name_only 2>/dev/null || true) printf ' %-16s %-10s %-9s %-34s %-10s %s\n' NAME VERSION HEALTH URL MEMORY "OTHER DEVICES" for _n in $_names; do NAME=$_n CNAME="kriya-one-$_n" KDIR=$(saved_dir "$_n") PORT="" HTTPS_URL="" PUBLIC_URL="" MEMORY="" DISPLAY_NAME="" DISPLAY_SET="" KDIR=$(expand "${KDIR:-$HOME/Kriya}") ENVFILE="$KDIR/$_n/kriya-one.env" IMAGE="" CPUS="" CPUS_SET="" DISK="" SHARES="" SHARES_SET=0 NETWORK="" BIND="" ACCENT="" SHARED_DIR="" DOCKER_NET="" HTTPS_SAVED="" load_env || true _v="" _h="-" _m="-" if exists "$CNAME"; then [ -n "$PORT" ] || PORT=$(live_port) _v=$(label org.opencontainers.image.version) _f=$($ENGINE container inspect --format '{{.State.Status}}|{{.HostConfig.Memory}}' "$CNAME") _h=${_f%%|*} _mb=${_f#*|} if [ "$_h" = running ]; then curl -fsS -m 3 "http://localhost:$PORT/healthz" >/dev/null 2>&1 && _h=healthy || _h=starting; fi if [ "${_mb:-0}" -gt 0 ] 2>/dev/null; then _m=$(human_bytes "$_mb" | tr -d ' '); else _m=none; fi else _h="removed" fi _mark=" "; [ "$_n" != "$_def" ] || _mark="*" printf '%s %-16s %-10s %-9s %-34s %-10s %s\n' "$_mark" "$_n" "${_v:--}" "$_h" "${PORT:+http://localhost:$PORT/}" "$_m" "$(remote_url)" done [ -z "$_def" ] || note "(* the default: kriya-one use NAME changes it)" } cli_update() { need_installed _img=$($ENGINE container inspect --format '{{.Config.Image}}' "$CNAME") _cur=$(label org.opencontainers.image.version) _iid=$($ENGINE container inspect --format '{{.Image}}' "$CNAME") _have=$($ENGINE image inspect --format '{{range .RepoDigests}}{{.}} {{end}}' "$_iid" 2>/dev/null || true) case "$_img" in */*) ;; *) note "$CNAME runs a local image ($_img): it isn't in a registry, so there is nothing to check. Rebuild it, then: kriya-one config$_nm --image $_img"; return 0 ;; esac # Its images moved (the old address stays as a mirror): check and update from the new one, same tag. _moved=0 if _mv=$(moved_image "$_img"); then _img=$_mv _moved=1; say "Kriya One now comes from ${DEFAULT_IMAGE%:*}"; fi reg_parse "$_img" # A version tag (…:0.4.0) follows latest; any other tag (latest, a channel) follows itself. _chan=$TAG echo "$TAG" | grep -Eq '^v?[0-9]+(\.[0-9]+)+' && _chan=latest _ref="${_img%:*}"; case "${_img##*/}" in *:*) ;; *) _ref=$_img ;; esac _ref="${_ref%@*}" note "Kriya One \"$NAME\": ${_cur:-unknown version} ($_img). Checking $_ref:${_chan}…" reg_latest "$_ref:$_chan" "$(engine_arch)" || die "couldn't read $_ref:$_chan from its registry (offline? the image private?)" _avail=0 case " $_have " in *"@$R_DIGEST "*|*"@$R_PDIGEST "*) _avail=0 ;; *) if [ -z "$_cur" ] || [ -z "$R_VERSION" ] || ver_gt "$R_VERSION" "$_cur"; then _avail=1 elif [ "$R_VERSION" = "$_cur" ]; then _avail=2 else _avail=3 fi ;; esac case "$_avail" in 0) say "Up to date: $_ref:$_chan is ${R_VERSION:-this one} ($(echo "${R_DIGEST#sha256:}" | cut -c1-12)…), the one running." [ "$_moved" = 1 ] || return 0 note "It moves to $_ref:$_chan (the same version)." ;; 3) say "Up to date: this one ($_cur) is newer than $_ref:$_chan ($R_VERSION)." [ "$_moved" = 1 ] || return 0 note "It moves to $_ref:$_chan." ;; 1) say "A newer Kriya One is available: ${R_VERSION:-?} (running: ${_cur:-?})${R_REVISION:+, revision $R_REVISION}" changes "$_cur" "$R_VERSION" ;; 2) say "A newer build of $R_VERSION is available (the same version, rebuilt${R_REVISION:+: revision $R_REVISION})" ;; esac if [ "$_check" = 1 ]; then note "To install it: kriya-one update$_nm"; return 0; fi if [ "$YES" != 1 ]; then [ "$TTY" = 1 ] || die "run it in a terminal to answer, or: kriya-one update$_nm --yes" yesno "Update $CNAME now? It restarts (a minute or two); the old container is kept for kriya-one rollback (y/n)" y || { note "nothing changed"; return 0; } fi KRIYA_ONE_QUIET_ENGINE=1 main --upgrade --yes --no-open --name "$NAME" --data "$KDIR" --image "$_ref:$_chan" } # share_overlap: a folder this one shares that another Kriya One here shares too (each is someone's own assistant). share_overlap() { [ -n "$SHARES" ] || return 0 for _of in "$HOME"/Kriya/*/kriya-one.env "$KDIR"/*/kriya-one.env; do [ -f "$_of" ] && [ "$_of" != "$ENVFILE" ] || continue _osh=$(sed -n 's/^KRIYA_ONE_SHARES=//p' "$_of" | head -1) [ -n "$_osh" ] || continue _old_ifs=$IFS; IFS='|' for _e in $SHARES; do for _o in $_osh; do [ "${_e%:*:*}" != "${_o%:*:*}" ] || warn "${_e%:*:*} is shared with the Kriya One \"$(basename "$(dirname "$_of")")\" too: both assistants see it"; done done IFS=$_old_ifs done } # --adopt: a container made by hand (docker run, the layout of INSTALL-ONE.md: ~/Kriya/NAME/{data,home}) becomes a # managed one: its settings are read into kriya-one.env as they are (a memory limit of none stays none), so a later # kriya-one config/update recreates it with the same ports, limits, mounts and image. Nothing is recreated now. adopt() { exists "$CNAME" || die "no container $CNAME to adopt (docker ps -a; --name NAME for kriya-one-NAME)" [ ! -f "$ENVFILE" ] || die "$CNAME is managed already ($ENVFILE): kriya-one config --name $NAME" _ins() { $ENGINE container inspect --format "$1" "$CNAME"; } _env=$(_ins '{{range .Config.Env}}{{println .}}{{end}}') _ev() { printf '%s\n' "$_env" | sed -n "s/^$1=//p" | head -1; } _mounts=$(_ins '{{range .Mounts}}{{.Source}}|{{.Destination}}|{{.RW}}{{println}}{{end}}') _src() { printf '%s\n' "$_mounts" | awk -F'|' -v d="$1" '$2 == d { print $1; exit }'; } _data=$(_src /agent) _home=$(_src /home/agent) [ -n "$_data" ] || die "$CNAME has no /agent mount: not a Kriya One container" _kd=$(dirname "$(dirname "$_data")") [ "$_data" = "$_kd/$NAME/data" ] && [ "${_home:-$_kd/$NAME/home}" = "$_kd/$NAME/home" ] || die "$CNAME's data is in $_data (and $_home): --adopt needs the installer's layout DIR/$NAME/data and DIR/$NAME/home. Move it, or name it with --name" [ -z "${KRIYA_DIR:-}" ] || [ "$(realdir "$KDIR")" = "$(realdir "$_kd")" ] || warn "--data $KDIR: the container's data is in $_kd: that is used" KDIR=$_kd ENVFILE="$KDIR/$NAME/kriya-one.env" SHARED_DIR=$(_src /shared); [ -n "$SHARED_DIR" ] || SHARED_DIR=$KDIR/$NAME/shared SHARES=$(printf '%s\n' "$_mounts" | awk -F'|' '$2 ~ /^\/shared\/[^/]+$/ { n = substr($2, 9); print $1 ":" n ":" ($3 == "true" ? "rw" : "ro") }' | paste -sd '|' -) _net=$(_ins '{{.HostConfig.NetworkMode}}') if [ "$_net" = host ]; then NETWORK=host DOCKER_NET=bridge PORT=$(_ev UPA_PORT) BIND=$(_ev UPA_HOST) else NETWORK=bridge case "$_net" in default|bridge|"") DOCKER_NET=bridge ;; *) DOCKER_NET=$_net ;; esac _pb=$(_ins '{{range $k, $v := .HostConfig.PortBindings}}{{if eq $k "7800/tcp"}}{{range $v}}{{.HostIp}} {{.HostPort}}{{end}}{{end}}{{end}}') BIND=${_pb% *} PORT=${_pb##* } _cp=$(_ins '{{range $k, $v := .HostConfig.PortBindings}}{{if eq $k "7900/tcp"}}{{range $v}}{{.HostPort}}{{end}}{{end}}{{end}}') [ -z "$_cp" ] || [ "$_cp" = $((PORT + 100)) ] || die "$CNAME publishes its canvas on $_cp, not on the panel's port + 100 ($((PORT + 100))): the panel finds it there. Recreate it with -p …:$((PORT + 100)):7900 first" fi [ -n "$PORT" ] || die "$CNAME publishes no panel port (7800)" # docker run -p 8810:7800 (no address) listens on every interface. BIND=${BIND:-0.0.0.0} _mem=$(_ins '{{.HostConfig.Memory}}') if [ "${_mem:-0}" -gt 0 ] 2>/dev/null; then MEMORY="$(( _mem / 1048576 ))m"; else MEMORY=none; fi _nc=$(_ins '{{.HostConfig.NanoCpus}}') if [ "${_nc:-0}" -gt 0 ] 2>/dev/null; then CPUS=$(awk -v n="$_nc" 'BEGIN { printf "%g", n / 1e9 }'); else CPUS=""; fi IMAGE=${IMAGE:-$(_ins '{{.Config.Image}}')} DISK=$(_ev KRIYA_ONE_DISK_GB); DISK=${DISK:-50} [ -n "$DISPLAY_SET" ] || DISPLAY_NAME=$(_ev KRIYA_ONE_DISPLAY_NAME) [ -n "$DISPLAY_SET$DISPLAY_NAME" ] || DISPLAY_NAME=$(default_display "$NAME") [ -n "$ACCENT" ] || ACCENT=$(_ev KRIYA_ONE_ACCENT) [ -n "$ACCENT" ] || ACCENT=$(default_accent "$NAME") [ -n "$PUBLIC_URL" ] || PUBLIC_URL=$(_ev KRIYA_ONE_PUBLIC_URL) save_env say "Adopted $CNAME: its settings are in $ENVFILE (nothing was recreated or restarted)." note " port $PORT (canvas $((PORT + 100))) on $BIND, $NETWORK network${DOCKER_NET:+ ($DOCKER_NET)}, memory $MEMORY, cpus ${CPUS:-all}, image $IMAGE" note " data $KDIR/$NAME, /shared ← $SHARED_DIR${SHARES:+, shares: $(echo "$SHARES" | tr '|' ' ')}" note "The next kriya-one config or update recreates it with these settings (the old container is kept for rollback)." install_cli } # ---------------------------------------------------------------- main main() { NAME="" PORT="" IMAGE="${KRIYA_ONE_IMAGE:-}" OPEN=1 MODEL_URL="" MODEL_NAME="" HTTPS="" YES="${KRIYA_ONE_YES:-0}" MEMORY="" CPUS="" CPUS_SET="" KDIR="${KRIYA_DIR:-}" DISK="" SHARES="" SHARES_SET=0 NETWORK="" BIND="" ACTION=install DESKTOP="" _given="" DISPLAY_NAME="" DISPLAY_SET="" ACCENT="" PUBLIC_URL="" SHARED_DIR="" DOCKER_NET="" HTTPS_SAVED="" HTTPS_URL="" SHORTCUT="" [ "${KRIYA_ONE_NO_SHORTCUT:-0}" != 1 ] || SHORTCUT=0 ENGINE_SETUP=1 ENGINE_KIND="" WITH_SUDO=0; [ "${KRIYA_ONE_NO_ENGINE_SETUP:-0}" != 1 ] || ENGINE_SETUP=0 while [ $# -gt 0 ]; do case "$1" in --*=*) _o="$1"; shift; set -- "${_o%%=*}" "${_o#*=}" "$@" ;; esac case "$1" in --name|--port|--image|--memory|--cpus|--data|--disk|--share|--network|--bind|--https|--engine|--model-url|--model|--display-name|--accent|--brand|--public-url) [ $# -ge 2 ] || die "$1 needs a value" case "$1" in --display-name) DISPLAY_NAME=$(check_display "$2") || die "$DISPLAY_NAME"; DISPLAY_SET=1 ;; --accent|--brand) ACCENT=$(check_accent "$2") || die "$ACCENT" ;; --public-url) PUBLIC_URL=$(check_public_url "$2") || die "$PUBLIC_URL" ;; --model-url) MODEL_URL=$(check_model_url "$2") || die "$MODEL_URL" ;; --model) MODEL_NAME=$(check_model_name "$2") || die "$MODEL_NAME" ;; --name) NAME="$2" ;; --port) PORT="$2" ;; --image) IMAGE="$2" ;; --memory) MEMORY="$2" ;; --cpus) CPUS="$2" CPUS_SET=1 ;; --data) KDIR="$2" ;; --disk) DISK="$2" ;; --share) _given="${_given:+$_given }$2" SHARES_SET=1 ;; --network) NETWORK="$2" ;; --bind) BIND="$2" ;; --https) HTTPS="$2" ;; --engine) case "$2" in colima|docker|apple) ENGINE_KIND="$2" ;; *) die "--engine: colima (default on macOS) or apple" ;; esac ;; esac shift 2 ;; --no-open) OPEN=0; shift ;; --no-shortcut) SHORTCUT=0; shift ;; --yes|-y) YES=1; shift ;; --desktop | --no-desktop) DESKTOP=retired; shift ;; --no-engine-setup) ENGINE_SETUP=0; shift ;; --check) CHECK=1; shift ;; --with-sudo) WITH_SUDO=1; shift ;; --no-share) _given="" SHARES_SET=1; shift ;; --upgrade) ACTION=upgrade; shift ;; --rollback) ACTION=rollback; shift ;; --uninstall) ACTION=uninstall; shift ;; --adopt) ACTION=adopt; shift ;; -h|--help) usage; exit 0 ;; *) die "unknown option: $1 (--help lists them)" ;; esac done [ -z "$MODEL_NAME" ] || [ -n "$MODEL_URL" ] || die "--model goes with --model-url (the address of the model's server)" if [ "$YES" != 1 ] && [ "$ACTION" = install ] && (: /dev/null; then TTY=1; fi # --check: the requirements list only (what this machine needs and what is here), then exit; nothing is changed. if [ "${CHECK:-0}" = 1 ]; then engine_env; req_report; exit 0; fi find_engine [ "${KRIYA_ONE_QUIET_ENGINE:-0}" = 1 ] || say "Kriya One: using $ENGINE" if [ "$ACTION" = install ] && [ -z "$NAME" ]; then _here=$(cli_names | tr '\n' ' ' | sed 's/ $//') if [ -n "$_here" ] && [ "$TTY" = 1 ]; then # Kriya One is here already: the same install command again updates it (Enter, when there is just one); a new # name is another person's (or another role's) assistant. note "Kriya One is installed here already: $_here. Its name again updates it to the newest version and keeps" note "its settings, sign-in and chats. A new name (e.g. therapist) installs another, separate assistant." if [ "$(printf '%s\n' "$_here" | wc -w)" -eq 1 ]; then _dn=$_here; else _dn=""; fi ask_valid NAME check_name "Name (the container is kriya-one-)" "$_dn" elif [ -n "$_here" ] && [ "$(printf '%s\n' "$_here" | wc -w)" -eq 1 ]; then # No questions (--yes, or no terminal), and one Kriya One here: that one is meant (a new one takes --name). NAME=$_here else ask_valid NAME check_name "Name (the container is kriya-one-)" assistant fi else NAME=$(check_name "${NAME:-assistant}") || die "$NAME"; fi CNAME="kriya-one-$NAME" # The same install command again, for a Kriya One that is here already (the portal's command pasted again, with or # without --name): it is updated in place, never set up from scratch. Its saved folder and options, the newest image, # the container recreated (also from a stopped one; the old one is kept for kriya-one rollback); the data stays. REINSTALL=0 if [ "$ACTION" = install ]; then [ -n "$KDIR" ] || KDIR=$(saved_dir "$NAME") _rk=$(expand "${KDIR:-$HOME/Kriya}") if [ -f "$_rk/$NAME/kriya-one.env" ]; then REINSTALL=1 ACTION=upgrade TTY=0 KDIR=$_rk elif exists "$CNAME"; then note "$CNAME is here, made by hand (no saved options in $_rk/$NAME): it is started as it is. To manage it: kriya-one adopt --name $NAME" fi fi if [ "$ACTION" = install ] && [ -z "$KDIR" ] && [ "$TTY" = 1 ]; then ask KDIR "Where the data lives" "$HOME/Kriya"; fi [ -n "$KDIR" ] || [ "$ACTION" = install ] || KDIR=$(saved_dir "$NAME") KDIR=$(expand "${KDIR:-$HOME/Kriya}") case "$KDIR" in /*) ;; *) KDIR="$(pwd)/$KDIR" ;; esac ENVFILE="$KDIR/$NAME/kriya-one.env" [ "$ACTION" != install ] || [ "$KDIR/$NAME" != "$KENG" ] || die "--name engine: $KENG is Kriya One's container engine (macOS); choose another name" case "$ACTION" in uninstall) load_env >/dev/null 2>&1 || true # Its own tailscale serve entries (by what they proxy to: this container's host ports), its Caddy, its network. if exists "$CNAME"; then ts_unserve "$(live_port)"; else [ -z "$PORT" ] || note "(no container $CNAME: tailscale serve entries for 127.0.0.1:$PORT, if any, are left alone: remove them with tailscale serve --https= off)"; fi if exists "$CNAME-caddy"; then $ENGINE rm -f "$CNAME-caddy" >/dev/null 2>&1 && note "removed $CNAME-caddy" || true; fi _any=0 for _c in $(containers_named "$CNAME") $(containers_named "kriya-one-$NAME-old-[0-9-]+"); do $ENGINE rm -f "$_c" >/dev/null && note "removed $_c" && _any=1 done [ "$_any" = 1 ] || note "no container $CNAME here" shortcut_remove server_shortcut_remove rm -rf "$KDIR/$NAME/kept" if [ -n "$DOCKER_NET" ] && [ "$DOCKER_NET" != bridge ] && $ENGINE network inspect "$DOCKER_NET" >/dev/null 2>&1; then $ENGINE network rm "$DOCKER_NET" >/dev/null 2>&1 && note "removed the network $DOCKER_NET" || true fi engine_offer_remove if [ "${KRIYA_ONE_PURGE:-0}" = 1 ]; then say "Kriya One \"$NAME\" is uninstalled." else say "Kriya One \"$NAME\" is uninstalled. Its data is still in $KDIR/$NAME (delete that folder yourself, or: kriya-one uninstall --purge)."; fi return 0 ;; rollback) _old=$(newest_old) [ -n "$_old" ] || die "nothing to roll back to: no kriya-one-$NAME-old-* container (--upgrade keeps one)" _aside="" if exists "$CNAME"; then _aside=$(stamp) $ENGINE stop "$CNAME" >/dev/null 2>&1 || true $ENGINE rename "$CNAME" "$_aside" keep_env "$_aside" fi $ENGINE rename "$_old" "$CNAME" $ENGINE start "$CNAME" >/dev/null say "Rolled back: $CNAME is the container from before ($_old)." # Its options come back with it (kept next to the data by --upgrade and kriya-one config/update). _kenv="$KDIR/$NAME/kept/$_old.env" if [ -f "$_kenv" ]; then mv -f "$_kenv" "$ENVFILE"; _same=""; else _same=" Its saved options in $ENVFILE are the newer ones."; fi [ -z "$_aside" ] || note "The newer one is kept, stopped, as $_aside (kriya-one rollback again swaps back).${_same:-}" load_env || true PORT=${PORT:-$(live_port)} wait_ready # Its shortcut follows the port it had. [ ! -f "$(launcher_dir)/$NAME.sh" ] || [ "${SHORTCUT:-1}" = 0 ] || shortcut_make echo " $OPENURL" return 0 ;; adopt) adopt; return 0 ;; upgrade) _img_given=$IMAGE load_env || die "no saved options in $ENVFILE: install it first (sh install.sh --name $NAME)" if [ "$REINSTALL" = 1 ]; then # The install command names no image: a saved version tag (…:0.4.7) follows latest, as kriya-one update does. # Only a registry image (repo/name:tag): a local one (kriya-one:0.4.11-…, from docker load or build-one.sh) has # no :latest to follow and would be looked up on Docker Hub. if [ -z "$_img_given" ] && [ -n "$IMAGE" ] && [ "${IMAGE#*/}" != "$IMAGE" ]; then case "${IMAGE##*/}" in *:*) echo "${IMAGE##*:}" | grep -Eq '^v?[0-9]+(\.[0-9]+)+' && IMAGE="${IMAGE%:*}:latest" ;; esac fi # Its images moved (the old address stays as a mirror): the same tag from the new one. if [ -z "$_img_given" ] && _mv=$(moved_image "$IMAGE"); then IMAGE=$_mv; say "Kriya One now comes from ${DEFAULT_IMAGE%:*}"; fi OLD_VERSION="" ! exists "$CNAME" || OLD_VERSION=$(label org.opencontainers.image.version) say "Found your existing Kriya One \"$NAME\"${OLD_VERSION:+ ($OLD_VERSION)}: updating it and keeping its settings, sign-in and data ($KDIR/$NAME)." else note "upgrading $CNAME with the options saved in $ENVFILE (and the ones given now)" fi ;; esac # Each answer: the command line, else (upgrade) the saved one, else a question, else the default. if [ -z "$PORT" ]; then # 8810 (canvas 8910), or the first free pair after it: another Kriya One (or a launchpad) may have it. _dp=8810 if port_taken 8810 || port_taken 8910; then _dp=$(free_pair 8810) || _dp=$(free_pair 8900) || die "no free pair of ports in 8810-8989 (and +100): choose one with --port" note "port 8810/8910 is taken (by $(port_why 8810)): this one uses $_dp (canvas $((_dp + 100))); --port P chooses another" fi ask_valid PORT check_port "Port for the panel (its canvas uses port + 100)" "$_dp" else PORT=$(check_port "$PORT") || die "$PORT"; fi IMAGE="${IMAGE:-$DEFAULT_IMAGE}" _total=$(mem_total_mb) if [ "${_total:-0}" -gt 0 ] && [ "$_total" -lt 3800 ]; then case "$(uname -s)" in Darwin) warn "Docker has only $((_total / 1024)) GB of memory here (its VM): give it 8 GB (Docker Desktop → Settings → Resources; Colima: colima stop && colima start --memory 8), then run this again" ;; *) warn "this machine (or Docker's VM) has only $((_total / 1024)) GB of memory: Kriya One wants 3 GB or more" ;; esac fi if [ -z "$MEMORY" ]; then ask_valid MEMORY check_memory "Memory limit" "$(default_memory)"; else MEMORY=$(check_memory "$MEMORY") || die "$MEMORY"; fi if [ -z "$CPUS_SET" ]; then ask_valid CPUS check_cpus "CPU limit (a number, or all)" all; else CPUS=$(check_cpus "$CPUS") || die "$CPUS"; fi _free=$(free_gb "$KDIR") if [ -z "$DISK" ]; then [ "$TTY" = 0 ] || note " $KDIR has ${_free:-?} GB free." ask_valid DISK check_disk "Disk soft cap in GB (Kriya One warns above 90 %)" 50 else DISK=$(check_disk "$DISK") || die "$DISK"; fi [ -z "$_free" ] || [ "$DISK" -le "$_free" ] || warn "the disk cap ($DISK GB) is more than the free space in $KDIR ($_free GB)" if [ "$SHARES_SET" = 1 ]; then SHARES="" _old_ifs=$IFS; IFS=' ' for _s in $_given; do IFS=$_old_ifs; add_share "$_s"; done IFS=$_old_ifs elif [ "$ACTION" = install ] && [ "$TTY" = 1 ]; then note "Folders to share with the assistant (PATH[:NAME][:ro], one per line; an empty line ends):" while :; do ask _s " folder" "" [ -n "$_s" ] || break if _ok=$(check_share "$_s"); then SHARES="${SHARES:+$SHARES|}$_ok"; note " → /shared/$(echo "$_ok" | cut -d: -f2)"; else note " $_ok"; fi done fi if linux_native; then if [ -z "$NETWORK" ] && [ "$TTY" = 1 ]; then note "Network: bridge (recommended): the LAN works, and this machine is closed except the ports you open in Settings;" note " host (advanced): the assistant shares this machine's network and its local-only services; no firewall." fi if [ -z "$NETWORK" ]; then ask_valid NETWORK check_network "Network mode (bridge or host)" bridge; else NETWORK=$(check_network "$NETWORK") || die "$NETWORK"; fi else NETWORK=$(check_network "${NETWORK:-bridge}") || die "$NETWORK" [ "$NETWORK" = bridge ] || die "--network host is for Linux with Docker Engine or Podman. Here (Docker Desktop) the container reaches the LAN already in the default bridge mode; open this machine's ports in Settings → Devices → Network." fi BIND=$(check_bind "${BIND:-127.0.0.1}") || die "$BIND" [ "$BIND" = 127.0.0.1 ] || warn "the panel listens on $BIND:$PORT: other machines can reach it (put HTTPS in front: --https)" [ "$NETWORK" != host ] || warn "host network: the assistant can reach everything on this machine (also services bound to 127.0.0.1) and the LAN, like any program you run" # Its identity (the app's name and icon badge), its own /shared folder and network (an upgrade keeps the saved ones). [ -n "$DISPLAY_SET" ] || DISPLAY_NAME=$(default_display "$NAME") [ -n "$ACCENT" ] || ACCENT=$(default_accent "$NAME") [ -n "$SHARED_DIR" ] || SHARED_DIR=$KDIR/$NAME/shared [ -n "$DOCKER_NET" ] || DOCKER_NET=kriya-one-$NAME share_overlap if [ "$ACTION" = install ] && exists "$CNAME"; then say "$CNAME exists already: starting it (to change its options: kriya-one config --name $NAME --memory …; your data stays in $KDIR/$NAME)" $ENGINE start "$CNAME" >/dev/null # Its own port and saved options, not the ones just worked out for a new one. PORT=$(live_port) HTTPS_URL="" PUBLIC_URL=""; DISPLAY_SET=1; load_env >/dev/null 2>&1 || true else mkdir -p "$KDIR/$NAME" get_image _old="" if [ "$ACTION" = upgrade ] && exists "$CNAME"; then _old=$(stamp) $ENGINE stop "$CNAME" >/dev/null 2>&1 || true $ENGINE rename "$CNAME" "$_old" keep_env "$_old" note "the old container is kept, stopped, as $_old (kriya-one rollback swaps back)" fi if ! (create_container); then if [ -n "$_old" ]; then $ENGINE rm -f "$CNAME" >/dev/null 2>&1 || true rm -f "$KDIR/$NAME/kept/$_old.env" $ENGINE rename "$_old" "$CNAME" && $ENGINE start "$CNAME" >/dev/null && warn "the new container didn't start: $CNAME is the old one again" fi die "couldn't create $CNAME" fi save_env fi wait_ready if [ "$REINSTALL" = 1 ]; then # A setup an earlier version moved aside comes back on this start; when it chose another engine, Kriya One # restarts onto it once (server/one.ts): wait for that, so the browser doesn't open on a restarting server. sleep 3 wait_ready _nv=$(label org.opencontainers.image.version) if [ -n "$_nv" ] && [ "$_nv" = "${OLD_VERSION:-}" ]; then say "Found your existing Kriya One \"$NAME\": it is up to date ($_nv), started again with your settings kept." else say "Found your existing Kriya One \"$NAME\": updated${_nv:+ to $_nv} and kept your settings."; fi if [ "$PENDING" = 0 ]; then note " Sign in with the password you chose before."; fi fi if [ "$PENDING" = 1 ]; then say "Kriya One is running. Finish the setup in your browser:"; else say "Kriya One is running:"; fi [ -z "$MODEL_URL" ] || [ "$PENDING" != 1 ] || note " Engine: Local model ${MODEL_NAME:-(the first it lists)} at $MODEL_URL, no sign-in needed (the setup page tests it)." if [ -n "$HTTPS" ]; then setup_https [ ! -f "$ENVFILE" ] || save_env fi if [ -n "$(remote_url)" ]; then echo " On this machine: $OPENURL" echo " On other devices: $(remote_url)$SETUP_PATH" else echo " $OPENURL" fi # This machine needs no setup code; another device does (it is in the link above, or kriya-one open shows it). if [ "$PENDING" = 1 ] && [ "$LOCAL_PATH" = /setup ]; then note " No setup code on this computer. Setting it up from another device instead? It asks for the setup code: $_code" fi echo " data: $KDIR/$NAME · options: $ENVFILE" note " disk: a soft cap of $DISK GB: Settings warns above 90 %; it is not enforced (Docker can't cap a folder)." install_cli # The shortcut on the desktop and in the app list (after the kriya-one command: its launcher uses it). if [ "${SHORTCUT:-1}" = 0 ]; then shortcut_remove; server_shortcut_remove; else shortcut_make; server_shortcut_make; fi # The cheat sheet: after an install, an update (kriya-one update, the install command again); not after kriya-one config. [ "${KRIYA_ONE_NOPULL:-0}" = 1 ] || cheat_sheet # The app: the web UI installs as one (a PWA, server/brand.ts webManifest). Kriya One Desktop is retired. [ "$DESKTOP" != retired ] || note "--desktop: Kriya One Desktop is retired; install the web UI as an app instead (below)." if [ "$ACTION" = install ]; then _app="Kriya One${DISPLAY_NAME:+ · $DISPLAY_NAME}" if [ -n "$(remote_url)" ]; then note "Install it as an app: open $(remote_url)/ in Chrome or Edge on each device that uses it and click Install in the" note "address bar (Safari: File → Add to Dock; a phone: Share → Add to Home Screen). It installs as \"$_app\". On this machine: $URL/" else note "Install it as an app: open $URL in Chrome or Edge and click Install in the address bar (Safari: File → Add to Dock). It installs as \"$_app\"." note "Phones need HTTPS first (--https tailscale, or Settings → Other devices), then Add to Home Screen." fi fi open_url "$OPENURL" } # kriya-one (or, installed as kriya-one, no arguments or --help): the command; anything else installs. entry() { case "${1:-}" in start|stop|restart|status|logs|open|update|config|rollback|uninstall|version|list|use|services|voice|vocab|brand|join-code|menu|panel|help) cli "$@" ;; adopt) shift; main --adopt "$@" ;; install) shift; main "$@" ;; ""|-h|--help) if [ "$(basename "$0")" = kriya-one ]; then cli_usage; else main "$@"; fi ;; *) main "$@" ;; esac } # In braces: a copy cut anywhere in this line is a syntax error, not a call with no options. { entry "$@"; }