install.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/bin/sh
  2. # Script to install the Docker ACI integration CLI on Ubuntu (Beta).
  3. set -eu
  4. CLI_VERSION=${CLI_VERSION:-0.1.4}
  5. DOWNLOAD_URL="${DOWNLOAD_URL:-https://github.com/docker/aci-integration-beta/releases/download/v${CLI_VERSION}/docker-linux-amd64}"
  6. LINK_NAME="${LINK_NAME:-com.docker.cli}"
  7. DRY_RUN="${DRY_RUN:-}"
  8. desktop_install_url="https://www.docker.com/products/docker-desktop"
  9. engine_install_url="https://docs.docker.com/get-docker/"
  10. link_path="/usr/local/bin/${LINK_NAME}"
  11. existing_cli_path="/usr/bin/docker"
  12. manual_install() {
  13. echo "Please follow the manual install instructions"
  14. }
  15. is_new_cli() {
  16. azure_version_str="$($1 version 2>/dev/null | grep 'Azure' || true)"
  17. if [ -n "$azure_version_str" ]; then
  18. echo 1
  19. else
  20. echo 0
  21. fi
  22. }
  23. echo "Running checks..."
  24. # Check OS
  25. if [ "$(command -v uname)" ]; then
  26. case "$(uname -s)" in
  27. "Linux")
  28. # Check for Ubuntu/Debian based distro
  29. if ! [ -f "/etc/lsb-release" ]; then
  30. echo "Warning: This script has been tested on Ubuntu and may not work on other distributions"
  31. fi
  32. # Pass
  33. ;;
  34. "Darwin")
  35. echo "Error: Script not needed on macOS, please install Docker Desktop Edge: $desktop_install_url"
  36. exit 1
  37. ;;
  38. "*")
  39. echo "Error: Unsupported OS, please follow manual instructions"
  40. exit 1
  41. ;;
  42. esac
  43. else
  44. # Assume Windows
  45. echo "Error: Script not needed on Windows, please install Docker Desktop Edge: $desktop_install_url"
  46. exit 1
  47. fi
  48. user="$(id -un 2>/dev/null || true)"
  49. sh_c='sh -c'
  50. sudo_sh_c='sh -c'
  51. if [ "$user" != 'root' ]; then
  52. if [ "$(command -v sudo)" ]; then
  53. sudo_sh_c='sudo -E sh -c'
  54. elif [ "$(command -v su)" ]; then
  55. sudo_sh_c='su -c'
  56. else
  57. echo "Error: This installer needs the ability to run commands as root."
  58. exit 1
  59. fi
  60. fi
  61. if [ -n "$DRY_RUN" ]; then
  62. sh_c="echo $sh_c"
  63. sudo_sh_c="echo $sudo_sh_c"
  64. fi
  65. # Check if Docker Engine is installed
  66. if ! [ "$(command -v docker)" ]; then
  67. echo "Error: Docker Engine not found"
  68. echo "You need to install Docker first: $engine_install_url"
  69. exit 1
  70. fi
  71. # Check if the ACI CLI is already installed
  72. if [ $(is_new_cli "docker") -eq 1 ]; then
  73. echo "You already have the Docker ACI Integration CLI installed"
  74. exit 1
  75. fi
  76. # Check if this script has already been run
  77. if [ -f "${link_path}" ]; then
  78. echo "Error: This script appears to have been run as ${link_path} exists"
  79. echo "Please uninstall and rerun this script or follow the manual instructions"
  80. exit 1
  81. fi
  82. # Check current Docker CLI is installed to /usr/bin/
  83. if ! [ -f "${existing_cli_path}" ]; then
  84. echo "Error: This script only works if the Docker CLI is installed to /usr/bin/"
  85. manual_install
  86. exit 1
  87. fi
  88. # Check that PATH contains /usr/bin and /usr/local/bin and that the latter is
  89. # higher priority
  90. path_directories=$(echo "${PATH}" | tr ":" "\n")
  91. usr_bin_pos=-1
  92. usr_local_bin_pos=-1
  93. count=0
  94. for d in ${path_directories}; do
  95. if [ "${d}" = '/usr/bin' ]; then
  96. usr_bin_pos=$count
  97. fi
  98. if [ "${d}" = '/usr/local/bin' ]; then
  99. usr_local_bin_pos=$count
  100. fi
  101. count=$((count + 1))
  102. done
  103. if [ $usr_bin_pos -eq -1 ]; then
  104. echo "Error: /usr/bin not found in PATH"
  105. manual_install
  106. exit 1
  107. elif [ $usr_local_bin_pos -eq -1 ]; then
  108. echo "Error: /usr/local/bin not found in PATH"
  109. manual_install
  110. exit 1
  111. elif ! [ $usr_local_bin_pos -lt $usr_bin_pos ]; then
  112. echo "Error: /usr/local/bin is not ordered higher than /usr/bin in your PATH"
  113. manual_install
  114. exit 1
  115. fi
  116. download_cmd='curl -fsSLo'
  117. # Check that system has curl installed
  118. if ! [ "$(command -v curl)" ]; then
  119. echo "Error: curl not found"
  120. echo "Please install curl"
  121. exit 1
  122. fi
  123. echo "Checks passed!"
  124. echo "Downloading CLI..."
  125. # Download CLI to temporary directory
  126. download_dir=$($sh_c 'mktemp -d')
  127. $sh_c "${download_cmd} ${download_dir}/docker-aci ${DOWNLOAD_URL}"
  128. echo "Downloaded CLI!"
  129. echo "Installing CLI..."
  130. # Link existing Docker CLI
  131. $sudo_sh_c "ln -s ${existing_cli_path} ${link_path}"
  132. # Install downloaded CLI
  133. $sudo_sh_c "install -m 775 ${download_dir}/docker-aci /usr/local/bin/docker"
  134. # Exit on dry run
  135. if [ -n "$DRY_RUN" ]; then
  136. exit 0
  137. fi
  138. # Clear cache
  139. cleared_cache=1
  140. if [ "$(command hash)" ]; then
  141. $sh_c "hash -r"
  142. elif [ "$(command rehash)" ]; then
  143. $sh_c "rehash"
  144. else
  145. cleared_cache=
  146. echo "Warning: Unable to clear command cache"
  147. fi
  148. if [ -n "$cleared_cache" ]; then
  149. # Check ACI CLI is working
  150. if [ $(is_new_cli "docker") -eq 0 ]; then
  151. echo "Error: Docker ACI Integration CLI installation error"
  152. exit 1
  153. fi
  154. echo "Done!"
  155. else
  156. echo "Please log out and in again to use the Docker ACI integration CLI"
  157. fi