install.sh 5.0 KB

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