#!/bin/sh # install.sh — download and install canopy from GitHub Releases # Usage: curl -fsSL https://raw.githubusercontent.com/UniverLab/harness-canopy/main/scripts/install.sh | sh set -eu REPO="UniverLab/harness-canopy" BINARY="canopy" INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}" info() { printf ' \033[1;34m%s\033[0m %s\n' "$1" "$2"; } error() { printf ' \033[1;31merror:\033[0m %s\n' "$1" >&2; exit 1; } # --- detect OS --- OS="$(uname -s)" case "$OS" in Linux*) OS_TARGET="unknown-linux-gnu" ;; Darwin*) OS_TARGET="apple-darwin" ;; *) error "Unsupported OS: $OS (only Linux and macOS are supported)" ;; esac # --- detect arch --- ARCH="$(uname -m)" case "$ARCH" in x86_64|amd64) ARCH_TARGET="x86_64" ;; arm64|aarch64) ARCH_TARGET="aarch64" ;; *) error "Unsupported architecture: $ARCH" ;; esac TARGET="${ARCH_TARGET}-${OS_TARGET}" info "platform" "$TARGET" TMPDIR="$(mktemp -d)" trap 'rm -rf "$TMPDIR"' EXIT # ============================================================ # 1. Install canopy # ============================================================ # --- resolve version --- if [ -n "${VERSION:-}" ]; then TAG="v$VERSION" info "version" "$TAG (pinned)" else # Prefer the releases/latest redirect (not rate-limited; GitHub resolves # it to the newest non-draft, non-prerelease tag). api.github.com is only # a fallback (60 anonymous requests/hour per IP). Every curl below is # silent (2>/dev/null) so no `curl: (22)` line ever reaches the user. TAG=$(curl -fsSL -o /dev/null -w '%{url_effective}' "https://github.com/$REPO/releases/latest" 2>/dev/null | rev | cut -d'/' -f1 | rev) if [ -z "$TAG" ]; then API_JSON="$TMPDIR/releases.json" if HTTP_CODE=$(curl -fSL -w '%{http_code}' -o "$API_JSON" "https://api.github.com/repos/$REPO/releases" 2>/dev/null); then TAG=$(grep -i '"tag_name"' "$API_JSON" | grep -v 'prerelease.*true' | head -1 | cut -d'"' -f4) elif [ "$HTTP_CODE" = "403" ]; then error "GitHub rate limit reached — try again in an hour" else error "Could not reach GitHub to resolve the latest release (HTTP ${HTTP_CODE:-000})" fi fi [ -z "$TAG" ] && error "Could not resolve latest stable release" info "version" "$TAG (latest stable)" fi # --- download --- ARCHIVE="${BINARY}-${TAG}-${TARGET}.tar.gz" URL="https://github.com/$REPO/releases/download/${TAG}/${ARCHIVE}" info "download" "$URL" HTTP_CODE=$(curl -fSL -w '%{http_code}' -o "$TMPDIR/$ARCHIVE" "$URL" 2>/dev/null) || true [ "$HTTP_CODE" = "200" ] || error "Download failed (HTTP $HTTP_CODE). Check that $TAG exists for $TARGET at:\n $URL" # --- verify checksum --- # Match against SHA256SUMS.txt from the same release. Missing sums file (older # releases) or no sha256 tool → skip; a present-but-mismatched checksum is fatal. SUMS_URL="https://github.com/$REPO/releases/download/${TAG}/SHA256SUMS.txt" if curl -fsSL -o "$TMPDIR/SHA256SUMS.txt" "$SUMS_URL" 2>/dev/null; then EXPECTED=$(awk -v f="$ARCHIVE" '$2 == f { print $1 }' "$TMPDIR/SHA256SUMS.txt" | head -1) [ -n "$EXPECTED" ] || error "No checksum listed for $ARCHIVE in SHA256SUMS.txt" if command -v sha256sum >/dev/null 2>&1; then ACTUAL=$(sha256sum "$TMPDIR/$ARCHIVE" | awk '{ print $1 }') elif command -v shasum >/dev/null 2>&1; then ACTUAL=$(shasum -a 256 "$TMPDIR/$ARCHIVE" | awk '{ print $1 }') else ACTUAL="" info "checksum" "no sha256 tool found — skipping verification" fi if [ -n "$ACTUAL" ]; then [ "$ACTUAL" = "$EXPECTED" ] || error "Checksum mismatch for $ARCHIVE (expected $EXPECTED, got $ACTUAL)" info "checksum" "verified" fi else info "checksum" "SHA256SUMS.txt not found for $TAG — skipping verification" fi # --- extract --- tar xzf "$TMPDIR/$ARCHIVE" -C "$TMPDIR" [ -f "$TMPDIR/$BINARY" ] || error "Binary not found in archive" # --- install --- mkdir -p "$INSTALL_DIR" mv "$TMPDIR/$BINARY" "$INSTALL_DIR/$BINARY" chmod +x "$INSTALL_DIR/$BINARY" info "installed" "$INSTALL_DIR/$BINARY" # ============================================================ # 2. Ensure PATH # ============================================================ PATHS_TO_ADD="" case ":$PATH:" in *":$INSTALL_DIR:"*) ;; *) PATHS_TO_ADD="$INSTALL_DIR" ;; esac if [ -n "$PATHS_TO_ADD" ]; then for dir in $PATHS_TO_ADD; do export PATH="$dir:$PATH" done for profile in "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.profile"; do if [ -f "$profile" ]; then for dir in $PATHS_TO_ADD; do if ! grep -q "export PATH=\"$dir:\$PATH\"" "$profile" 2>/dev/null; then printf '\n# Added by canopy installer\nexport PATH="%s:$PATH"\n' "$dir" >> "$profile" info "updated" "$profile" fi done fi done fi # ============================================================ # 3. Verify # ============================================================ info "done" "$($INSTALL_DIR/$BINARY --version 2>/dev/null || echo "$BINARY installed")" echo "" info "ready" "Run 'canopy daemon start' to get started!"