-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinstall.ps1
374 lines (312 loc) · 13.5 KB
/
install.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#region variables
param(
[switch]$Help = $false,
[switch]$Alias = $false,
[switch]$VPN = $false,
[switch]$WindowsContainers = $false,
[switch]$RenameBinaries = $false
)
# When run from context menu (also other situations, but not relevant)
if($MyInvocation.InvocationName -eq "&" -and $PSBoundParameters.count -eq 0)
{
$PSBoundParameters."Alias" = $true
$PSBoundParameters."VPN" = $true
$PSBoundParameters."WindowsContainers" = $true
}
$script:parameters = ""
foreach ($boundParam in $PSBoundParameters.GetEnumerator())
{
$script:parameters += '-{0} ' -f $boundParam.Key
}
$script:rancherDesktopExe = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\Rancher Desktop.exe"
$script:windowsBinariesPath = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\resources\resources\win32\bin"
$script:linuxBinariesPath = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\resources\resources\linux\bin"
$script:profilePath = "C:\Users\$env:UserName\Documents\WindowsPowerShell\old-profile.ps1"
$script:panicFilePath = "C:\ProgramData\docker\panic.log"
$script:dockerPackageUrl = "https://download.docker.com/win/static/stable/x86_64/docker-20.10.8.zip"
$script:rancherDesktopVersion = "1.1.1"
$script:rancherDesktopInstallerName = "Rancher.Desktop.Setup.$script:rancherDesktopVersion"
$script:rancherDesktopInstallerHash = "DD3D52501963FD1757E8D0B972DEDA264AFE38D8F0EF3383AAA5B1BD6B6C0747"
$script:rancherDesktopUrl = "https://github.com/rancher-sandbox/rancher-desktop/releases/download/v$script:rancherDesktopVersion/$script:rancherDesktopInstallerName.exe"
$script:wslVpnKitUrl = "https://github.com/sakai135/wsl-vpnkit/releases/download/v0.3.1/wsl-vpnkit.tar.gz"
$script:restartRequired = $false
$script:bashProfilePath = "C:\Users\$env:UserName\.bash_profile"
$script:appDataSettingsPath = "C:\Users\$env:UserName\AppData\Roaming\rancher-desktop\settings.json"
#endregion
#region functions
function Help
{
Write-Host ""
Write-Host "Usage:"
Write-Host " .\install.ps1 [flags]"
Write-Host ""
Write-Host "Flags:"
Write-Host " -Alias Creates alias for usual Docker commands in Powershell and Bash."
Write-Host " -VPN Enables support for enterprise VPNs."
Write-Host " -WindowsContainers Enables support for Windows Containers using Docker binary."
Write-Host ""
Write-Host "Advanced Flags:"
Write-Host " -RenameBinaries Renames binaries to provide universal docker command support in cases where shell profiles are of no use, but comes with some caveats (e.g. requires using docker compose instead of docker-compose). Incompatible with -Alias flag."
Write-Host ""
}
function EnableContainerFeature
{
$containerExists = Get-WindowsOptionalFeature -Online -FeatureName Containers
if($containerExists.State -eq 'Enabled')
{
Write-Host "Containers feature is already installed. Skipping the install." -ForegroundColor Green
return
} else {
Write-Host "Installing Containers feature..." -ForegroundColor Blue
Enable-WindowsOptionalFeature -NoRestart -Online -FeatureName Containers -All
$script:restartRequired = $true
}
Write-Host "Containers feature enabled." -ForegroundColor Green
}
function EnableWslFeature
{
$wslExists = Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
if($wslExists.State -eq 'Enabled')
{
Write-Host "WSL feature is already installed. Skipping the install." -ForegroundColor Green
return
} else {
Write-Host "Installing WSL feature..." -ForegroundColor Blue
Enable-WindowsOptionalFeature -NoRestart -Online -FeatureName Microsoft-Windows-Subsystem-Linux -All
$script:restartRequired = $true
}
}
function EnableVirtualMachinePlatformFeature
{
$vmpExists = Get-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
if($vmpExists.State -eq 'Enabled')
{
Write-Host "Virtual Machine Platform feature is already installed. Skipping the install." -ForegroundColor Green
return
} else {
Write-Host "Installing Virtual Machine Platform feature..." -ForegroundColor Blue
Enable-WindowsOptionalFeature -NoRestart -Online -FeatureName VirtualMachinePlatform -All
$script:restartRequired = $true
}
}
function DownloadDockerD
{
Write-Host "Installing dockerd..." -ForegroundColor Blue
Invoke-WebRequest $script:dockerPackageUrl -OutFile "docker.zip"
Expand-Archive docker.zip -DestinationPath "C:\"
Copy-Item "C:\docker\dockerd.exe" $script:windowsBinariesPath -Recurse -Force
Remove-Item docker.zip
Remove-Item "C:\docker" -Recurse -Force
[Environment]::SetEnvironmentVariable("Path", "$($env:path);$script:windowsBinariesPath", [System.EnvironmentVariableTarget]::Machine)
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
dockerd --register-service
Write-Host "dockerd successfully installed." -ForegroundColor Green
}
function InstallDockerAccessHelperModule
{
Write-Host "Installing dockeraccesshelper module..." -Foregroundcolor Blue
if (Get-Module -ListAvailable -Name dockeraccesshelper) {
Write-Host "Module already exists. Skipping the install." -ForegroundColor Green
Import-Module dockeraccesshelper
} else {
Install-Module -Name dockeraccesshelper -Force
Import-Module dockeraccesshelper
Write-Host "dockeraccesshelper module successfully installed." -ForegroundColor Green
}
}
function CreateWindowsContext
{
$winContextExists = $false
$contextList = docker context ls | ConvertFrom-String
Write-Host "Checking if the windows context already exists..." -ForegroundColor Blue
for($i=1; $i -le $contextList.Count; $i++)
{
if($contextList[$i].P1 -eq 'win')
{
Write-Host "Windows context already exists. Skipping the install." -ForegroundColor Green
$winContextExists = $true
return
}
}
if(-Not($winContextExists))
{
docker context create win --docker host=npipe:////./pipe/docker_engine
}
Write-Host "Windows context successfully installed." -ForegroundColor Green
}
function CreatePowershellProfile
{
if(!(Test-Path -Path $PROFILE))
{
New-Item -Type File -Path $PROFILE -Force
}
Write-Host "" >> $PROFILE
Add-Content $PROFILE (Get-Content "profile.ps1")
. $PROFILE
}
function UpdateGitBashProfile
{
$search = (Get-Content $script:bashProfilePath | Select-String -Pattern '#region generated by rd-installer for Alias support in Git Bash').Matches.Success
if( -Not $search){
Add-Content $script:bashProfilePath "#region generated by rd-installer for Alias support in Git Bash"
Add-Content $script:bashProfilePath "alias docker=""nerdctl"""
Add-Content $script:bashProfilePath "alias docker-compose=""nerdctl compose"""
Add-Content $script:bashProfilePath "alias dockerw=""/c/Users/$env:UserName/AppData/Local/Programs/Rancher\ Desktop/resources/resources/win32/bin/docker.exe --context win"""
Add-Content $script:bashProfilePath "alias dockerw-compose=""/c/Users/$env:UserName/AppData/Local/Programs/Rancher\ Desktop/resources/resources/win32/bin/docker-compose.exe --context win"""
Add-Content $script:bashProfilePath "#endregion"
}
}
function RestartRequired
{
if($script:restartRequired) {
Write-Warning "Before proceeding, a restart is required to enable some Windows features. Please execute the installer again after reboot."
$user_input = Read-Host -Prompt "Would you like to restart now? (Type 'Y' for 'Yes' or 'N' for 'No')."
if($user_input -eq 'Y')
{
Restart-computer
} elseif ($user_input -eq 'N') {
Stop-Process -Force -Name powershell
}
}
}
function CopyStartScript
{
Copy-Item "start.ps1" "$script:windowsBinariesPath" -Force
}
function IsDockerDesktopInstalled
{
$dockerDesktopExists = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -eq "Docker Desktop" }) -ne $null
if($dockerDesktopExists)
{
Write-Host "Please make sure Docker Desktop is uninstalled before installing Rancher Desktop." -ForegroundColor Red
exit 1
}
}
function InstallRancherDesktop
{
Write-Host "Installing Rancher Desktop..." -ForegroundColor Blue
if(!(Test-Path -Path "$script:rancherDesktopInstallerName.exe") -or (Get-FileHash -Algorithm SHA256 "$script:rancherDesktopInstallerName.exe").Hash -ne "$script:rancherDesktopInstallerHash")
{
Invoke-WebRequest $script:rancherDesktopUrl -OutFile "$script:rancherDesktopInstallerName.exe"
if((Get-FileHash -Algorithm SHA256 "$script:rancherDesktopInstallerName.exe").Hash -ne "$script:rancherDesktopInstallerHash")
{
Write-Host "Checksum validation of Rancher Desktop installer failed." -ForegroundColor Red
exit 1
}
}
Invoke-Expression ".\$script:rancherDesktopInstallerName.exe"
$setupId = (Get-Process $script:rancherDesktopInstallerName).id 2> $null
Wait-Process -Id $setupId
Write-Host "Rancher Desktop successfully installed." -ForegroundColor Green
}
function ActivateWslVpnkit
{
Write-Host "Activating the tool for the VPN..." -ForegroundColor Blue
Invoke-WebRequest $script:wslVpnKitUrl -OutFile "wsl-vpnkit.tar.gz"
wsl --import wsl-vpnkit $env:USERPROFILE\wsl-vpnkit wsl-vpnkit.tar.gz --version 2
Remove-Item wsl-vpnkit.tar.gz -Force
$search = (Get-Content $PROFILE | Select-String -Pattern '#region generated by rd-installer for VPN support in Powershell').Matches.Success
if( -Not $search){
Add-Content $PROFILE "#region generated by rd-installer for VPN support in Powershell"
Add-Content $PROFILE "# Start the VPN support"
Add-Content $PROFILE "wsl -d wsl-vpnkit service wsl-vpnkit start 2> `$null"
Add-Content $PROFILE "#endregion"
Add-Content $PROFILE ""
}
$search = (Get-Content $script:bashProfilePath | Select-String -Pattern '#region generated by rd-installer for VPN support in Git Bash').Matches.Success
if( -Not $search){
Add-Content $script:bashProfilePath "#region generated by rd-installer for VPN support in Git Bash"
Add-Content $script:bashProfilePath "# Start the VPN support"
Add-Content $script:bashProfilePath "wsl -d wsl-vpnkit service wsl-vpnkit start 2> /dev/null"
Add-Content $script:bashProfilePath "#endregion"
Add-Content $script:bashProfilePath ""
}
Write-Host "VPN support successfully installed." -ForegroundColor Green
}
function ChangeFilePermissions
{
$isReadOnly = Get-ItemProperty -Path $script:panicFilePath 2> $null | Select-Object IsReadOnly
if($isReadOnly -match "True")
{
Set-ItemProperty -Path $script:panicFilePath -Name IsReadOnly -Value $false
}
}
function RenameBinariesFunction
{
Write-Host "Renaming the Rancher Desktop binaries..." -ForegroundColor Blue
Rename-Item -Path "$script:windowsBinariesPath\docker.exe" -NewName dockerw.exe
Rename-Item -Path "$script:windowsBinariesPath\docker-compose.exe" -NewName dockerw-compose.exe
Copy-Item "$script:windowsBinariesPath\nerdctl.exe" "$script:windowsBinariesPath\docker.exe" -Force
Rename-Item -Path "$script:linuxBinariesPath\docker" -NewName docker.old
Copy-Item "$script:linuxBinariesPath\nerdctl" "$script:linuxBinariesPath\docker" -Force
Write-Host "Renaming done." -ForegroundColor Green
}
function SetAppDataSettings
{
if(!(Test-Path -Path $script:appDataSettingsPath))
{
New-Item -Type File -Path $script:appDataSettingsPath -Force
Add-Content $script:appDataSettingsPath (Get-Content "settings.json")
}
else
{
$settingsContent = Get-Content $script:appDataSettingsPath -raw | ConvertFrom-Json
$settingsContent.kubernetes.enabled=$false
$settingsContent.kubernetes.containerEngine="containerd"
$settingsContent.updater=$false
$settingsContent | ConvertTo-Json | set-content $script:appDataSettingsPath
}
}
#endregion
#region main
if($Alias -and $RenameBinaries)
{
Write-Host "The flags -Alias and -RenameBinaries cannot be activated together." -ForegroundColor Red
Write-Host "Please choose only one of them." -ForegroundColor Red
exit 1
}
if($Help)
{
Help
exit 0
}
# Elevate script if needed.
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" $($script:parameters)" -Verb RunAs; exit }
IsDockerDesktopInstalled
if($WindowsContainers)
{
EnableContainerFeature
}
EnableWslFeature
EnableVirtualMachinePlatformFeature
RestartRequired
SetAppDataSettings
InstallRancherDesktop
if($VPN)
{
ActivateWslVpnkit
}
if($WindowsContainers)
{
InstallDockerAccessHelperModule
DownloadDockerD
ChangeFilePermissions
CreateWindowsContext
CopyStartScript
Start-Service docker
Add-AccountToDockerAccess "$env:UserDomain\$env:UserName"
}
if($Alias -and -Not($RenameBinaries))
{
CreatePowershellProfile
UpdateGitBashProfile
}
if($RenameBinaries -and -Not($Alias))
{
RenameBinariesFunction
}
Write-Host "Installation finished." -ForegroundColor Green
Write-Host -NoNewLine "Press any key to continue..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
Stop-Process -Force -Id $PID
#endregion