azure_cli_deb_install.sh 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env bash
  2. #######################################################################################################################
  3. # This script does three fundamental things: #
  4. # 1. Add Microsoft's GPG Key has a trusted source of apt packages. #
  5. # 2. Add Microsoft's repositories as a source for apt packages. #
  6. # 3. Installs the Azure CLI from those repositories. #
  7. # Given the nature of this script, it must be executed with elevated privileges, i.e. with `sudo`. #
  8. # #
  9. # Copied from https://azurecliprod.blob.core.windows.net/$root/deb_install.sh #
  10. #######################################################################################################################
  11. set -e
  12. if [[ $# -ge 1 && $1 == "-y" ]]; then
  13. global_consent=0
  14. else
  15. global_consent=1
  16. fi
  17. function assert_consent {
  18. if [[ $2 -eq 0 ]]; then
  19. return 0
  20. fi
  21. echo -n "$1 [Y/n] "
  22. read consent
  23. if [[ ! "${consent}" == "y" && ! "${consent}" == "Y" && ! "${consent}" == "" ]]; then
  24. echo "'${consent}'"
  25. exit 1
  26. fi
  27. }
  28. global_consent=0 # Artificially giving global consent after review-feedback. Remove this line to enable interactive mode
  29. setup() {
  30. assert_consent "Add packages necessary to modify your apt-package sources?" ${global_consent}
  31. set -v
  32. export DEBIAN_FRONTEND=noninteractive
  33. apt-get update
  34. apt-get install -y apt-transport-https lsb-release gnupg curl
  35. set +v
  36. assert_consent "Add Microsoft as a trusted package signer?" ${global_consent}
  37. set -v
  38. curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /etc/apt/trusted.gpg.d/microsoft.gpg
  39. set +v
  40. assert_consent "Add the Azure CLI Repository to your apt sources?" ${global_consent}
  41. set -v
  42. # Use env var DIST_CODE for the package dist name if provided
  43. if [[ -z $DIST_CODE ]]; then
  44. CLI_REPO=$(lsb_release -cs)
  45. shopt -s nocasematch
  46. ERROR_MSG="Unable to find a package for your system. Please check if an existing package in https://packages.microsoft.com/repos/azure-cli/dists/ can be used in your system and install with the dist name: 'curl -sL https://aka.ms/InstallAzureCLIDeb | sudo DIST_CODE=<dist_code_name> bash'"
  47. if [[ ! $(curl -sL https://packages.microsoft.com/repos/azure-cli/dists/) =~ $CLI_REPO ]]; then
  48. DIST=$(lsb_release -is)
  49. if [[ $DIST =~ "Ubuntu" ]]; then
  50. CLI_REPO="jammy"
  51. elif [[ $DIST =~ "Debian" ]]; then
  52. CLI_REPO="bookworm"
  53. elif [[ $DIST =~ "LinuxMint" ]]; then
  54. CLI_REPO=$(cat /etc/os-release | grep -Po 'UBUNTU_CODENAME=\K.*') || true
  55. if [[ -z $CLI_REPO ]]; then
  56. echo $ERROR_MSG
  57. exit 1
  58. fi
  59. else
  60. echo $ERROR_MSG
  61. exit 1
  62. fi
  63. fi
  64. else
  65. CLI_REPO=$DIST_CODE
  66. if [[ ! $(curl -sL https://packages.microsoft.com/repos/azure-cli/dists/) =~ $CLI_REPO ]]; then
  67. echo "Unable to find an azure-cli package with DIST_CODE=$CLI_REPO in https://packages.microsoft.com/repos/azure-cli/dists/."
  68. exit 1
  69. fi
  70. fi
  71. echo "deb [arch=$(dpkg --print-architecture)] https://packages.microsoft.com/repos/azure-cli/ ${CLI_REPO} main" \
  72. > /etc/apt/sources.list.d/azure-cli.list
  73. apt-get update
  74. set +v
  75. assert_consent "Install the Azure CLI?" ${global_consent}
  76. apt-get install -y azure-cli
  77. }
  78. setup # ensure the whole file is downloaded before executing