#!/bin/sh # Lettuce installer: https://lettuce.work/install.sh # # curl -fsSL https://lettuce.work/install.sh | sh # curl -fsSL https://lettuce.work/install.sh | sh -s -- --version v0.25.0 --dir "$HOME/bin" # # Downloads the lettuce binary for this machine (macOS on Apple Silicon or # Intel, Linux on x86-64 or ARM64, e.g. a Docker container on an Apple-silicon # Mac), verifies its sha256 against the published checksum, proves it runs, # and installs it atomically. No sudo: the default directory is ~/.local/bin. # Needs curl or wget, and sha256sum or shasum. Never needs jq. # # Signed releases (LET-2340, ADR 0042 ยง1): a release carries release-manifest.json and its # minisign signature. With `minisign` installed the installer verifies the signature against # the keys below and the binary against the signed manifest; without it, it checks the # binary against the manifest and says the signature was not checked (--require-signature # refuses instead). The keys are also at https://lettuce.work/release-signing.pub and in # docs/operations/RELEASE-SIGNING.md, for an out-of-band comparison. # # The binary is free to use under the Lettuce Binary License: # https://lettuce.work/BINARY-LICENSE (also printed offline by `lettuce license`). # # LETTUCE_DOWNLOAD_BASE overrides https://lettuce.work (a mirror, or a test). # # Slow links (LET-2493): a download is bounded by progress, never by a fixed time. It fails # only when it moves slower than 1 KB/s for LETTUCE_INSTALL_STALL_SECONDS (default 60), and a # cut download is resumed where it stopped (up to 4 attempts). The binary shows its progress. set -eu BASE=${LETTUCE_DOWNLOAD_BASE:-https://lettuce.work} BASE=${BASE%/} STALL=${LETTUCE_INSTALL_STALL_SECONDS:-60} case $STALL in '' | *[!0-9]* | 0) STALL=60 ;; esac # INFRA-8: no default directory without a HOME (it used to become /.local/bin). DIR= VERSION= REQUIRE_SIGNATURE= # The trusted release signing keys, one " " per line; the same # ring the binary embeds (internal/buildinfo/release_signing.go, # TestLET2340ReleaseSigningKeysAgreeEverywhere). Empty until the key ceremony. RELEASE_SIGNING_KEYS='' # The published platforms (buildinfo.ClientPlatforms; TestLET2221PlatformListIsSingleSourced). SUPPORTED="darwin-arm64 darwin-amd64 linux-amd64 linux-arm64" say() { printf '%s\n' "$*"; } die() { printf 'lettuce install: error: %s\n' "$1" >&2 shift for hint in "$@"; do printf ' -> %s\n' "$hint" >&2; done exit 1 } while [ $# -gt 0 ]; do case $1 in --dir) [ $# -ge 2 ] || die "--dir needs a directory" DIR=$2 shift 2 ;; --dir=*) DIR=${1#--dir=} shift ;; --version) [ $# -ge 2 ] || die "--version needs a release tag, e.g. v0.25.0" VERSION=$2 shift 2 ;; --version=*) VERSION=${1#--version=} shift ;; --require-signature) REQUIRE_SIGNATURE=1 shift ;; -h | --help) say "usage: install.sh [--dir DIR] [--version vX.Y.Z] [--require-signature]" say " --dir DIR install into DIR (default ~/.local/bin; no sudo)" say " --version vX.Y.Z install that release, one the site keeps (default: the newest one;" say " $BASE/download/latest.txt lists the kept ones on its kept= line)" say " --require-signature refuse unless minisign verifies the release's signature" say " LETTUCE_DOWNLOAD_BASE download from this base URL instead of https://lettuce.work" say " (https; plain http only for localhost/127.0.0.1)" say " LETTUCE_INSTALL_STALL_SECONDS give up on a download slower than 1 KB/s for this long (default 60)" exit 0 ;; *) die "unknown argument: $1" "run: sh install.sh --help" ;; esac done if [ -z "$DIR" ]; then [ -n "${HOME:-}" ] || die "no install directory: HOME is not set" "pass --dir DIR (e.g. --dir /usr/local/bin in a container)" DIR=$HOME/.local/bin fi # INFRA-7: a binary is never fetched over plain http from another machine (an on-path # attacker would choose it), and an https download never follows a redirect to http. case $BASE in https://*) PROTO=https ;; http://localhost | http://localhost[:/]* | http://127.0.0.1 | http://127.0.0.1[:/]* | file://*) PROTO= ;; *) die "LETTUCE_DOWNLOAD_BASE must be an https URL (plain http only for localhost): $BASE" "unset LETTUCE_DOWNLOAD_BASE to use https://lettuce.work" ;; esac # --- platform ----------------------------------------------------------------- os=$(uname -s 2>/dev/null || echo unknown) arch=$(uname -m 2>/dev/null || echo unknown) case $os in Darwin) os=darwin ;; Linux) os=linux ;; *) die "unsupported operating system: $os" \ "lettuce publishes binaries for: $SUPPORTED" \ "on Windows, install inside WSL2 (it is linux-amd64, or linux-arm64 on an ARM machine)" ;; esac case $arch in x86_64 | amd64) arch=amd64 ;; arm64 | aarch64) arch=arm64 ;; *) die "unsupported CPU architecture: $arch" \ "lettuce publishes binaries for: $SUPPORTED" ;; esac # A shell running under Rosetta on Apple Silicon reports x86_64: prefer the native binary. if [ "$os" = darwin ] && [ "$arch" = amd64 ] && [ "$(sysctl -n hw.optional.arm64 2>/dev/null || echo 0)" = 1 ]; then arch=arm64 fi platform=$os-$arch case " $SUPPORTED " in *" $platform "*) ;; *) die "no lettuce binary is published for $platform" \ "lettuce publishes binaries for: $SUPPORTED" \ "for another platform, contact info@huru.io" ;; esac # --- tools -------------------------------------------------------------------- # LET-2493: no --max-time anywhere. A transfer slower than 1 KB/s for $STALL seconds is a # stall (curl exit 28). A small file retries a transient failure (--retry); the binary # resumes a cut or stalled transfer instead (fetch_binary below) and shows a progress bar # on stderr. if command -v curl >/dev/null 2>&1; then TOOL=curl curl_get() { if [ -n "$PROTO" ]; then curl -fL --proto '=https' --proto-redir '=https' --tlsv1.2 --connect-timeout 30 --speed-limit 1024 --speed-time "$STALL" "$@" else curl -fL --connect-timeout 30 --speed-limit 1024 --speed-time "$STALL" "$@" fi } fetch() { curl_get -sS --retry 3 --retry-delay 2 -o "$2" "$1"; } # A cut transfer resumes from the bytes already written (-C -, a Range request). fetch_resume() { curl_get --progress-bar -C - -o "$2" "$1"; } elif command -v wget >/dev/null 2>&1; then TOOL=wget # GNU wget can refuse a redirect off https; busybox wget (minimal images) has no such flag. # -T bounds every read (a stall); -c resumes a cut transfer. if [ -n "$PROTO" ] && wget --help 2>&1 | grep -q -- --https-only; then fetch() { wget -q -T "$STALL" --https-only --secure-protocol=TLSv1_2 -O "$2" "$1"; } fetch_resume() { wget -q -T "$STALL" -c --https-only --secure-protocol=TLSv1_2 -O "$2" "$1"; } else fetch() { wget -q -T "$STALL" -O "$2" "$1"; } fetch_resume() { wget -q -T "$STALL" -c -O "$2" "$1"; } fi else die "neither curl nor wget is installed" "install one of them, then run this installer again" fi # fetch_binary URL FILE downloads the binary, resuming a cut transfer up to 4 times # (-C - and wget -c send a Range request for the rest). A source that cannot resume # (curl exit 33) starts over from the first byte; an HTTP refusal (curl exit 22, e.g. a # 404) is not retried. fetch_binary() { attempt=1 while :; do rc=0 fetch_resume "$1" "$2" || rc=$? [ "$rc" -ne 0 ] || return 0 if [ "$TOOL" = curl ] && [ "$rc" -eq 22 ]; then return "$rc" fi if [ "$TOOL" = curl ] && [ "$rc" -eq 33 ]; then rm -f "$2" fi [ "$attempt" -lt 4 ] || return "$rc" attempt=$((attempt + 1)) printf 'lettuce install: the download stopped (%s exit %s); resuming, attempt %s of 4 ...\n' "$TOOL" "$rc" "$attempt" >&2 done } # The other ways in, named on a failed download: a configured lettuce server serves its # own client, and the release can be built from its tag. fallback_server="if you already have a lettuce binary and a lettuce server is configured (a .lettuce pointer, --server-url or a credentials entry): lettuce self-update downloads the server's own client" fallback_build() { printf '%s' "Huru-internal: build $1 from its tag in a lettuce source clone (git checkout --detach $1 && make build) and copy bin/lettuce into $DIR"; } if command -v sha256sum >/dev/null 2>&1; then sha256() { sha256sum "$1" | awk '{print $1}'; } elif command -v shasum >/dev/null 2>&1; then sha256() { shasum -a 256 "$1" | awk '{print $1}'; } else die "no sha256 tool (sha256sum or shasum) is installed; refusing to install a binary that cannot be verified" \ "install coreutils (sha256sum) or perl (shasum), then run this installer again" fi tmp=$(mktemp -d 2>/dev/null || mktemp -d -t lettuce-install) staged= cleanup() { rm -rf "$tmp" if [ -n "$staged" ]; then rm -f "$staged"; fi } trap cleanup EXIT trap 'cleanup; exit 130' INT TERM # --- which release ------------------------------------------------------------ # latest.txt names the newest release (tag=) and every release the site keeps # (kept=, newest first; LET-2246): --version must name a kept one. if [ -n "$VERSION" ]; then case $VERSION in v[0-9]*.[0-9]*.[0-9]*) ;; *) die "not a release tag: '$VERSION'" "pass --version vMAJOR.MINOR.PATCH, or omit it for the newest release" ;; esac case $VERSION in *[!v0-9.]*) die "not a release tag: '$VERSION'" "pass --version vMAJOR.MINOR.PATCH, or omit it for the newest release" ;; esac fi fetch "$BASE/download/latest.txt" "$tmp/latest.txt" || die "could not read $BASE/download/latest.txt" \ "check the network connection (and LETTUCE_DOWNLOAD_BASE, if set), then retry" \ "$fallback_server" NEWEST=$(sed -n 's/^tag=//p' "$tmp/latest.txt" | head -n 1) KEPT=$(sed -n 's/^kept=//p' "$tmp/latest.txt" | head -n 1) if [ -z "$VERSION" ]; then VERSION=$NEWEST fi # INFRA-7: the tag is interpolated into paths and a sed program below, so whatever its # source (--version or latest.txt) it must be exactly vMAJOR.MINOR.PATCH. case $VERSION in v[0-9]*.[0-9]*.[0-9]*) ;; *) die "$BASE/download/latest.txt names no release tag ('$VERSION')" "retry later; if it persists, report it to info@huru.io" ;; esac case $VERSION in *[!v0-9.]* | *..* | v*v*) die "$BASE/download/latest.txt names no release tag ('$VERSION')" "retry later; if it persists, report it to info@huru.io" ;; esac # A mirror whose latest.txt predates kept= is not judged here: the download says. if [ -n "$KEPT" ]; then case " $KEPT " in *" $VERSION "*) ;; *) die "$BASE does not keep lettuce $VERSION: it keeps the last few releases ($KEPT)" \ "omit --version to install the newest release, $NEWEST" \ "to match a server that runs $VERSION: ask its operator to upgrade the server (or to serve its own client binaries with serve --client-binaries-dir)" ;; esac fi # b144: a kept release built before a platform was published (linux-arm64) has no # binary for it; latest.txt names each kept release's platforms. A mirror whose # latest.txt has no platforms- line for the release is not judged here. if grep -q "^platforms-$VERSION=" "$tmp/latest.txt"; then PUBLISHED=$(sed -n "s/^platforms-$VERSION=//p" "$tmp/latest.txt" | head -n 1) case " $PUBLISHED " in *" $platform "*) ;; *) die "lettuce $VERSION publishes no binary for $platform (it publishes: $PUBLISHED)" \ "omit --version to install the newest release, $NEWEST" \ "to match a server that runs $VERSION: ask its operator to upgrade the server (or to serve its own client binaries with serve --client-binaries-dir)" ;; esac fi asset=lettuce-$platform url=$BASE/download/$VERSION/$asset # --- download and verify -------------------------------------------------------- say "Downloading lettuce $VERSION for $platform from $BASE ..." say " (a slow link is fine: a download fails only below 1 KB/s for ${STALL}s, and a cut one resumes)" fetch "$url.sha256" "$tmp/$asset.sha256" || die "could not download $url.sha256" \ "check the network connection, then retry" \ "or omit --version to install the newest release, $NEWEST" \ "$fallback_server" rc=0 fetch_binary "$url" "$tmp/$asset" || rc=$? if [ "$rc" -ne 0 ]; then why="check the network connection, then run this installer again" if [ "$TOOL" = curl ] && [ "$rc" -eq 28 ]; then why="the download stalled (slower than 1 KB/s for ${STALL}s); run this installer again on a better connection, or raise LETTUCE_INSTALL_STALL_SECONDS" fi die "could not download $url ($TOOL exit $rc). Nothing was installed." "$why" "$fallback_server" "$(fallback_build "$VERSION")" fi want=$(awk 'NR==1 {print $1}' "$tmp/$asset.sha256") got=$(sha256 "$tmp/$asset") case $want in [0-9a-f]*) ;; *) die "the published checksum file is not a sha256 line: $url.sha256" "retry later; if it persists, report it to info@huru.io" ;; esac if [ "$want" != "$got" ]; then die "checksum mismatch for $asset: published $want, downloaded $got. Nothing was installed." \ "retry once (a download damaged in transit passes on a retry)" \ "if it fails again, do not use the file; report it to info@huru.io" fi say "Verified sha256 $got" # Release signing is deferred (ADR 0042): with no key listed above this is the policy, so it # is said once, plainly (buildinfo.ChecksumOnlyReleasesNote), and never as a warning. [ -n "$RELEASE_SIGNING_KEYS" ] || say "lettuce releases are checksum-verified, not signed." # --- signature (LET-2340) ------------------------------------------------------ # A release that publishes a signed manifest is checked against it: with minisign, the # signature against the keys above; in every case, the binary against the manifest's # entry for this platform. signed= if [ -n "$RELEASE_SIGNING_KEYS" ] && fetch "$BASE/download/$VERSION/release-manifest.json" "$tmp/manifest.json" 2>/dev/null && fetch "$BASE/download/$VERSION/release-manifest.json.minisig" "$tmp/manifest.json.minisig" 2>/dev/null; then m_commit=$(sed -n 's/^ "commit": "\([0-9a-f]\{40\}\)",$/\1/p' "$tmp/manifest.json" | head -n 1) m_sha=$(awk -v f="\"file\": \"$asset\"," '$0 ~ f {getline; gsub(/[^0-9a-f]/, "", $2); print $2; exit}' "$tmp/manifest.json") { [ -n "$m_commit" ] && [ -n "$m_sha" ]; } || die "the release manifest for $VERSION names no $asset. Nothing was installed." "report it to info@huru.io" [ "$m_sha" = "$got" ] || die "$asset is not the binary the release manifest names (manifest $m_sha, downloaded $got). Nothing was installed." \ "do not use the file; report it to info@huru.io" if command -v minisign >/dev/null 2>&1; then comment= while read -r key_id key; do [ -n "$key" ] || continue if out=$(minisign -V -m "$tmp/manifest.json" -x "$tmp/manifest.json.minisig" -P "$key" 2>/dev/null); then comment=$(printf '%s\n' "$out" | sed -n 's/^Trusted comment: //p') signed=$key_id break fi done <" fi elif [ -n "$REQUIRE_SIGNATURE" ]; then die "--require-signature: lettuce $VERSION publishes no signed release manifest" \ "omit --require-signature to install a release from before signing (checksum only)" elif [ -n "$RELEASE_SIGNING_KEYS" ]; then say "lettuce $VERSION publishes no signed release manifest: it was checked against its published checksum only (--require-signature refuses this)." fi # --- install atomically ------------------------------------------------------- mkdir -p "$DIR" || die "cannot create $DIR" "pass --dir DIR naming a directory you can write (no sudo needed)" staged=$DIR/.lettuce-install.$$ cp "$tmp/$asset" "$staged" || die "cannot write into $DIR" "pass --dir DIR naming a directory you can write (no sudo needed)" chmod 755 "$staged" reported=$("$staged" version 2>&1) || die "the downloaded binary does not run on this machine ($platform): $reported" "report it to info@huru.io" # INFRA-7: the release on a word boundary (v0.25.1 must not accept v0.25.10-evil). case " $reported " in *" lettuce $VERSION "* | *" $VERSION "*) ;; *) die "the downloaded binary reports '$reported', not $VERSION. Nothing was installed." "report it to info@huru.io" ;; esac mv -f "$staged" "$DIR/lettuce" || die "cannot replace $DIR/lettuce" "pass --dir DIR naming a directory you can write" staged= say "" say "Installed $DIR/lettuce" "$DIR/lettuce" version case ":${PATH:-}:" in *":$DIR:"*) ;; *) say "" say "$DIR is not on your PATH. Add it (and to your shell profile):" say " export PATH=\"$DIR:\$PATH\"" ;; esac if [ "$os" = darwin ]; then say "" say "macOS: this download carries no quarantine flag. A copy downloaded with a browser does;" say "clear it with: xattr -d com.apple.quarantine /path/to/lettuce" fi say "" say "Next steps (the full guide is in the binary: lettuce skill):" say " One project, local store in your repository:" say " cd your-repo && lettuce init --root .lettuce --author you --bootstrap-project myproj --bootstrap-author" say " lettuce status && lettuce work --project myproj" say " A personal server for your agents: $BASE/server.md" say " Agents: $BASE/agent.md" say "" say "lettuce is free to use under the Lettuce Binary License: $BASE/BINARY-LICENSE (offline: lettuce license)." say "Updates: lettuce self-update"