install_linux.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/bin/sh
  2. # Copyright 2020 Docker Compose CLI authors
  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 Compose 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. if [ "$(uname -m)" = "aarch64" ]; then
  92. DOWNLOAD_URL=${DOWNLOAD_URL:-$(curl -s ${RELEASE_URL} | grep "browser_download_url.*docker-linux-arm64" | cut -d : -f 2,3)}
  93. else
  94. DOWNLOAD_URL=${DOWNLOAD_URL:-$(curl -s ${RELEASE_URL} | grep "browser_download_url.*docker-linux-amd64" | cut -d : -f 2,3)}
  95. fi
  96. # Check if the Compose CLI is already installed
  97. if [ $(is_new_cli "docker") -eq 1 ]; then
  98. if [ $(is_new_cli "/usr/local/bin/docker") -eq 1 ]; then
  99. echo "You already have the Docker Compose CLI installed, overriding with latest version"
  100. download_dir=$($sh_c 'mktemp -d')
  101. $sh_c "${download_cmd} ${download_dir}/docker ${DOWNLOAD_URL}"
  102. $sudo_sh_c "install -m 775 ${download_dir}/docker /usr/local/bin/docker"
  103. exit 0
  104. fi
  105. echo "You already have the Docker Compose CLI installed, in a different location."
  106. exit 1
  107. fi
  108. # Check if this script has already been run
  109. if [ -f "${link_path}" ]; then
  110. echo "Error: This script appears to have been run as ${link_path} exists"
  111. echo "Please uninstall and rerun this script or follow the manual instructions"
  112. exit 1
  113. fi
  114. # Check current Docker CLI is installed to /usr/bin/
  115. if ! [ -f "${existing_cli_path}" ]; then
  116. echo "Error: This script only works if the Docker CLI is installed to /usr/bin/"
  117. manual_install
  118. exit 1
  119. fi
  120. # Check that PATH contains /usr/bin and /usr/local/bin and that the latter is
  121. # higher priority
  122. path_directories=$(echo "${PATH}" | tr ":" "\n")
  123. usr_bin_pos=-1
  124. usr_local_bin_pos=-1
  125. count=0
  126. for d in ${path_directories}; do
  127. if [ "${d}" = '/usr/bin' ]; then
  128. usr_bin_pos=$count
  129. fi
  130. if [ "${d}" = '/usr/local/bin' ]; then
  131. usr_local_bin_pos=$count
  132. fi
  133. count=$((count + 1))
  134. done
  135. if [ $usr_bin_pos -eq -1 ]; then
  136. echo "Error: /usr/bin not found in PATH"
  137. manual_install
  138. exit 1
  139. elif [ $usr_local_bin_pos -eq -1 ]; then
  140. echo "Error: /usr/local/bin not found in PATH"
  141. manual_install
  142. exit 1
  143. elif ! [ $usr_local_bin_pos -lt $usr_bin_pos ]; then
  144. echo "Error: /usr/local/bin is not ordered higher than /usr/bin in your PATH"
  145. manual_install
  146. exit 1
  147. fi
  148. echo "Checks passed!"
  149. echo "Downloading CLI..."
  150. # Download CLI to temporary directory
  151. download_dir=$($sh_c 'mktemp -d')
  152. $sh_c "${download_cmd} ${download_dir}/docker ${DOWNLOAD_URL}"
  153. echo "Downloaded CLI!"
  154. echo "Installing CLI..."
  155. # Link existing Docker CLI
  156. $sudo_sh_c "ln -s ${existing_cli_path} ${link_path}"
  157. # Install downloaded CLI
  158. $sudo_sh_c "install -m 775 ${download_dir}/docker /usr/local/bin/docker"
  159. # Clear cache
  160. cleared_cache=1
  161. if [ "$(command hash)" ]; then
  162. $sh_c "hash -r"
  163. elif [ "$(command rehash)" ]; then
  164. $sh_c "rehash"
  165. else
  166. cleared_cache=
  167. echo "Warning: Unable to clear command cache"
  168. fi
  169. if [ -n "$DRY_RUN" ]; then
  170. exit 0
  171. fi
  172. if [ -n "$cleared_cache" ]; then
  173. # Check Compose CLI is working
  174. if [ $(is_new_cli "docker") -eq 0 ]; then
  175. echo "Error: Docker Compose CLI installation error"
  176. exit 1
  177. fi
  178. echo "Done!"
  179. else
  180. echo "Please log out and in again to use the Docker Compose CLI"
  181. fi