install.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env bash
  2. # Install the New Relic CLI.
  3. # https://github.com/newrelic/newrelic-cli
  4. #
  5. # Dependencies: curl, cut, tar, gzip
  6. #
  7. # The version to install and the binary location can be passed in via VERSION and DESTDIR respectively.
  8. #
  9. set -o errexit
  10. echo "Starting installation."
  11. # Determine release filename. This can be expanded with CPU arch in the future.
  12. if [ "$(uname)" == "Linux" ]; then
  13. OS="Linux"
  14. elif [ "$(uname)" == "Darwin" ]; then
  15. OS="Darwin"
  16. else
  17. echo "This operating system is not supported. The supported operating systems are Linux and Darwin"
  18. exit 1
  19. fi
  20. if [ "$(uname -m)" == "x86_64" ]; then
  21. MACHINE="x86_64"
  22. elif [ "$(uname -m)" == "aarch64" ] || [ "$(uname -m)" == "arm64" ]; then
  23. MACHINE="arm64"
  24. elif [ "$(uname -m)" == "armv7l" ]; then
  25. MACHINE="armv7"
  26. else
  27. echo "This machine architecture is not supported. The supported architectures are x86_64, aarch64, armv7."
  28. exit 1
  29. fi
  30. for x in cut tar gzip sudo; do
  31. which $x > /dev/null || (echo "Unable to continue. Please install $x before proceeding."; exit 1)
  32. done
  33. DISTRO=$(cat /etc/issue /etc/system-release /etc/redhat-release /etc/os-release 2>/dev/null | grep -m 1 -Eo "(Ubuntu|Amazon|CentOS|Debian|Red Hat|SUSE)" || true)
  34. IS_CURL_INSTALLED=$(which curl | wc -l)
  35. if [ $IS_CURL_INSTALLED -eq 0 ]; then
  36. echo "curl is required to install, please confirm Y/N to install (default Y): "
  37. read -r CONFIRM_CURL
  38. if [ "$CONFIRM_CURL" == "Y" ] || [ "$CONFIRM_CURL" == "y" ] || [ "$CONFIRM_CURL" == "" ]; then
  39. if [ "$DISTRO" == "Ubuntu" ] || [ "$DISTRO" == "Debian" ]; then
  40. sudo apt-get update
  41. sudo apt-get install curl -y
  42. elif [ "$DISTRO" == "Amazon" ] || [ "$DISTRO" == "CentOS" ] || [ "$DISTRO" == "Red Hat" ]; then
  43. sudo yum install curl -y
  44. elif [ "$DISTRO" == "SUSE" ]; then
  45. sudo zypper -n install curl
  46. else
  47. echo "Unable to continue. Please install curl manually before proceeding."; exit 131
  48. fi
  49. else
  50. echo "Unable to continue without curl. Please install curl before proceeding."; exit 131
  51. fi
  52. fi
  53. # GitHub's URL for the latest release, will redirect.
  54. LATEST_URL="https://download.newrelic.com/install/newrelic-cli/currentVersion.txt"
  55. DESTDIR="${DESTDIR:-/usr/local/bin}"
  56. if [ -z "$VERSION" ]; then
  57. VERSION=$(curl -sL $LATEST_URL | cut -d "v" -f 2)
  58. fi
  59. echo "Installing New Relic CLI v${VERSION}"
  60. # Run the script in a temporary directory that we know is empty.
  61. SCRATCH=$(mktemp -d || mktemp -d -t 'tmp')
  62. cd "$SCRATCH"
  63. function error {
  64. echo "An error occurred installing the tool."
  65. echo "The contents of the directory $SCRATCH have been left in place to help to debug the issue."
  66. }
  67. trap error ERR
  68. RELEASE_URL="https://download.newrelic.com/install/newrelic-cli/v${VERSION}/newrelic-cli_${VERSION}_${OS}_${MACHINE}.tar.gz"
  69. # Download & unpack the release tarball.
  70. curl -sL --retry 3 "${RELEASE_URL}" | tar -xz
  71. if [ "$UID" != "0" ]; then
  72. echo "Installing to $DESTDIR using sudo"
  73. sudo mv newrelic "$DESTDIR"
  74. sudo chmod +x "$DESTDIR/newrelic"
  75. sudo chown root:0 "$DESTDIR/newrelic"
  76. else
  77. echo "Installing to $DESTDIR"
  78. mv newrelic "$DESTDIR"
  79. chmod +x "$DESTDIR/newrelic"
  80. chown root:0 "$DESTDIR/newrelic"
  81. fi
  82. # Delete the working directory when the install was successful.
  83. rm -r "$SCRATCH"