This repository has been archived by the owner on Sep 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
installer.sh
490 lines (435 loc) · 13.6 KB
/
installer.sh
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/bin/bash
# set -euo pipefail
IFS=$'\n\t'
### Set here your environment configuration
PUPPET_SERVER=${puppet:-'puppet'} # Puppet Server host
PUPPET_SERVER_PORT=${port:-'8140'} # Puppet Server port
PUPPET_SERVER_CA=${ca_server:-'puppet'} # Puppet CA Server host
PUPPET_ENVIRONMENT=${environment:-'production'} # Puppet environment
PUPPET_RUN_INTERVAL=${runinterval:-'180'} # Puppet run interval
PUPPET_WAIT_FOR_CERT=${waitforcert:-'30'} # Puppet wait for cert
PUPPET_CERTNAME=${certname:-$1}
: "${PUPPET_CERTNAME?"Usage: $0 certname"} "
LOGDIR="/var/log" # logs folder
LOCKFILE="/tmp/puppet.installer.lock" # lock file
TIMESTAMP=$(date -u +%Y%m%d.%H%M%S) # run timestamp, UTC
LOGFILE="$LOGDIR/puppet.installer.$TIMESTAMP.log" # standard out log file
OS='undef' # detected operating system
PURGE=false # whether to clean a previous install or not
log() {
echo -e "====>>>> $1" | tee -a "$LOGFILE"
}
check_bash() {
if [ ! -n "${BASH}" ]; then
echo "This script only works on BASH shell"
exit 1
elif [ -n "${DEBUG}" ]; then
echo "===== PARAMETERS ====="
echo "Puppet Server CA: ${PUPPET_SERVER_CA}"
echo "Puppet Server: ${PUPPET_SERVER}"
echo "Puppet Server port: ${PUPPET_SERVER_PORT}"
echo "Puppet Server environment: ${PUPPET_ENVIRONMENT}"
echo "Puppet certname: ${PUPPET_CERTNAME}"
echo "===== ===== ====="
fi
}
check_root() {
ID=$(id -u)
if [ "$ID" -ne 0 ]; then
echo -e "\\nError: you must be root to run this script"
echo -e "Become root with the command 'sudo -i'.\\n"
exit 2
fi
}
check_lock_file() {
if [ -f "${LOCKFILE}" ]; then
echo "Install lock found at ${LOCKFILE}, check if the script is already running"
exit 3
fi
}
create_lock_file() {
mkdir -p "${LOGDIR}"
touch "${LOGFILE}"
touch "${LOCKFILE}"
echo -e "Log messages are on $LOGFILE\\n"
log "Puppet agent installation started at $(date -uIs)"
}
check_download_tool() {
if hash curl &> /dev/null; then
DOWNLOADER='curl -s -O'
log "Found curl"
else
if hash wget &> /dev/null; then
DOWNLOADER='wget -q'
log "Found wget"
else
log "Failure: curl/wget not found but required to proceed."
exit 4
fi
fi
}
check_os_el7() {
if grep -E '^CentOS Linux release 7' /etc/redhat-release &> /dev/null || \
grep -E '^Red Hat Enterprise Linux Server release 7' /etc/redhat-release &> /dev/null; then
OS='el7'
REPOURL='http://yum.puppet.com/puppet5/puppet5-release-el-7.noarch.rpm'
CONNECTION_COMMAND='tcping'
CONNECTION_TOOL='tcping'
CONNECTION_PACKAGES='tcping'
return 0
else
return 1
fi
}
check_os_el6() {
if grep -E '^CentOS release 6' /etc/redhat-release &> /dev/null || \
grep -E '^Red Hat Enterprise Linux Server release 6' /etc/redhat-release &> /dev/null; then
OS='el6'
REPOURL='http://yum.puppet.com/puppet5/puppet5-release-el-6.noarch.rpm'
CONNECTION_COMMAND='nc -z'
CONNECTION_TOOL='nc'
CONNECTION_PACKAGES='nc'
return 0
else
return 1
fi
}
check_os_el5() {
if grep -E '^CentOS release 5' /etc/redhat-release &> /dev/null || \
grep -E '^Red Hat Enterprise Linux Server release 5' /etc/redhat-release &> /dev/null; then
OS='el5'
REPOFILE='puppet5-release-el-5.noarch.rpm'
REPOURL="http://yum.puppet.com/puppet5/$REPOFILE"
CONNECTION_COMMAND='nc -z'
CONNECTION_TOOL='nc'
CONNECTION_PACKAGES='nc'
return 0
else
return 1
fi
}
check_os_debian6() {
if grep -E '^6\.' /etc/debian_version &> /dev/null; then
OS='squeeze'
REPOURL='http://apt.puppet.com/pool/squeeze/PC1/p/puppetlabs-release-pc1/puppetlabs-release-pc1_1.1.0-4squeeze_all.deb'
REPOFILE='puppetlabs-release-pc1_1.1.0-4squeeze_all.deb'
return 0
else
return 1
fi
}
check_os_debian7() {
if grep -E '^7\.' /etc/debian_version &> /dev/null; then
OS='wheezy'
REPOFILE='puppet5-release_5.0.0-1wheezy_all.deb'
REPOURL="http://apt.puppet.com/pool/wheezy/puppet5/p/puppet5-release/$REPOFILE"
return 0
else
return 1
fi
}
check_os_debian8() {
if grep -E '^8\.' /etc/debian_version &> /dev/null; then
OS='jessie'
REPOFILE='puppet5-release_5.0.0-1jessie_all.deb'
REPOURL="http://apt.puppet.com/pool/jessie/puppet5/p/puppet5-release/$REPOFILE"
return 0
else
return 1
fi
}
check_os_debian9() {
if grep -E '^9\.' /etc/debian_version &> /dev/null; then
OS='stretch'
REPOFILE='puppet5-release_5.0.0-1stretch_all.deb'
REPOURL="http://apt.puppet.com/pool/stretch/puppet5/p/puppet5-release/$REPOFILE"
return 0
else
return 1
fi
}
check_os_ubuntu12() {
if grep -E 'DISTRIB_CODENAME=precise' /etc/lsb-release &> /dev/null; then
OS='precise'
REPOFILE='puppetlabs-release-pc1_1.1.0-4precise_all.deb'
REPOURL="http://apt.puppet.com/pool/precise/PC1/p/puppetlabs-release-pc1/$REPOFILE"
return 0
else
return 1
fi
}
check_os_ubuntu14() {
if grep -E 'DISTRIB_CODENAME=trusty' /etc/lsb-release &> /dev/null; then
OS='trusty'
REPOFILE='puppet-release_1.0.0-1trusty_all.deb'
REPOURL="http://apt.puppet.com/pool/trusty/puppet5/p/puppet-release/$REPOFILE"
return 0
else
return 1
fi
}
check_os_ubuntu16() {
if grep -E 'DISTRIB_CODENAME=xenial' /etc/lsb-release &> /dev/null; then
OS='xenial'
REPOFILE='puppet5-release_5.0.0-1xenial_all.deb'
REPOURL="http://apt.puppet.com/pool/xenial/puppet5/p/puppet5-release/$REPOFILE"
return 0
else
return 1
fi
}
check_os_ubuntu18() {
if grep -E 'DISTRIB_CODENAME=bionic' /etc/lsb-release &> /dev/null; then
OS='bionic'
REPOFILE='puppet5-release_5.0.0-2bionic_all.deb'
REPOURL="http://apt.puppet.com/pool/bionic/puppet5/p/puppet5-release/$REPOFILE"
return 0
else
return 1
fi
}
check_os_sles12() {
if grep -E '^SUSE Linux Enterprise Server 12' /etc/SuSE-release &> /dev/null; then
OS='sles12'
REPOFILE='puppet5-release-5.0.0-1.sles12.noarch.rpm'
REPOURL="http://yum.puppet.com/puppet5/sles/12/x86_64/$REPOFILE"
CONNECTION_COMMAND='nc -z'
CONNECTION_TOOL='nc'
CONNECTION_PACKAGES='netcat-openbsd'
return 0
else
return 1
fi
}
check_os_sles11() {
if grep -E '^SUSE Linux Enterprise Server 11' /etc/SuSE-release &> /dev/null; then
OS='sles11'
REPOFILE='puppet5-release-5.0.0-1.sles11.noarch.rpm'
REPOURL="http://yum.puppet.com/puppet5/sles/11/x86_64/$REPOFILE"
CONNECTION_COMMAND='netcat -z'
CONNECTION_TOOL='netcat'
CONNECTION_PACKAGES='netcat'
return 0
else
return 1
fi
}
clean_lock_file() {
rm -f "${LOCKFILE}"
log "Puppet agent installation finished at $(date -u +%Y%m%d.%H%M%S)...\\n"
echo -e "You can check details on file $LOGFILE \\n"
}
clean_install() {
PACKAGE_MANAGER=$1
if [ -d /opt/puppetlabs ] && [ $PURGE = true ]; then
log "Removing old Puppet repo package"
$PACKAGE_MANAGER remove -y puppet.*release >> "${LOGFILE}" 2>&1
log "Removing old Puppet agent package"
$PACKAGE_MANAGER remove -y puppet-agent >> "${LOGFILE}" 2>&1
rm -rf /opt/puppetlabs >> "${LOGFILE}" 2>&1
rm -rf /etc/puppetlabs >> "${LOGFILE}" 2>&1
fi
}
install_agent_el() {
if [[ "$OS" =~ ^el[567]$ ]]; then
export LC_ALL="C"
clean_install "yum"
yum clean all >> "${LOGFILE}" 2>&1
log "Installing Puppet repo for $OS"
if [[ "$OS" =~ ^el[5]$ ]]; then
{
cd /tmp && \
eval "$DOWNLOADER $REPOURL" && \
rpm -ivh $REPOFILE && \
rm -fv $REPOFILE && \
cd - || return
} >> "${LOGFILE}" 2>&1
fi
if [[ "$OS" =~ ^el[67]$ ]]; then
yum install -y $REPOURL >> "${LOGFILE}" 2>&1
fi
log "Installing Puppet agent"
yum install -y puppet-agent >> "${LOGFILE}" 2>&1
if ! hash $CONNECTION_TOOL &> /dev/null; then
log "Installing connection tool"
yum -y install epel-release >> "${LOGFILE}" 2>&1
yum -y install $CONNECTION_PACKAGES >> "${LOGFILE}" 2>&1
fi
echo "PUPPET_EXTRA_OPTS=--waitforcert=${PUPPET_WAIT_FOR_CERT}" >> /etc/sysconfig/puppet
return 0
else
return 1
fi
}
install_agent_debian_ubuntu() {
if [[ "$OS" =~ ^(squeeze|wheezy|jessie|stretch|precise|trusty|xenial|bionic)$ ]]; then
clean_install "apt-get"
log "Installing Puppet repo for $OS"
cd /tmp || return 1
eval "$DOWNLOADER $REPOURL" >> "${LOGFILE}" 2>&1
dpkg -i $REPOFILE >> "${LOGFILE}" 2>&1
rm -f $REPOFILE
cd - || return 1
log "Verifying dpkg database consistency"
apt-get check >> "${LOGFILE}" 2>&1
log "Updating packages index"
if apt-get update >> "${LOGFILE}" 2>&1; then
log "Package index updated successfully"
else
log "Package index has issues. Check the log file at $LOGFILE"
fi
export LC_ALL="C"
log "Installing Puppet agent"
apt-get install puppet-agent -y >> "${LOGFILE}" 2>&1
CONNECTION_COMMAND='nc -z'
if ! hash nc &> /dev/null; then
log "Installing connection tool"
apt-get -y install netcat-openbsd >> "${LOGFILE}" 2>&1
fi
echo "PUPPET_EXTRA_OPTS=--waitforcert=${PUPPET_WAIT_FOR_CERT}" >> /etc/default/puppet
return 0
else
return 1
fi
}
install_agent_sles() {
if [[ "$OS" =~ ^sles1[12]$ ]]; then
clean_install "zypper"
log "Installing Puppet repo for $OS"
cd /tmp && \
eval "$DOWNLOADER -O http://yum.puppetlabs.com/RPM-GPG-KEY-puppet" && \
rpm --import RPM-GPG-KEY-puppet && \
rm -f RPM-GPG-KEY-puppet && \
eval "$DOWNLOADER $REPOURL" >> "${LOGFILE}" 2>&1
zypper install --no-confirm $REPOFILE >> "${LOGFILE}" 2>&1
rm -f $REPOFILE
cd - || return
log "Verifying zypper database consistency"
zypper verify 2>&1 >> "${LOGFILE}" 2>&1
log "Updating packages index"
if zypper refresh >> "${LOGFILE}" 2>&1; then
log "Package index updated successfully"
else
log "Package index has issues. Check the log file at $LOGFILE"
fi
log "Installing Puppet agent"
zypper install --no-confirm puppet-agent >> "${LOGFILE}" 2>&1
if ! hash $CONNECTION_TOOL &> /dev/null; then
log "Installing connection tool"
zypper install $CONNECTION_PACKAGES >> "${LOGFILE}" 2>&1
fi
echo "PUPPET_EXTRA_OPTS=--waitforcert=${PUPPET_WAIT_FOR_CERT}" >> /etc/sysconfig/puppet
return 0
else
return 1
fi
}
config_puppet_conf() {
log "Configuring puppet.conf file"
rm -f /etc/puppetlabs/puppet/puppet.conf
/opt/puppetlabs/puppet/bin/puppet config set --section main ca_server "${PUPPET_SERVER_CA}"
/opt/puppetlabs/puppet/bin/puppet config set --section main server "${PUPPET_SERVER}"
/opt/puppetlabs/puppet/bin/puppet config set --section main port "${PUPPET_SERVER_PORT}"
/opt/puppetlabs/puppet/bin/puppet config set --section agent environment "${PUPPET_ENVIRONMENT}"
/opt/puppetlabs/puppet/bin/puppet config set --section agent certname "${PUPPET_CERTNAME}"
/opt/puppetlabs/puppet/bin/puppet config set --section agent runinterval "${PUPPET_RUN_INTERVAL}"
/opt/puppetlabs/puppet/bin/puppet config set --section agent waitforcert "${PUPPET_WAIT_FOR_CERT}"
}
test_connection() {
log "Testing Puppet Server CA connection"
if eval "$CONNECTION_COMMAND $PUPPET_SERVER_CA $PUPPET_SERVER_PORT" >> "${LOGFILE}" 2>&1; then
log "Connection to Puppet Server CA at $PUPPET_SERVER_CA:$PUPPET_SERVER_PORT ok."
log "Testing Puppet Server connection"
if eval "$CONNECTION_COMMAND $PUPPET_SERVER $PUPPET_SERVER_PORT" >> "${LOGFILE}" 2>&1; then
log "Connection to Puppet Server at $PUPPET_SERVER:$PUPPET_SERVER_PORT ok."
return 0
else
log "Could not connect to Puppet Server at $PUPPET_SERVER:$PUPPET_SERVER_PORT."
return 1
fi
else
log "Could not connect to Puppet Server CA at $PUPPET_SERVER_CA:$PUPPET_SERVER_PORT."
return 1
fi
}
run_puppet_agent() {
/opt/puppetlabs/bin/puppet agent --test
}
show_help() {
echo "Usage: bash puppet-installer.sh [-h|--help] [-p|--purge certname] certname"
echo "Install the Puppet agent and test the connection with the Puppet Server"
echo " "
echo " -h, --help Print this page"
echo " -p, --purge Clean a previous Puppet install"
echo " "
echo "The script requires the certname parameter, or a \"certname\" environment variable"
exit 1
}
while :; do
case $1 in
-h|-\?|--help)
show_help
exit
;;
-p|--purge)
PURGE=true
if [ "$2" ]; then
PUPPET_CERTNAME=$2
break
else
if [ ! $certname ]; then
echo 'Error: "--purge" requires a non-empty option argument.'
exit 1
else
PUPPET_CERTNAME=$certname
break
fi
fi
;;
*)
if ! [ "$2" ] && [ "$1" ]; then
PUPPET_CERTNAME=$1
break
else
if [ ! $certname ]; then
echo 'Error: Wrong use of options/arguments. Use --help to see the command usage.'
exit 1
else
PUPPET_CERTNAME=$certname
break
fi
fi
break
esac
shift
done
check_bash
check_root
check_lock_file
create_lock_file
check_download_tool
check_os_el7 || \
check_os_el6 || \
check_os_el5 || \
check_os_debian6 || \
check_os_debian7 || \
check_os_debian8 || \
check_os_debian9 || \
check_os_ubuntu12 || \
check_os_ubuntu14 || \
check_os_ubuntu16 || \
check_os_ubuntu18 || \
check_os_sles11 || \
check_os_sles12 || \
{
log "Unexpected OS detected."
exit 5
}
log "Detected OS: ${OS}"
install_agent_el || install_agent_debian_ubuntu || install_agent_sles || {
log "Agent installation failed"
exit 6
}
config_puppet_conf
test_connection && run_puppet_agent
clean_lock_file