|
1 | 1 | #!/bin/bash |
2 | | -# shellcheck disable=2181 |
3 | | -# shellcheck disable=2013 |
4 | | -declare PT_dnsaltname_override |
5 | | -CERTNAME="$(puppet config print certname)" |
6 | | -PUPPET_BIN_DIR="/opt/puppetlabs/bin" |
7 | | -PUPPETCMD="${PUPPET_BIN_DIR}/puppet" |
8 | | -PUPPETSERVERCMD="${PUPPET_BIN_DIR}/puppetserver" |
9 | | -PATH="${PUPPET_BIN_DIR}":"${PATH}" |
10 | 2 |
|
11 | | -backup_ssl() { |
12 | | - tar -cvf "/etc/puppetlabs/puppet/ssl_$(date +%Y-%m-%d-%M-%S).tar.gz" /etc/puppetlabs/puppet/ssl /etc/puppetlabs/puppetdb/ssl /opt/puppetlabs/server/data/console-services/certs /opt/puppetlabs/server/data/postgresql/9.6/data/certs /etc/puppetlabs/orchestration-services/ssl |
13 | | -} |
| 3 | +declare PT__installdir |
| 4 | +source "$PT__installdir/support_tasks/files/common.sh" |
14 | 5 |
|
15 | | -exit_if_compile_master() { |
16 | | - grep reverse-proxy-ca-service /etc/puppetlabs/puppetserver/bootstrap.cfg 2>&1 /dev/null |
17 | | - if [ $? -eq 0 ]; then |
18 | | - echo "Target server appears to be a PE compile master. This script is intended to be targeted only at a PE Master of Masters. Exiting." |
19 | | - exit 255 |
20 | | - elif [ $? -eq 2 ]; then |
21 | | - echo "Target server does not appear to be a PE master. This script is intended to be targeted only at a PE Master of Masters. Exiting." |
22 | | - exit 255 |
23 | | - fi |
24 | | -} |
| 6 | +PUPPET_BIN='/opt/puppetlabs/puppet/bin' |
| 7 | +ssldir="$($PUPPET_BIN/puppet config print ssldir)" |
| 8 | +cadir="$($PUPPET_BIN/puppet config print cadir)" |
| 9 | +certname="$($PUPPET_BIN/puppet config print certname).pem" |
| 10 | +# Starting in 2021, the CA directory may or may not be under the ssldir |
| 11 | +# Add cadir and ssldir to an array and pass them to find to ensure we delete all necessary files |
| 12 | +ca_dirs=("$ssldir" "$cadir") |
25 | 13 |
|
26 | | -check_dns_alt_names() { |
27 | | - if [ "${PUPPET_6}" = true ]; then |
28 | | - str=$(/opt/puppetlabs/bin/puppetserver ca list --all |grep "${CERTNAME}") |
29 | | - else |
30 | | - str=$(puppet cert list "${CERTNAME}") |
31 | | - fi |
| 14 | +[[ -d $cadir ]] || fail 'ERROR: could not find cadir. Please ensure this task is run on the primary Puppet server' |
32 | 15 |
|
33 | | - for host in $(grep -oP '(?<="DNS:)(.*?)(?<=")' <<<"${str}"); do |
34 | | - tmphost="$(echo "${host}"|cut -d'"' -f 1)" |
35 | | - if [ "$tmphost" == "$CERTNAME" ] || [ "$tmphost" == "puppet" ] |
36 | | - then |
37 | | - continue |
38 | | - fi |
39 | | - if ! grep "pe_install::puppet_master_dnsaltname.*${tmphost}" /etc/puppetlabs/enterprise/conf.d/pe.conf > /dev/null 2>&1 |
40 | | - then |
41 | | - echo "'${tmphost}' is set up as a DNS alt name in the existing certificate, but is not present in the 'pe_install::puppet_master_dnsaltnames' setting of '/etc/puppetlabs/enterprise/conf.d/pe.conf'. Please add it to continue, or use the 'dnsaltname_override' task parameter to skip this check." |
42 | | - exit 255 |
43 | | - fi |
44 | | - done |
45 | | -} |
| 16 | +mkdir -p /var/puppetlabs/backups/ |
| 17 | +cp -aR "$ssldir" /var/puppetlabs/backups || fail "Error backing up ssldir" |
46 | 18 |
|
47 | | -# main |
| 19 | +find "${ca_dirs[@]}" -name "$certname" -delete |
| 20 | +# shellcheck disable=SC2154 |
| 21 | +PATH="${PATH}:/opt/puppetlabs/bin" puppet infrastructure configure --no-recover &>"$_tmp" || fail "Error running 'puppet infrastructure configure'" |
48 | 22 |
|
49 | | -puppet_version=$("${PUPPET_BIN_DIR?}/puppet" --version) |
50 | | -if [[ ${puppet_version%%.*} -ge 6 ]];then |
51 | | - PUPPET_6=true |
52 | | -else |
53 | | - PUPPET_6=false |
54 | | -fi |
55 | | - |
56 | | -exit_if_compile_master |
57 | | - |
58 | | -if ! "$PT_dnsaltname_override" |
59 | | -then |
60 | | - check_dns_alt_names |
61 | | -fi |
62 | | - |
63 | | -if [ ! -x $PUPPETCMD ]; then |
64 | | - echo "Unable to locate executable Puppet command at ${PUPPETCMD}" |
65 | | - exit 255 |
66 | | -fi |
67 | | - |
68 | | -# Back up the SSL directories |
69 | | -backup_ssl |
70 | | - |
71 | | -rm -f "/opt/puppetlabs/puppet/cache/client_data/catalog/${CERTNAME}.json" |
72 | | - |
73 | | -if [ ${PUPPET_6} = true ]; then |
74 | | - "${PUPPETSERVERCMD}" ca clean --certname "${CERTNAME}" |
75 | | - find /etc/puppetlabs/puppet/ssl -name "${CERTNAME}".pem -delete |
76 | | -else |
77 | | - "${PUPPETCMD}" cert clean "${CERTNAME}" |
78 | | -fi |
79 | | - |
80 | | -"${PUPPETCMD}" infrastructure configure --no-recover |
81 | | -"${PUPPETCMD}" agent -t |
82 | | - |
83 | | -if [ $? -eq 2 ]; then |
84 | | - exit 0 |
85 | | -fi |
| 23 | +success '{ "status": "success" }' |
0 commit comments