forked from hashicorp/puppet-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
windows.ps1
235 lines (205 loc) · 7.86 KB
/
windows.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
<#
.SYNOPSIS
Installs Puppet on this machine.
.DESCRIPTION
Downloads and installs the PuppetLabs Puppet MSI package.
This script requires administrative privileges.
You can run this script from an old-style cmd.exe prompt using the
following:
powershell.exe -ExecutionPolicy Unrestricted -NoLogo -NoProfile -Command "& '.\windows.ps1'"
.PARAMETER PuppetPackage
This is the Chocolatey Package you want to install.
This defaults to puppet (puppet 3.x).
.PARAMETER PuppetCollection
This is the Collection of Puppet that you want to install. If you pass this it will override the PuppetPackage.
This defaults to $null.
.PARAMETER PuppetVersion
This is the Version of Puppet that you want to install. puppet-agent jumps major versions in Chocolatey.
This defaults to $null.
.PARAMETER PuppetCertname
The certname to use for this puppet agent.
This defaults to $env:computername.
.PARAMETER PuppetEnvironment
The environment to use for this puppet agent.
This defaults to "test".
.PARAMETER PuppetServer
The Puppet Server to use for this puppet agent.
This defaults to a guess based on PuppetEnvironment.
.PARAMETER PuppetCAServer
The Puppet CA Server to use for this puppet agent.
This defaults to a guess based on PuppetEnvironment.
.PARAMETER chocolateyProxyLocation
Use this web proxy to do the install.
.PARAMETER PuppetScheduledTask
Set to undef to skip setting up the ScheduledTask.
#>
param(
[string]$PuppetPackage = "puppet"
, [string]$PuppetCollection = $env:PuppetCollection
, [string]$PuppetVersion = $env:PuppetVersion
, [string]$PuppetCertname = $env:PuppetCertname
, [string]$PuppetEnvironment = $env:PuppetEnvironment
, [string]$PuppetServer = $env:PuppetServer
, [string]$PuppetCAServer = $env:PuppetCAServer
, [string]$chocolateyProxyLocation = $env:chocolateyProxyLocation
, [string]$PuppetScheduledTask = $env:PuppetScheduledTask
)
if ($PuppetCollection) {
$PuppetPackage = "puppet-agent"
Write-Host "Puppet Collection $PuppetCollection specified, updated PuppetPackage to `"$PuppetPackage`""
$PuppetApplyManifests = "C:\ProgramData\PuppetLabs\code\environments\$PuppetEnvironment\manifests"
}
else {
$PuppetApplyManifests = "C:\ProgramData\PuppetLabs\puppet\etc\manifests"
}
if (!($PuppetEnvironment)) { $PuppetEnvironment = "test" }
if (!($PuppetCertname)) {
# Try and query the system
$sysinfo = Get-WmiObject -Class Win32_ComputerSystem
if ($sysinfo.Name) {
if ($sysinfo.Domain) {
$PuppetCertname = ($sysinfo.Name + "." + $sysinfo.Domain).tolower()
}
else {
switch -regex ($PuppetEnvironment) {
'locprd|production' { $PuppetCertname = ($sysinfo.Name + ".it.muohio.edu").tolower() }
default { $PuppetCertname = ($sysinfo.Name + ".ittst.muohio.edu").tolower() }
}
}
}
else {
Write-Error "Can not auto determine PuppetCertname."
Exit 1
}
}
if (!($PuppetServer)) {
switch -regex ($PuppetEnvironment) {
'locdev|loctst|locprd|vagrant|all' { $PuppetServer = "localhost" }
'esodev|esotst' { $PuppetServer = "uitlpupt10.mcs.miamioh.edu" }
'development|test|staging|production|operations|shared_services' { $PuppetServer = "uitlpupp10.mcs.miamioh.edu" }
default {
Write-Error "Unknown/Unsupported PuppetEnvironment."
Exit 1
}
}
}
if (!($PuppetCAServer)) { $PuppetCAServer = $PuppetServer }
$PuppetCmd = "C:\Program Files\Puppet Labs\Puppet\bin\puppet.bat"
switch -regex ($PuppetEnvironment) {
'locdev|loctst|locprd|vagrant|all' { $PuppetArg = "apply --config C:\ProgramData\PuppetLabs\puppet\etc\puppet.conf $PuppetApplyManifests" }
default { $PuppetArg = "agent --config C:\ProgramData\PuppetLabs\puppet\etc\puppet.conf --onetime --no-daemonize" }
}
$PuppetInstalled = $false
try {
$ErrorActionPreference = "Stop";
Get-Command choco | Out-Null
$ChocoList = &choco list --limit-output --local-only --exact $PuppetPackage
if ($ChocoList) {
Get-Command puppet | Out-Null
$PuppetInstalled = $true
$PuppetVersion = &puppet "--version"
Write-Host "Puppet $PuppetVersion is installed. This process does not ensure the exact version or at least version specified, but only that puppet is installed. Exiting..."
Exit 0
}
else {
Write-Host "Puppet is not installed, continuing..."
}
}
catch {
Write-Host "Puppet is not installed, continuing..."
}
if (!($PuppetInstalled)) {
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (! ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))) {
Write-Error "You must run this script as an administrator."
Exit 1
}
# Install chocolatey
Write-Host "Installing Chocolatey"
$WebClient = New-Object System.Net.WebClient
if ($chocolateyProxyLocation) {
$WebProxy = New-Object System.Net.WebProxy($chocolateyProxyLocation, $true)
$WebClient.Proxy = $WebProxy
iex ($WebClient.DownloadString('https://chocolatey.org/install.ps1'))
choco config set proxy $chocolateyProxyLocation
}
else {
iex ($WebClient.DownloadString('https://chocolatey.org/install.ps1'))
}
# Install it - use chocolatey
$install_args = @("install", $PuppetPackage, "-y")
if ($PuppetVersion) {
$install_args += "--version"
$install_args += $PuppetVersion
}
Write-Host "Installing Puppet. Running choco.exe $install_args"
$process = Start-Process -FilePath choco.exe -ArgumentList $install_args -Wait -PassThru
if ($process.ExitCode -ne 0) {
Write-Error "Installer failed."
Exit 1
}
# Stop the service that it autostarts
Write-Host "Stopping Puppet service that is running by default..."
Start-Sleep -s 5
if ($PuppetCollection) {
New-Item -path "C:\ProgramData\PuppetLabs\code\environments\$PuppetEnvironment" -type directory -force
try {
Set-Service -Name mcollective -StartupType Disabled -Status Stopped
}
catch {
Write-Host "Service mcollective not installed"
}
Set-Service -Name pxp-agent -StartupType Disabled -Status Stopped
}
Set-Service -Name puppet -StartupType Disabled -Status Stopped
Write-Host "Puppet successfully installed."
if ($PuppetEnvironment -ne "vagrant") {
$dir_prefix = 'C:/ProgramData/PuppetLabs/puppet'
$var_dir = "${dir_prefix}/var"
$log_dir = "${dir_prefix}/var/log"
$run_dir = "${dir_prefix}/var/run"
$ssl_dir = "${dir_prefix}/etc/ssl"
if ($PuppetCollection) {
$extra_a_options = ''
$extra_u_options = ''
}
else {
$extra_a_options = '
stringify_facts = false'
$extra_u_options = '
parser = future
stringify_facts = false
ordering = manifest'
}
Write-Host "Configuring Puppet..."
@"
### File placed by puppet-bootstrap ###
## https://docs.puppet.com/puppet/latest/reference/configuration.html
#
[main]
vardir = $var_dir
logdir = $log_dir
rundir = $run_dir
ssldir = $ssl_dir
[agent]
pluginsync = true
report = true
ignoreschedules = true
daemon = false
ca_server = $PuppetCAServer
certname = $PuppetCertname
environment = $PuppetEnvironment
server = $PuppetServer$extra_a_options
[user]
environment = $PuppetEnvironment$extra_u_options
"@ | Out-File C:\ProgramData\PuppetLabs\puppet\etc\puppet.conf -encoding ASCII
if ($PuppetScheduledTask -eq "undef") {
Write-Host "NOT Starting Puppet ScheduledTask..."
}
else {
Write-Host "Starting Puppet ScheduledTask..."
schtasks /create /tn "puppet" /tr "'$PuppetCmd' $PuppetArg" /sc DAILY /st 12:00 /ri 60 /du 24:00 /ru SYSTEM /rl HIGHEST /f
}
}
Write-Host "Success!!"
}