Skip to content

Commit df4c606

Browse files
authored
Fix Install-Choco-GenerateProfile step (#5350)
Fixes the issue with AMI build when Miniconda variables in profile where overwritten by `` Install-Choco.ps1 `` Fixes the issue with Windows AMI: pytorch/pytorch#128854
1 parent f25e921 commit df4c606

File tree

4 files changed

+81
-55
lines changed

4 files changed

+81
-55
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
$ErrorActionPreference = "Continue"
2+
$VerbosePreference = "Continue"
3+
4+
$parentDir = "C:\Jenkins"
5+
$condaInstallationDir = "$parentDir\Miniconda3"
6+
7+
# Install Chocolatey
8+
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
9+
$env:chocolateyUseWindowsCompression = 'true'
10+
Invoke-WebRequest https://community.chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression
11+
12+
# Add Chocolatey to powershell profile
13+
$ChocoProfileValue = @'
14+
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
15+
if (Test-Path($ChocolateyProfile)) {
16+
Import-Module "$ChocolateyProfile"
17+
}
18+
Remove-Item Alias:curl
19+
Remove-Item Alias:wget
20+
refreshenv
21+
'@
22+
23+
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles
24+
$PS_PROFILE = "$PsHome\Microsoft.PowerShell_profile.ps1"
25+
26+
# Write it to the $profile location
27+
Set-Content -Path $PS_PROFILE -Value $ChocoProfileValue -Force
28+
29+
$PYTHON_PATH = '$Env:PATH += ' + "';$condaInstallationDir'"
30+
# Add conda path to the powershell profile to make its commands, i.e. python, available when logging
31+
# in to Windows runners or when the CI uses powershell
32+
Add-Content "$PS_PROFILE" "$PYTHON_PATH"
33+
34+
# Source it
35+
. $PS_PROFILE
36+
37+
$condaHook = "$condaInstallationDir\shell\condabin\conda-hook.ps1"
38+
if (-Not (Test-Path -Path $condaHook -PathType Leaf)) {
39+
Write-Error "Miniconda installation failed, no hook found at $condaHook"
40+
exit 1
41+
}
42+
43+
# Load conda into powershell
44+
& $condaHook
45+
46+
# According to https://docs.conda.io/en/latest/miniconda.html, Miniconda have only one built-in
47+
# python executable, and it can be Python3 or 2 depending on which installation package is used
48+
try {
49+
$PYTHON = (Get-Command python).Source
50+
} catch {
51+
$PYTHON = ""
52+
}
53+
54+
If ("$PYTHON" -eq "") {
55+
Write-Output "Found no Python in $Env:PATH. Double check that Miniconda3 is setup correctly in the AMI"
56+
}
57+
Else {
58+
Write-Output "Found Python command at $PYTHON"
59+
}
60+
61+
try {
62+
$PYTHON3 = (Get-Command python3).Source
63+
} catch {
64+
$PYTHON3 = ""
65+
}
66+
67+
If ("$PYTHON3" -eq "") {
68+
Write-Output "Found no Python 3 in $Env:PATH. This is expected for Miniconda3, and the command will be an alias to Python"
69+
}
70+
Else {
71+
Write-Output "Found Python 3 command at $PYTHON3"
72+
}
73+
74+
If (("$PYTHON3" -eq "") -and ("$PYTHON" -ne "")) {
75+
# Setup an alias from Python3 to Python when only the latter exists in Miniconda3
76+
Add-Content "$PS_PROFILE" "Set-Alias -Name python3 -Value $PYTHON"
77+
}

aws/ami/windows/scripts/Installers/Install-Choco.ps1

Lines changed: 0 additions & 22 deletions
This file was deleted.

aws/ami/windows/scripts/Installers/Install-Miniconda3.ps1

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,9 @@ Remove-Item -Path "$downloadDir\*" -Recurse -Force -ErrorAction SilentlyContinue
3939
$PS_PROFILE = "$PSHOME\Microsoft.PowerShell_profile.ps1"
4040

4141
$PYTHON_PATH = '$Env:PATH += ' + "';$installationDir'"
42+
4243
# Add conda path to the powershell profile to make its commands, i.e. python, available when logging
4344
# in to Windows runners or when the CI uses powershell
4445
Add-Content "$PS_PROFILE" "$PYTHON_PATH"
4546

4647
$Env:PATH += ";$installationDir"
47-
# According to https://docs.conda.io/en/latest/miniconda.html, Miniconda have only one built-in
48-
# python executable, and it can be Python3 or 2 depending on which installation package is used
49-
try {
50-
$PYTHON = (Get-Command python).Source
51-
} catch {
52-
$PYTHON = ""
53-
}
54-
55-
If ("$PYTHON" -eq "") {
56-
Write-Output "Found no Python in $Env:PATH. Double check that Miniconda3 is setup correctly in the AMI"
57-
}
58-
Else {
59-
Write-Output "Found Python command at $PYTHON"
60-
}
61-
62-
try {
63-
$PYTHON3 = (Get-Command python3).Source
64-
} catch {
65-
$PYTHON3 = ""
66-
}
67-
68-
If ("$PYTHON3" -eq "") {
69-
Write-Output "Found no Python 3 in $Env:PATH. This is expected for Miniconda3, and the command will be an alias to Python"
70-
}
71-
Else {
72-
Write-Output "Found Python 3 command at $PYTHON3"
73-
}
74-
75-
If (("$PYTHON3" -eq "") -and ("$PYTHON" -ne "")) {
76-
# Setup an alias from Python3 to Python when only the latter exists in Miniconda3
77-
Add-Content "$PS_PROFILE" "Set-Alias -Name python3 -Value $PYTHON"
78-
}

aws/ami/windows/windows.pkr.hcl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ build {
8686
}
8787

8888
# Install the rest of the dependencies
89+
# Please note: When modifying Microsoft.PowerShell_profile for a user
90+
# all modifications need to be done to Install-Choco-GenerateProfile script
8991
provisioner "powershell" {
9092
execution_policy = "unrestricted"
9193
scripts = [
9294
"${path.root}/scripts/Helpers/Reset-UserData.ps1",
93-
"${path.root}/scripts/Installers/Install-Choco.ps1",
95+
"${path.root}/scripts/Installers/Install-Choco-GenerateProfile.ps1",
9496
"${path.root}/scripts/Installers/Install-Tools.ps1",
9597
]
9698
}

0 commit comments

Comments
 (0)