agave-install-init.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/sh
  2. # Copyright 2016 The Rust Project Developers. See the COPYRIGHT
  3. # file at the top-level directory of this distribution and at
  4. # http://rust-lang.org/COPYRIGHT.
  5. #
  6. # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  7. # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  8. # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  9. # option. This file may not be copied, modified, or distributed
  10. # except according to those terms.
  11. # This is just a little script that can be downloaded from the internet to
  12. # install agave-install. It just does platform detection, downloads the installer
  13. # and runs it.
  14. { # this ensures the entire script is downloaded #
  15. if [ -z "$SOLANA_DOWNLOAD_ROOT" ]; then
  16. SOLANA_DOWNLOAD_ROOT="https://github.com/anza-xyz/agave/releases/download/"
  17. fi
  18. GH_LATEST_RELEASE="https://api.github.com/repos/anza-xyz/agave/releases/latest"
  19. set -e
  20. usage() {
  21. cat 1>&2 <<EOF
  22. agave-install-init
  23. initializes a new installation
  24. USAGE:
  25. agave-install-init [FLAGS] [OPTIONS] --data_dir <PATH> --pubkey <PUBKEY>
  26. FLAGS:
  27. -h, --help Prints help information
  28. --no-modify-path Don't configure the PATH environment variable
  29. OPTIONS:
  30. -d, --data-dir <PATH> Directory to store install data
  31. -u, --url <URL> JSON RPC URL for the solana cluster
  32. -p, --pubkey <PUBKEY> Public key of the update manifest
  33. EOF
  34. }
  35. main() {
  36. downloader --check
  37. need_cmd uname
  38. need_cmd mktemp
  39. need_cmd chmod
  40. need_cmd mkdir
  41. need_cmd rm
  42. need_cmd sed
  43. need_cmd grep
  44. for arg in "$@"; do
  45. case "$arg" in
  46. -h|--help)
  47. usage
  48. exit 0
  49. ;;
  50. *)
  51. ;;
  52. esac
  53. done
  54. _ostype="$(uname -s)"
  55. _cputype="$(uname -m)"
  56. case "$_ostype" in
  57. Linux)
  58. _ostype=unknown-linux-gnu
  59. ;;
  60. Darwin)
  61. if [[ $_cputype = arm64 ]]; then
  62. _cputype=aarch64
  63. fi
  64. _ostype=apple-darwin
  65. ;;
  66. *)
  67. err "machine architecture is currently unsupported"
  68. ;;
  69. esac
  70. TARGET="${_cputype}-${_ostype}"
  71. temp_dir="$(mktemp -d 2>/dev/null || ensure mktemp -d -t agave-install-init)"
  72. ensure mkdir -p "$temp_dir"
  73. # Check for SOLANA_RELEASE environment variable override. Otherwise fetch
  74. # the latest release tag from github
  75. if [ -n "$SOLANA_RELEASE" ]; then
  76. release="$SOLANA_RELEASE"
  77. else
  78. release_file="$temp_dir/release"
  79. printf 'looking for latest release\n' 1>&2
  80. ensure downloader "$GH_LATEST_RELEASE" "$release_file"
  81. release=$(\
  82. grep -m 1 \"tag_name\": "$release_file" \
  83. | sed -ne 's/^ *"tag_name": "\([^"]*\)",$/\1/p' \
  84. )
  85. if [ -z "$release" ]; then
  86. err 'Unable to figure latest release'
  87. fi
  88. fi
  89. download_url="$SOLANA_DOWNLOAD_ROOT/$release/agave-install-init-$TARGET"
  90. solana_install_init="$temp_dir/agave-install-init"
  91. printf 'downloading %s installer\n' "$release" 1>&2
  92. ensure mkdir -p "$temp_dir"
  93. ensure downloader "$download_url" "$solana_install_init"
  94. ensure chmod u+x "$solana_install_init"
  95. if [ ! -x "$solana_install_init" ]; then
  96. printf '%s\n' "Cannot execute $solana_install_init (likely because of mounting /tmp as noexec)." 1>&2
  97. printf '%s\n' "Please copy the file to a location where you can execute binaries and run ./agave-install-init." 1>&2
  98. exit 1
  99. fi
  100. if [ -z "$1" ]; then
  101. #shellcheck disable=SC2086
  102. ignore "$solana_install_init" $SOLANA_INSTALL_INIT_ARGS
  103. else
  104. ignore "$solana_install_init" "$@"
  105. fi
  106. retval=$?
  107. ignore rm "$solana_install_init"
  108. ignore rm -rf "$temp_dir"
  109. return "$retval"
  110. }
  111. err() {
  112. printf 'agave-install-init: %s\n' "$1" >&2
  113. exit 1
  114. }
  115. need_cmd() {
  116. if ! check_cmd "$1"; then
  117. err "need '$1' (command not found)"
  118. fi
  119. }
  120. check_cmd() {
  121. command -v "$1" > /dev/null 2>&1
  122. }
  123. # Run a command that should never fail. If the command fails execution
  124. # will immediately terminate with an error showing the failing
  125. # command.
  126. ensure() {
  127. if ! "$@"; then
  128. err "command failed: $*"
  129. fi
  130. }
  131. # This is just for indicating that commands' results are being
  132. # intentionally ignored. Usually, because it's being executed
  133. # as part of error handling.
  134. ignore() {
  135. "$@"
  136. }
  137. # This wraps curl or wget. Try curl first, if not installed,
  138. # use wget instead.
  139. downloader() {
  140. if check_cmd curl; then
  141. program=curl
  142. elif check_cmd wget; then
  143. program=wget
  144. else
  145. program='curl or wget' # to be used in error message of need_cmd
  146. fi
  147. if [ "$1" = --check ]; then
  148. need_cmd "$program"
  149. elif [ "$program" = curl ]; then
  150. curl -sSfL "$1" -o "$2"
  151. elif [ "$program" = wget ]; then
  152. wget "$1" -O "$2"
  153. else
  154. err "Unknown downloader" # should not reach here
  155. fi
  156. }
  157. main "$@"
  158. } # this ensures the entire script is downloaded #