|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +APP_NAME="bee" |
| 4 | +REPO_URL="https://github.com/ethersphere/bee" |
| 5 | + |
| 6 | +: "${USE_SUDO:="true"}" |
| 7 | +: "${BEE_INSTALL_DIR:="/usr/local/bin"}" |
| 8 | + |
| 9 | +detect_arch() { |
| 10 | + ARCH=$(uname -m) |
| 11 | + case $ARCH in |
| 12 | + armv5*) ARCH="armv5";; |
| 13 | + armv6*) ARCH="armv6";; |
| 14 | + armv7*) ARCH="arm";; |
| 15 | + aarch64) ARCH="arm64";; |
| 16 | + x86) ARCH="386";; |
| 17 | + x86_64) ARCH="amd64";; |
| 18 | + i686) ARCH="386";; |
| 19 | + i386) ARCH="386";; |
| 20 | + esac |
| 21 | +} |
| 22 | + |
| 23 | +detect_os() { |
| 24 | + OS=$(uname|tr '[:upper:]' '[:lower:]') |
| 25 | + |
| 26 | + case "$OS" in |
| 27 | + # Minimalist GNU for Windows |
| 28 | + mingw*) OS='windows';; |
| 29 | + esac |
| 30 | +} |
| 31 | + |
| 32 | +run_as_root() { |
| 33 | + local CMD="$*" |
| 34 | + |
| 35 | + if [ $EUID -ne 0 ] && [ $USE_SUDO = "true" ]; then |
| 36 | + CMD="sudo $CMD" |
| 37 | + fi |
| 38 | + |
| 39 | + $CMD |
| 40 | +} |
| 41 | + |
| 42 | +supported() { |
| 43 | + local supported="darwin-amd64\nlinux-386\nlinux-amd64\nlinux-arm64\nlinux-armv6" |
| 44 | + if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then |
| 45 | + if [ $OS == "windows" ]; then |
| 46 | + echo "Auto install not supported for Windows." |
| 47 | + echo "Install binary from here $REPO_URL/releases" |
| 48 | + exit 1 |
| 49 | + else |
| 50 | + echo "No prebuilt binary for ${OS}-${ARCH}." |
| 51 | + echo "To build from source, go to $REPO_URL" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + fi |
| 55 | + |
| 56 | + if ! command -v curl &> /dev/null && ! command -v wget &> /dev/null; then |
| 57 | + echo "Either curl or wget is required" |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +# check_installed_version checks which version of bee is installed and |
| 63 | +# if it needs to be changed. |
| 64 | +check_installed_version() { |
| 65 | + if [[ -f "${BEE_INSTALL_DIR}/${APP_NAME}" ]]; then |
| 66 | + local version=$(bee version 2>&1) |
| 67 | + if [[ "${version%-*}" == "${TAG#v}" ]]; then |
| 68 | + echo "bee ${version} is already ${DESIRED_VERSION:-latest}" |
| 69 | + return 0 |
| 70 | + else |
| 71 | + echo "bee ${TAG} is available. Changing from version ${version}." |
| 72 | + return 1 |
| 73 | + fi |
| 74 | + else |
| 75 | + return 1 |
| 76 | + fi |
| 77 | +} |
| 78 | + |
| 79 | +# check_tag_provided checks whether TAG has provided as an environment variable so we can skip check_latest_version. |
| 80 | +check_tag_provided() { |
| 81 | + [[ ! -z "$TAG" ]] |
| 82 | +} |
| 83 | + |
| 84 | +# check_latest_version grabs the latest version string from the releases |
| 85 | +check_latest_version() { |
| 86 | + local latest_release_url="$REPO_URL/releases/latest" |
| 87 | + if command -v curl &> /dev/null; then |
| 88 | + TAG=$(curl -Ls -o /dev/null -w %{url_effective} $latest_release_url | grep -oE "[^/]+$" ) |
| 89 | + elif command -v wget &> /dev/null; then |
| 90 | + TAG=$(wget $latest_release_url --server-response -O /dev/null 2>&1 | awk '/^ Location: /{DEST=$2} END{ print DEST}' | grep -oE "[^/]+$") |
| 91 | + fi |
| 92 | +} |
| 93 | + |
| 94 | +# download_file downloads the latest binary package and also the checksum |
| 95 | +# for that binary. |
| 96 | +download_file() { |
| 97 | + BEE_DIST="bee-$OS-$ARCH" |
| 98 | + if [ "$OS" == "windows" ]; then |
| 99 | + BEE_DIST="bee-$OS-$ARCH.exe" |
| 100 | + fi |
| 101 | + DOWNLOAD_URL="$REPO_URL/releases/download/$TAG/$BEE_DIST" |
| 102 | + BEE_TMP_ROOT="$(mktemp -dt bee-binary-XXXXXX)" |
| 103 | + BEE_TMP_FILE="$BEE_TMP_ROOT/$BEE_DIST" |
| 104 | + if command -v curl &> /dev/null; then |
| 105 | + curl -SsL "$DOWNLOAD_URL" -o "$BEE_TMP_FILE" |
| 106 | + elif command -v wget &> /dev/null; then |
| 107 | + wget -q -O "$BEE_TMP_FILE" "$DOWNLOAD_URL" |
| 108 | + fi |
| 109 | +} |
| 110 | + |
| 111 | +# install_file verifies the SHA256 for the file, then unpacks and |
| 112 | +# installs it. |
| 113 | +install_file() { |
| 114 | + echo "Preparing to install $APP_NAME into ${BEE_INSTALL_DIR}" |
| 115 | + run_as_root chmod +x "$BEE_TMP_FILE" |
| 116 | + run_as_root cp "$BEE_TMP_FILE" "$BEE_INSTALL_DIR/$APP_NAME" |
| 117 | + echo "$APP_NAME installed into $BEE_INSTALL_DIR/$APP_NAME" |
| 118 | +} |
| 119 | + |
| 120 | +# fail_trap is executed if an error occurs. |
| 121 | +fail_trap() { |
| 122 | + result=$? |
| 123 | + if [ "$result" != "0" ]; then |
| 124 | + if [[ -n "$INPUT_ARGUMENTS" ]]; then |
| 125 | + echo "Failed to install $APP_NAME with the arguments provided: $INPUT_ARGUMENTS" |
| 126 | + help |
| 127 | + else |
| 128 | + echo "Failed to install $APP_NAME" |
| 129 | + fi |
| 130 | + echo -e "\tFor support, go to $REPO_URL." |
| 131 | + fi |
| 132 | + cleanup |
| 133 | + exit $result |
| 134 | +} |
| 135 | + |
| 136 | +# test_binary tests the installed client to make sure it is working. |
| 137 | +test_binary() { |
| 138 | + if ! command -v $APP_NAME &> /dev/null; then |
| 139 | + echo "$APP_NAME not found. Is $BEE_INSTALL_DIR on your "'$PATH?' |
| 140 | + exit 1 |
| 141 | + fi |
| 142 | + echo "Run '$APP_NAME --help' to see what you can do with it." |
| 143 | +} |
| 144 | + |
| 145 | +# help provides possible cli installation arguments |
| 146 | +help () { |
| 147 | + echo "Accepted cli arguments are:" |
| 148 | + echo -e "\t[--help|-h] ->> prints this help" |
| 149 | + echo -e "\t[--no-sudo] ->> install without sudo" |
| 150 | +} |
| 151 | + |
| 152 | +# cleanup temporary files |
| 153 | +cleanup() { |
| 154 | + if [[ -d "${BEE_TMP_ROOT:-}" ]]; then |
| 155 | + rm -rf "$BEE_TMP_ROOT" |
| 156 | + fi |
| 157 | +} |
| 158 | + |
| 159 | +# Execution |
| 160 | + |
| 161 | +#Stop execution on any error |
| 162 | +trap "fail_trap" EXIT |
| 163 | +set -e |
| 164 | + |
| 165 | +# Parsing input arguments (if any) |
| 166 | +export INPUT_ARGUMENTS="${@}" |
| 167 | +set -u |
| 168 | +while [[ $# -gt 0 ]]; do |
| 169 | + case $1 in |
| 170 | + '--no-sudo') |
| 171 | + USE_SUDO="false" |
| 172 | + ;; |
| 173 | + '--help'|-h) |
| 174 | + help |
| 175 | + exit 0 |
| 176 | + ;; |
| 177 | + *) exit 1 |
| 178 | + ;; |
| 179 | + esac |
| 180 | + shift |
| 181 | +done |
| 182 | +set +u |
| 183 | + |
| 184 | +detect_arch |
| 185 | +detect_os |
| 186 | +supported |
| 187 | +check_tag_provided || check_latest_version |
| 188 | +if ! check_installed_version; then |
| 189 | + download_file |
| 190 | + install_file |
| 191 | +fi |
| 192 | + |
| 193 | +test_binary |
| 194 | +cleanup |
0 commit comments