From 878a900d55e60add9c7a76b433c7d4a4adf299bc Mon Sep 17 00:00:00 2001 From: Julian Pawlowski <75446+jpawlowski@users.noreply.github.com> Date: Wed, 28 Aug 2024 01:56:57 +0200 Subject: [PATCH] `[powershell]`: Fix manual PowerShell installation binary execution permissions (#1050) * Align manual pwsh install with DEB package - ensure execution permissions for pwsh - correct path from /usr/local/bin/pwsh to /usr/bin/pwsh - register as shell in /etc/shells * Update versions * Update repositories * Check for existing pwsh installation This will avoid build failures when multiple features depend on this PowerShell package * test: Fix PS profile test * Test: Fix install_using_github * Test: Fix getcwd warning * fix: Move out of directory before deleting it * test: Add validate_powershell_installation * test: Change Ubuntu test base image to upcoming new LTS version --- src/powershell/devcontainer-feature.json | 6 ++- src/powershell/install.sh | 37 +++++++++++-------- .../install_powershell_fallback_test.sh | 10 +++-- test/powershell/scenarios.json | 12 ++++-- .../validate_powershell_installation.sh | 19 ++++++++++ 5 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 test/powershell/validate_powershell_installation.sh diff --git a/src/powershell/devcontainer-feature.json b/src/powershell/devcontainer-feature.json index d51b5c03f..585e34630 100644 --- a/src/powershell/devcontainer-feature.json +++ b/src/powershell/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "powershell", - "version": "1.4.0", + "version": "1.5.0", "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.", @@ -10,7 +10,9 @@ "proposals": [ "latest", "none", - "7.1" + "7.4", + "7.3", + "7.2" ], "default": "latest", "description": "Select or enter a version of PowerShell." diff --git a/src/powershell/install.sh b/src/powershell/install.sh index 43773b3f6..4953a49d8 100755 --- a/src/powershell/install.sh +++ b/src/powershell/install.sh @@ -18,7 +18,7 @@ POWERSHELL_PROFILE_URL="${POWERSHELLPROFILEURL}" MICROSOFT_GPG_KEYS_URI="https://packages.microsoft.com/keys/microsoft.asc" POWERSHELL_ARCHIVE_ARCHITECTURES="amd64" -POWERSHELL_ARCHIVE_VERSION_CODENAMES="stretch buster bionic focal bullseye jammy" +POWERSHELL_ARCHIVE_VERSION_CODENAMES="stretch buster bionic focal bullseye jammy bookworm noble" GPG_KEY_SERVERS="keyserver hkp://keyserver.ubuntu.com keyserver hkp://keyserver.ubuntu.com:80 keyserver hkps://keys.openpgp.org @@ -221,25 +221,32 @@ install_using_github() { echo "${powershell_archive_sha256} *${powershell_filename}" | sha256sum -c - fi tar xf "${powershell_filename}" -C "${powershell_target_path}" - ln -s "${powershell_target_path}/pwsh" /usr/local/bin/pwsh + chmod 755 "${powershell_target_path}/pwsh" + ln -sf "${powershell_target_path}/pwsh" /usr/bin/pwsh + add-shell "/usr/bin/pwsh" + cd /tmp rm -rf /tmp/pwsh } -export DEBIAN_FRONTEND=noninteractive - -# Source /etc/os-release to get OS info -. /etc/os-release -architecture="$(dpkg --print-architecture)" +if ! type pwsh >/dev/null 2>&1; then + export DEBIAN_FRONTEND=noninteractive + + # Source /etc/os-release to get OS info + . /etc/os-release + architecture="$(dpkg --print-architecture)" -if [[ "${POWERSHELL_ARCHIVE_ARCHITECTURES}" = *"${architecture}"* ]] && [[ "${POWERSHELL_ARCHIVE_VERSION_CODENAMES}" = *"${VERSION_CODENAME}"* ]]; then - install_using_apt || use_github="true" + if [[ "${POWERSHELL_ARCHIVE_ARCHITECTURES}" = *"${architecture}"* ]] && [[ "${POWERSHELL_ARCHIVE_VERSION_CODENAMES}" = *"${VERSION_CODENAME}"* ]]; then + install_using_apt || use_github="true" + else + use_github="true" + fi + + if [ "${use_github}" = "true" ]; then + echo "Attempting install from GitHub release..." + install_using_github + fi else - use_github="true" -fi - -if [ "${use_github}" = "true" ]; then - echo "Attempting install from GitHub release..." - install_using_github + echo "PowerShell is already installed." fi # If PowerShell modules are requested, loop through and install diff --git a/test/powershell/install_powershell_fallback_test.sh b/test/powershell/install_powershell_fallback_test.sh index 39c8bc49e..169863c7b 100644 --- a/test/powershell/install_powershell_fallback_test.sh +++ b/test/powershell/install_powershell_fallback_test.sh @@ -8,7 +8,7 @@ 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 "profile" pwsh -Command "if (\$null -eq \$env:ProfileLoaded) { echo 'Not set!'; exit 1 } else { if ( [bool]\$env:ProfileLoaded ) { echo 'Profile loaded.'; exit 0 } else { echo 'False value!'; exit 1 } }" check "Powershell version as installed by feature" bash -c "pwsh --version" @@ -168,9 +168,11 @@ install_using_github() { 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 /usr/local/bin/pwsh - + sudo chmod 755 "${powershell_target_path}/pwsh" + sudo ln -sf "${powershell_target_path}/pwsh" /usr/bin/pwsh + sudo add-shell "/usr/bin/pwsh" + cd /tmp + sudo rm -rf /tmp/pwsh } echo -e "\nInstalling Powershell with find_prev_version_from_git_tags() fn 👈🏻" diff --git a/test/powershell/scenarios.json b/test/powershell/scenarios.json index 757a8cc97..87a5b9d16 100644 --- a/test/powershell/scenarios.json +++ b/test/powershell/scenarios.json @@ -1,6 +1,6 @@ { "install_modules": { - "image": "mcr.microsoft.com/devcontainers/base:jammy", + "image": "mcr.microsoft.com/devcontainers/base:noble", "features": { "powershell": { "modules": "az.resources, az.storage", @@ -9,7 +9,7 @@ } }, "install_powershell_fallback_test": { - "image": "mcr.microsoft.com/devcontainers/base:jammy", + "image": "mcr.microsoft.com/devcontainers/base:noble", "features": { "powershell": { "modules": "az.resources, az.storage", @@ -18,11 +18,17 @@ } }, "install_modules_version": { - "image": "mcr.microsoft.com/devcontainers/base:jammy", + "image": "mcr.microsoft.com/devcontainers/base:noble", "features": { "powershell": { "modules": "az.resources==2.5.0, az.storage==4.3.0" } } + }, + "validate_powershell_installation": { + "image": "mcr.microsoft.com/devcontainers/base:noble", + "features": { + "powershell": {} + } } } diff --git a/test/powershell/validate_powershell_installation.sh b/test/powershell/validate_powershell_installation.sh new file mode 100644 index 000000000..88544f28b --- /dev/null +++ b/test/powershell/validate_powershell_installation.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +set -e + +# Import test library for `check` command +source dev-container-features-test-lib + +# Extension-specific tests +check "pwsh file is symlink" bash -c "[ -L /usr/bin/pwsh ]" +check "pwsh symlink is registered as shell" bash -c "[ $(grep -c '/usr/bin/pwsh' /etc/shells) -eq 1 ]" +check "pwsh target is correct" bash -c "[ $(readlink /usr/bin/pwsh) = /opt/microsoft/powershell/7/pwsh ]" +check "pwsh target is registered as shell" bash -c "[ $(grep -c '/opt/microsoft/powershell/7/pwsh' /etc/shells) -eq 1 ]" +check "pwsh owner is root" bash -c "[ $(stat -c %U /opt/microsoft/powershell/7/pwsh) = root ]" +check "pwsh group is root" bash -c "[ $(stat -c %G /opt/microsoft/powershell/7/pwsh) = root ]" +check "pwsh file mode is -rwxr-xr-x" bash -c "[ $(stat -c '%A' /opt/microsoft/powershell/7/pwsh) = '-rwxr-xr-x' ]" +check "pwsh is in PATH" bash -c "command -v pwsh" + +# Report result +reportResults