From 7d4d1e92976fcd20639f935b0d690138ab2252af Mon Sep 17 00:00:00 2001 From: gauravsaini04 <147703805+gauravsaini04@users.noreply.github.com> Date: Thu, 7 Mar 2024 23:42:24 +0000 Subject: [PATCH 1/2] compose-switch fallback previous version --- src/docker-outside-of-docker/install.sh | 18 +++- .../docker_build_compose_fallback.sh | 92 +++++++++++++++++++ test/docker-outside-of-docker/scenarios.json | 10 ++ 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 test/docker-outside-of-docker/docker_build_compose_fallback.sh diff --git a/src/docker-outside-of-docker/install.sh b/src/docker-outside-of-docker/install.sh index 65424740e..762b3663c 100755 --- a/src/docker-outside-of-docker/install.sh +++ b/src/docker-outside-of-docker/install.sh @@ -99,6 +99,22 @@ find_version_from_git_tags() { echo "${variable_name}=${!variable_name}" } +# Function to fetch the previous version of the plugin +get_previous_version() { + repo_url=$1 + # this would del the assets key and then get the second encountered tag_name's value from the filtered array of objects + curl -s "$repo_url" | jq -r 'del(.[].assets) | .[1].tag_name' +} + + +install_compose_switch_fallback() { + echo -e "\n(!) Failed to fetch the latest artifacts for compose-switch v${compose_switch_version}..." + previous_version=$(get_previous_version "https://api.github.com/repos/docker/compose-switch/releases") + echo -e "\nAttempting to install ${previous_version}" + compose_switch_version=${previous_version#v} + curl -fsSL "https://github.com/docker/compose-switch/releases/download/v${compose_switch_version}/docker-compose-linux-${architecture}" -o /usr/local/bin/docker-compose +} + # Ensure apt is in non-interactive to avoid prompts export DEBIAN_FRONTEND=noninteractive @@ -255,7 +271,7 @@ if [ "${DOCKER_DASH_COMPOSE_VERSION}" != "none" ]; then echo "(*) Installing compose-switch as docker-compose..." compose_switch_version="latest" find_version_from_git_tags compose_switch_version "https://github.com/docker/compose-switch" - curl -fsSL "https://github.com/docker/compose-switch/releases/download/v${compose_switch_version}/docker-compose-linux-${architecture}" -o /usr/local/bin/docker-compose + curl -fsSL "https://github.com/docker/compose-switch/releases/download/v${compose_switch_version}/docker-compose-linux-${architecture}" -o /usr/local/bin/docker-compose || install_compose_switch_fallback chmod +x /usr/local/bin/docker-compose # TODO: Verify checksum once available: https://github.com/docker/compose-switch/issues/11 fi diff --git a/test/docker-outside-of-docker/docker_build_compose_fallback.sh b/test/docker-outside-of-docker/docker_build_compose_fallback.sh new file mode 100644 index 000000000..53319d3da --- /dev/null +++ b/test/docker-outside-of-docker/docker_build_compose_fallback.sh @@ -0,0 +1,92 @@ +#!/bin/bash + +# Optional: Import test library +source dev-container-features-test-lib + +check "installs compose-switch as docker-compose" bash -c "[[ -f /usr/local/bin/docker-compose ]]" + +# Fetch host/container arch. +architecture="$(dpkg --print-architecture)" + +repo_url="https://api.github.com/repos/docker/compose-switch/releases" + +# Function to fetch the latest version of the plugin +get_latest_version() { + sudo curl -s "$repo_url/latest" | jq -r '.tag_name' +} + +# Function to fetch the previous version of the plugin +get_previous_version() { + sudo curl -s "$repo_url" | jq -r 'del(.[].assets) | .[1].tag_name' # this would del the assets key and then get the second encountered tag_name's value from the filtered array of objects +} + +desired_version=$(get_latest_version) +desired_version=${desired_version#v} + +check_docker_compose_version() { + + # Check if docker-compose-switch is installed and get its version + docker_compose_version=$(docker-compose version --short 2>/dev/null) + + if [ -n "$docker_compose_version" ]; then + # Docker Compose is installed + echo -e "\nInstalled docker-compose version: $docker_compose_version" + + # Check if installed version matches the desired version + if [ "$docker_compose_version" = "$desired_version" ]; then + echo -e "\ndocker-compose version $desired_version is installed." + else + echo -e "\ndocker-compose version $desired_version is not installed." + fi + else + # Docker Compose is not installed + echo -e "\ndocker-compose is not installed." + fi +} + +check_docker_compose_version + +# Function to change the patch number in a semver version +change_patch_number() { + local version="$1" # Input version + local new_patch="$2" # New patch number + # Extract major, minor, and current patch numbers + local major=$(echo "$version" | cut -d. -f1) + local minor=$(echo "$version" | cut -d. -f2) + local current_patch=$(echo "$version" | cut -d. -f3) + # Construct the new version with the updated patch number + local new_version="$major.$minor.$new_patch" + echo "$new_version" +} + +change_version_to_fail() { + new_patch_number="xyz" # for testing a tag not found scenario for docker/buildx plugin + latest_version=$(get_latest_version) # can take latest_version from fn get_latest_version + compose_version_fallback_test=$(change_patch_number "$latest_version" "$new_patch_number") # for testing a tag not found scenario for docker/buildx plugin + echo "${compose_version_fallback_test}" +} + +install_compose_switch_fallback() { + echo -e "\n(!) Failed to fetch the latest artifacts for compose-switch ${test_compose_switch_version}..." + previous_version=$(get_previous_version) + echo -e "\nAttempting to install ${previous_version}" + compose_switch_version=${previous_version} + sudo curl -fsSL "https://github.com/docker/compose-switch/releases/download/${compose_switch_version}/docker-compose-linux-${architecture}" -o /usr/local/bin/docker-compose +} + +install_compose-switch_as_docker-compose() { + echo "(*) Installing compose-switch as docker-compose..." + test_compose_switch_version=$(change_version_to_fail) + echo -e "\nTesting with $test_compose_switch_version..." + sudo curl -fsSL "https://github.com/docker/compose-switch/releases/download/${test_compose_switch_version}/docker-compose-linux-${architecture}" -o /usr/local/bin/docker-compose || install_compose_switch_fallback + sudo chmod +x /usr/local/bin/docker-compose +} + +install_compose-switch_as_docker-compose + +desired_version=$(get_previous_version) +desired_version=${desired_version#v} + +check_docker_compose_version + +check "installs compose-switch as docker-compose" bash -c "[[ -f /usr/local/bin/docker-compose ]]" \ No newline at end of file diff --git a/test/docker-outside-of-docker/scenarios.json b/test/docker-outside-of-docker/scenarios.json index 61f1ab402..6239a1ada 100644 --- a/test/docker-outside-of-docker/scenarios.json +++ b/test/docker-outside-of-docker/scenarios.json @@ -1,4 +1,14 @@ { + "docker_build_compose_fallback": { + "image": "mcr.microsoft.com/devcontainers/base:ubuntu-20.04", + "features": { + "docker-outside-of-docker": { + "moby": false, + "dockerDashComposeVersion": "latest" + } + }, + "containerUser": "vscode" + }, "docker_init_moby": { "image": "mcr.microsoft.com/devcontainers/base:ubuntu-20.04", "features": { From a0bb41ba9d8cd3b23e231b14d2c14fa798b1005c Mon Sep 17 00:00:00 2001 From: gauravsaini04 <147703805+gauravsaini04@users.noreply.github.com> Date: Sun, 10 Mar 2024 00:13:10 +0000 Subject: [PATCH 2/2] changes required --- .../devcontainer-feature.json | 2 +- src/docker-outside-of-docker/install.sh | 2 +- .../docker_build_compose_fallback.sh | 60 +------------------ 3 files changed, 4 insertions(+), 60 deletions(-) diff --git a/src/docker-outside-of-docker/devcontainer-feature.json b/src/docker-outside-of-docker/devcontainer-feature.json index d4c1447ba..54fca5ec1 100644 --- a/src/docker-outside-of-docker/devcontainer-feature.json +++ b/src/docker-outside-of-docker/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "docker-outside-of-docker", - "version": "1.4.2", + "version": "1.4.3", "name": "Docker (docker-outside-of-docker)", "documentationURL": "https://github.com/devcontainers/features/tree/main/src/docker-outside-of-docker", "description": "Re-use the host docker socket, adding the Docker CLI to a container. Feature invokes a script to enable using a forwarded Docker socket within a container to run Docker commands.", diff --git a/src/docker-outside-of-docker/install.sh b/src/docker-outside-of-docker/install.sh index 762b3663c..dd39e1f68 100755 --- a/src/docker-outside-of-docker/install.sh +++ b/src/docker-outside-of-docker/install.sh @@ -103,7 +103,7 @@ find_version_from_git_tags() { get_previous_version() { repo_url=$1 # this would del the assets key and then get the second encountered tag_name's value from the filtered array of objects - curl -s "$repo_url" | jq -r 'del(.[].assets) | .[1].tag_name' + curl -s "$repo_url" | jq -r 'del(.[].assets) | .[0].tag_name' } diff --git a/test/docker-outside-of-docker/docker_build_compose_fallback.sh b/test/docker-outside-of-docker/docker_build_compose_fallback.sh index 53319d3da..e1c0885e6 100644 --- a/test/docker-outside-of-docker/docker_build_compose_fallback.sh +++ b/test/docker-outside-of-docker/docker_build_compose_fallback.sh @@ -10,60 +10,9 @@ architecture="$(dpkg --print-architecture)" repo_url="https://api.github.com/repos/docker/compose-switch/releases" -# Function to fetch the latest version of the plugin -get_latest_version() { - sudo curl -s "$repo_url/latest" | jq -r '.tag_name' -} - # Function to fetch the previous version of the plugin get_previous_version() { - sudo curl -s "$repo_url" | jq -r 'del(.[].assets) | .[1].tag_name' # this would del the assets key and then get the second encountered tag_name's value from the filtered array of objects -} - -desired_version=$(get_latest_version) -desired_version=${desired_version#v} - -check_docker_compose_version() { - - # Check if docker-compose-switch is installed and get its version - docker_compose_version=$(docker-compose version --short 2>/dev/null) - - if [ -n "$docker_compose_version" ]; then - # Docker Compose is installed - echo -e "\nInstalled docker-compose version: $docker_compose_version" - - # Check if installed version matches the desired version - if [ "$docker_compose_version" = "$desired_version" ]; then - echo -e "\ndocker-compose version $desired_version is installed." - else - echo -e "\ndocker-compose version $desired_version is not installed." - fi - else - # Docker Compose is not installed - echo -e "\ndocker-compose is not installed." - fi -} - -check_docker_compose_version - -# Function to change the patch number in a semver version -change_patch_number() { - local version="$1" # Input version - local new_patch="$2" # New patch number - # Extract major, minor, and current patch numbers - local major=$(echo "$version" | cut -d. -f1) - local minor=$(echo "$version" | cut -d. -f2) - local current_patch=$(echo "$version" | cut -d. -f3) - # Construct the new version with the updated patch number - local new_version="$major.$minor.$new_patch" - echo "$new_version" -} - -change_version_to_fail() { - new_patch_number="xyz" # for testing a tag not found scenario for docker/buildx plugin - latest_version=$(get_latest_version) # can take latest_version from fn get_latest_version - compose_version_fallback_test=$(change_patch_number "$latest_version" "$new_patch_number") # for testing a tag not found scenario for docker/buildx plugin - echo "${compose_version_fallback_test}" + sudo curl -s "$repo_url" | jq -r 'del(.[].assets) | .[0].tag_name' # this would del the assets key and then get the second encountered tag_name's value from the filtered array of objects } install_compose_switch_fallback() { @@ -76,7 +25,7 @@ install_compose_switch_fallback() { install_compose-switch_as_docker-compose() { echo "(*) Installing compose-switch as docker-compose..." - test_compose_switch_version=$(change_version_to_fail) + test_compose_switch_version="1.2.xyz" echo -e "\nTesting with $test_compose_switch_version..." sudo curl -fsSL "https://github.com/docker/compose-switch/releases/download/${test_compose_switch_version}/docker-compose-linux-${architecture}" -o /usr/local/bin/docker-compose || install_compose_switch_fallback sudo chmod +x /usr/local/bin/docker-compose @@ -84,9 +33,4 @@ install_compose-switch_as_docker-compose() { install_compose-switch_as_docker-compose -desired_version=$(get_previous_version) -desired_version=${desired_version#v} - -check_docker_compose_version - check "installs compose-switch as docker-compose" bash -c "[[ -f /usr/local/bin/docker-compose ]]" \ No newline at end of file