#!/bin/sh # # Installs Drift for the first time. # # curl -fsSL https://download.pactflow.io/drift/install.sh | sh # # Works out which platform this is, downloads the drift-component executable for it, checks it # against the release's SHA256SUMS, and runs `drift-component bootstrap`, which installs the rest. # Nothing is installed by this script itself, and nothing is left behind but the installation. # # Arguments after `sh -s --` are passed to `drift-component bootstrap`: # # curl -fsSL https://download.pactflow.io/drift/install.sh | sh -s -- --drift-home-dir ~/tools/drift # curl -fsSL https://download.pactflow.io/drift/install.sh | sh -s -- --dry-run # # Environment: # # DRIFT_VERSION the bundle version to install, instead of the latest release # DRIFT_DOWNLOAD_BASE_URL where the release is downloaded from, for testing this script against a # mirror. drift-component itself always installs from download.pactflow.io # Everything is in functions and the work starts on the last line, so a download cut short part way # through runs nothing at all rather than half of the script. set -eu BASE_URL="${DRIFT_DOWNLOAD_BASE_URL:-https://download.pactflow.io/drift}" # The executable and its checksum come from the same server, so a downgrade to plain HTTP would let # whoever is in the middle replace both. Only a mirror named for testing may use it. if [ -n "${DRIFT_DOWNLOAD_BASE_URL:-}" ]; then PROTOCOLS='=https,http' WGET_REDIRECTS=20 else PROTOCOLS='=https' # wget cannot be told to follow only HTTPS redirects, so it follows none. The release is served # directly, and a redirect is as likely to be someone else's as a move. WGET_REDIRECTS=0 fi say() { printf '%s\n' "$*" } fail() { printf 'ERROR: %s\n' "$*" >&2 exit 1 } # The distribution name the release publishes builds under, such as linux-x86_64. detect_dist() { os="$(uname -s)" arch="$(uname -m)" case "$os" in Linux) os=linux ;; Darwin) os=macos ;; MINGW* | MSYS* | CYGWIN*) os=windows ;; *) fail "Drift does not publish builds for $os" ;; esac case "$arch" in x86_64 | amd64) arch=x86_64 ;; aarch64 | arm64) arch=aarch64 ;; *) fail "Drift does not publish $os builds for $arch" ;; esac # A shell running under Rosetta reports x86_64 on Apple silicon. The native build is the better # one to install. if [ "$os" = macos ] && [ "$arch" = x86_64 ] \ && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || true)" = 1 ]; then arch=aarch64 fi if [ "$os" = windows ] && [ "$arch" != x86_64 ]; then fail "Drift does not publish windows builds for $arch" fi printf '%s-%s' "$os" "$arch" } # Fetch a URL to a file, with whichever of curl or wget is installed. # # Only GNU wget will do: BusyBox wget can neither be stopped from following a redirect to plain # HTTP nor, in some builds, be relied on to check the server's certificate. download() { if command -v curl >/dev/null 2>&1; then curl --proto "$PROTOCOLS" --proto-redir "$PROTOCOLS" -fsSL "$1" -o "$2" elif command -v wget >/dev/null 2>&1 && wget --help 2>&1 | grep -q -- --max-redirect; then wget -q --max-redirect="$WGET_REDIRECTS" "$1" -O "$2" else fail "Installing Drift needs curl or GNU wget" fi } # The hex SHA-256 of a file. sha256_of() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d ' ' -f 1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d ' ' -f 1 elif command -v openssl >/dev/null 2>&1; then openssl dgst -sha256 -r "$1" | cut -d ' ' -f 1 else fail "Checking the download needs sha256sum, shasum or openssl" fi } main() { for arg in "$@"; do case "$arg" in --version | --version=*) fail "Set DRIFT_VERSION to choose the release, rather than --version" ;; esac done dist="$(detect_dist)" exe="" if [ "${dist%%-*}" = windows ]; then exe=".exe" fi file="drift-component-$dist$exe" work="$(mktemp -d 2>/dev/null || mktemp -d -t drift-install)" trap 'rm -rf "$work"' EXIT trap 'exit 1' HUP INT TERM # Install from one named release rather than from latest/, so the executable, its checksum and the # bundle it installs cannot come from either side of a release that happens part way through. version="${DRIFT_VERSION:-}" if [ -z "$version" ]; then download "$BASE_URL/version.txt" "$work/version.txt" \ || fail "Could not find out which Drift release is current from $BASE_URL/version.txt" version="$(tr -d ' \r\n' < "$work/version.txt")" [ -n "$version" ] || fail "$BASE_URL/version.txt did not name a release" fi say "Downloading drift-component $version for $dist" download "$BASE_URL/$version/$file" "$work/$file" \ || fail "Release $version publishes no $file. Releases before standalone drift-component \ executables were published cannot be installed this way; set DRIFT_VERSION to a later one." download "$BASE_URL/$version/SHA256SUMS" "$work/SHA256SUMS" \ || fail "Release $version publishes no SHA256SUMS, so $file cannot be checked" expected="$(awk -v name="$file" '$2 == name || $2 == "*" name { print $1 }' "$work/SHA256SUMS")" [ -n "$expected" ] || fail "SHA256SUMS for release $version does not list $file" actual="$(sha256_of "$work/$file")" if [ "$expected" != "$actual" ]; then fail "Checksum mismatch for $file: expected $expected, got $actual" fi chmod +x "$work/$file" # Piped into a shell, standard input is this script rather than the keyboard, so a prompt from # bootstrap would read the script and give up. Answer from the terminal when there is one. if [ -t 1 ] && (exec /dev/null; then "$work/$file" bootstrap --version "$version" "$@"