| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 | #!/bin/sh#   Copyright 2020 Docker Compose CLI authors#   Licensed under the Apache License, Version 2.0 (the "License");#   you may not use this file except in compliance with the License.#   You may obtain a copy of the License at#       http://www.apache.org/licenses/LICENSE-2.0#   Unless required by applicable law or agreed to in writing, software#   distributed under the License is distributed on an "AS IS" BASIS,#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#   See the License for the specific language governing permissions and#   limitations under the License.# Script to install the Docker Compose CLI on Ubuntu (Beta).set -euRELEASE_URL=https://api.github.com/repos/docker/compose-cli/releases/latestLINK_NAME="${LINK_NAME:-com.docker.cli}"DRY_RUN="${DRY_RUN:-}"desktop_install_url="https://www.docker.com/products/docker-desktop"engine_install_url="https://docs.docker.com/get-docker/"link_path="/usr/local/bin/${LINK_NAME}"existing_cli_path="/usr/bin/docker"manual_install() {	echo "Please follow the manual install instructions"}is_new_cli() {	cloud_version_str="$($1 version 2>/dev/null | grep 'Cloud integration' || true)"	if [ -n "$cloud_version_str" ]; then		echo 1	else        azure_version_str="$($1 version 2>/dev/null | grep 'Azure' || true)"	    if [ -n "$azure_version_str" ]; then		    echo 1        fi		echo 0	fi}echo "Running checks..."# Check OSif [ "$(command -v uname)" ]; then	case "$(uname -s)" in		"Linux")			# Check for Ubuntu/Debian based distro			if ! [ -f "/etc/lsb-release" ]; then				echo "Warning: This script has been tested on Ubuntu and may not work on other distributions"			fi			# Pass			;;		"Darwin")			echo "Error: Script not needed on macOS, please install Docker Desktop Edge: $desktop_install_url"			exit 1			;;		"*")			echo "Error: Unsupported OS, please follow manual instructions"			exit 1			;;	esacelse	# Assume Windows	echo "Error: Script not needed on Windows, please install Docker Desktop Edge: $desktop_install_url"	exit 1fiuser="$(id -un 2>/dev/null || true)"sh_c='sh -c'sudo_sh_c='sh -c'if [ "$user" != 'root' ]; then    if [ "$(command -v sudo)" ]; then        sudo_sh_c='sudo -E sh -c'    elif [ "$(command -v su)" ]; then        sudo_sh_c='su -c'    else        echo "Error: This installer needs the ability to run commands as root."        exit 1    fifiif [ -n "$DRY_RUN" ]; then	sh_c='echo $sh_c'	sudo_sh_c='echo $sudo_sh_c'fi# Check if Docker Engine is installedif ! [ "$(command -v docker)" ]; then	echo "Error: Docker Engine not found"	echo "You need to install Docker first: $engine_install_url"	exit 1fidownload_cmd='curl -fsSLo'# Check that system has curl installedif ! [ "$(command -v curl)" ]; then	echo "Error: curl not found"	echo "Please install curl"	exit 1fiDOWNLOAD_URL=${DOWNLOAD_URL:-$(curl -s ${RELEASE_URL} | grep "browser_download_url.*docker-linux-amd64.tar.gz" | cut -d : -f 2,3)}# Check if the Compose CLI is already installedif [ $(is_new_cli "docker") -eq 1 ]; then	if [ $(is_new_cli "/usr/local/bin/docker") -eq 1 ]; then		echo "You already have the Docker Compose CLI installed, overriding with latest version"		download_dir=$($sh_c 'mktemp -d')		$sh_c "${download_cmd} ${download_dir}/docker-compose-cli.tar.gz ${DOWNLOAD_URL}"		$sh_c "tar xzf ${download_dir}/docker-compose-cli.tar.gz -C ${download_dir} --strip-components 1"		$sudo_sh_c "install -m 775 ${download_dir}/docker-linux-amd64 /usr/local/bin/docker"		exit 0	fi	echo "You already have the Docker Compose CLI installed, in a different location."	exit 1fi# Check if this script has already been runif [ -f "${link_path}" ]; then	echo "Error: This script appears to have been run as ${link_path} exists"	echo "Please uninstall and rerun this script or follow the manual instructions"	exit 1fi# Check current Docker CLI is installed to /usr/bin/if ! [ -f "${existing_cli_path}" ]; then	echo "Error: This script only works if the Docker CLI is installed to /usr/bin/"	manual_install	exit 1fi# Check that PATH contains /usr/bin and /usr/local/bin and that the latter is# higher prioritypath_directories=$(echo "${PATH}" | tr ":" "\n")usr_bin_pos=-1usr_local_bin_pos=-1count=0for d in ${path_directories}; do	if [ "${d}" = '/usr/bin' ]; then		usr_bin_pos=$count	fi	if [ "${d}" = '/usr/local/bin' ]; then		usr_local_bin_pos=$count	fi	count=$((count + 1))doneif [ $usr_bin_pos -eq -1 ]; then	echo "Error: /usr/bin not found in PATH"	manual_install	exit 1elif [ $usr_local_bin_pos -eq -1 ]; then	echo "Error: /usr/local/bin not found in PATH"	manual_install	exit 1elif ! [ $usr_local_bin_pos -lt $usr_bin_pos ]; then	echo "Error: /usr/local/bin is not ordered higher than /usr/bin in your PATH"	manual_install	exit 1fiecho "Checks passed!"echo "Downloading CLI..."# Download CLI to temporary directorydownload_dir=$($sh_c 'mktemp -d')$sh_c "${download_cmd} ${download_dir}/docker-compose-cli.tar.gz ${DOWNLOAD_URL}"$sh_c "tar xzf ${download_dir}/docker-compose-cli.tar.gz -C ${download_dir} --strip-components 1"echo "Downloaded CLI!"echo "Installing CLI..."# Link existing Docker CLI$sudo_sh_c "ln -s ${existing_cli_path} ${link_path}"# Install downloaded CLI$sudo_sh_c "install -m 775 ${download_dir}/docker-linux-amd64 /usr/local/bin/docker"# Clear cachecleared_cache=1if [ "$(command hash)" ]; then	$sh_c "hash -r"elif [ "$(command rehash)" ]; then	$sh_c "rehash"else	cleared_cache=	echo "Warning: Unable to clear command cache"fiif [ -n "$DRY_RUN" ]; then	exit 0fiif [ -n "$cleared_cache" ]; then	# Check Compose CLI is working	if [ $(is_new_cli "docker") -eq 0 ]; then		echo "Error: Docker Compose CLI installation error"		exit 1	fi	echo "Done!"else	echo "Please log out and in again to use the Docker Compose CLI"fi
 |