-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-setup-local.ps1
346 lines (280 loc) · 10.5 KB
/
2-setup-local.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
$ErrorActionPreference = "Stop"
# Function to download a file if it does not exist
function Download-File {
param (
[string]$Uri,
[string]$OutputPath
)
if (-not (Test-Path $OutputPath)) {
Write-Output "Downloading $Uri to $OutputPath"
Invoke-WebRequest -Uri $Uri -OutFile $OutputPath
} else {
Write-Output "$OutputPath already exists"
}
}
# Function to add directory to PATH if not already present
function Add-ToPath {
param (
[string]$Directory
)
$currentPath = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine)
if ($currentPath -notlike "*$Directory*") {
Write-Output "Adding $Directory to PATH"
$newPath = $currentPath + ";$Directory"
[System.Environment]::SetEnvironmentVariable('Path', $newPath, [System.EnvironmentVariableTarget]::Machine)
} else {
Write-Output "$Directory is already in PATH"
}
}
function Compare-String {
param(
[String] $string1,
[String] $string2
)
if ( $string1 -ceq $string2 ) {
return -1
}
for ( $i = 0; $i -lt $string1.Length; $i++ ) {
if ( $string1[$i] -cne $string2[$i] ) {
Write-Host "one='$([byte][char]$string1[$i])' two='$([byte][char]$string2[$i])'"
return $i
}
}
return $string1.Length
}
# Define Variables
$DownloadPath = "C:\Temp"
$DockerCliVersion = "26.1.3"
$DockerCliFileUrl = "https://download.docker.com/win/static/stable/x86_64/docker-$($DockerCliVersion).zip"
$DockerCliTempPath = "$($DownloadPath)\docker-$($DockerCliVersion).zip"
$DockerCliInstallPath = "C:\Program Files\DockerCLI"
$DockerComposeVersion = "2.27.1"
$DockerComposeFileUrl = "https://github.com/docker/compose/releases/download/v$($DockerComposeVersion)/docker-compose-windows-x86_64.exe"
$DockerComposeTempPath = "$($DownloadPath)\docker-compose-windows-x86_64.exe"
$DockerComposeInstallPath = "C:\Program Files\DockerCLI"
$DockerBuildXVersion = "0.14.1"
$DockerBuildXFileUrl = "https://github.com/docker/buildx/releases/download/v$($DockerBuildXVersion)/buildx-v$($DockerBuildXVersion).windows-amd64.exe"
$DockerBuildXTempPath = "$($DownloadPath)\buildx-v$($DockerBuildXVersion).windows-amd64.exe"
$DockerBuildXInstallPath = "$($env:USERPROFILE)\.docker\cli-plugins"
$UnboundVersion = "1.20.0"
$UnboundFileUrl = "https://nlnetlabs.nl/downloads/unbound/unbound-$($UnboundVersion).zip"
$UnboundTempPath = "$($DownloadPath)\unbound-$($UnboundVersion).zip"
$UnboundInstallPath = "C:\Program Files\Unbound"
# Create Download Path if it does not exist
if (-not (Test-Path $DownloadPath)) {
New-Item -Path $DownloadPath -ItemType Directory
}
# Ensure Docker CLI is installed
if (-not (Test-Path (Join-Path $DockerCliInstallPath 'docker.exe'))) {
# Download Docker CLI
Download-File -Uri $DockerCliFileUrl -OutputPath $DockerCliTempPath
# Create Docker CLI Install Directory
if (-not (Test-Path $DockerCliInstallPath)) {
New-Item -Path $DockerCliInstallPath -ItemType Directory
}
# Extract Docker CLI
Write-Output "Extracting Docker CLI to $DockerCliInstallPath"
Expand-Archive -Path $DockerCliTempPath -DestinationPath $DockerCliInstallPath -Force
# Update PATH Environment Variable
Add-ToPath -Directory $DockerCliInstallPath
} else {
Write-Output "Docker CLI is already installed"
}
# Set Docker CLI Context
$ipAddresses = wsl bash -c "hostname -I"
$wslIp = $ipAddresses.Trim() -split "\s+" | Select-Object -First 1
$dockerContextEndpoint="tcp://$($wslIp):2375"
$dockerContexts = docker context ls --format '{{.Name}}\t{{.Current}}\t{{.DockerEndpoint}}'
$currentDockerContext = $dockerContexts | Where-Object { $_ -match "true" }
if ($currentDockerContext) {
$currentDockerContextParts = $currentDockerContext -split "\t"
$currentDockerContextEndpoint = $currentDockerContextParts[2]
if ($dockerContextEndpoint -ne $currentDockerContextEndpoint) {
Write-Output "Updating Docker context 'wsl' with endpoint $dockerContextEndpoint"
docker context create wsl --docker "host=tcp://$($wslIp):2375" -Force
docker context use wsl
} else {
Write-Output "Docker Context is already 'wsl'"
}
} else {
Write-Output "Creating Docker context 'wsl' with endpoint $dockerContextEndpoint"
docker context create wsl --docker "host=tcp://$($wslIp):2375"
docker context use wsl
}
# Ensure Docker Compose is installed
if (-not (Test-Path (Join-Path $DockerComposeInstallPath 'docker-compose.exe'))) {
# Download Docker Compose
Download-File -Uri $DockerComposeFileUrl -OutputPath $DockerComposeTempPath
# Create Docker Compose Install Directory
if (-not (Test-Path $DockerComposeInstallPath)) {
New-Item -Path $DockerComposeInstallPath -ItemType Directory
}
# Install Docker Compose
Write-Output "Installing Docker Compose to $DockerComposeInstallPath"
Copy-Item -Path $DockerComposeTempPath -Destination (Join-Path $DockerComposeInstallPath 'docker-compose.exe') -Force
} else {
Write-Output "Docker Compose is already installed"
}
# Ensure Docker BuildX is installed
if (-not (Test-Path (Join-Path $DockerBuildXInstallPath 'buildx.exe'))) {
# Download Docker BuildX
Download-File -Uri $DockerBuildXFileUrl -OutputPath $DockerBuildXTempPath
# Create Docker BuildX Install Directory
if (-not (Test-Path $DockerBuildXInstallPath)) {
New-Item -Path $DockerBuildXInstallPath -ItemType Directory
}
# Install Docker BuildX
Write-Output "Installing Docker BuildX to $DockerBuildXInstallPath"
Copy-Item -Path $DockerBuildXTempPath -Destination (Join-Path $DockerBuildXInstallPath 'buildx.exe') -Force
} else {
Write-Output "Docker BuildX is already installed"
}
# Ensure Unbound is installed
if (-not (Test-Path (Join-Path $UnboundInstallPath 'unbound.exe'))) {
# Download Unbound
Download-File -Uri $UnboundFileUrl -OutputPath $UnboundTempPath
# Create Unbound Install Directory
if (-not (Test-Path $UnboundInstallPath)) {
New-Item -Path $UnboundInstallPath -ItemType Directory
}
# Extract Unbound
Write-Output "Extracting Unbound to $UnboundInstallPath"
Expand-Archive -Path $UnboundTempPath -DestinationPath $UnboundInstallPath -Force
} else {
Write-Output "Unbound is already installed"
}
#Ensure Unbound is configured
$currentUnboundConfig = ""
if (Test-Path (Join-Path $UnboundInstallPath 'service.conf')) {
$currentUnboundConfig = Get-Content -Path "$(Join-Path $UnboundInstallPath 'service.conf')" -Raw
}
# Get the current DNS server addresses for all network interfaces
$dnsServers = Get-DnsClientServerAddress
# Get the primary network adapter (commonly the first one in the list)
$primaryAdapter = $dnsServers | Sort-Object InterfaceAlias | Select-Object -First 1
$primaryDnsServer = "8.8.8.8"
# Output the DNS server addresses for the primary adapter
if ($primaryAdapter) {
$primaryDnsServer = $primaryAdapter.ServerAddresses | Select-Object -First 1
} else {
Write-Output "No network adapters found."
}
$ingressServer = "10.152.255.3"
$privateDomains = @(
"local"
)
$insecureDomains = @(
"local"
)
$wildcardForwarders = @(
@{DomainName="k8s.lan.talifun.com.";Ipv4Addresses=@($ingressServer);},
@{DomainName="local.";Ipv4Addresses=@($ingressServer);}
)
$dnsForwarders = @(
@{DomainName=".";DnsServers=@("$($primaryDnsServer)@53");}
)
$unboundConfig = @"
# Unbound configuration file on windows.
# See example.conf for more settings and syntax
server:
#interface: 0.0.0.0@853
#interface: ::0@853
interface: 127.0.0.1@53
interface: ::1@53
access-control: 0.0.0.0/0 allow
access-control: ::0/0 allow
# verbosity level 0-4 of logging
verbosity: 0
# On windows you may want to make all the paths relative to the
# directory that has the executable in it (unbound.exe). Use this.
#directory: "%EXECUTABLE%"
# if you want to log to a file use
logfile: "C:\Program Files\Unbound\unbound.log"
log-queries: yes
log-replies: yes
log-local-actions: yes
log-servfail: yes
# or use "unbound.log" and the directory clause above to put it in
# the directory where the executable is.
# on Windows, this setting makes reports go into the Application log
# found in ControlPanels - System tasks - Logs
#use-syslog: yes
# on Windows, this setting adds the certificates from the Windows
# Cert Store. For when you want to use forwarders with TLS.
tls-win-cert: yes
#aggressive-nsec: no
# DNSSEC doesn't work with private TLDs so we need to mark them as insecure
private-address: 10.0.0.0/8
private-address: 172.16.0.0/12
private-address: 192.168.0.0/16
private-address: 100.64.0.0/10
$(
foreach ($privateDomain in $privateDomains) {
@"
private-domain: "$($privateDomain)"
"@
}
)
$(
foreach ($insecureDomain in $insecureDomains) {
@"
domain-insecure: "$($insecureDomain)"
"@
}
)
# remote-control:
# If you want to use unbound-control.exe from the command line, use
#control-enable: yes
#control-interface: 127.0.0.1
#control-use-cert: no
server:
auto-trust-anchor-file: "C:\Program Files\Unbound\root.key"
$(
foreach ($wildcardForwarder in $wildcardForwarders) {
@"
local-zone: "$($wildcardForwarder.DomainName)" redirect
$(
foreach ($Ipv4Address in $wildcardForwarder.Ipv4Addresses) {
@"
local-data: "$($wildcardForwarder.DomainName) IN A $($Ipv4Address)"
"@
}
)
"@
}
)
$(
foreach ($dnsForwarder in $dnsForwarders) {
@"
forward-zone:
name: "$($dnsForwarder.DomainName)"
$(
foreach ($dnsServer in $dnsForwarder.DnsServers) {
@"
forward-addr: $($dnsServer)
"@
}
)
"@
}
)
"@
$unboundConfig = ($unboundConfig -replace "`r`n", "`n").Trim()
$currentUnboundConfig = ($currentUnboundConfig -replace "`r`n", "`n").Trim()
if ($currentUnboundConfig -eq $unboundConfig) {
Write-Output "Unbound is already configured"
} else {
[System.IO.File]::WriteAllText((Join-Path $UnboundInstallPath 'service.conf'), $unboundConfig, [System.Text.Encoding]::ASCII)
$serviceName = "Unbound"
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($null -ne $service) {
Write-Output "Service '$serviceName' exists."
Restart-Service unbound
} else {
& "$(Join-Path $UnboundInstallPath 'unbound-service-install.exe')"
Write-Output "Service '$serviceName' does not exist."
Start-Service unbound
}
Write-Output "Unbound configured"
}
Write-Output "Installation completed successfully!"