|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Licensed under the MIT license |
| 4 | +# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 5 | +# option. This file may not be copied, modified, or distributed |
| 6 | +# except according to those terms. |
| 7 | + |
| 8 | +# Installs the latest version of the Apollo MCP Server. |
| 9 | +# Specify a specific version to install with the $VERSION variable. |
| 10 | + |
| 11 | +# Example to bypass binary overwrite [y/N] prompt |
| 12 | +# curl -sSL https://mcp.apollo.dev/nix/latest | sh -s -- --force |
| 13 | + |
| 14 | +set -u |
| 15 | + |
| 16 | +BINARY_DOWNLOAD_PREFIX="${APOLLO_MCP_SERVER_BINARY_DOWNLOAD_PREFIX:="https://github.com/apollographql/apollo-mcp-server/releases/download"}" |
| 17 | + |
| 18 | +# Apollo MCP Server version defined in apollo-mcp-server's Cargo.toml |
| 19 | +# Note: Change this line manually during the release steps. |
| 20 | +PACKAGE_VERSION="v0.1.0-rc.0" |
| 21 | + |
| 22 | +download_binary_and_run_installer() { |
| 23 | + downloader --check |
| 24 | + need_cmd mktemp |
| 25 | + need_cmd chmod |
| 26 | + need_cmd mkdir |
| 27 | + need_cmd rm |
| 28 | + need_cmd rmdir |
| 29 | + need_cmd tar |
| 30 | + need_cmd which |
| 31 | + need_cmd dirname |
| 32 | + need_cmd awk |
| 33 | + need_cmd cut |
| 34 | + |
| 35 | + # if $VERSION isn't provided or has 0 length, use version from Rover cargo.toml |
| 36 | + # ${VERSION:-} checks if version exists, and if doesn't uses the default |
| 37 | + # which is after the :-, which in this case is empty. -z checks for empty str |
| 38 | + if [ -z ${VERSION:-} ]; then |
| 39 | + # VERSION is either not set or empty |
| 40 | + DOWNLOAD_VERSION=$PACKAGE_VERSION |
| 41 | + else |
| 42 | + # VERSION set and not empty |
| 43 | + DOWNLOAD_VERSION=$VERSION |
| 44 | + fi |
| 45 | + |
| 46 | + |
| 47 | + get_architecture || return 1 |
| 48 | + local _arch="$RETVAL" |
| 49 | + assert_nz "$_arch" "arch" |
| 50 | + |
| 51 | + local _ext="" |
| 52 | + case "$_arch" in |
| 53 | + *windows*) |
| 54 | + _ext=".exe" |
| 55 | + ;; |
| 56 | + esac |
| 57 | + |
| 58 | + local _tardir="apollo-mcp-server-$DOWNLOAD_VERSION-${_arch}" |
| 59 | + local _url="$BINARY_DOWNLOAD_PREFIX/$DOWNLOAD_VERSION/${_tardir}.tar.gz" |
| 60 | + local _dir="$(mktemp -d 2>/dev/null || ensure mktemp -d -t apollo-mcp-server)" |
| 61 | + local _file="$_dir/input.tar.gz" |
| 62 | + local _apollo_mcp_server="$_dir/apollo-mcp-server$_ext" |
| 63 | + local _safe_url |
| 64 | + |
| 65 | + # Remove credentials from the URL for logging |
| 66 | + _safe_url=$(echo "$_url" | awk '{sub("https://[^@]+@","https://");}1') |
| 67 | + say "downloading apollo-mcp-server from $_safe_url" 1>&2 |
| 68 | + |
| 69 | + ensure mkdir -p "$_dir" |
| 70 | + downloader "$_url" "$_file" |
| 71 | + if [ $? != 0 ]; then |
| 72 | + say "failed to download $_safe_url" |
| 73 | + say "this may be a standard network error, but it may also indicate" |
| 74 | + say "that the MCP Server's release process is not working. When in doubt" |
| 75 | + say "please feel free to open an issue!" |
| 76 | + say "https://github.com/apollographql/apollo-mcp-server/issues/new/choose" |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | + |
| 80 | + ensure tar xf "$_file" --strip-components 1 -C "$_dir" |
| 81 | + |
| 82 | + outfile="./apollo-mcp-server" |
| 83 | + |
| 84 | + say "Moving $_apollo_mcp_server to $outfile ..." |
| 85 | + mv "$_apollo_mcp_server" "$outfile" |
| 86 | + |
| 87 | + local _retval=$? |
| 88 | + |
| 89 | + say "" |
| 90 | + say "You can now run the Apollo MCP Server using '$outfile'" |
| 91 | + |
| 92 | + ignore rm -rf "$_dir" |
| 93 | + |
| 94 | + return "$_retval" |
| 95 | +} |
| 96 | + |
| 97 | +get_architecture() { |
| 98 | + local _ostype="$(uname -s)" |
| 99 | + local _cputype="$(uname -m)" |
| 100 | + |
| 101 | + if [ "$_ostype" = Darwin -a "$_cputype" = i386 ]; then |
| 102 | + # Darwin `uname -s` lies |
| 103 | + if sysctl hw.optional.x86_64 | grep -q ': 1'; then |
| 104 | + local _cputype=x86_64 |
| 105 | + fi |
| 106 | + fi |
| 107 | + |
| 108 | + if [ "$_ostype" = Darwin -a "$_cputype" = arm64 ]; then |
| 109 | + # Darwin `uname -s` doesn't seem to lie on Big Sur |
| 110 | + # but the cputype we want is called aarch64, not arm64 (they are equivalent) |
| 111 | + local _cputype=aarch64 |
| 112 | + fi |
| 113 | + |
| 114 | + case "$_ostype" in |
| 115 | + Linux) |
| 116 | + if has_required_glibc; then |
| 117 | + local _ostype=unknown-linux-gnu |
| 118 | + else |
| 119 | + local _ostype=unknown-linux-musl |
| 120 | + |
| 121 | + # We do not currently release builds for aarch64-unknown-linux-musl |
| 122 | + if [ "$_cputype" = aarch64 ]; then |
| 123 | + err "Unsupported platform: aarch64-$_ostype" |
| 124 | + fi |
| 125 | + |
| 126 | + say "Downloading musl binary" |
| 127 | + fi |
| 128 | + ;; |
| 129 | + |
| 130 | + Darwin) |
| 131 | + local _ostype=apple-darwin |
| 132 | + ;; |
| 133 | + |
| 134 | + MINGW* | MSYS* | CYGWIN*) |
| 135 | + local _ostype=pc-windows-msvc |
| 136 | + ;; |
| 137 | + |
| 138 | + *) |
| 139 | + err "no precompiled binaries available for OS: $_ostype" |
| 140 | + ;; |
| 141 | + esac |
| 142 | + |
| 143 | + case "$_cputype" in |
| 144 | + # these are the only two acceptable values for cputype |
| 145 | + x86_64 | aarch64 ) |
| 146 | + ;; |
| 147 | + *) |
| 148 | + err "no precompiled binaries available for CPU architecture: $_cputype" |
| 149 | + |
| 150 | + esac |
| 151 | + |
| 152 | + local _arch="$_cputype-$_ostype" |
| 153 | + |
| 154 | + RETVAL="$_arch" |
| 155 | +} |
| 156 | + |
| 157 | +say() { |
| 158 | + local green=`tput setaf 2 2>/dev/null || echo ''` |
| 159 | + local reset=`tput sgr0 2>/dev/null || echo ''` |
| 160 | + echo "$1" |
| 161 | +} |
| 162 | + |
| 163 | +err() { |
| 164 | + local red=`tput setaf 1 2>/dev/null || echo ''` |
| 165 | + local reset=`tput sgr0 2>/dev/null || echo ''` |
| 166 | + say "${red}ERROR${reset}: $1" >&2 |
| 167 | + exit 1 |
| 168 | +} |
| 169 | + |
| 170 | +has_required_glibc() { |
| 171 | + local _ldd_version="$(ldd --version 2>&1 | head -n1)" |
| 172 | + # glibc version string is inconsistent across distributions |
| 173 | + # instead check if the string does not contain musl (case insensitive) |
| 174 | + if echo "${_ldd_version}" | grep -iv musl >/dev/null; then |
| 175 | + local _glibc_version=$(echo "${_ldd_version}" | awk 'NR==1 { print $NF }') |
| 176 | + local _glibc_major_version=$(echo "${_glibc_version}" | cut -d. -f1) |
| 177 | + local _glibc_min_version=$(echo "${_glibc_version}" | cut -d. -f2) |
| 178 | + local _min_major_version=2 |
| 179 | + local _min_minor_version=17 |
| 180 | + if [ "${_glibc_major_version}" -gt "${_min_major_version}" ] \ |
| 181 | + || { [ "${_glibc_major_version}" -eq "${_min_major_version}" ] \ |
| 182 | + && [ "${_glibc_min_version}" -ge "${_min_minor_version}" ]; }; then |
| 183 | + return 0 |
| 184 | + else |
| 185 | + say "This operating system needs glibc >= ${_min_major_version}.${_min_minor_version}, but only has ${_libc_version} installed." |
| 186 | + fi |
| 187 | + else |
| 188 | + say "This operating system does not support dynamic linking to glibc." |
| 189 | + fi |
| 190 | + |
| 191 | + return 1 |
| 192 | +} |
| 193 | + |
| 194 | +need_cmd() { |
| 195 | + if ! check_cmd "$1" |
| 196 | + then err "need '$1' (command not found)" |
| 197 | + fi |
| 198 | +} |
| 199 | + |
| 200 | +check_cmd() { |
| 201 | + command -v "$1" > /dev/null 2>&1 |
| 202 | + return $? |
| 203 | +} |
| 204 | + |
| 205 | +need_ok() { |
| 206 | + if [ $? != 0 ]; then err "$1"; fi |
| 207 | +} |
| 208 | + |
| 209 | +assert_nz() { |
| 210 | + if [ -z "$1" ]; then err "assert_nz $2"; fi |
| 211 | +} |
| 212 | + |
| 213 | +# Run a command that should never fail. If the command fails execution |
| 214 | +# will immediately terminate with an error showing the failing |
| 215 | +# command. |
| 216 | +ensure() { |
| 217 | + "$@" |
| 218 | + need_ok "command failed: $*" |
| 219 | +} |
| 220 | + |
| 221 | +# This is just for indicating that commands' results are being |
| 222 | +# intentionally ignored. Usually, because it's being executed |
| 223 | +# as part of error handling. |
| 224 | +ignore() { |
| 225 | + "$@" |
| 226 | +} |
| 227 | + |
| 228 | +# This wraps curl or wget. Try curl first, if not installed, |
| 229 | +# use wget instead. |
| 230 | +downloader() { |
| 231 | + if check_cmd curl |
| 232 | + then _dld=curl |
| 233 | + elif check_cmd wget |
| 234 | + then _dld=wget |
| 235 | + else _dld='curl or wget' # to be used in error message of need_cmd |
| 236 | + fi |
| 237 | + |
| 238 | + if [ "$1" = --check ] |
| 239 | + then need_cmd "$_dld" |
| 240 | + elif [ "$_dld" = curl ] |
| 241 | + then curl -sSfL "$1" -o "$2" |
| 242 | + elif [ "$_dld" = wget ] |
| 243 | + then wget "$1" -O "$2" |
| 244 | + else err "Unknown downloader" # should not reach here |
| 245 | + fi |
| 246 | +} |
| 247 | + |
| 248 | +download_binary_and_run_installer "$@" || exit 1 |
0 commit comments