-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.ps1
More file actions
316 lines (273 loc) · 13.7 KB
/
main.ps1
File metadata and controls
316 lines (273 loc) · 13.7 KB
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
param(
# Path to the configuration file
[Parameter(Mandatory = $true,
HelpMessage = "Path to the .psd1 configuration file containing stack deployment settings")]
[ValidateScript({
$fullPath = [System.IO.Path]::GetFullPath($_)
# Validate file existence and configuration values
# With help of Qwen
if (![System.IO.File]::Exists($fullPath)) {
throw "The specified file path '$fullPath' does not exist."
}
try {
$config = Import-PowerShellDataFile -Path $fullPath -ErrorAction Stop
}
catch {
Write-Warning "Failed to import: $_ Please ensure the file is a valid PowerShell data file."
}
# Prevent default password usage
if ($config.piholePassword -eq "admin") {
throw "The default password 'admin' is not allowed. Please change the password in the configuration file."
}
# Validate restart policy
$validRestartPolicies = @("no", "always", "unless-stopped", "on-failure")
if ($config.restartPolicy -notin $validRestartPolicies) {
throw "The 'restartPolicy' in the configuration file is invalid. Accepted values are: $($validRestartPolicies -join ', ')."
}
# Validate network configuration
$validContainerNetwork = @("bridge", "host", "none")
if ($config.containerNetwork -notin $validContainerNetwork) {
throw "The 'containerNetwork' in the configuration file is invalid. Accepted values are: $($validContainerNetwork -join ', ')."
}
# When using the host network all ports should be empty
if ($config.containerNetwork -eq "host" -and ($config.piholeUiPort -ne "" -or $config.piholeDnsPort -ne "" -or $config.cloudflaredPort -ne "" -or $config.unboundPort -ne "")) {
throw "When using the 'host' network configuration, all port values should be empty."
}
# Validate DNS listening mode
$validListenValues = @("local", "all", "bind", "single", "")
if ($config.listen -notin $validListenValues) {
throw "The 'listen' in the configuration file is invalid. Accepted values are: $($validListenValues -join ', ')."
}
# Validate port numbers
$portParams = @{
'piholeUiPort' = $config.piholeUiPort
'piholeDnsPort' = $config.piholeDnsPort
'cloudflaredPort' = $config.cloudflaredPort
'unboundPort' = $config.unboundPort
}
foreach ($param in $portParams.GetEnumerator()) {
if ($param.Value -ne "" -and (-not [int]::TryParse($param.Value, [ref]$null) -or [int]$param.Value -lt 1 -or [int]$param.Value -gt 65535)) {
throw "The '$($param.Key)' in the configuration file is invalid. Accepted values are between 1 and 65535 or empty."
}
}
# Validate boolean flags
$boolParams = @('DNSSECEnabled', 'cloudflaredEnabled', 'unboundEnabled')
foreach ($param in $boolParams) {
if ($config.$param -notin @($true, $false)) {
throw "The '$param' in the configuration file is invalid. Accepted values are: `$true, `$false."
}
}
# Validate adlist format
if ($config.adlists -isnot [array]) {
throw "The 'adlists' in the configuration file is invalid. It should be an array of strings."
}
# Validate DNS IP addresses
foreach ($dns in $config.extraDNS) {
if (-not [System.Net.IPAddress]::TryParse($dns, [ref]$null)) {
throw "The 'extraDNS' value '$dns' is not a valid IP address."
}
}
# Validate volume paths, with help of copilot
foreach ($volume in $config.piholeVolumes) {
$volume = $volume -replace " ", ""
if ($volume -notmatch "^/[^/]+(/[^/]+)*$") {
throw "The 'piholeVolumes' value '$volume' is not a valid volume path."
}
}
# Validate DHCP settings (with help of copilot)
if ($config.configureDHCP) {
if ($config.dhcpServer -isnot [array]) {
throw "The 'dhcpServer' in the configuration file is invalid. It should be an array of strings."
}
foreach ($server in $config.dhcpServer) {
if ($server -isnot [string] -or [string]::IsNullOrWhiteSpace($server)) {
throw "Each 'dhcpServer' value in the configuration file should be a non-empty string."
}
}
if ($config.dhcpScopeId -isnot [string]) {
throw "The 'dhcpScopeId' in the configuration file is invalid. It should be a string."
}
if ($config.dhcpPolicyName -isnot [string]) {
throw "The 'dhcpPolicyName' in the configuration file is invalid. It should be a string."
}
}
# Validate container engine setting
if ($config.ContainsKey('containerEngine')) {
if ($config.containerEngine -isnot [string] -or [string]::IsNullOrWhiteSpace($config.containerEngine)) {
throw "The 'containerEngine' in the configuration file is invalid. It should be a non-empty string (e.g. 'docker' or 'podman')."
}
}
return $true
})]
[string]$ConfigPath,
# Path to the Ansible inventory file
[Parameter(Mandatory = $false,
HelpMessage = "Path to the Ansible inventory file listing target hosts")]
[ValidateScript({
$fullPath = [System.IO.Path]::GetFullPath($_)
[System.IO.File]::Exists($fullPath) -and [System.IO.Directory]::Exists([System.IO.Path]::GetDirectoryName($fullPath))
})]
[string]$InventoryPath = "./inventory.ini",
# Ansible privilege escalation method
# For more information, see: https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_privilege_escalation.html
[Parameter(Mandatory = $false,
HelpMessage = "Ansible privilege escalation method (e.g., 'ask-become-pass' or 'become-method=sudo')")]
[string]$become = "ask-become-pass"
)
[hashtable]$data = Import-PowerShellDataFile -Path $ConfigPath
# Create log file
if (-not (Test-Path $data['logFile'])) {
$logDir = [System.IO.Path]::GetDirectoryName($data['logFile'])
if (-not (Test-Path $logDir)) {
New-Item -Path $logDir -ItemType Directory -Force
}
New-Item -Path $data['logFile'] -ItemType File
}
# Start logging
Start-Transcript -Path $data['logFile'] -Append
Import-Module ./main.psm1
# Ensure Ansible is available locally
Install-Ansible
# Prepare remote hosts with required dependencies (Docker, PowerShell)
Install-DependenciesRemotely -TempPath ./temp -InventoryPath $InventoryPath -become $become
# Get host information from Ansible
[Array]$servers = Get-Content -Path "./temp/host_info.csv"
Remove-Item -Path ./temp -Recurse -Force
# Prepare function definitions for remote execution
$functions = @(
"Deploy-Container",
"Deploy-Pihole",
"Deploy-Unbound",
"Deploy-Cloudflared",
"Set-PiholeConfiguration",
"Invoke-CommandWithCheck",
"ConfigDifferent",
"Get-CurrentContainerConfig",
"Remove-OldContainers",
"Get-DnsIp"
)
$functionsDefinitions = Get-FunctionDefinitions -functions $functions
#region Remote deployment
# Deploy stack to all hosts in parallel
$serverDeploymentJobs = @()
foreach ($server in $servers) {
$serverDeploymentJobs += Start-ThreadJob -ScriptBlock {
param(
[Parameter(Mandatory = $true)]
[string]$server,
[Parameter(Mandatory = $true)]
[hashtable]$data,
[Parameter(Mandatory = $true)]
[array]$functionsDefinitions
)
# SSH connection
[string]$hostname, $username = $server -split ','
try {
$session = New-PSSession -HostName $hostname -UserName $username -SSHTransport -ErrorAction Stop
}
catch {
throw "Failed to create SSH session, please check the credentials and network connectivity."
}
# Execute deployment on remote host
Invoke-Command -Session $session -ScriptBlock {
param(
[Parameter(Mandatory = $true)]
[hashtable]$data,
[Parameter(Mandatory = $true)]
[array]$functionDefinitions
)
# Initialize functions in remote session
# https://stackoverflow.com/questions/77900019/piping-to-where-object-and-foreach-object-not-working-in-module-delayed-loaded-i/77903771#77903771
$functionDefinitions | ForEach-Object {
. ([ScriptBlock]::Create($_))
}
# Prepare function for thread jobs
# https://stackoverflow.com/questions/75609709/start-threadjob-is-not-detecting-my-variables-i-pass-to-it
$deployContainerAst = ${function:Deploy-Container}.Ast.Body
$deployPiholeAst = ${function:Deploy-Pihole}.Ast.Body
$getContainerConfigAst = ${function:Get-CurrentContainerConfig}.Ast.Body
$configDifferenceAst = ${function:ConfigDifferent}.Ast.Body
$deployUnboundAst = ${function:Deploy-Unbound}.Ast.Body
$deployCloudflaredAst = ${function:Deploy-Cloudflared}.Ast.Body
# Execute deployment jobs in parallel
@(
# Remove disabled containers
Start-ThreadJob ${function:Remove-OldContainers} -ArgumentList $data
# Deploy Pi-hole
Start-ThreadJob -ScriptBlock {
param($data, $deployContainerAst, $getContainerConfigAst, $configDifferenceAst, $deployPiholeAst)
${function:Deploy-Container} = $deployContainerAst.GetScriptBlock()
${function:Get-CurrentContainerConfig} = $getContainerConfigAst.GetScriptBlock()
${function:ConfigDifferent} = $configDifferenceAst.GetScriptBlock()
& $deployPiholeAst.GetScriptBlock() -data $data
} -ArgumentList $data, $deployContainerAst, $getContainerConfigAst, $configDifferenceAst, $deployPiholeAst
# Deploy Unbound (if enabled)
Start-ThreadJob -ScriptBlock {
param($data, $deployContainerAst, $getContainerConfigAst, $configDifferenceAst, $deployUnboundAst)
if ($data['unboundEnabled']) {
${function:Deploy-Container} = $deployContainerAst.GetScriptBlock()
${function:Get-CurrentContainerConfig} = $getContainerConfigAst.GetScriptBlock()
${function:ConfigDifferent} = $configDifferenceAst.GetScriptBlock()
& $deployUnboundAst.GetScriptBlock() -data $data
}
else {
Write-Host "Skipping Unbound deployment..."
}
} -ArgumentList $data, $deployContainerAst, $getContainerConfigAst, $configDifferenceAst, $deployUnboundAst
# Deploy Cloudflared (if enabled)
Start-ThreadJob -ScriptBlock {
param($data, $deployContainerAst, $getContainerConfigAst, $configDifferenceAst, $deployCloudflaredAst)
if ($data['cloudflaredEnabled']) {
${function:Deploy-Container} = $deployContainerAst.GetScriptBlock()
${function:Get-CurrentContainerConfig} = $getContainerConfigAst.GetScriptBlock()
${function:ConfigDifferent} = $configDifferenceAst.GetScriptBlock()
& $deployCloudflaredAst.GetScriptBlock() -data $data
}
else {
Write-Host "Skipping Cloudflared deployment..."
}
} -ArgumentList $data, $deployContainerAst, $getContainerConfigAst, $configDifferenceAst, $deployCloudflaredAst
) | Wait-Job | Receive-Job | Remove-Job
# Configure Pi-hole
Set-PiholeConfiguration -data $data
# Get Pi-Hole DNS IP for dhcp settings
if ($data['configureDHCP']) {
return Get-DnsIp -data $data
}
} -ArgumentList $data, $functionsDefinitions
Write-Host "Stack deployed on $hostname"
Remove-PSSession -Session $session
} -ArgumentList $server, $data, $functionsDefinitions
}
# endregion
# Process job results (and print them)
$serverDeploymentJobs | ForEach-Object {
$job = Wait-Job $_
$job.Information | ForEach-Object { Write-Host $_ }
if ($job.State -eq "Failed") {
Write-Host "Deployment failed" -ForegroundColor Red
Write-Host "Errors:" -ForegroundColor Red
foreach ($joberror in $job.Error) {
Write-Host $joberror -ForegroundColor Red
}
# print thread job error
foreach ($joberrror in $job.JobStateInfo.Reason) {
Write-Host $joberrror -ForegroundColor Red
}
}
else {
Write-Host "Deployment succeeded" -ForegroundColor Green
if ($data['configureDHCP'] -and $job.Output) {
try {
Update-DHCPSettings -data $data -dnsServer $job.Output -ErrorAction Stop
Write-Host "DHCP configuration updated successfully" -ForegroundColor Green
}
catch {
Write-Host "DHCP configuration failed: $_" -ForegroundColor Red
}
}
}
Remove-Job $job
}
# Stop logging
Stop-Transcript