|
| 1 | +# @summary Replace a replica host for a Standard or Large architecture. |
| 2 | +# Supported use cases: |
| 3 | +# 1: The existing replica is broken, we have a fresh new VM we want to provision the replica to. |
| 4 | +# The new replica should have the same certname as the broken one. |
| 5 | +# @param primary_host - The hostname and certname of the primary Puppet server |
| 6 | +# @param replica_host - The hostname and certname of the replica VM |
| 7 | +# @param replica_postgresql_host - The hostname and certname of the host with the replica PE-PosgreSQL database. |
| 8 | +# Can be a separate host in an XL architecture, or undef in Standard or Large. |
| 9 | +plan peadm::add_replica( |
| 10 | + # Standard or Large |
| 11 | + Peadm::SingleTargetSpec $primary_host, |
| 12 | + Peadm::SingleTargetSpec $replica_host, |
| 13 | + |
| 14 | + # Extra Large |
| 15 | + Optional[Peadm::SingleTargetSpec] $replica_postgresql_host = undef, |
| 16 | + |
| 17 | + # Common Configuration |
| 18 | + Optional[String] $token_file = undef, |
| 19 | +) { |
| 20 | + |
| 21 | + $primary_target = peadm::get_targets($primary_host, 1) |
| 22 | + $replica_target = peadm::get_targets($replica_host, 1) |
| 23 | + $replica_postgresql_target = peadm::get_targets($replica_postgresql_host, 1) |
| 24 | + |
| 25 | + $certdata = run_task('peadm::cert_data', $primary_target).first.value |
| 26 | + $primary_avail_group_letter = $certdata['extensions'][peadm::oid('peadm_availability_group')] |
| 27 | + $replica_avail_group_letter = $primary_avail_group_letter ? { 'A' => 'B', 'B' => 'A' } |
| 28 | + |
| 29 | + # replica certname + any non-certname alt-names from the primary. Make sure |
| 30 | + # to Handle the case where there are no alt-names in the primary's certdata. |
| 31 | + $dns_alt_names = [$replica_target.peadm::certname()] + (pick($certdata['dns-alt-names'], []) - $certdata['certname']) |
| 32 | + |
| 33 | + # This has the effect of revoking the node's certificate, if it exists |
| 34 | + run_command("puppet infrastructure forget ${replica_target.peadm::certname()}", $primary_target, _catch_errors => true) |
| 35 | + |
| 36 | + run_task('peadm::agent_install', $replica_target, |
| 37 | + server => $primary_target.peadm::certname(), |
| 38 | + install_flags => [ |
| 39 | + '--puppet-service-ensure', 'stopped', |
| 40 | + "extension_requests:${peadm::oid('peadm_role')}=puppet/server", |
| 41 | + "extension_requests:${peadm::oid('peadm_availability_group')}=${replica_avail_group_letter}", |
| 42 | + "main:certname=${replica_target.peadm::certname()}", |
| 43 | + "main:dns_alt_names=${dns_alt_names.join(',')}", |
| 44 | + ], |
| 45 | + ) |
| 46 | + |
| 47 | + # clean the cert to make the plan idempotent |
| 48 | + run_task('peadm::ssl_clean', $replica_target, |
| 49 | + certname => $replica_target.peadm::certname(), |
| 50 | + ) |
| 51 | + |
| 52 | + # Manually submit a CSR |
| 53 | + run_task('peadm::submit_csr', $replica_target) |
| 54 | + |
| 55 | + # On primary, if necessary, sign the certificate request |
| 56 | + run_task('peadm::sign_csr', $primary_target, |
| 57 | + certnames => [$replica_target.peadm::certname()], |
| 58 | + ) |
| 59 | + |
| 60 | + # On <replica_target>, run the puppet agent |
| 61 | + run_task('peadm::puppet_runonce', $replica_target) |
| 62 | + |
| 63 | + # On the PE-PostgreSQL server in the <replacement-avail-group-letter> group |
| 64 | + |
| 65 | + # Stop puppet and add the following two lines to |
| 66 | + # /opt/puppetlabs/server/data/postgresql/11/data/pg_ident.conf |
| 67 | + # pe-puppetdb-pe-puppetdb-map <replacement-replica-fqdn> pe-puppetdb |
| 68 | + # pe-puppetdb-pe-puppetdb-migrator-map <replacement-replica-fqdn> pe-puppetdb-migrator |
| 69 | + apply($replica_postgresql_target) { |
| 70 | + service { 'puppet': |
| 71 | + ensure => stopped, |
| 72 | + before => File_line['puppetdb-map', 'migrator-map'], |
| 73 | + } |
| 74 | + |
| 75 | + file_line { 'puppetdb-map': |
| 76 | + path => '/opt/puppetlabs/server/data/postgresql/11/data/pg_ident.conf', |
| 77 | + line => "pe-puppetdb-pe-puppetdb-map ${replica_target.peadm::certname()} pe-puppetdb", |
| 78 | + } |
| 79 | + |
| 80 | + file_line { 'migrator-map': |
| 81 | + path => '/opt/puppetlabs/server/data/postgresql/11/data/pg_ident.conf', |
| 82 | + line => "pe-puppetdb-pe-puppetdb-migrator-map ${replica_target.peadm::certname()} pe-puppetdb-migrator", |
| 83 | + } |
| 84 | + |
| 85 | + service { 'pe-postgresql': |
| 86 | + ensure => running, |
| 87 | + subscribe => File_line['puppetdb-map', 'migrator-map'], |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + # Provision the new system as a replica |
| 92 | + run_task('peadm::provision_replica', $primary_target, |
| 93 | + replica => $replica_target.peadm::certname(), |
| 94 | + token_file => $token_file, |
| 95 | + |
| 96 | + # Race condition, where the provision command checks PuppetDB status and |
| 97 | + # probably gets "starting", but fails out because that's not "running". |
| 98 | + # Can remove flag when that issue is fixed. |
| 99 | + legacy => true, |
| 100 | + ) |
| 101 | + |
| 102 | + # start puppet service on postgresql host |
| 103 | + run_command('systemctl start puppet.service', $replica_postgresql_target) |
| 104 | + |
| 105 | + return("Added replica ${replica_target}") |
| 106 | +} |
0 commit comments