install_linux.sh 5.8 KB

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