install_linux.sh 6.2 KB

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