Skip to content

Commit a674273

Browse files
[Powershell] - Fallback to prev. version - fix (devcontainers#906)
* [Powershell] - Fallback to prev. version - fix * patch version updated
1 parent 460684d commit a674273

4 files changed

Lines changed: 135 additions & 6 deletions

File tree

src/powershell/devcontainer-feature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "powershell",
3-
"version": "1.3.3",
3+
"version": "1.3.4",
44
"name": "PowerShell",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/powershell",
66
"description": "Installs PowerShell along with needed dependencies. Useful for base Dockerfiles that often are missing required install dependencies like gpg.",

src/powershell/install.sh

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,29 @@ install_using_apt() {
106106
apt-get install -yq powershell${version_suffix} || return 1
107107
}
108108

109+
# Function to fetch the version released prior to the latest version
110+
get_previous_version() {
111+
repo_url=$1
112+
curl -s "$repo_url" | jq -r 'del(.[].assets) | .[0].tag_name'
113+
}
114+
115+
install_prev_pwsh() {
116+
echo -e "\n(!) Failed to fetch the latest artifacts for powershell v${POWERSHELL_VERSION}..."
117+
previous_version=$(get_previous_version "https://api.github.com/repos/PowerShell/PowerShell/releases")
118+
echo -e "\nAttempting to install ${previous_version}"
119+
POWERSHELL_VERSION="${previous_version#v}"
120+
install_pwsh "${POWERSHELL_VERSION}"
121+
}
122+
123+
install_pwsh() {
124+
POWERSHELL_VERSION=$1
125+
powershell_filename="powershell-${POWERSHELL_VERSION}-linux-${architecture}.tar.gz"
126+
powershell_target_path="/opt/microsoft/powershell/$(echo ${POWERSHELL_VERSION} | grep -oE '[^\.]+' | head -n 1)"
127+
mkdir -p /tmp/pwsh "${powershell_target_path}"
128+
cd /tmp/pwsh
129+
curl -sSL -o "${powershell_filename}" "https://github.com/PowerShell/PowerShell/releases/download/v${POWERSHELL_VERSION}/${powershell_filename}"
130+
}
131+
109132
install_using_github() {
110133
# Fall back on direct download if no apt package exists in microsoft pool
111134
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() {
116139
architecture="x64"
117140
fi
118141
find_version_from_git_tags POWERSHELL_VERSION https://github.com/PowerShell/PowerShell
119-
powershell_filename="powershell-${POWERSHELL_VERSION}-linux-${architecture}.tar.gz"
120-
powershell_target_path="/opt/microsoft/powershell/$(echo ${POWERSHELL_VERSION} | grep -oE '[^\.]+' | head -n 1)"
121-
mkdir -p /tmp/pwsh "${powershell_target_path}"
122-
cd /tmp/pwsh
123-
curl -sSL -o "${powershell_filename}" "https://github.com/PowerShell/PowerShell/releases/download/v${POWERSHELL_VERSION}/${powershell_filename}"
142+
install_pwsh "${POWERSHELL_VERSION}"
143+
if grep -q "Not Found" "${powershell_filename}"; then
144+
install_prev_pwsh
145+
fi
146+
124147
# 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.
125148
curl -sSL -o "release.html" "https://github.com/PowerShell/PowerShell/releases/tag/v${POWERSHELL_VERSION}"
126149
powershell_archive_sha256="$(cat release.html | tr '\n' ' ' | sed 's|<[^>]*>||g' | grep -oP "${powershell_filename}\s+\K[0-9a-fA-F]{64}" || echo '')"
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Import test library for `check` command
6+
source dev-container-features-test-lib
7+
8+
# Extension-specific tests
9+
check "az.resources" pwsh -Command "(Get-Module -ListAvailable -Name Az.Resources).Version.ToString()"
10+
check "az.storage" pwsh -Command "(Get-Module -ListAvailable -Name Az.Storage).Version.ToString()"
11+
check "profile" pwsh -Command "(Get-Variable $env:ProfileLoaded).Value"
12+
13+
check "Powershell version as installed by feature" bash -c "pwsh --version"
14+
15+
. /etc/os-release
16+
architecture="$(dpkg --print-architecture)"
17+
18+
get_previous_version() {
19+
repo_url=$1
20+
curl -s "$repo_url" | jq -r 'del(.[].assets) | .[0].tag_name'
21+
}
22+
23+
install_prev_pwsh() {
24+
echo -e "\n(!) Failed to fetch the latest artifacts for powershell v${POWERSHELL_VERSION}..."
25+
previous_version=$(get_previous_version "https://api.github.com/repos/PowerShell/PowerShell/releases")
26+
echo -e "\nAttempting to install ${previous_version}"
27+
POWERSHELL_VERSION="${previous_version#v}"
28+
install_pwsh "${POWERSHELL_VERSION}"
29+
}
30+
31+
install_pwsh() {
32+
POWERSHELL_VERSION=$1
33+
powershell_filename="powershell-${POWERSHELL_VERSION}-linux-${architecture}.tar.gz"
34+
powershell_target_path="/opt/microsoft/powershell/$(echo ${POWERSHELL_VERSION} | grep -oE '[^\.]+' | head -n 1)"
35+
sudo mkdir -p /tmp/pwsh "${powershell_target_path}"
36+
cd /tmp/pwsh
37+
sudo curl -sSL -o "${powershell_filename}" "https://github.com/PowerShell/PowerShell/releases/download/v${POWERSHELL_VERSION}/${powershell_filename}"
38+
}
39+
40+
apt_get_update()
41+
{
42+
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
43+
echo "Running apt-get update..."
44+
sudo apt-get update -y
45+
fi
46+
}
47+
48+
check_packages() {
49+
if ! dpkg -s "$@" > /dev/null 2>&1; then
50+
sudo chmod +x /var/lib/apt/lists/
51+
sudo mkdir -p /var/lib/apt/lists/partial
52+
sudo chmod +rx /var/lib/dpkg/lock-frontend
53+
apt_get_update
54+
sudo apt-get -y install --no-install-recommends "$@"
55+
fi
56+
}
57+
58+
install_using_github() {
59+
# Fall back on direct download if no apt package exists in microsoft pool
60+
check_packages curl ca-certificates gnupg2 dirmngr libc6 libgcc1 libgssapi-krb5-2 libstdc++6 libunwind8 libuuid1 zlib1g libicu[0-9][0-9]
61+
if ! type git > /dev/null 2>&1; then
62+
check_packages git
63+
fi
64+
if [ "${architecture}" = "amd64" ]; then
65+
architecture="x64"
66+
fi
67+
68+
echo -e "\nTrying to install a non-existing version for Powershell..."
69+
70+
POWERSHELL_VERSION="1.2.XYZ"
71+
install_pwsh "${POWERSHELL_VERSION}"
72+
73+
if grep -q "Not Found" "${powershell_filename}"; then
74+
install_prev_pwsh
75+
fi
76+
77+
echo -e "\n" $POWERSHELL_VERSION "=powershell_version\n";
78+
# 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.
79+
sudo curl -sSL -o "release.html" "https://github.com/PowerShell/PowerShell/releases/tag/v${POWERSHELL_VERSION}"
80+
powershell_archive_sha256="$(cat release.html | tr '\n' ' ' | sed 's|<[^>]*>||g' | grep -oP "${powershell_filename}\s+\K[0-9a-fA-F]{64}" || echo '')"
81+
if [ -z "${powershell_archive_sha256}" ]; then
82+
echo "(!) WARNING: Failed to retrieve SHA256 for archive. Skipping validaiton."
83+
else
84+
echo "SHA256: ${powershell_archive_sha256}"
85+
echo "${powershell_archive_sha256} *${powershell_filename}" | sha256sum -c -
86+
fi
87+
sudo tar xf "${powershell_filename}" -C "${powershell_target_path}"
88+
sudo ln -s "${powershell_target_path}/pwsh" /usr/local/bin/pwsh
89+
sudo rm -rf /tmp/pwsh
90+
}
91+
92+
install_using_github
93+
94+
check "Powershell version as installed by test" bash -c "pwsh --version"
95+
96+
# Report result
97+
reportResults

test/powershell/scenarios.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,14 @@
77
"powershellProfileURL": "https://raw.githubusercontent.com/codspace/powershell-profile/main/Test-Profile.ps1"
88
}
99
}
10+
},
11+
"install_powershell_fallback_test": {
12+
"image": "mcr.microsoft.com/devcontainers/base:jammy",
13+
"features": {
14+
"powershell": {
15+
"modules": "az.resources, az.storage",
16+
"powershellProfileURL": "https://raw.githubusercontent.com/codspace/powershell-profile/main/Test-Profile.ps1"
17+
}
18+
}
1019
}
1120
}

0 commit comments

Comments
 (0)