install_linux.sh 5.5 KB

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