From 00eae8e41772d740e9e52f3a6d6642fb79cb676b Mon Sep 17 00:00:00 2001 From: gauravsaini04 <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 12 Mar 2024 13:52:49 +0000 Subject: [PATCH 1/2] [Powershell] - Fallback to prev. version - fix --- src/powershell/install.sh | 33 ++++++- .../install_powershell_fallback_test.sh | 97 +++++++++++++++++++ test/powershell/scenarios.json | 9 ++ 3 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 test/powershell/install_powershell_fallback_test.sh diff --git a/src/powershell/install.sh b/src/powershell/install.sh index 0ce63c95d..4ba8bcf5c 100755 --- a/src/powershell/install.sh +++ b/src/powershell/install.sh @@ -106,6 +106,29 @@ install_using_apt() { apt-get install -yq powershell${version_suffix} || return 1 } +# Function to fetch the version released prior to the latest version +get_previous_version() { + repo_url=$1 + curl -s "$repo_url" | jq -r 'del(.[].assets) | .[0].tag_name' +} + +install_prev_pwsh() { + echo -e "\n(!) Failed to fetch the latest artifacts for powershell v${POWERSHELL_VERSION}..." + previous_version=$(get_previous_version "https://api.github.com/repos/PowerShell/PowerShell/releases") + echo -e "\nAttempting to install ${previous_version}" + POWERSHELL_VERSION="${previous_version#v}" + install_pwsh "${POWERSHELL_VERSION}" +} + +install_pwsh() { + POWERSHELL_VERSION=$1 + powershell_filename="powershell-${POWERSHELL_VERSION}-linux-${architecture}.tar.gz" + powershell_target_path="/opt/microsoft/powershell/$(echo ${POWERSHELL_VERSION} | grep -oE '[^\.]+' | head -n 1)" + mkdir -p /tmp/pwsh "${powershell_target_path}" + cd /tmp/pwsh + curl -sSL -o "${powershell_filename}" "https://github.com/PowerShell/PowerShell/releases/download/v${POWERSHELL_VERSION}/${powershell_filename}" +} + install_using_github() { # Fall back on direct download if no apt package exists in microsoft pool check_packages curl ca-certificates gnupg2 dirmngr libc6 libgcc1 libgssapi-krb5-2 libstdc++6 libunwind8 libuuid1 zlib1g libicu[0-9][0-9] @@ -116,11 +139,11 @@ install_using_github() { architecture="x64" fi find_version_from_git_tags POWERSHELL_VERSION https://github.com/PowerShell/PowerShell - powershell_filename="powershell-${POWERSHELL_VERSION}-linux-${architecture}.tar.gz" - powershell_target_path="/opt/microsoft/powershell/$(echo ${POWERSHELL_VERSION} | grep -oE '[^\.]+' | head -n 1)" - mkdir -p /tmp/pwsh "${powershell_target_path}" - cd /tmp/pwsh - curl -sSL -o "${powershell_filename}" "https://github.com/PowerShell/PowerShell/releases/download/v${POWERSHELL_VERSION}/${powershell_filename}" + install_pwsh "${POWERSHELL_VERSION}" + if grep -q "Not Found" "${powershell_filename}"; then + install_prev_pwsh + fi + # Ugly - but only way to get sha256 is to parse release HTML. Remove newlines and tags, then look for filename followed by 64 hex characters. curl -sSL -o "release.html" "https://github.com/PowerShell/PowerShell/releases/tag/v${POWERSHELL_VERSION}" powershell_archive_sha256="$(cat release.html | tr '\n' ' ' | sed 's|<[^>]*>||g' | grep -oP "${powershell_filename}\s+\K[0-9a-fA-F]{64}" || echo '')" diff --git a/test/powershell/install_powershell_fallback_test.sh b/test/powershell/install_powershell_fallback_test.sh new file mode 100644 index 000000000..852d7d103 --- /dev/null +++ b/test/powershell/install_powershell_fallback_test.sh @@ -0,0 +1,97 @@ +#!/bin/bash + +set -e + +# Import test library for `check` command +source dev-container-features-test-lib + +# Extension-specific tests +check "az.resources" pwsh -Command "(Get-Module -ListAvailable -Name Az.Resources).Version.ToString()" +check "az.storage" pwsh -Command "(Get-Module -ListAvailable -Name Az.Storage).Version.ToString()" +check "profile" pwsh -Command "(Get-Variable $env:ProfileLoaded).Value" + +check "Powershell version as installed by feature" bash -c "pwsh --version" + +. /etc/os-release +architecture="$(dpkg --print-architecture)" + +get_previous_version() { + repo_url=$1 + curl -s "$repo_url" | jq -r 'del(.[].assets) | .[0].tag_name' +} + +install_prev_pwsh() { + echo -e "\n(!) Failed to fetch the latest artifacts for powershell v${POWERSHELL_VERSION}..." + previous_version=$(get_previous_version "https://api.github.com/repos/PowerShell/PowerShell/releases") + echo -e "\nAttempting to install ${previous_version}" + POWERSHELL_VERSION="${previous_version#v}" + install_pwsh "${POWERSHELL_VERSION}" +} + +install_pwsh() { + POWERSHELL_VERSION=$1 + powershell_filename="powershell-${POWERSHELL_VERSION}-linux-${architecture}.tar.gz" + powershell_target_path="/opt/microsoft/powershell/$(echo ${POWERSHELL_VERSION} | grep -oE '[^\.]+' | head -n 1)" + sudo mkdir -p /tmp/pwsh "${powershell_target_path}" + cd /tmp/pwsh + sudo curl -sSL -o "${powershell_filename}" "https://github.com/PowerShell/PowerShell/releases/download/v${POWERSHELL_VERSION}/${powershell_filename}" +} + +apt_get_update() +{ + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then + echo "Running apt-get update..." + sudo apt-get update -y + fi +} + +check_packages() { + if ! dpkg -s "$@" > /dev/null 2>&1; then + sudo chmod +x /var/lib/apt/lists/ + sudo mkdir -p /var/lib/apt/lists/partial + sudo chmod +rx /var/lib/dpkg/lock-frontend + apt_get_update + sudo apt-get -y install --no-install-recommends "$@" + fi +} + +install_using_github() { + # Fall back on direct download if no apt package exists in microsoft pool + check_packages curl ca-certificates gnupg2 dirmngr libc6 libgcc1 libgssapi-krb5-2 libstdc++6 libunwind8 libuuid1 zlib1g libicu[0-9][0-9] + if ! type git > /dev/null 2>&1; then + check_packages git + fi + if [ "${architecture}" = "amd64" ]; then + architecture="x64" + fi + + echo -e "\nTrying to install a non-existing version for Powershell..." + + POWERSHELL_VERSION="1.2.XYZ" + install_pwsh "${POWERSHELL_VERSION}" + + if grep -q "Not Found" "${powershell_filename}"; then + install_prev_pwsh + fi + + echo -e "\n" $POWERSHELL_VERSION "=powershell_version\n"; + # Ugly - but only way to get sha256 is to parse release HTML. Remove newlines and tags, then look for filename followed by 64 hex characters. + sudo curl -sSL -o "release.html" "https://github.com/PowerShell/PowerShell/releases/tag/v${POWERSHELL_VERSION}" + powershell_archive_sha256="$(cat release.html | tr '\n' ' ' | sed 's|<[^>]*>||g' | grep -oP "${powershell_filename}\s+\K[0-9a-fA-F]{64}" || echo '')" + if [ -z "${powershell_archive_sha256}" ]; then + echo "(!) WARNING: Failed to retrieve SHA256 for archive. Skipping validaiton." + else + echo "SHA256: ${powershell_archive_sha256}" + echo "${powershell_archive_sha256} *${powershell_filename}" | sha256sum -c - + fi + sudo tar xf "${powershell_filename}" -C "${powershell_target_path}" + sudo ln -s "${powershell_target_path}/pwsh" /usr/local/bin/pwsh + sudo rm -rf /tmp/pwsh +} + +install_using_github + +check "Powershell version as installed by test" bash -c "pwsh --version" + +# Report result +reportResults diff --git a/test/powershell/scenarios.json b/test/powershell/scenarios.json index b2659d7ad..6a810b0c9 100644 --- a/test/powershell/scenarios.json +++ b/test/powershell/scenarios.json @@ -7,5 +7,14 @@ "powershellProfileURL": "https://raw.githubusercontent.com/codspace/powershell-profile/main/Test-Profile.ps1" } } + }, + "install_powershell_fallback_test": { + "image": "mcr.microsoft.com/devcontainers/base:jammy", + "features": { + "powershell": { + "modules": "az.resources, az.storage", + "powershellProfileURL": "https://raw.githubusercontent.com/codspace/powershell-profile/main/Test-Profile.ps1" + } + } } } From d098b6abaac214188ddc42b60a44a3f4fbc866be Mon Sep 17 00:00:00 2001 From: gauravsaini04 <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 12 Mar 2024 13:54:27 +0000 Subject: [PATCH 2/2] patch version updated --- src/powershell/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/powershell/devcontainer-feature.json b/src/powershell/devcontainer-feature.json index 86314be64..dd7a96b90 100644 --- a/src/powershell/devcontainer-feature.json +++ b/src/powershell/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "powershell", - "version": "1.3.3", + "version": "1.3.4", "name": "PowerShell", "documentationURL": "https://github.com/devcontainers/features/tree/main/src/powershell", "description": "Installs PowerShell along with needed dependencies. Useful for base Dockerfiles that often are missing required install dependencies like gpg.",