-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.ps1
273 lines (251 loc) · 12.5 KB
/
bootstrap.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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
function Write-Log {
param (
[string] $message,
[string] $severity = 'INFO',
[string] $source = 'BootStrap',
[string] $logName = 'Application'
)
if (!([Diagnostics.EventLog]::Exists($logName)) -or !([Diagnostics.EventLog]::SourceExists($source))) {
New-EventLog -LogName $logName -Source $source
}
switch ($severity) {
'DEBUG' {
$entryType = 'SuccessAudit'
$eventId = 2
break
}
'WARN' {
$entryType = 'Warning'
$eventId = 3
break
}
'ERROR' {
$entryType = 'Error'
$eventId = 4
break
}
default {
$entryType = 'Information'
$eventId = 1
break
}
}
Write-EventLog -LogName $logName -Source $source -EntryType $entryType -Category 0 -EventID $eventId -Message $message
if ([Environment]::UserInteractive) {
$fc = @{ 'Information' = 'White'; 'Error' = 'Red'; 'Warning' = 'DarkYellow'; 'SuccessAudit' = 'DarkGray' }[$entryType]
Write-Host -object $message -ForegroundColor $fc
}
}
function Set-RoninRegOptions {
param (
[string] $mozilla_key = "HKLM:\SOFTWARE\Mozilla\",
[string] $ronnin_key = "$mozilla_key\ronin_puppet",
[string] $source_key = "$ronnin_key\source",
[string] $workerType = 'geckotwin1064hw',
[string] $src_Organisation = 'markcor',
[string] $src_Repository = 'ronin_puppet',
[string] $src_Revision = 'master'
)
begin {
Write-Log -message ('{0} :: begin - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
process {
New-Item -Path HKLM:\SOFTWARE -Name Mozilla –Force
New-Item -Path HKLM:\SOFTWARE\Mozilla -name ronin_puppet –Force
New-Item -Path $ronnin_key -Name source –Force
New-ItemProperty -Path "$ronnin_key" -Name 'workerType' -Value "$workerType" -PropertyType String
New-ItemProperty -Path "$ronnin_key" -Name 'inmutable' -Value 'false' -PropertyType String
New-ItemProperty -Path "$ronnin_key" -Name 'runtosuccess' -Value 'true' -PropertyType String
New-ItemProperty -Path "$ronnin_key" -Name 'last_run_exit' -Value '0' -PropertyType Dword
New-ItemProperty -Path "$ronnin_key" -Name 'bootstrap_stage' -Value 'setup' -PropertyType String
New-ItemProperty -Path "$source_key" -Name 'Organisation' -Value "$src_Organisation" -PropertyType String
New-ItemProperty -Path "$source_key" -Name 'Repository' -Value "$src_Repository" -PropertyType String
New-ItemProperty -Path "$source_key" -Name 'Revision' -Value "$src_Revision" -PropertyType String
}
end {
Write-Log -message ('{0} :: end - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
}
Function Clone-Ronin {
param (
[string] $sourceOrg = (Get-ItemProperty "HKLM:\SOFTWARE\Mozilla\ronin_puppet\source").Organisation,
[string] $sourceRepo = (Get-ItemProperty "HKLM:\SOFTWARE\Mozilla\ronin_puppet\source").Repository,
[string] $sourceRev = (Get-ItemProperty "HKLM:\SOFTWARE\Mozilla\ronin_puppet\source").Revision,
[string] $ronin_repo = "$env:systemdrive\ronin"
)
begin {
Write-Log -message ('{0} :: begin - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
process {
If(!(test-path $env:systemdrive\ronin)) {
git clone --single-branch --branch $sourceRev https://github.com/$sourceOrg/$sourceRepo $ronin_repo
$git_exit = $LastExitCode
if ($git_exit -eq 0) {
$git_hash = (git rev-parse --verify HEAD)
Set-ItemProperty -Path HKLM:\SOFTWARE\Mozilla\ronin_puppet -name githash -type string -value $git_hash
Write-Log -message ('{0} :: Checking/pulling updates from https://github.com/{1}/{2}. Branch: {3}.' -f $($MyInvocation.MyCommand.Name), ($sourceOrg), ($sourceRepo), ($sourceRev)) -severity 'DEBUG'
} else {
Write-Log -message ('{0} :: Git pull failed! https://github.com/{1}/{2}. Branch: {3}.' -f $($MyInvocation.MyCommand.Name), ($sourceOrg), ($sourceRepo), ($sourceRev)) -severity 'DEBUG'
DO {
Start-Sleep -s 60
git clone --single-branch --branch $sourceRev https://github.com/$sourceOrg/$sourceRepo $ronin_repo
$git_exit = $LastExitCode
} Until ( $git_exit -eq 0)
}
}
}
end {
Write-Log -message ('{0} :: end - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
}
Function Ronin-PreRun {
param (
[string] $nodes_def_src = "$env:systemdrive\BootStrap\nodes.pp",
[string] $nodes_def = "$env:systemdrive\ronin\manifests\nodes\nodes.pp",
[string] $bootstrap_dir = "$env:systemdrive\BootStrap\",
[string] $secret_src = "$env:systemdrive\BootStrap\secrets\",
[string] $secrets = "$env:systemdrive\ronin\data\secrets\",
[String] $sentry_reg = "HKLM:SYSTEM\CurrentControlSet\Services",
[string] $workerType = (Get-ItemProperty "HKLM:\SOFTWARE\Mozilla\ronin_puppet").workerType,
[string] $sourceOrg = (Get-ItemProperty "HKLM:\SOFTWARE\Mozilla\ronin_puppet\source").Organisation,
[string] $sourceRepo = (Get-ItemProperty "HKLM:\SOFTWARE\Mozilla\ronin_puppet\source").Repository,
[string] $sourceRev = (Get-ItemProperty "HKLM:\SOFTWARE\Mozilla\ronin_puppet\source").Revision
)
begin {
Write-Log -message ('{0} :: begin - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
process {
Clone-Ronin
if (!(Test-path $nodes_def)) {
Copy-item -path $nodes_def_src -destination $nodes_def -force
write-host here
write-host $workerType
(Get-Content -path $nodes_def) -replace 'roles::role', "roles::$workerType" | Set-Content $nodes_def
}
if (!(Test-path $secrets)) {
Copy-item -path $secret_src -destination $secrets -recurse -force
}
Set-ItemProperty -Path "$sentry_reg\SecurityHealthService" -name "start" -Value '4' -Type Dword
Set-ItemProperty -Path "$sentry_reg\sense" -name "start" -Value '4' -Type Dword
Invoke-WebRequest https://raw.githubusercontent.com/$sourceOrg/$sourceRepo/$sourceRev/provisioners/windows/geckotwin1064hw-bootstrap.ps1 -OutFile "$env:systemdrive\BootStrap\geckotwin1064hw-bootstrap.ps1"
Schtasks /create /RU system /tn bootstrap /tr "powershell -file $env:systemdrive\BootStrap\geckotwin1064hw-bootstrap.ps1" /sc onstart /RL HIGHEST /f
}
end {
Write-Log -message ('{0} :: end - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
}
function Bootstrap-Puppet {
param (
[int] $exit,
[string] $lock = "$env:programdata\PuppetLabs\ronin\semaphore\ronin_run.lock",
[int] $last_exit = (Get-ItemProperty "HKLM:\SOFTWARE\Mozilla\ronin_puppet").last_run_exit,
[string] $run_to_success = (Get-ItemProperty "HKLM:\SOFTWARE\Mozilla\ronin_puppet").runtosuccess,
[string] $nodes_def = "$env:systemdrive\ronin\manifests\nodes\odes.pp",
[string] $puppetfile = "$env:systemdrive\ronin\Puppetfile",
[string] $logdir = "$env:systemdrive\logs",
[string] $datetime = (get-date -format yyyyMMdd-HHmm),
[string] $flagfile = "$env:programdata\PuppetLabs\ronin\semaphore\task-claim-state.valid",
[string] $sourceOrg = (Get-ItemProperty "HKLM:\SOFTWARE\Mozilla\ronin_puppet\source").Organisation,
[string] $sourceRepo = (Get-ItemProperty "HKLM:\SOFTWARE\Mozilla\ronin_puppet\source").Repository,
[string] $sourceRev = (Get-ItemProperty "HKLM:\SOFTWARE\Mozilla\ronin_puppet\source").Revision,
[string] $stage = (Get-ItemProperty -path "HKLM:\SOFTWARE\Mozilla\ronin_puppet").bootstrap_stage
)
begin {
Write-Log -message ('{0} :: begin - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
process {
#Setting the enviroment for Puppet and r10k
Set-Location $env:systemdrive\ronin
If(!(test-path $logdir\old)) {
New-Item -ItemType Directory -Force -Path $logdir\old
}
If ($stage -eq "inprogress") {
git pull https://github.com/$sourceOrg/$sourceRepo $sourceRev
$git_exit = $LastExitCode
if ($git_exit -eq 0) {
$git_hash = (git rev-parse --verify HEAD)
Set-ItemProperty -Path HKLM:\SOFTWARE\Mozilla\ronin_puppet -name githash -type string -value $git_hash
Write-Log -message ('{0} :: Checking/pulling updates from https://github.com/{1}/{2}. Branch: {3}.' -f $($MyInvocation.MyCommand.Name), ($sourceOrg), ($sourceRepo), ($sourceRev)) -severity 'DEBUG'
} else {
Write-Log -message ('{0} :: Git pull failed! https://github.com/{1}/{2}. Branch: {3}.' -f $($MyInvocation.MyCommand.Name), ($sourceOrg), ($sourceRepo), ($sourceRev)) -severity 'DEBUG'
DO {
Start-Sleep -s 60
git pull https://github.com/$sourceOrg/$sourceRepo $sourceRev
$git_exit = $LastExitCode
} Until ( $git_exit -eq 0)
}
}
Set-ItemProperty -Path "HKLM:\SOFTWARE\Mozilla\ronin_puppet" -Name 'bootstrap_stage' -Value 'inprogress'
$env:path = "$env:programfiles\Puppet Labs\Puppet\puppet\bin;$env:programfiles\Puppet Labs\Puppet\bin;$env:path"
$env:SSL_CERT_FILE = "$env:programfiles\Puppet Labs\Puppet\puppet\ssl\cert.pem"
$env:SSL_CERT_DIR = "$env:programfiles\Puppet Labs\Puppet\puppet\ssl"
write-host $env:path
write-host $path
R10k puppetfile install -v
$env:path = ($env:path.Split(';') | Where-Object { $_ -ne "$env:programfiles\Puppet Labs\Puppet\puppet\bin" }) -join ';'
Get-ChildItem -Path $logdir\*.log -Recurse | Move-Item -Destination $logdir\old -ErrorAction SilentlyContinue
puppet apply manifests\nodes\nodes.pp --test --modulepath=modules`;r10k_modules --hiera_config=hiera.yaml --logdest $logdir\$datetime-puppetrun.log
$puppet_exit = $LastExitCode
Write-host $puppet_exit
if ($run_to_success -eq 'true') {
if ($puppet_exit -ne '0' -or '2') {
if($last_exit -eq 0) {
Write-Log -message ('{0} :: Puppet apply failed.' -f $($MyInvocation.MyCommand.Name)) -severity 'DEBUG'
Set-ItemProperty -Path HKLM:\SOFTWARE\Mozilla\ronin_puppet -name last_exit -type dword -value $puppet_exit
Remove-Item $lock -ErrorAction SilentlyContinue
shutdown @('-r', '-t', '0', '-c', 'Reboot; Puppet apply failed', '-f', '-d', '4:5')
} elseif ($last_exit -ne 0){
Set-ItemProperty -Path HKLM:\SOFTWARE\Mozilla\ronin_puppet -name last_exit -type dword -value $puppet_exit
Write-Log -message ('{0} :: Puppet apply failed. Waiting 10 minutes beofre Reboot' -f $($MyInvocation.MyCommand.Name)) -severity 'DEBUG'
sleep 600
shutdown @('-r', '-t', '0', '-c', 'Reboot; Puppet apply failed', '-f', '-d', '4:5')
}
} elseif ($puppet_exit -eq '0' -or '2') {
Write-Log -message ('{0} :: Puppet apply successful' -f $($MyInvocation.MyCommand.Name)) -severity 'DEBUG'
Set-ItemProperty -Path HKLM:\SOFTWARE\Mozilla\ronin_puppet -name last_exit -type dword -value $puppet_exit
Remove-Item -path $lock
New-ItemProperty -Path "$ronnin_key" -Name 'bootstrap_stage' -Value 'complete' -PropertyType String
} else {
Write-Log -message ('{0} :: Unable to detrimine state post Puppet apply' -f $($MyInvocation.MyCommand.Name)) -severity 'DEBUG'
Set-ItemProperty -Path HKLM:\SOFTWARE\Mozilla\ronin_puppet -name last_exit -type dword -value $last_exit
sleep 600
shutdown @('-r', '-t', '0', '-c', 'Reboot; Unveriable state', '-f', '-d', '4:5')
}
}
}
end {
Write-Log -message ('{0} :: end - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
}
Function Bootstrap-CleanUp {
param (
[string] $bootstrapdir = "$env:systemdrive\BootStrap\"
)
begin {
Write-Log -message ('{0} :: begin - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
process {
Remove-Item -Recurse -Force $bootstrapdir
Schtasks /delete /tn bootstrap /f
}
end {
Write-Log -message ('{0} :: end - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
}
If(test-path HKLM:\SOFTWARE\Mozilla\ronin) {
$stage = (Get-ItemProperty -path "HKLM:\SOFTWARE\Mozilla\ronin_puppet").bootstrap_stage
}
If(!(test-path HKLM:\SOFTWARE\Mozilla\ronin)) {
Set-RoninRegOptions
Ronin-PreRun
}
If ($stage -eq 'setup' -or 'inprogress') {
#If ($stage -eq 'setup') {
Bootstrap-Puppet
}
If ($stage -eq 'complete') {
Bootstrap-CleanUp
}