|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# |
| 4 | +# ci_download_cockroachdb: fetches the appropriate CockroachDB binary tarball |
| 5 | +# based on the currently running operating system, unpacks it, and creates a |
| 6 | +# symlink called "cockroach", all in the current directory. |
| 7 | +# |
| 8 | + |
| 9 | +set -o pipefail |
| 10 | +set -o xtrace |
| 11 | +set -o errexit |
| 12 | + |
| 13 | +# If you change this, you must also update the md5sums below |
| 14 | +ARG0="$(basename ${BASH_SOURCE[0]})" |
| 15 | +CIDL_VERSION="v20.2.5" |
| 16 | +CIDL_MD5_DARWIN="4c4b84861c313884a4ef5fbbe57cb543" |
| 17 | +CIDL_MD5_LINUX="8fe4f47413e2c6570da0e661af716f9a" |
| 18 | +CIDL_MD5_WINDOWS="3617e25afd6660116d59d7556ff99c6e" |
| 19 | + |
| 20 | +function main |
| 21 | +{ |
| 22 | + # |
| 23 | + # Process command-line arguments. We generally don't expect any, but |
| 24 | + # we allow callers to specify a value to override OSTYPE, just for |
| 25 | + # testing. |
| 26 | + # |
| 27 | + if [[ $# != 0 ]]; then |
| 28 | + CIDL_OS="$1" |
| 29 | + shift |
| 30 | + else |
| 31 | + CIDL_OS="$OSTYPE" |
| 32 | + fi |
| 33 | + |
| 34 | + if [[ $# != 0 ]]; then |
| 35 | + echo "unexpected arguments" >&2 |
| 36 | + exit 2 |
| 37 | + fi |
| 38 | + |
| 39 | + # Configure this program |
| 40 | + configure_os "$CIDL_OS" |
| 41 | + CIDL_URL="https://binaries.cockroachdb.com/$CIDL_FILE" |
| 42 | + |
| 43 | + # Download the file. |
| 44 | + echo "URL: $CIDL_URL" |
| 45 | + echo "Local file: $CIDL_FILE" |
| 46 | + $CIDL_DOWNLOAD "$CIDL_URL" "$CIDL_FILE" || \ |
| 47 | + fail "failed to download file" |
| 48 | + |
| 49 | + # Verify the md5sum. |
| 50 | + calculated_md5="$($CIDL_MD5FUNC "$CIDL_FILE")" || \ |
| 51 | + fail "failed to calculate md5sum" |
| 52 | + if [[ "$calculated_md5" != "$CIDL_MD5" ]]; then |
| 53 | + fail "md5sum mismatch \ |
| 54 | + (expected $CIDL_MD5, found $calculated_md5)" |
| 55 | + fi |
| 56 | + |
| 57 | + # Unpack the tarball |
| 58 | + $CIDL_UNPACK "$CIDL_FILE" |
| 59 | + |
| 60 | + # Symlink the "cockroach" binary to the right spot. |
| 61 | + ln -fs "$CIDL_DIR/cockroach" "cockroach" |
| 62 | + |
| 63 | + # Attempt to run it as a sanity-check |
| 64 | + ./cockroach version |
| 65 | +} |
| 66 | + |
| 67 | +function fail |
| 68 | +{ |
| 69 | + echo "$ARG0: $@" >&2 |
| 70 | + exit 1 |
| 71 | +} |
| 72 | + |
| 73 | +function configure_os |
| 74 | +{ |
| 75 | + echo "current directory: $PWD" |
| 76 | + echo "configuring based on OS: \"$OSTYPE\"" |
| 77 | + case "$1" in |
| 78 | + darwin*) |
| 79 | + CIDL_BUILD="darwin-10.9-amd64" |
| 80 | + CIDL_SUFFIX="tgz" |
| 81 | + CIDL_MD5="$CIDL_MD5_DARWIN" |
| 82 | + CIDL_MD5FUNC="do_md5" |
| 83 | + CIDL_DOWNLOAD="do_download_curl" |
| 84 | + CIDL_UNPACK="do_untar" |
| 85 | + ;; |
| 86 | + linux-gnu*) |
| 87 | + CIDL_BUILD="linux-amd64" |
| 88 | + CIDL_SUFFIX="tgz" |
| 89 | + CIDL_MD5="$CIDL_MD5_LINUX" |
| 90 | + CIDL_MD5FUNC="do_md5sum" |
| 91 | + CIDL_DOWNLOAD="do_download_curl" |
| 92 | + CIDL_UNPACK="do_untar" |
| 93 | + ;; |
| 94 | + *) |
| 95 | + echo "assuming Windows" |
| 96 | + CIDL_BUILD="windows-6.2-amd64" |
| 97 | + CIDL_SUFFIX="zip" |
| 98 | + CIDL_MD5="$CIDL_MD5_WINDOWS" |
| 99 | + CIDL_MD5FUNC="do_md5_windows" |
| 100 | + CIDL_DOWNLOAD="do_download_windows" |
| 101 | + CIDL_UNPACK="do_unzip" |
| 102 | + ;; |
| 103 | + esac |
| 104 | + |
| 105 | + CIDL_DIR="cockroach-$CIDL_VERSION.$CIDL_BUILD" |
| 106 | + CIDL_FILE="$CIDL_DIR.$CIDL_SUFFIX" |
| 107 | +} |
| 108 | + |
| 109 | +function do_download_curl |
| 110 | +{ |
| 111 | + curl --silent --show-error --fail --location --output "$2" "$1" |
| 112 | +} |
| 113 | + |
| 114 | +function do_download_windows |
| 115 | +{ |
| 116 | + pwsh -Command "& {Invoke-WebRequest -Uri \"$1\" -OutFile \"$2\"}" |
| 117 | +} |
| 118 | + |
| 119 | +function do_md5 |
| 120 | +{ |
| 121 | + md5 < "$1" |
| 122 | +} |
| 123 | + |
| 124 | +function do_md5sum |
| 125 | +{ |
| 126 | + md5sum < "$1" | awk '{print $1}' |
| 127 | +} |
| 128 | + |
| 129 | +function do_md5_windows |
| 130 | +{ |
| 131 | + pwsh -Command "& {Get-FileHash \"$1\" -Algorithm MD5 | Format-List -Property Hash}" | \ |
| 132 | + awk '$1 == "Hash"{ print $NF }' | |
| 133 | + tr '[A-Z]' '[a-z]' |
| 134 | +} |
| 135 | + |
| 136 | +function do_untar |
| 137 | +{ |
| 138 | + tar xzf "$1" |
| 139 | +} |
| 140 | + |
| 141 | +function do_unzip |
| 142 | +{ |
| 143 | + pwsh -Command "& {Expand-Archive \"$1\" \".\"}" |
| 144 | +} |
| 145 | + |
| 146 | +main "$@" |
0 commit comments