Skip to content

Commit

Permalink
[powershell]: Fix manual PowerShell installation binary execution p…
Browse files Browse the repository at this point in the history
…ermissions (#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
  • Loading branch information
jpawlowski authored Aug 27, 2024
1 parent 3bf4e20 commit 878a900
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 24 deletions.
6 changes: 4 additions & 2 deletions src/powershell/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand All @@ -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."
Expand Down
37 changes: 22 additions & 15 deletions src/powershell/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions test/powershell/install_powershell_fallback_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 👈🏻"
Expand Down
12 changes: 9 additions & 3 deletions test/powershell/scenarios.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -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": {}
}
}
}
19 changes: 19 additions & 0 deletions test/powershell/validate_powershell_installation.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 878a900

Please sign in to comment.