11<#
22Copyright (c) 2024 3M Company
3- This script installs Miniforge3 for the 3M corporate environment .
3+ This script installs Miniforge3.
44It can be run as part of gitpyup for now.
55#>
66
77# run Utility-Functions.ps1 to get the utility functions
88. " ./Utility-Functions.ps1"
99Start-Logging
1010
11- Write-Log " Setup-Python v1"
12-
13- # check if admin and warn
11+ # check if admin and exit if true
1412$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity ]::GetCurrent())
1513$RunningAsAdmin = $currentPrincipal.IsInRole ([Security.Principal.WindowsBuiltInRole ]::Administrator)
1614if ($RunningAsAdmin ) {
1715 if (Test-Path " $Env: TEMP \gitpyup-as-admin" ) {
1816 Remove-Item - Force " $Env: TEMP \gitpyup-as-admin"
1917 } else {
2018 Write-Log " Please run this script as a regular user"
19+ Wait-Logging
2120 Read-Host - Prompt " Press enter key to exit" | Out-Null
2221 exit
2322 }
@@ -78,12 +77,29 @@ $MiniforgeInstall = {
7877 Write-Log " ...Miniforge folder removed"
7978 }
8079
81- Write-Log " Miniforge not installed, downloading..."
82- $Link = " https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Windows-x86_64.exe"
83- Invoke-WebRequest $Link - OutFile " $env: UserProfile \Downloads\Miniforge3-Windows-x86_64.exe"
80+ # check current folder for Miniforge exe
81+ $InstallerName = " Miniforge3-Windows-x86_64.exe"
82+ $DownloadLocation = " $env: UserProfile \Downloads\$InstallerName "
83+
84+ if (Test-Path $InstallerName ) {
85+ Write-Log " Miniforge already downloaded"
86+ $Installer = " $PWD \$InstallerName "
87+ } else {
88+ if (Test-Path $DownloadLocation ) {
89+ Write-Log " Miniforge already downloaded"
90+ } else {
91+ Write-Log " Miniforge downloading..."
92+ $Link = " https://github.com/conda-forge/miniforge/releases/latest/download/$InstallerName "
93+ Invoke-WebRequest $Link - OutFile $DownloadLocation
8494 Write-Log " ...Miniforge downloaded"
95+ }
96+ $Installer = $DownloadLocation
97+ }
98+
99+ # unblock the installer
100+ Unblock-File $Installer
101+
85102 Write-Log " Installing Miniforge..."
86- $Installer = " $env: UserProfile \Downloads\Miniforge3-Windows-x86_64.exe"
87103 $ArgumentList = " /InstallationType=JustMe /RegisterPython=1 /AddToPath=0 /S /D=$MiniforgeInstallPath "
88104 $Proc = Start-Process $Installer - Wait - ArgumentList $ArgumentList - PassThru
89105 if ($Proc.ExitCode -ne 0 ) {
@@ -100,16 +116,30 @@ $MiniforgeInstall = {
100116 $Proc = Start-Process @Conda - ArgumentList init
101117 Write-Log " ...Miniforge initialized"
102118}
119+ $MiniforgeInstallArgs = $MiniforgeInstallPath , $InstallType , $Conda
120+ $MiniforgeInstallEncoded , $MiniforgeInstallArgsEncoded = ConvertTo-Base64String $MiniforgeInstall $MiniforgeInstallArgs
103121
104122# Check if miniforge's conda.bat runs
105- $CondaVersion = conda -- version
106- if (! ($CondaVersion )) {
123+ $CondaVersion = Get-StandardOutput - Command " conda --version"
124+ if ($CondaVersion | Select-String - Pattern " CommandNotFoundException" ) {
125+ if ($InstallType -eq " AllUsers" ) {
126+ # Start-Process -FilePath "powershell" -Verb RunAs -Wait -ArgumentList (
127+ Start-Process - FilePath " powershell" - Wait - NoNewWindow - ArgumentList (
128+ " -EncodedCommand $MiniforgeInstallEncoded " ,
129+ " -EncodedArguments $MiniforgeInstallArgsEncoded "
130+ )
131+ } else {
107132 & $MiniforgeInstall $MiniforgeInstallPath $InstallType $Conda
133+ }
108134} else {
109135 Write-Log " Miniforge3 already available"
110136 Write-Log " version: $CondaVersion "
111137}
112138
139+ # need to update to recent conda version for truststore support
140+ Write-Log " updating conda base env..."
141+ $Proc = Start-Process @Conda - ArgumentList " update -n base -c conda-forge conda -y"
142+
113143$EnvSetupScript = {
114144 param (
115145 [string ]$EnvName ,
@@ -124,90 +154,29 @@ $EnvSetupScript = {
124154 # this prevents a halts due to an interactive conda message about reporting errors
125155 conda config -- set report_errors false
126156
127- # this sections is needed if the device is subject to SSL inspection
128- # currently Minforge/conda does not have a way to use the system certs
129- # The bundle URL is set by an environment variable
130- if ($Env: GITPYUP_BUNDLE_URL ) {
131-
132- # remove existing gitpyup-tls-ca-bundle.pem if it exists
133- $BundlePath = " $env: ProgramData \gitpyup-tls-ca-bundle.pem"
134- if (Test-Path $BundlePath ) {
135- Remove-Item - Force $BundlePath
136- }
137-
138- # download the tls bundle
139- $URL = $Env: GITPYUP_BUNDLE_URL
140- Write-Log " downloading tls bundle from $URL "
141- Invoke-WebRequest $URL - OutFile $BundlePath
142- # are these redundant because setting the .condarc file?
143- conda config -- set ssl_verify True
144- conda config -- set ssl_verify $BundlePath
145-
146- # Check for existing conda environment
147- # TODO get any existing conda environment directories
157+ # set conda to use the system truststore
158+ conda config -- set ssl_verify truststore
148159
149- # configure path to save environments depending on installation type
150- if ($InstallType -eq " AllUsers" ) {
151- $EnvDir = " $env: ProgramData \.conda\envs"
152- } else {
153- $EnvDir = " $env: UserProfile \.conda\envs"
154- }
155-
156- # Create a .condarc file in the root dir of the MiniForge installation
157- $CondarcPath = " $MiniforgeInstallPath \.condarc"
158- $CondarcContent =
160+ # Check for existing conda environment
161+ # configure path to save environments depending on installation type
162+ if ($InstallType -eq " AllUsers" ) {
163+ $EnvDir = " $env: ProgramData \.conda\envs"
164+ } else {
165+ $EnvDir = " $env: UserProfile \.conda\envs"
166+ }
167+ # make sure the envs directory exists
168+ New-Item - Path $EnvDir - ItemType Directory - Force
169+ # Create a .condarc file in the root dir of the MiniForge installation
170+ $CondarcPath = " $MiniforgeInstallPath \.condarc"
171+ $CondarcContent =
159172" channels:
160173 - conda-forge
161- ssl_verify: $BundlePath
174+ ssl_verify: truststore
162175envs_dirs:
163176 - $EnvDir
164177 "
165- Set-Content - Force - Path $CondarcPath - Value $CondarcContent
178+ Set-Content - Force - Path $CondarcPath - Value $CondarcContent
166179
167- } else {
168- Write-Log " No bundle URL provided"
169- }
170-
171- # function to check if pip has SSL errors, return true if error detected
172- function Test-PipTlsError {
173- # Define the command
174- $Command = " conda run -n $EnvName python -m pip install --dry-run tiny"
175- Write-Log " Running SSL test command: $Command "
176- # Create a temporary file for output
177- $TempFile = [System.IO.Path ]::GetTempFileName()
178- # Execute the command and redirect output to the temporary file
179- Start-Process - FilePath " powershell.exe" - ArgumentList " -Command" , $Command - RedirectStandardOutput $TempFile - NoNewWindow - Wait
180- # Read the output from the temporary file
181- $TlsTest = Get-Content - Path $TempFile
182- # Clean up the temporary file
183- Remove-Item - Path $TempFile
184-
185- # $TlsTest = conda run -n $EnvName python -m pip install --dry-run tiny
186- if ($TlsTest | Select-String - Pattern " SSL: CERTIFICATE_VERIFY_FAILED" ) {
187- Write-Log " pip SSL error detected"
188- return $true
189- } else {
190- Write-Log " pip SSL error not detected"
191- return $false
192- }
193- }
194-
195- # check if pip has SSL errors, install or uninstall pip-system-certs
196- if (Test-PipTlsError ) {
197- # check if pip-system-certs is installed
198- if (! (conda run - n $EnvName python - m pip list | Select-String - Pattern pip- system- certs)) {
199- # patch pip and requests to use system certs
200- Write-Log " installing pip-system-certs..."
201- conda install - n $EnvName pip- system- certs - y
202- # conda run -n $EnvName python -m pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org pip-system-certs
203- }
204-
205- # check if pip still has SSL errors, set pip to use the tls-ca-bundle.pem
206- if (Test-PipTlsError ) {
207- Write-Log " pip still has SSL errors, setting pip to use tls-ca-bundle.pem"
208- conda run - n $EnvName python - m pip config set global.cert $BundlePath
209- }
210- }
211180
212181 # attempt to update conda base environment
213182 Write-Log " updating conda base env..."
@@ -216,6 +185,7 @@ envs_dirs:
216185
217186 # only wait if in debug mode
218187 if ($Env: GITPYUP_DEPLOY_DEBUG ) {
188+ Wait-Logging
219189 Read-Host - Prompt " Press enter key to exit" | Out-Null
220190 }
221191}
0 commit comments