#!/bin/sh # Numes provider installer. # # curl -fsSL https://numes.ai/install | sh # curl -fsSL https://numes.ai/install | sh -s -- --version 0.1.0 # sh install.sh --binary ./numes-0.1.0-linux-x64.tar.gz # # The script downloads the release archive for this machine, verifies it # against the release SHA256SUMS, and hands over to `numes install`, which # verifies the release manifest and installs into the managed layout. It # never edits shell startup files and sends no telemetry. # # Environment: # NUMES_INSTALL_DIR directory for the `numes` command link # (default: ~/.local/bin) # NUMES_RELEASE_ORIGIN advanced: alternative HTTPS release origin set -eu NUMES_DEFAULT_RELEASE_ORIGIN="https://releases.numes.ai" NUMES_CHANNEL="stable" NUMES_CONTAINER_HINT="Run the Numes provider container image instead, or use a glibc-based Linux distribution." say() { printf '%s\n' "$*" } fail() { printf 'numes install: %s\n' "$*" >&2 exit 1 } usage() { say "Install the Numes command-line application." say "" say "Usage: install.sh [--version VERSION] [--binary ARCHIVE]" say "" say "Options:" say " --version VERSION install this release instead of the latest stable one" say " --binary ARCHIVE install a local numes release archive (.tar.gz)" say " -h, --help show this help" } parse_arguments() { install_version="" install_binary="" while [ "$#" -gt 0 ]; do case "$1" in --version) [ "$#" -ge 2 ] || fail "--version needs a value" install_version="$2" shift 2 ;; --version=*) install_version="${1#--version=}" shift ;; --binary) [ "$#" -ge 2 ] || fail "--binary needs a file" install_binary="$2" shift 2 ;; --binary=*) install_binary="${1#--binary=}" shift ;; -h | --help) usage exit 0 ;; *) fail "unknown option: $1 (see --help)" ;; esac done if [ -n "$install_version" ] && [ -n "$install_binary" ]; then fail "--version and --binary cannot be combined" fi if [ -n "$install_version" ]; then validate_version "$install_version" fi } validate_version() { case "$1" in '' | *[!0-9A-Za-z.-]*) fail "invalid version: $1" ;; esac if ! printf '%s\n' "$1" | grep -Eqx '[0-9]+\.[0-9]+\.[0-9]+(-dev\.[0-9]{8}T[0-9]{6}Z)?'; then fail "invalid version: $1" fi } is_musl() { [ -f /etc/alpine-release ] && return 0 command -v ldd >/dev/null 2>&1 || return 1 ldd --version 2>&1 | grep -qi musl } detect_target() { target_os="$(uname -s)" target_arch="$(uname -m)" case "$target_os" in Linux) if is_musl; then fail "musl-based Linux (such as Alpine) is not supported. $NUMES_CONTAINER_HINT" fi case "$target_arch" in x86_64 | amd64) install_target="linux-x64" ;; aarch64 | arm64) install_target="linux-arm64" ;; *) fail "unsupported Linux architecture: $target_arch (supported: x86_64, arm64)" ;; esac ;; Darwin) case "$target_arch" in arm64) install_target="darwin-arm64" ;; x86_64) # A shell translated by Rosetta reports x86_64 on Apple silicon. if [ "$(sysctl -in hw.optional.arm64 2>/dev/null || true)" = "1" ]; then install_target="darwin-arm64" else fail "macOS on Intel processors is not supported (supported: Apple silicon)" fi ;; *) fail "unsupported macOS architecture: $target_arch" ;; esac ;; MINGW* | MSYS* | CYGWIN* | Windows_NT) fail "Windows is not supported. Use Linux (x86_64 or arm64) or macOS on Apple silicon." ;; *) fail "unsupported operating system: $target_os (supported: Linux, macOS)" ;; esac } resolve_origin() { release_origin="${NUMES_RELEASE_ORIGIN:-$NUMES_DEFAULT_RELEASE_ORIGIN}" release_origin="${release_origin%/}" case "$release_origin" in *[!A-Za-z0-9.:/_-]*) fail "NUMES_RELEASE_ORIGIN contains unsupported characters" ;; esac case "$release_origin" in https://?*) curl_protocols="=https" ;; http://127.0.0.1 | http://127.0.0.1:* | http://127.0.0.1/* | \ http://localhost | http://localhost:* | http://localhost/*) curl_protocols="=http,https" ;; *) fail "NUMES_RELEASE_ORIGIN must be an https:// origin" ;; esac } fetch() { curl -fsSL --proto "$curl_protocols" --proto-redir "$curl_protocols" \ --tlsv1.2 --retry 3 --connect-timeout 20 -o "$2" "$1" } sha256_of() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{ print $1 }' elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | awk '{ print $1 }' else return 1 fi } verify_checksum() { expected="$(awk -v name="$2" '$2 == name || $2 == "*" name { print $1; exit }' "$3")" expected="$(printf '%s' "$expected" | tr 'A-F' 'a-f')" case "$expected" in *[!0-9a-f]*) fail "SHA256SUMS has a malformed entry for $2" ;; esac [ "${#expected}" -eq 64 ] || fail "SHA256SUMS has no entry for $2" actual="$(sha256_of "$1")" || fail "sha256sum or shasum is required to verify the download" actual="$(printf '%s' "$actual" | tr 'A-F' 'a-f')" [ "$actual" = "$expected" ] || fail "checksum mismatch for $2; the download was not installed" } extract_executable() { if ! tar -tzf "$1" | awk ' $0 == "./" { next } $0 == "numes" || $0 == "./numes" { found++; next } { other = 1 } END { exit (other || found != 1) }'; then fail "the archive must contain exactly one numes executable" fi mkdir "$work_dir/extract" tar -xzf "$1" -C "$work_dir/extract" || fail "could not extract the archive" executable="$work_dir/extract/numes" if [ -L "$executable" ] || [ ! -f "$executable" ]; then fail "the archive must contain exactly one numes executable" fi chmod 0755 "$executable" } download_release() { if [ -z "$install_version" ]; then fetch "$release_origin/numes/channels/$NUMES_CHANNEL.txt" "$work_dir/channel.txt" || fail "could not read the latest $NUMES_CHANNEL version from $release_origin" install_version="$(sed -n '1p' "$work_dir/channel.txt" | tr -d ' \t\r')" validate_version "$install_version" fi archive_name="numes-$install_version-$install_target.tar.gz" release_base="$release_origin/numes/$install_version" say "Downloading numes $install_version for $install_target" fetch "$release_base/SHA256SUMS" "$work_dir/SHA256SUMS" || fail "could not download SHA256SUMS for numes $install_version" fetch "$release_base/release.json" "$work_dir/release.json" || fail "could not download the release manifest for numes $install_version" fetch "$release_base/$archive_name" "$work_dir/$archive_name" || fail "could not download $archive_name" verify_checksum "$work_dir/$archive_name" "$archive_name" "$work_dir/SHA256SUMS" extract_executable "$work_dir/$archive_name" } create_work_dir() { work_dir="$(mktemp -d 2>/dev/null || mktemp -d -t numes-install)" || fail "could not create a temporary directory" trap 'rm -rf "$work_dir"' EXIT trap 'exit 130' INT trap 'exit 143' TERM } print_next_steps() { # `numes install` already reported the command link and any PATH change. say "" say "Next steps:" say " numes auth login" say " numes provider tui" } main() { parse_arguments "$@" detect_target create_work_dir if [ -n "$install_binary" ]; then [ -f "$install_binary" ] || fail "no such archive: $install_binary" extract_executable "$install_binary" "$executable" install || fail "numes install failed" else command -v curl >/dev/null 2>&1 || fail "curl is required to download numes" resolve_origin download_release "$executable" install --manifest "$work_dir/release.json" || fail "numes install failed" fi print_next_steps } main "$@"