From 164ce1d6965b59b10562f86544ab05f2dd5dfd94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20Rhyme=20Guibone=20A=C3=B1asco?= <35908542+OliverRhyme@users.noreply.github.com> Date: Sun, 10 Oct 2021 20:42:07 +0800 Subject: [PATCH 1/2] Improve root privilege elevation This should enable calling the script without calling sudo on the whole script which breaks the `docker compose` detection for user-only installations --- install_on_linux.sh | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/install_on_linux.sh b/install_on_linux.sh index 8215727..eb09cf8 100755 --- a/install_on_linux.sh +++ b/install_on_linux.sh @@ -2,6 +2,7 @@ set -e + ARCHITECTURE=amd64 if [ "$(uname -m)" = "aarch64" ]; then ARCHITECTURE=arm64 @@ -10,25 +11,40 @@ COMPOSE_SWITCH_VERSION="v1.0.2" COMPOSE_SWITCH_URL="https://github.com/docker/compose-switch/releases/download/${COMPOSE_SWITCH_VERSION}/docker-compose-linux-${ARCHITECTURE}" error=$(docker compose version 2>&1 >/dev/null) +echo "After error" if [ $? -ne 0 ]; then echo "Docker Compose V2 is not installed" exit 1 fi -curl -fL $COMPOSE_SWITCH_URL -o /usr/local/bin/compose-switch -chmod +x /usr/local/bin/compose-switch +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 + fi +fi + +$sudo_sh_c "curl -fL $COMPOSE_SWITCH_URL -o /usr/local/bin/compose-switch" +$sudo_sh_c "chmod +x /usr/local/bin/compose-switch" COMPOSE=$(command -v docker-compose) if [ "$COMPOSE" = /usr/local/bin/docker-compose ]; then # This is a manual installation of docker-compose # so, safe for us to rename binary - mv /usr/local/bin/docker-compose /usr/local/bin/docker-compose-v1 + $sudo_sh_c "mv /usr/local/bin/docker-compose /usr/local/bin/docker-compose-v1" COMPOSE=/usr/local/bin/docker-compose-v1 fi -ALTERNATIVES="update-alternatives" + +ALTERNATIVES="$sudo_sh_c update-alternatives" if ! command -v $ALTERNATIVES; then - ALTERNATIVES=alternatives + ALTERNATIVES="$sudo_sh_c alternatives" fi echo "Configuring `docker-compose` alternatives" @@ -38,4 +54,4 @@ fi $ALTERNATIVES --install /usr/local/bin/docker-compose docker-compose /usr/local/bin/compose-switch 99 echo "'docker-compose' is now set to run Compose V2" -echo "use '$ALTERNATIVES --config docker-compose' if you want to switch back to Compose V1" \ No newline at end of file +echo "use '$ALTERNATIVES --config docker-compose' if you want to switch back to Compose V1" From cc1c9f4d17b799cb1358621a118346a27cf9e6ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20Rhyme=20Guibone=20A=C3=B1asco?= <35908542+OliverRhyme@users.noreply.github.com> Date: Sun, 10 Oct 2021 20:44:07 +0800 Subject: [PATCH 2/2] Cleanup code --- install_on_linux.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/install_on_linux.sh b/install_on_linux.sh index eb09cf8..5ed71fb 100755 --- a/install_on_linux.sh +++ b/install_on_linux.sh @@ -2,7 +2,6 @@ set -e - ARCHITECTURE=amd64 if [ "$(uname -m)" = "aarch64" ]; then ARCHITECTURE=arm64 @@ -11,7 +10,6 @@ COMPOSE_SWITCH_VERSION="v1.0.2" COMPOSE_SWITCH_URL="https://github.com/docker/compose-switch/releases/download/${COMPOSE_SWITCH_VERSION}/docker-compose-linux-${ARCHITECTURE}" error=$(docker compose version 2>&1 >/dev/null) -echo "After error" if [ $? -ne 0 ]; then echo "Docker Compose V2 is not installed" exit 1